vrshoot

changeset 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents b2f14e535253
children 334d17aed7de
files sdr/level.p.glsl sdr/texmap.p.glsl src/level.cc src/level.h src/scr_game.cc src/texture.cc
diffstat 6 files changed, 105 insertions(+), 44 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/sdr/level.p.glsl	Sun Feb 02 00:35:22 2014 +0200
     1.3 @@ -0,0 +1,14 @@
     1.4 +uniform sampler2D tex;
     1.5 +
     1.6 +varying vec3 vpos;
     1.7 +varying vec2 texcoord;
     1.8 +
     1.9 +void main()
    1.10 +{
    1.11 +	vec4 tex = texture2D(tex, texcoord.xy);
    1.12 +
    1.13 +	float fog = clamp(40.0 / (vpos.z * vpos.z), 0.0, 1.0);
    1.14 +
    1.15 +	gl_FragColor.rgb = tex.rgb * fog;
    1.16 +	gl_FragColor.a = tex.a;
    1.17 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/sdr/texmap.p.glsl	Sun Feb 02 00:35:22 2014 +0200
     2.3 @@ -0,0 +1,20 @@
     2.4 +uniform sampler2D tex;
     2.5 +
     2.6 +varying vec3 vpos, norm;
     2.7 +varying vec2 texcoord;
     2.8 +
     2.9 +void main()
    2.10 +{
    2.11 +	vec3 n = normalize(norm);
    2.12 +	vec3 ldir = normalize(vec3(-0.5, 0.5, 2));
    2.13 +
    2.14 +	float ndotl = max(dot(n, ldir), 0.0);
    2.15 +
    2.16 +	vec3 color = vec3(0.8, 0.8, 0.8) * ndotl;
    2.17 +
    2.18 +	vec4 tex = texture2D(tex, texcoord.xy);
    2.19 +
    2.20 +
    2.21 +	gl_FragColor.rgb = color * tex.rgb;
    2.22 +	gl_FragColor.a = tex.a;
    2.23 +}
     3.1 --- a/src/level.cc	Sat Feb 01 19:58:19 2014 +0200
     3.2 +++ b/src/level.cc	Sun Feb 02 00:35:22 2014 +0200
     3.3 @@ -1,17 +1,27 @@
     3.4 +#include <math.h>
     3.5  #include "level.h"
     3.6  #include "logger.h"
     3.7 +#include "texman.h"
     3.8 +#include "sdrman.h"
     3.9  
    3.10  static void gen_mesh(Mesh *mesh);
    3.11  
    3.12 +static ShaderProg *level_sdr;
    3.13 +
    3.14  Level::Level()
    3.15  {
    3.16 -	dlimit_near = -2;
    3.17 -	dlimit_far = 8;
    3.18 +	dlimit_near = 0;
    3.19 +	dlimit_far = 25;
    3.20 +
    3.21 +	fly_speed = 3.0;
    3.22  
    3.23  	gen_mesh(&tile_mesh);
    3.24  
    3.25  	tile_obj = new Object;
    3.26  	tile_obj->set_mesh(&tile_mesh);
    3.27 +	tile_obj->material.tex[0] = texset.get("data/purple_grid.png");
    3.28 +
    3.29 +	level_sdr = get_sdrprog("sdr/default.v.glsl", "sdr/level.p.glsl");
    3.30  }
    3.31  
    3.32  Level::~Level()
    3.33 @@ -25,58 +35,79 @@
    3.34  	dlimit_far = dfar;
    3.35  }
    3.36  
    3.37 -void Level::draw() const
    3.38 +void Level::draw(long msec) const
    3.39  {
    3.40  	AABox bbox = tile_obj->get_aabbox();
    3.41  	float zsize = bbox.max.z - bbox.min.z;
    3.42  	float zoffs = -zsize / 2.0;
    3.43  
    3.44 +	float tm = (float)msec / 1000.0;
    3.45 +
    3.46 +	level_sdr->bind();
    3.47 +
    3.48  	float z = dlimit_near;
    3.49  	while(z < dlimit_far) {
    3.50 -		tile_obj->set_position(Vector3(0, 0, -(z - zoffs)));
    3.51 +		tile_obj->set_position(Vector3(0, 0, -(z - zoffs) + fmod(tm * fly_speed, zsize)));
    3.52 +
    3.53 +		// draw mirrored
    3.54 +		glFrontFace(GL_CW);
    3.55 +		tile_obj->set_scaling(Vector3(-1, 1, 1));
    3.56  		tile_obj->draw();
    3.57 +		glFrontFace(GL_CCW);
    3.58 +		tile_obj->set_scaling(Vector3(1, 1, 1));
    3.59 +		tile_obj->draw();
    3.60 +
    3.61  		z += zsize;
    3.62  	}
    3.63  }
    3.64  
    3.65 +#define DEPTH	2.0
    3.66 +#define POSTZ	(0.8 * DEPTH)
    3.67 +
    3.68  static void gen_mesh(Mesh *mesh)
    3.69  {
    3.70 -	/* left vertical side */
    3.71 +	// vertical side
    3.72  	mesh->normal(1, 0, 0);
    3.73 -	mesh->texcoord(0, 0); mesh->vertex(-2, 0, 1);
    3.74 -	mesh->texcoord(1, 0); mesh->vertex(-2, 0, -1);
    3.75 -	mesh->texcoord(1, 1); mesh->vertex(-2, 1, -1);
    3.76 -	mesh->texcoord(0, 1); mesh->vertex(-2, 1, 1);
    3.77 +	mesh->texcoord(0, 0); mesh->vertex(-2, 0, DEPTH);
    3.78 +	mesh->texcoord(3, 0); mesh->vertex(-2, 0, -DEPTH);
    3.79 +	mesh->texcoord(3, 2); mesh->vertex(-2, 1, -DEPTH);
    3.80 +	mesh->texcoord(0, 2); mesh->vertex(-2, 1, DEPTH);
    3.81  
    3.82 -	/* left diagonal */
    3.83 +	// left diagonal
    3.84  	Vector3 dnorm = Vector3(0.5, 1, 0).normalized();
    3.85  	mesh->normal(dnorm.x, dnorm.y, dnorm.z);
    3.86 -	mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, 1);
    3.87 -	mesh->texcoord(1, 0); mesh->vertex(-1, -0.5, -1);
    3.88 -	mesh->texcoord(1, 1); mesh->vertex(-2, 0, -1);
    3.89 -	mesh->texcoord(0, 1); mesh->vertex(-2, 0, 1);
    3.90 +	mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, DEPTH);
    3.91 +	mesh->texcoord(3, 0); mesh->vertex(-1, -0.5, -DEPTH);
    3.92 +	mesh->texcoord(3, 2); mesh->vertex(-2, 0, -DEPTH);
    3.93 +	mesh->texcoord(0, 2); mesh->vertex(-2, 0, DEPTH);
    3.94 +
    3.95 +	// vertical post front
    3.96 +	mesh->normal(0, 0, 1);
    3.97 +	mesh->texcoord(0, 0); mesh->vertex(-2, -0.5, -POSTZ);
    3.98 +	mesh->texcoord(1, 0); mesh->vertex(-1.7, -0.5, -POSTZ);
    3.99 +	mesh->texcoord(1, 3); mesh->vertex(-1.7, 1, -POSTZ);
   3.100 +	mesh->texcoord(0, 3); mesh->vertex(-2, 1, -POSTZ);
   3.101 +
   3.102 +	// vertical post side
   3.103 +	mesh->normal(1, 0, 0);
   3.104 +	mesh->texcoord(0, 0); mesh->vertex(-1.7, -0.5, -POSTZ);
   3.105 +	mesh->texcoord(1, 0); mesh->vertex(-1.7, -0.5, -DEPTH);
   3.106 +	mesh->texcoord(1, 3); mesh->vertex(-1.7, 1, -DEPTH);
   3.107 +	mesh->texcoord(0, 3); mesh->vertex(-1.7, 1, -POSTZ);
   3.108 +
   3.109 +	// vertical post top
   3.110 +	mesh->normal(0, 1, 0);
   3.111 +	mesh->texcoord(0, 0); mesh->vertex(-2, 1, -POSTZ);
   3.112 +	mesh->texcoord(1, 0); mesh->vertex(-1.7, 1, -POSTZ);
   3.113 +	mesh->texcoord(1, 1); mesh->vertex(-1.7, 1, -DEPTH);
   3.114 +	mesh->texcoord(0, 1); mesh->vertex(-2, 1, -DEPTH);
   3.115  
   3.116  	/* floor */
   3.117  	mesh->normal(0, 1, 0);
   3.118 -	mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, 1);
   3.119 -	mesh->texcoord(1, 0); mesh->vertex(1, -0.5, 1);
   3.120 -	mesh->texcoord(1, 1); mesh->vertex(1, -0.5, -1);
   3.121 -	mesh->texcoord(0, 1); mesh->vertex(-1, -0.5, -1);
   3.122 -
   3.123 -	/* right vertical side */
   3.124 -	mesh->normal(-1, 0, 0);
   3.125 -	mesh->texcoord(0, 0); mesh->vertex(2, 0, -1);
   3.126 -	mesh->texcoord(1, 0); mesh->vertex(2, 0, 1);
   3.127 -	mesh->texcoord(1, 1); mesh->vertex(2, 1, 1);
   3.128 -	mesh->texcoord(0, 1); mesh->vertex(2, 1, -1);
   3.129 -
   3.130 -	/* right diagonal */
   3.131 -	dnorm = Vector3(-0.5, 1, 0).normalized();
   3.132 -	mesh->normal(dnorm.x, dnorm.y, dnorm.z);
   3.133 -	mesh->texcoord(0, 0); mesh->vertex(1, -0.5, -1);
   3.134 -	mesh->texcoord(1, 0); mesh->vertex(1, -0.5, 1);
   3.135 -	mesh->texcoord(1, 1); mesh->vertex(2, 0, 1);
   3.136 -	mesh->texcoord(0, 1); mesh->vertex(2, 0, -1);
   3.137 +	mesh->texcoord(0, 0); mesh->vertex(-1, -0.5, DEPTH);
   3.138 +	mesh->texcoord(1, 0); mesh->vertex(0, -0.5, DEPTH);
   3.139 +	mesh->texcoord(1, 3); mesh->vertex(0, -0.5, -DEPTH);
   3.140 +	mesh->texcoord(0, 3); mesh->vertex(-1, -0.5, -DEPTH);
   3.141  
   3.142  	// quad face index buffer
   3.143  	int nverts = mesh->get_attrib_count(MESH_ATTR_VERTEX);
     4.1 --- a/src/level.h	Sat Feb 01 19:58:19 2014 +0200
     4.2 +++ b/src/level.h	Sun Feb 02 00:35:22 2014 +0200
     4.3 @@ -13,6 +13,7 @@
     4.4  	std::list<Enemy> enemies;
     4.5  
     4.6  	float dlimit_near, dlimit_far;
     4.7 +	float fly_speed;
     4.8  
     4.9  public:
    4.10  	Level();
    4.11 @@ -20,7 +21,7 @@
    4.12  
    4.13  	void set_draw_limits(float dnear, float dfar);
    4.14  
    4.15 -	void draw() const;
    4.16 +	void draw(long msec) const;
    4.17  };
    4.18  
    4.19  #endif	// LEVEL_H_
     5.1 --- a/src/scr_game.cc	Sat Feb 01 19:58:19 2014 +0200
     5.2 +++ b/src/scr_game.cc	Sun Feb 02 00:35:22 2014 +0200
     5.3 @@ -10,9 +10,10 @@
     5.4  static int win_width, win_height;
     5.5  static bool bnstate[32];
     5.6  static int prev_x, prev_y;
     5.7 -static float cam_theta, cam_phi = 20, cam_dist = 10;
     5.8 +static float cam_theta, cam_phi = 23, cam_dist = 0;
     5.9  static Object *floor_obj;
    5.10  static Level *level;
    5.11 +static unsigned long msec;
    5.12  
    5.13  const char *GameScreen::get_name() const
    5.14  {
    5.15 @@ -24,11 +25,6 @@
    5.16  	glEnable(GL_LIGHTING);
    5.17  	glEnable(GL_LIGHT0);
    5.18  
    5.19 -	floor_obj = new Object;
    5.20 -	floor_obj->set_mesh(new Mesh);
    5.21 -	gen_plane(floor_obj->get_mesh(), 10, 10, 4, 4);
    5.22 -	floor_obj->set_rotation(Quaternion(Vector3(1, 0, 0), -DEG_TO_RAD(90)));
    5.23 -
    5.24  	level = new Level;
    5.25  
    5.26  	return true;
    5.27 @@ -40,6 +36,7 @@
    5.28  
    5.29  void GameScreen::update(unsigned long tmsec)
    5.30  {
    5.31 +	msec = tmsec;
    5.32  }
    5.33  
    5.34  void GameScreen::display() const
    5.35 @@ -48,14 +45,13 @@
    5.36  	matrix.translate(Vector3(0, 0, -cam_dist));
    5.37  	matrix.rotate(Vector3(1, 0, 0), DEG_TO_RAD(cam_phi));
    5.38  	matrix.rotate(Vector3(0, 1, 0), DEG_TO_RAD(cam_theta));
    5.39 +	matrix.translate(Vector3(0, -2, 0));
    5.40  	set_view_matrix(matrix);
    5.41  
    5.42  	//glUseProgram(0);
    5.43  
    5.44  	bind_shader(defsdr);
    5.45 -	//floor_obj->draw();
    5.46 -
    5.47 -	level->draw();
    5.48 +	level->draw(msec);
    5.49  }
    5.50  
    5.51  void GameScreen::reshape(int x, int y)
     6.1 --- a/src/texture.cc	Sat Feb 01 19:58:19 2014 +0200
     6.2 +++ b/src/texture.cc	Sun Feb 02 00:35:22 2014 +0200
     6.3 @@ -307,7 +307,6 @@
     6.4  		return load_multi(img, hsix[0], hsix[1], ysz / 2);
     6.5  	}
     6.6  
     6.7 -	error_log("failed to load %s: unknown cubemap configuration\n", fname);
     6.8  	return false;
     6.9  }
    6.10