intravenous

changeset 4:c6a6a64df6de

normalmap
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Apr 2012 05:20:03 +0300
parents 94d4c60af435
children aab0d8ea21cd
files sdr/vein.p.glsl sdr/vein.v.glsl src/cockpit.cc src/game.cc src/vein.cc src/vein.h
diffstat 6 files changed, 51 insertions(+), 18 deletions(-) [+]
line diff
     1.1 --- a/sdr/vein.p.glsl	Sun Apr 22 03:35:18 2012 +0300
     1.2 +++ b/sdr/vein.p.glsl	Sun Apr 22 05:20:03 2012 +0300
     1.3 @@ -1,15 +1,18 @@
     1.4 +uniform sampler2D tex_norm;
     1.5 +
     1.6  varying vec3 vpos, vnorm;
     1.7  varying vec3 ldir, vdir;
     1.8  
     1.9  void main()
    1.10  {
    1.11 -	vec3 n = normalize(vnorm);
    1.12 -	vec3 l = normalize(ldir);
    1.13 +	vec3 tnorm = texture2D(tex_norm, gl_TexCoord[0].st).xyz * 2.0 - 1.0;
    1.14 +	vec3 n = normalize(tnorm);
    1.15  	vec3 v = normalize(vdir);
    1.16 +	vec3 l = v;//normalize(ldir);
    1.17  	vec3 h = normalize(l + v);
    1.18  
    1.19 -	vec3 diff = vec3(0.8, 0.25, 0.2) * max(dot(l, n), 0.0);
    1.20 -	vec3 spec = vec3(0.6, 0.4, 0.32) * pow(max(dot(h, n), 0.0), 60.0);
    1.21 +	vec3 diff = vec3(0.8, 0.25, 0.2) * (1.0 - max(dot(l, n), 0.0));
    1.22 +	vec3 spec = 0.5 * vec3(0.9, 0.4, 0.3) * pow(max(dot(h, n), 0.0), 10.0);
    1.23  
    1.24  	gl_FragColor = vec4(diff + spec, 1.0);
    1.25  }
     2.1 --- a/sdr/vein.v.glsl	Sun Apr 22 03:35:18 2012 +0300
     2.2 +++ b/sdr/vein.v.glsl	Sun Apr 22 05:20:03 2012 +0300
     2.3 @@ -22,5 +22,5 @@
     2.4  	vdir = tbn_mat * -vpos;
     2.5  	vnorm = n;
     2.6  
     2.7 -	gl_TexCoord[0] = gl_MultiTexCoord0;
     2.8 +	gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
     2.9  }
     3.1 --- a/src/cockpit.cc	Sun Apr 22 03:35:18 2012 +0300
     3.2 +++ b/src/cockpit.cc	Sun Apr 22 05:20:03 2012 +0300
     3.3 @@ -2,6 +2,9 @@
     3.4  #include "cockpit.h"
     3.5  #include "tex.h"
     3.6  
     3.7 +#define TEX_ASPECT	(16.0 / 9.0)
     3.8 +extern int win_xsz, win_ysz;
     3.9 +
    3.10  static unsigned int tex;
    3.11  
    3.12  bool cockpit_init()
    3.13 @@ -19,6 +22,9 @@
    3.14  
    3.15  void cockpit_draw()
    3.16  {
    3.17 +	float view_aspect = (float)win_xsz / (float)win_ysz;
    3.18 +	float aspect = TEX_ASPECT / view_aspect;
    3.19 +
    3.20  	glPushAttrib(GL_ENABLE_BIT);
    3.21  	glDisable(GL_LIGHTING);
    3.22  
    3.23 @@ -36,10 +42,10 @@
    3.24  
    3.25  	glBegin(GL_QUADS);
    3.26  	glColor3f(1, 1, 1);
    3.27 -	glTexCoord2f(0, 1); glVertex2f(-1, -1);
    3.28 -	glTexCoord2f(1, 1); glVertex2f(1, -1);
    3.29 -	glTexCoord2f(1, 0); glVertex2f(1, 1);
    3.30 -	glTexCoord2f(0, 0); glVertex2f(-1, 1);
    3.31 +	glTexCoord2f(0, 1); glVertex2f(-aspect, -1);
    3.32 +	glTexCoord2f(1, 1); glVertex2f(aspect, -1);
    3.33 +	glTexCoord2f(1, 0); glVertex2f(aspect, 1);
    3.34 +	glTexCoord2f(0, 0); glVertex2f(-aspect, 1);
    3.35  	glEnd();
    3.36  
    3.37  	glPopMatrix();
     4.1 --- a/src/game.cc	Sun Apr 22 03:35:18 2012 +0300
     4.2 +++ b/src/game.cc	Sun Apr 22 05:20:03 2012 +0300
     4.3 @@ -11,6 +11,7 @@
     4.4  
     4.5  static int keystate[256];
     4.6  static int dbg_inside = false;
     4.7 +static bool ignore_next_motion;
     4.8  
     4.9  static OrbitCamera dbgcam;
    4.10  
    4.11 @@ -113,6 +114,9 @@
    4.12  			if(dbg_inside) {
    4.13  				glutPassiveMotionFunc(game_input_mmotion);
    4.14  				glutSetCursor(GLUT_CURSOR_NONE);
    4.15 +
    4.16 +				ignore_next_motion = true;
    4.17 +				glutWarpPointer(win_xsz / 2, win_ysz / 2);
    4.18  			} else {
    4.19  				glutPassiveMotionFunc(0);
    4.20  				glutSetCursor(GLUT_CURSOR_INHERIT);
    4.21 @@ -143,21 +147,20 @@
    4.22  
    4.23  void game_input_mmotion(int x, int y)
    4.24  {
    4.25 -	static bool ignore_next;
    4.26  	int dx = x - prev_x;
    4.27  	int dy = y - prev_y;
    4.28  	prev_x = x;
    4.29  	prev_y = y;
    4.30  
    4.31 -	if(ignore_next) {
    4.32 -		ignore_next = false;
    4.33 +	if(ignore_next_motion) {
    4.34 +		ignore_next_motion = false;
    4.35  		return;
    4.36  	}
    4.37  
    4.38  	if(dbg_inside) {
    4.39  		ship.turn((float)-dx / win_xsz, (float)-dy / win_ysz);
    4.40  
    4.41 -		ignore_next = true;
    4.42 +		ignore_next_motion = true;
    4.43  		glutWarpPointer(win_xsz / 2, win_ysz / 2);
    4.44  	} else {
    4.45  		if(bnstate[0]) {
     5.1 --- a/src/vein.cc	Sun Apr 22 03:35:18 2012 +0300
     5.2 +++ b/src/vein.cc	Sun Apr 22 05:20:03 2012 +0300
     5.3 @@ -7,20 +7,26 @@
     5.4  #include "vein.h"
     5.5  #include "geom.h"
     5.6  #include "sdr.h"
     5.7 +#include "tex.h"
     5.8  
     5.9  Vein::Vein()
    5.10  {
    5.11  	gen_dist = 16.0;
    5.12 -	subdiv = 16;
    5.13 -	ring_subdiv = 16;
    5.14 -	rad = 1.0;
    5.15 +	subdiv = 32;
    5.16 +	ring_subdiv = 24;
    5.17 +	rad = 2.0;
    5.18  
    5.19  	idxbuf = 0;
    5.20 +
    5.21 +	sdr = tex_norm = 0;
    5.22  }
    5.23  
    5.24  Vein::~Vein()
    5.25  {
    5.26  	delete [] idxbuf;
    5.27 +
    5.28 +	free_texture(tex_norm);
    5.29 +	free_program(sdr);
    5.30  }
    5.31  
    5.32  Vector3 Vein::calc_center(const Vector3 &ppos) const
    5.33 @@ -66,6 +72,11 @@
    5.34  	if((attr_tang_loc = get_attrib_loc(sdr, "attr_tang")) == -1) {
    5.35  		fprintf(stderr, "can't find tangent attribute!\n");
    5.36  	}
    5.37 +	set_uniform_int(sdr, "tex_norm", 0);
    5.38 +
    5.39 +	if(!(tex_norm = load_texture("data/normal1.png"))) {
    5.40 +		return false;
    5.41 +	}
    5.42  	return true;
    5.43  }
    5.44  
    5.45 @@ -93,7 +104,7 @@
    5.46  		Matrix3x3 vrot{right, up, dir};
    5.47  		vrot.transpose();
    5.48  
    5.49 -		float theta = 0.0, dtheta = 2.0 * M_PI / ring_subdiv;
    5.50 +		float theta = 0.0, dtheta = 2.0 * M_PI / (ring_subdiv - 1);
    5.51  		for(int j=0; j<ring_subdiv; j++) {
    5.52  			Vector3 vec = Vector3{-cos(theta) * rad, sin(theta) * rad, 0.0};
    5.53  			vec.transform(vrot);
    5.54 @@ -102,7 +113,7 @@
    5.55  			vptr->pos = vec;
    5.56  			vptr->norm = cent - vec;
    5.57  			vptr->tang = dir;
    5.58 -			vptr->tc = Vector2();	// TODO
    5.59 +			vptr->tc = Vector2(start_z + gen_dist * i / nslices, theta / (2.0 * M_PI));
    5.60  			vptr++;
    5.61  
    5.62  			theta += dtheta;
    5.63 @@ -117,7 +128,15 @@
    5.64  	}
    5.65  
    5.66  	// awesome, now draw it
    5.67 +	glMatrixMode(GL_TEXTURE);
    5.68 +	glPushMatrix();
    5.69 +	glScalef(0.125, 1.0, 1.0);
    5.70 +
    5.71 +	bind_texture(tex_norm);
    5.72  	bind_program(sdr);
    5.73  	draw_mesh(GL_QUADS, nfaces * 4, vbuf, idxbuf, attr_tang_loc);
    5.74  	bind_program(0);
    5.75 +	bind_texture(0);
    5.76 +
    5.77 +	glPopMatrix();
    5.78  }
     6.1 --- a/src/vein.h	Sun Apr 22 03:35:18 2012 +0300
     6.2 +++ b/src/vein.h	Sun Apr 22 05:20:03 2012 +0300
     6.3 @@ -13,6 +13,8 @@
     6.4  	unsigned int sdr;
     6.5  	int attr_tang_loc;
     6.6  
     6.7 +	unsigned int tex_norm;
     6.8 +
     6.9  	Vector3 calc_center(const Vector3 &ppos) const;
    6.10  	Vector3 calc_dir(const Vector3 &ppos) const;
    6.11