dungeon_crawler

changeset 35:d0e93b4d9ec9

normal mapping
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Aug 2012 06:28:22 +0300
parents 85734f319626
children 80dab4000413
files prototype/sdr/deferred.p.glsl prototype/sdr/mrt.p.glsl prototype/sdr/mrt.v.glsl prototype/src/material.cc prototype/src/mesh.cc prototype/src/renderer.cc prototype/src/renderer.h prototype/src/tile.cc
diffstat 8 files changed, 128 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/prototype/sdr/deferred.p.glsl	Tue Aug 28 03:34:56 2012 +0300
     1.2 +++ b/prototype/sdr/deferred.p.glsl	Tue Aug 28 06:28:22 2012 +0300
     1.3 @@ -1,4 +1,4 @@
     1.4 -uniform sampler2D mrt0, mrt1, mrt2, mrt3;
     1.5 +uniform sampler2D mrt0, mrt1, mrt2;
     1.6  uniform vec2 tex_scale;
     1.7  
     1.8  void main()
     1.9 @@ -6,14 +6,12 @@
    1.10  	vec2 tc = gl_TexCoord[0].st;
    1.11  
    1.12  	vec4 texel;
    1.13 -	if(tc.x < 0.25) {
    1.14 -		texel = texture2D(mrt0, tc * vec2(4.0, 1.0) * tex_scale);
    1.15 -	} else if(tc.x < 0.5) {
    1.16 -		texel = texture2D(mrt1, (tc - vec2(0.25, 0.0)) * vec2(4.0, 1.0) * tex_scale);
    1.17 -	} else if(tc.x < 0.75) {
    1.18 -		texel = texture2D(mrt2, (tc - vec2(0.5, 0.0)) * vec2(4.0, 1.0) * tex_scale);
    1.19 +	if(tc.x < 0.33333333) {
    1.20 +		texel = texture2D(mrt0, tc * vec2(3.0, 1.0) * tex_scale);
    1.21 +	} else if(tc.x < 0.66666666) {
    1.22 +		texel = texture2D(mrt1, (tc - vec2(0.33333333, 0.0)) * vec2(3.0, 1.0) * tex_scale);
    1.23  	} else {
    1.24 -		texel = texture2D(mrt3, (tc - vec2(0.75, 0.0)) * vec2(4.0, 1.0) * tex_scale);
    1.25 +		texel = texture2D(mrt2, (tc - vec2(0.66666666, 0.0)) * vec2(3.0, 1.0) * tex_scale);
    1.26  	}
    1.27  
    1.28  	gl_FragColor = texel;
     2.1 --- a/prototype/sdr/mrt.p.glsl	Tue Aug 28 03:34:56 2012 +0300
     2.2 +++ b/prototype/sdr/mrt.p.glsl	Tue Aug 28 06:28:22 2012 +0300
     2.3 @@ -1,5 +1,5 @@
     2.4  /* MRT assignments
     2.5 - * 0 - RGB: position
     2.6 + * 0 - RGB: position, A: shininess
     2.7   * 1 - RGB: normal
     2.8   * 3 - RGB: diffuse color, A: shininess str.
     2.9   * 4 - unused
    2.10 @@ -7,19 +7,35 @@
    2.11  
    2.12  uniform sampler2D tex_dif, tex_norm;
    2.13  
    2.14 -varying vec3 pos, norm;
    2.15 +varying vec3 pos, norm, tang;
    2.16 +
    2.17 +const float fog_start = 3.0;
    2.18 +const float fog_end = 6.0;
    2.19  
    2.20  void main()
    2.21  {
    2.22  	vec3 n = normalize(norm);
    2.23 +	vec3 t = normalize(tang);
    2.24 +	vec3 b = cross(n, t);
    2.25 +
    2.26 +	mat3 tbn_mat = mat3(
    2.27 +			t.x, t.y, t.z,
    2.28 +			b.x, b.y, b.z,
    2.29 +			n.x, n.y, n.z);
    2.30 +
    2.31 +	// grab normal from the normal map, remap it and transform it to view space
    2.32 +	n = texture2D(tex_norm, gl_TexCoord[0].st).xyz * 2.0 - 1.0;
    2.33 +	n = normalize(tbn_mat * n);
    2.34 +
    2.35 +	float fog = clamp((fog_end + pos.z) / (fog_end - fog_start), 0.0, 1.0);
    2.36  
    2.37  	vec4 texel = texture2D(tex_dif, gl_TexCoord[0].st);
    2.38 -	vec3 diffuse = (gl_FrontMaterial.diffuse * texel).xyz;
    2.39 -	vec3 spec = gl_FrontMaterial.specular.xyz;
    2.40 +	vec3 diffuse = fog * (gl_FrontMaterial.diffuse * texel).xyz;
    2.41 +	vec3 spec = fog * gl_FrontMaterial.specular.xyz;
    2.42  	float sstr = (spec.x + spec.y + spec.z) / 3.0;
    2.43  
    2.44  	gl_FragData[0] = vec4(pos, gl_FrontMaterial.shininess);
    2.45 -	gl_FragData[1] = vec4(norm, 0.0);
    2.46 +	gl_FragData[1] = vec4(n, 0.0);
    2.47  	gl_FragData[2] = vec4(diffuse, sstr);
    2.48  	//gl_FragData[3] = vec4(0.0, 0.0, 0.0, 0.0);
    2.49  }
     3.1 --- a/prototype/sdr/mrt.v.glsl	Tue Aug 28 03:34:56 2012 +0300
     3.2 +++ b/prototype/sdr/mrt.v.glsl	Tue Aug 28 06:28:22 2012 +0300
     3.3 @@ -1,4 +1,6 @@
     3.4 -varying vec3 pos, norm;
     3.5 +attribute vec3 attr_tangent;
     3.6 +
     3.7 +varying vec3 pos, norm, tang;
     3.8  
     3.9  void main()
    3.10  {
    3.11 @@ -6,6 +8,7 @@
    3.12  
    3.13  	pos = (gl_ModelViewMatrix * gl_Vertex).xyz;
    3.14  	norm = gl_NormalMatrix * gl_Normal;
    3.15 +	tang = gl_NormalMatrix * attr_tangent;
    3.16  
    3.17  	gl_TexCoord[0] = gl_MultiTexCoord0;
    3.18  }
     4.1 --- a/prototype/src/material.cc	Tue Aug 28 03:34:56 2012 +0300
     4.2 +++ b/prototype/src/material.cc	Tue Aug 28 06:28:22 2012 +0300
     4.3 @@ -70,7 +70,18 @@
     4.4  		glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]);
     4.5  		glEnable(GL_TEXTURE_2D);
     4.6  	} else {
     4.7 +		glActiveTextureARB(GL_TEXTURE0);
     4.8  		glDisable(GL_TEXTURE_2D);
     4.9  	}
    4.10 +
    4.11 +	if(tex[TEXTYPE_NORMAL]) {
    4.12 +		glActiveTextureARB(GL_TEXTURE1);
    4.13 +		glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_NORMAL]);
    4.14 +		glEnable(GL_TEXTURE_2D);
    4.15 +	} else {
    4.16 +		glActiveTextureARB(GL_TEXTURE1);
    4.17 +		glDisable(GL_TEXTURE_2D);
    4.18 +	}
    4.19 +
    4.20  	glActiveTextureARB(GL_TEXTURE0);
    4.21  }
     5.1 --- a/prototype/src/mesh.cc	Tue Aug 28 03:34:56 2012 +0300
     5.2 +++ b/prototype/src/mesh.cc	Tue Aug 28 06:28:22 2012 +0300
     5.3 @@ -177,7 +177,7 @@
     5.4  	glGetIntegerv(GL_CURRENT_PROGRAM, &prog);
     5.5  	glUseProgram(0);
     5.6  
     5.7 -	Vector3 *varr, *narr;
     5.8 +	Vector3 *varr, *narr, *tarr;
     5.9  
    5.10  	glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
    5.11  	varr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
    5.12 @@ -185,17 +185,27 @@
    5.13  	glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
    5.14  	narr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
    5.15  
    5.16 +	glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]);
    5.17 +	tarr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
    5.18 +
    5.19  	glBegin(GL_LINES);
    5.20  	for(unsigned int i=0; i<nverts; i++) {
    5.21  		glColor3f(0, 1, 0);
    5.22  		glVertex3f(varr->x, varr->y, varr->z);
    5.23  		glVertex3f(varr->x + narr->x * NSZ, varr->y + narr->y * NSZ, varr->z + narr->z * NSZ);
    5.24 +
    5.25 +		glColor3f(1, 1, 0);
    5.26 +		glVertex3f(varr->x, varr->y, varr->z);
    5.27 +		glVertex3f(varr->x + tarr->x * NSZ, varr->y + tarr->y * NSZ, varr->z + tarr->z * NSZ);
    5.28  		varr++;
    5.29  		narr++;
    5.30 +		tarr++;
    5.31  	}
    5.32  	glEnd();
    5.33  
    5.34  	glUnmapBuffer(GL_ARRAY_BUFFER);
    5.35 +	glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]);
    5.36 +	glUnmapBuffer(GL_ARRAY_BUFFER);
    5.37  	glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]);
    5.38  	glUnmapBuffer(GL_ARRAY_BUFFER);
    5.39  
     6.1 --- a/prototype/src/renderer.cc	Tue Aug 28 03:34:56 2012 +0300
     6.2 +++ b/prototype/src/renderer.cc	Tue Aug 28 06:28:22 2012 +0300
     6.3 @@ -9,13 +9,18 @@
     6.4  #include "sdr.h"
     6.5  #include "datapath.h"
     6.6  
     6.7 +#undef DBG_VIS_MRT
     6.8 +
     6.9 +#ifdef DBG_VIS_MRT
    6.10 +static void deferred_debug();
    6.11 +#endif
    6.12  
    6.13  static bool create_fbo(int xsz, int ysz);
    6.14  static unsigned int load_sdr(const char *vfname, const char *pfname);
    6.15  static int round_pow2(int x);
    6.16  
    6.17  
    6.18 -#define MRT_COUNT	4
    6.19 +#define MRT_COUNT	3
    6.20  static unsigned int mrt_tex[MRT_COUNT];
    6.21  
    6.22  static unsigned int mrt_prog;
    6.23 @@ -102,6 +107,11 @@
    6.24  	return deferred_omni;
    6.25  }
    6.26  
    6.27 +int get_tangent_location(void)
    6.28 +{
    6.29 +	return get_attrib_loc(mrt_prog, "attr_tangent");
    6.30 +}
    6.31 +
    6.32  void resize_renderer(int xsz, int ysz)
    6.33  {
    6.34  	fb_xsz = xsz;
    6.35 @@ -143,6 +153,9 @@
    6.36  
    6.37  	glBindFramebufferEXT(GL_FRAMEBUFFER, 0);
    6.38  
    6.39 +#ifdef DBG_VIS_MRT
    6.40 +	deferred_debug();
    6.41 +#else
    6.42  
    6.43  	// post-process lighting
    6.44  	glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
    6.45 @@ -172,12 +185,14 @@
    6.46  
    6.47  	glUseProgram(0);
    6.48  	glPopAttrib();
    6.49 +#endif	// DBG_VIS_MRT
    6.50  }
    6.51  
    6.52  static bool create_fbo(int xsz, int ysz)
    6.53  {
    6.54 -	unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
    6.55 +	unsigned int clamp = GLEW_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP;
    6.56  
    6.57 +	// round the texture size up to the next power of 2
    6.58  	tex_xsz = round_pow2(xsz);
    6.59  	tex_ysz = round_pow2(ysz);
    6.60  	fb_xsz = xsz;
    6.61 @@ -262,3 +277,50 @@
    6.62  	x = (x >> 16) | x;
    6.63  	return x + 1;
    6.64  }
    6.65 +
    6.66 +#ifdef DBG_VIS_MRT
    6.67 +// visualize the MRT buffers
    6.68 +static void deferred_debug()
    6.69 +{
    6.70 +	glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT);
    6.71 +	glUseProgram(deferred_debug);
    6.72 +	glDisable(GL_DEPTH_TEST);
    6.73 +
    6.74 +	glMatrixMode(GL_PROJECTION);
    6.75 +	glPushMatrix();
    6.76 +	glLoadIdentity();
    6.77 +	glMatrixMode(GL_MODELVIEW);
    6.78 +	glPushMatrix();
    6.79 +	glLoadIdentity();
    6.80 +
    6.81 +	for(int i=0; i<MRT_COUNT; i++) {
    6.82 +		glActiveTexture(GL_TEXTURE0 + i);
    6.83 +		glBindTexture(GL_TEXTURE_2D, mrt_tex[i]);
    6.84 +		glEnable(GL_TEXTURE_2D);
    6.85 +	}
    6.86 +
    6.87 +	glBegin(GL_QUADS);
    6.88 +	glTexCoord2f(0, 0);
    6.89 +	glVertex2f(-1, -1);
    6.90 +	glTexCoord2f(1, 0);
    6.91 +	glVertex2f(1, -1);
    6.92 +	glTexCoord2f(1, 1);
    6.93 +	glVertex2f(1, 1);
    6.94 +	glTexCoord2f(0, 1);
    6.95 +	glVertex2f(-1, 1);
    6.96 +	glEnd();
    6.97 +
    6.98 +	for(int i=0; i<MRT_COUNT; i++) {
    6.99 +		glActiveTexture(GL_TEXTURE0 + MRT_COUNT - i - 1);
   6.100 +		glDisable(GL_TEXTURE_2D);
   6.101 +	}
   6.102 +
   6.103 +	glMatrixMode(GL_PROJECTION);
   6.104 +	glPopMatrix();
   6.105 +	glMatrixMode(GL_MODELVIEW);
   6.106 +	glPopMatrix();
   6.107 +
   6.108 +	glUseProgram(0);
   6.109 +	glPopAttrib();
   6.110 +}
   6.111 +#endif
     7.1 --- a/prototype/src/renderer.h	Tue Aug 28 03:34:56 2012 +0300
     7.2 +++ b/prototype/src/renderer.h	Tue Aug 28 06:28:22 2012 +0300
     7.3 @@ -7,6 +7,7 @@
     7.4  void destroy_renderer();
     7.5  
     7.6  unsigned int get_deferred_shader(void);
     7.7 +int get_tangent_location(void);
     7.8  
     7.9  void resize_renderer(int xsz, int ysz);
    7.10  
     8.1 --- a/prototype/src/tile.cc	Tue Aug 28 03:34:56 2012 +0300
     8.2 +++ b/prototype/src/tile.cc	Tue Aug 28 06:28:22 2012 +0300
     8.3 @@ -6,6 +6,7 @@
     8.4  #include <assimp/postprocess.h>
     8.5  #include "tile.h"
     8.6  #include "tileset.h"
     8.7 +#include "renderer.h"
     8.8  
     8.9  using std::map;
    8.10  
    8.11 @@ -29,6 +30,7 @@
    8.12  	strcpy(saved_fname, fname);
    8.13  
    8.14  	unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
    8.15 +		aiProcess_CalcTangentSpace |
    8.16  		aiProcess_Triangulate |
    8.17  		aiProcess_SortByPType |
    8.18  		aiProcess_FlipUVs;
    8.19 @@ -111,12 +113,20 @@
    8.20  {
    8.21  	int count = 0;
    8.22  
    8.23 +	int attr_loc = get_tangent_location();
    8.24 +	if(attr_loc == -1) {
    8.25 +		fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n");
    8.26 +	}
    8.27 +
    8.28  	for(int i=0; i<(int)scn->mNumMeshes; i++) {
    8.29  		Mesh *mesh = new Mesh;
    8.30  		if(!mesh->create(scn, scn->mMeshes[i])) {
    8.31  			delete mesh;
    8.32  			continue;
    8.33  		}
    8.34 +		if(attr_loc != -1) {
    8.35 +			mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
    8.36 +		}
    8.37  
    8.38  		Material mat;
    8.39  		mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());