# HG changeset patch # User John Tsiombikas # Date 1346124502 -10800 # Node ID d0e93b4d9ec907b5a7c352d74394c46ef7765ec5 # Parent 85734f319626e31e4e0c2c88fa4b2fedbc0f0566 normal mapping diff -r 85734f319626 -r d0e93b4d9ec9 prototype/sdr/deferred.p.glsl --- a/prototype/sdr/deferred.p.glsl Tue Aug 28 03:34:56 2012 +0300 +++ b/prototype/sdr/deferred.p.glsl Tue Aug 28 06:28:22 2012 +0300 @@ -1,4 +1,4 @@ -uniform sampler2D mrt0, mrt1, mrt2, mrt3; +uniform sampler2D mrt0, mrt1, mrt2; uniform vec2 tex_scale; void main() @@ -6,14 +6,12 @@ vec2 tc = gl_TexCoord[0].st; vec4 texel; - if(tc.x < 0.25) { - texel = texture2D(mrt0, tc * vec2(4.0, 1.0) * tex_scale); - } else if(tc.x < 0.5) { - texel = texture2D(mrt1, (tc - vec2(0.25, 0.0)) * vec2(4.0, 1.0) * tex_scale); - } else if(tc.x < 0.75) { - texel = texture2D(mrt2, (tc - vec2(0.5, 0.0)) * vec2(4.0, 1.0) * tex_scale); + if(tc.x < 0.33333333) { + texel = texture2D(mrt0, tc * vec2(3.0, 1.0) * tex_scale); + } else if(tc.x < 0.66666666) { + texel = texture2D(mrt1, (tc - vec2(0.33333333, 0.0)) * vec2(3.0, 1.0) * tex_scale); } else { - texel = texture2D(mrt3, (tc - vec2(0.75, 0.0)) * vec2(4.0, 1.0) * tex_scale); + texel = texture2D(mrt2, (tc - vec2(0.66666666, 0.0)) * vec2(3.0, 1.0) * tex_scale); } gl_FragColor = texel; diff -r 85734f319626 -r d0e93b4d9ec9 prototype/sdr/mrt.p.glsl --- a/prototype/sdr/mrt.p.glsl Tue Aug 28 03:34:56 2012 +0300 +++ b/prototype/sdr/mrt.p.glsl Tue Aug 28 06:28:22 2012 +0300 @@ -1,5 +1,5 @@ /* MRT assignments - * 0 - RGB: position + * 0 - RGB: position, A: shininess * 1 - RGB: normal * 3 - RGB: diffuse color, A: shininess str. * 4 - unused @@ -7,19 +7,35 @@ uniform sampler2D tex_dif, tex_norm; -varying vec3 pos, norm; +varying vec3 pos, norm, tang; + +const float fog_start = 3.0; +const float fog_end = 6.0; void main() { vec3 n = normalize(norm); + vec3 t = normalize(tang); + vec3 b = cross(n, t); + + mat3 tbn_mat = mat3( + t.x, t.y, t.z, + b.x, b.y, b.z, + n.x, n.y, n.z); + + // grab normal from the normal map, remap it and transform it to view space + n = texture2D(tex_norm, gl_TexCoord[0].st).xyz * 2.0 - 1.0; + n = normalize(tbn_mat * n); + + float fog = clamp((fog_end + pos.z) / (fog_end - fog_start), 0.0, 1.0); vec4 texel = texture2D(tex_dif, gl_TexCoord[0].st); - vec3 diffuse = (gl_FrontMaterial.diffuse * texel).xyz; - vec3 spec = gl_FrontMaterial.specular.xyz; + vec3 diffuse = fog * (gl_FrontMaterial.diffuse * texel).xyz; + vec3 spec = fog * gl_FrontMaterial.specular.xyz; float sstr = (spec.x + spec.y + spec.z) / 3.0; gl_FragData[0] = vec4(pos, gl_FrontMaterial.shininess); - gl_FragData[1] = vec4(norm, 0.0); + gl_FragData[1] = vec4(n, 0.0); gl_FragData[2] = vec4(diffuse, sstr); //gl_FragData[3] = vec4(0.0, 0.0, 0.0, 0.0); } diff -r 85734f319626 -r d0e93b4d9ec9 prototype/sdr/mrt.v.glsl --- a/prototype/sdr/mrt.v.glsl Tue Aug 28 03:34:56 2012 +0300 +++ b/prototype/sdr/mrt.v.glsl Tue Aug 28 06:28:22 2012 +0300 @@ -1,4 +1,6 @@ -varying vec3 pos, norm; +attribute vec3 attr_tangent; + +varying vec3 pos, norm, tang; void main() { @@ -6,6 +8,7 @@ pos = (gl_ModelViewMatrix * gl_Vertex).xyz; norm = gl_NormalMatrix * gl_Normal; + tang = gl_NormalMatrix * attr_tangent; gl_TexCoord[0] = gl_MultiTexCoord0; } diff -r 85734f319626 -r d0e93b4d9ec9 prototype/src/material.cc --- a/prototype/src/material.cc Tue Aug 28 03:34:56 2012 +0300 +++ b/prototype/src/material.cc Tue Aug 28 06:28:22 2012 +0300 @@ -70,7 +70,18 @@ glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_DIFFUSE]); glEnable(GL_TEXTURE_2D); } else { + glActiveTextureARB(GL_TEXTURE0); glDisable(GL_TEXTURE_2D); } + + if(tex[TEXTYPE_NORMAL]) { + glActiveTextureARB(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, tex[TEXTYPE_NORMAL]); + glEnable(GL_TEXTURE_2D); + } else { + glActiveTextureARB(GL_TEXTURE1); + glDisable(GL_TEXTURE_2D); + } + glActiveTextureARB(GL_TEXTURE0); } diff -r 85734f319626 -r d0e93b4d9ec9 prototype/src/mesh.cc --- a/prototype/src/mesh.cc Tue Aug 28 03:34:56 2012 +0300 +++ b/prototype/src/mesh.cc Tue Aug 28 06:28:22 2012 +0300 @@ -177,7 +177,7 @@ glGetIntegerv(GL_CURRENT_PROGRAM, &prog); glUseProgram(0); - Vector3 *varr, *narr; + Vector3 *varr, *narr, *tarr; glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]); varr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); @@ -185,17 +185,27 @@ glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]); narr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); + glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_TANGENT]); + tarr = (Vector3*)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY); + glBegin(GL_LINES); for(unsigned int i=0; ix, varr->y, varr->z); glVertex3f(varr->x + narr->x * NSZ, varr->y + narr->y * NSZ, varr->z + narr->z * NSZ); + + glColor3f(1, 1, 0); + glVertex3f(varr->x, varr->y, varr->z); + glVertex3f(varr->x + tarr->x * NSZ, varr->y + tarr->y * NSZ, varr->z + tarr->z * NSZ); varr++; narr++; + tarr++; } glEnd(); glUnmapBuffer(GL_ARRAY_BUFFER); + glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_NORMAL]); + glUnmapBuffer(GL_ARRAY_BUFFER); glBindBuffer(GL_ARRAY_BUFFER, vbo[MESH_ATTR_VERTEX]); glUnmapBuffer(GL_ARRAY_BUFFER); diff -r 85734f319626 -r d0e93b4d9ec9 prototype/src/renderer.cc --- a/prototype/src/renderer.cc Tue Aug 28 03:34:56 2012 +0300 +++ b/prototype/src/renderer.cc Tue Aug 28 06:28:22 2012 +0300 @@ -9,13 +9,18 @@ #include "sdr.h" #include "datapath.h" +#undef DBG_VIS_MRT + +#ifdef DBG_VIS_MRT +static void deferred_debug(); +#endif static bool create_fbo(int xsz, int ysz); static unsigned int load_sdr(const char *vfname, const char *pfname); static int round_pow2(int x); -#define MRT_COUNT 4 +#define MRT_COUNT 3 static unsigned int mrt_tex[MRT_COUNT]; static unsigned int mrt_prog; @@ -102,6 +107,11 @@ return deferred_omni; } +int get_tangent_location(void) +{ + return get_attrib_loc(mrt_prog, "attr_tangent"); +} + void resize_renderer(int xsz, int ysz) { fb_xsz = xsz; @@ -143,6 +153,9 @@ glBindFramebufferEXT(GL_FRAMEBUFFER, 0); +#ifdef DBG_VIS_MRT + deferred_debug(); +#else // post-process lighting glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT); @@ -172,12 +185,14 @@ glUseProgram(0); glPopAttrib(); +#endif // DBG_VIS_MRT } static bool create_fbo(int xsz, int ysz) { - unsigned int clamp = GL_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP; + unsigned int clamp = GLEW_ARB_texture_border_clamp ? GL_CLAMP_TO_EDGE : GL_CLAMP; + // round the texture size up to the next power of 2 tex_xsz = round_pow2(xsz); tex_ysz = round_pow2(ysz); fb_xsz = xsz; @@ -262,3 +277,50 @@ x = (x >> 16) | x; return x + 1; } + +#ifdef DBG_VIS_MRT +// visualize the MRT buffers +static void deferred_debug() +{ + glPushAttrib(GL_ENABLE_BIT | GL_POLYGON_BIT); + glUseProgram(deferred_debug); + glDisable(GL_DEPTH_TEST); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + for(int i=0; i #include "tile.h" #include "tileset.h" +#include "renderer.h" using std::map; @@ -29,6 +30,7 @@ strcpy(saved_fname, fname); unsigned int proc_flags = aiProcess_JoinIdenticalVertices | + aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_SortByPType | aiProcess_FlipUVs; @@ -111,12 +113,20 @@ { int count = 0; + int attr_loc = get_tangent_location(); + if(attr_loc == -1) { + fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n"); + } + for(int i=0; i<(int)scn->mNumMeshes; i++) { Mesh *mesh = new Mesh; if(!mesh->create(scn, scn->mMeshes[i])) { delete mesh; continue; } + if(attr_loc != -1) { + mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc); + } Material mat; mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());