From 6e5f8cc918e89b279070b0335648149af43ac1bd Mon Sep 17 00:00:00 2001 From: SmileTheory Date: Tue, 12 Nov 2013 19:21:51 -0800 Subject: [PATCH] OpenGL2: Calculate bitangent in shader, and store normal/tangent as normalized byte arrays. --- code/renderergl2/glsl/generic_vp.glsl | 30 ++++---- code/renderergl2/glsl/lightall_vp.glsl | 24 ++++--- code/renderergl2/tr_animation.c | 12 ++-- code/renderergl2/tr_bsp.c | 6 +- code/renderergl2/tr_curve.c | 3 +- code/renderergl2/tr_glsl.c | 83 +++++----------------- code/renderergl2/tr_local.h | 54 ++++++-------- code/renderergl2/tr_main.c | 25 +++++-- code/renderergl2/tr_model.c | 37 +++++----- code/renderergl2/tr_model_iqm.c | 23 +++--- code/renderergl2/tr_shade.c | 1 - code/renderergl2/tr_shade_calc.c | 46 ++++++++---- code/renderergl2/tr_shader.c | 2 +- code/renderergl2/tr_surface.c | 97 +++++++++++++++++--------- code/renderergl2/tr_vbo.c | 96 +++++++++++-------------- 15 files changed, 268 insertions(+), 271 deletions(-) diff --git a/code/renderergl2/glsl/generic_vp.glsl b/code/renderergl2/glsl/generic_vp.glsl index 94e3c83e..baae6046 100644 --- a/code/renderergl2/glsl/generic_vp.glsl +++ b/code/renderergl2/glsl/generic_vp.glsl @@ -1,8 +1,8 @@ -attribute vec4 attr_Position; +attribute vec3 attr_Position; attribute vec3 attr_Normal; #if defined(USE_VERTEX_ANIMATION) -attribute vec4 attr_Position2; +attribute vec3 attr_Position2; attribute vec3 attr_Normal2; #endif @@ -167,7 +167,7 @@ vec4 CalcColor(vec3 position, vec3 normal) if (u_AlphaGen == AGEN_LIGHTING_SPECULAR) { - vec3 lightDir = normalize(vec3(-960.0, 1980.0, 96.0) - position.xyz); + vec3 lightDir = normalize(vec3(-960.0, 1980.0, 96.0) - position); vec3 reflected = -reflect(lightDir, normal); color.a = clamp(dot(reflected, normalize(viewer)), 0.0, 1.0); @@ -184,10 +184,10 @@ vec4 CalcColor(vec3 position, vec3 normal) #endif #if defined(USE_FOG) -float CalcFog(vec4 position) +float CalcFog(vec3 position) { - float s = dot(position, u_FogDistance) * 8.0; - float t = dot(position, u_FogDepth); + float s = (dot(position, u_FogDistance.xyz) + u_FogDistance.w) * 8.0; + float t = dot(position, u_FogDepth.xyz) + u_FogDepth.w; float eyeOutside = step(0.0, -u_FogEyeT); float fogged = step(eyeOutside, t); @@ -202,27 +202,27 @@ float CalcFog(vec4 position) void main() { #if defined(USE_VERTEX_ANIMATION) - vec4 position = mix(attr_Position, attr_Position2, u_VertexLerp); - vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp)); + vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp); + vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp) * 2.0 - vec3(1.0)); #else - vec4 position = attr_Position; - vec3 normal = attr_Normal; + vec3 position = attr_Position; + vec3 normal = attr_Normal * 2.0 - vec3(1.0); #endif #if defined(USE_DEFORM_VERTEXES) - position.xyz = DeformPosition(position.xyz, normal, attr_TexCoord0.st); + position = DeformPosition(position, normal, attr_TexCoord0.st); #endif - gl_Position = u_ModelViewProjectionMatrix * position; + gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0); #if defined(USE_TCGEN) - vec2 tex = GenTexCoords(u_TCGen0, position.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1); + vec2 tex = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1); #else vec2 tex = attr_TexCoord0.st; #endif #if defined(USE_TCMOD) - var_DiffuseTex = ModTexCoords(tex, position.xyz, u_DiffuseTexMatrix, u_DiffuseTexOffTurb); + var_DiffuseTex = ModTexCoords(tex, position, u_DiffuseTexMatrix, u_DiffuseTexOffTurb); #else var_DiffuseTex = tex; #endif @@ -232,7 +232,7 @@ void main() #endif #if defined(USE_RGBAGEN) - var_Color = CalcColor(position.xyz, normal); + var_Color = CalcColor(position, normal); #else var_Color = u_VertColor * attr_Color + u_BaseColor; #endif diff --git a/code/renderergl2/glsl/lightall_vp.glsl b/code/renderergl2/glsl/lightall_vp.glsl index aa0fe25b..f6164957 100644 --- a/code/renderergl2/glsl/lightall_vp.glsl +++ b/code/renderergl2/glsl/lightall_vp.glsl @@ -7,8 +7,7 @@ attribute vec4 attr_Color; attribute vec3 attr_Position; attribute vec3 attr_Normal; #if defined(USE_VERT_TANGENT_SPACE) -attribute vec3 attr_Tangent; -attribute vec3 attr_Bitangent; +attribute vec4 attr_Tangent; #endif #if defined(USE_VERTEX_ANIMATION) @@ -163,21 +162,24 @@ float CalcLightAttenuation(vec3 dir, float sqrRadius) void main() { #if defined(USE_VERTEX_ANIMATION) - vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp); - vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp)); - #if defined(USE_VERT_TANGENT_SPACE) - vec3 tangent = normalize(mix(attr_Tangent, attr_Tangent2, u_VertexLerp)); - vec3 bitangent = normalize(mix(attr_Bitangent, attr_Bitangent2, u_VertexLerp)); + vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp); + vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp) * 2.0 - vec3(1.0)); + #if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) + vec3 tangent = normalize(mix(attr_Tangent.xyz, attr_Tangent2.xyz, u_VertexLerp) * 2.0 - vec3(1.0)); #endif #else vec3 position = attr_Position; - vec3 normal = attr_Normal; - #if defined(USE_VERT_TANGENT_SPACE) - vec3 tangent = attr_Tangent; - vec3 bitangent = attr_Bitangent; + vec3 normal = attr_Normal * 2.0 - vec3(1.0); + #if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) + vec3 tangent = attr_Tangent.xyz * 2.0 - vec3(1.0); #endif #endif +#if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) + vec3 bitangent = cross(normal, tangent); + bitangent *= attr_Tangent.w * 2.0 - 1.0; +#endif + #if defined(USE_TCGEN) vec2 texCoords = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1); #else diff --git a/code/renderergl2/tr_animation.c b/code/renderergl2/tr_animation.c index 985fe9bb..f3329ee4 100644 --- a/code/renderergl2/tr_animation.c +++ b/code/renderergl2/tr_animation.c @@ -152,9 +152,9 @@ void RB_SurfaceAnim( md4Surface_t *surface ) { tess.xyz[baseVertex + j][1] = tempVert[1]; tess.xyz[baseVertex + j][2] = tempVert[2]; - tess.normal[baseVertex + j][0] = tempNormal[0]; - tess.normal[baseVertex + j][1] = tempNormal[1]; - tess.normal[baseVertex + j][2] = tempNormal[2]; + tess.normal[baseVertex + j][0] = (uint8_t)(tempNormal[0] * 127.5f + 128.0f); + tess.normal[baseVertex + j][1] = (uint8_t)(tempNormal[1] * 127.5f + 128.0f); + tess.normal[baseVertex + j][2] = (uint8_t)(tempNormal[2] * 127.5f + 128.0f); tess.texCoords[baseVertex + j][0][0] = v->texCoords[0]; tess.texCoords[baseVertex + j][0][1] = v->texCoords[1]; @@ -548,9 +548,9 @@ void RB_MDRSurfaceAnim( md4Surface_t *surface ) tess.xyz[baseVertex + j][1] = tempVert[1]; tess.xyz[baseVertex + j][2] = tempVert[2]; - tess.normal[baseVertex + j][0] = tempNormal[0]; - tess.normal[baseVertex + j][1] = tempNormal[1]; - tess.normal[baseVertex + j][2] = tempNormal[2]; + tess.normal[baseVertex + j][0] = (uint8_t)(tempNormal[0] * 127.5f + 128.0f); + tess.normal[baseVertex + j][1] = (uint8_t)(tempNormal[1] * 127.5f + 128.0f); + tess.normal[baseVertex + j][2] = (uint8_t)(tempNormal[2] * 127.5f + 128.0f); tess.texCoords[baseVertex + j][0][0] = v->texCoords[0]; tess.texCoords[baseVertex + j][0][1] = v->texCoords[1]; diff --git a/code/renderergl2/tr_bsp.c b/code/renderergl2/tr_bsp.c index 28b99dd3..2947e8ff 100644 --- a/code/renderergl2/tr_bsp.c +++ b/code/renderergl2/tr_bsp.c @@ -1810,12 +1810,14 @@ static void CopyVert(const srfVert_t * in, srfVert_t * out) out->xyz[j] = in->xyz[j]; #ifdef USE_VERT_TANGENT_SPACE out->tangent[j] = in->tangent[j]; - out->bitangent[j] = in->bitangent[j]; + //out->bitangent[j] = in->bitangent[j]; #endif out->normal[j] = in->normal[j]; out->lightdir[j] = in->lightdir[j]; } + out->tangent[3] = in->tangent[3]; + for(j = 0; j < 2; j++) { out->st[j] = in->st[j]; @@ -2000,7 +2002,7 @@ static void R_CreateWorldVBOs(void) #ifdef USE_VERT_TANGENT_SPACE vbo = R_CreateVBO2(va("staticBspModel0_VBO %i", k), numVerts, verts, - ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_TANGENT | ATTR_BITANGENT | + ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_TANGENT | ATTR_NORMAL | ATTR_COLOR | ATTR_LIGHTDIRECTION, VBO_USAGE_STATIC); #else vbo = R_CreateVBO2(va("staticBspModel0_VBO %i", k), numVerts, verts, diff --git a/code/renderergl2/tr_curve.c b/code/renderergl2/tr_curve.c index 4f36243b..91c80a4d 100644 --- a/code/renderergl2/tr_curve.c +++ b/code/renderergl2/tr_curve.c @@ -247,8 +247,7 @@ static void MakeMeshTangentVectors(int width, int height, srfVert_t ctrl[MAX_GRI dv[0] = &ctrl2[j * width + i]; dv[1] = &ctrl[j][i]; - VectorCopy(dv[0]->tangent, dv[1]->tangent); - VectorCopy(dv[0]->bitangent, dv[1]->bitangent); + VectorCopy4(dv[0]->tangent, dv[1]->tangent); } } } diff --git a/code/renderergl2/tr_glsl.c b/code/renderergl2/tr_glsl.c index d67744f8..d0573b97 100644 --- a/code/renderergl2/tr_glsl.c +++ b/code/renderergl2/tr_glsl.c @@ -528,9 +528,6 @@ static int GLSL_InitGPUShader2(shaderProgram_t * program, const char *name, int #ifdef USE_VERT_TANGENT_SPACE if(attribs & ATTR_TANGENT) qglBindAttribLocationARB(program->program, ATTR_INDEX_TANGENT, "attr_Tangent"); - - if(attribs & ATTR_BITANGENT) - qglBindAttribLocationARB(program->program, ATTR_INDEX_BITANGENT, "attr_Bitangent"); #endif if(attribs & ATTR_NORMAL) @@ -554,9 +551,6 @@ static int GLSL_InitGPUShader2(shaderProgram_t * program, const char *name, int #ifdef USE_VERT_TANGENT_SPACE if(attribs & ATTR_TANGENT2) qglBindAttribLocationARB(program->program, ATTR_INDEX_TANGENT2, "attr_Tangent2"); - - if(attribs & ATTR_BITANGENT2) - qglBindAttribLocationARB(program->program, ATTR_INDEX_BITANGENT2, "attr_Bitangent2"); #endif GLSL_LinkProgram(program->program); @@ -1069,7 +1063,7 @@ void GLSL_InitGPUShaders(void) #ifdef USE_VERT_TANGENT_SPACE Q_strcat(extradefines, 1024, "#define USE_VERT_TANGENT_SPACE\n"); - attribs |= ATTR_TANGENT | ATTR_BITANGENT; + attribs |= ATTR_TANGENT; #endif if ((i & LIGHTDEF_USE_PARALLAXMAP) && !(i & LIGHTDEF_ENTITY) && r_parallaxMapping->integer) @@ -1133,7 +1127,7 @@ void GLSL_InitGPUShaders(void) #ifdef USE_VERT_TANGENT_SPACE if (r_normalMapping->integer) { - attribs |= ATTR_TANGENT2 | ATTR_BITANGENT2; + attribs |= ATTR_TANGENT2; } #endif } @@ -1400,12 +1394,10 @@ void GLSL_ShutdownGPUShaders(void) qglDisableVertexAttribArrayARB(ATTR_INDEX_NORMAL); #ifdef USE_VERT_TANGENT_SPACE qglDisableVertexAttribArrayARB(ATTR_INDEX_TANGENT); - qglDisableVertexAttribArrayARB(ATTR_INDEX_BITANGENT); #endif qglDisableVertexAttribArrayARB(ATTR_INDEX_NORMAL2); #ifdef USE_VERT_TANGENT_SPACE qglDisableVertexAttribArrayARB(ATTR_INDEX_TANGENT2); - qglDisableVertexAttribArrayARB(ATTR_INDEX_BITANGENT2); #endif qglDisableVertexAttribArrayARB(ATTR_INDEX_COLOR); qglDisableVertexAttribArrayARB(ATTR_INDEX_LIGHTDIRECTION); @@ -1565,20 +1557,6 @@ void GLSL_VertexAttribsState(uint32_t stateBits) qglDisableVertexAttribArrayARB(ATTR_INDEX_TANGENT); } } - - if(diff & ATTR_BITANGENT) - { - if(stateBits & ATTR_BITANGENT) - { - GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_BITANGENT )\n"); - qglEnableVertexAttribArrayARB(ATTR_INDEX_BITANGENT); - } - else - { - GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_BITANGENT )\n"); - qglDisableVertexAttribArrayARB(ATTR_INDEX_BITANGENT); - } - } #endif if(diff & ATTR_COLOR) @@ -1651,20 +1629,6 @@ void GLSL_VertexAttribsState(uint32_t stateBits) qglDisableVertexAttribArrayARB(ATTR_INDEX_TANGENT2); } } - - if(diff & ATTR_BITANGENT2) - { - if(stateBits & ATTR_BITANGENT2) - { - GLimp_LogComment("qglEnableVertexAttribArrayARB( ATTR_INDEX_BITANGENT2 )\n"); - qglEnableVertexAttribArrayARB(ATTR_INDEX_BITANGENT2); - } - else - { - GLimp_LogComment("qglDisableVertexAttribArrayARB( ATTR_INDEX_BITANGENT2 )\n"); - qglDisableVertexAttribArrayARB(ATTR_INDEX_BITANGENT2); - } - } #endif glState.vertexAttribsState = stateBits; @@ -1674,17 +1638,18 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { qboolean animated; int newFrame, oldFrame; + VBO_t *vbo = glState.currentVBO; - if(!glState.currentVBO) + if(!vbo) { ri.Error(ERR_FATAL, "GL_VertexAttribPointers: no VBO bound"); return; } // don't just call LogComment, or we will get a call to va() every frame! - GLimp_LogComment(va("--- GL_VertexAttribPointers( %s ) ---\n", glState.currentVBO->name)); + GLimp_LogComment(va("--- GL_VertexAttribPointers( %s ) ---\n", vbo->name)); - // position/normal/tangent/bitangent are always set in case of animation + // position/normal/tangent are always set in case of animation oldFrame = glState.vertexAttribsOldFrame; newFrame = glState.vertexAttribsNewFrame; animated = glState.vertexAnimation; @@ -1693,7 +1658,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_POSITION )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_POSITION, 3, GL_FLOAT, 0, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz + newFrame * glState.currentVBO->size_xyz)); + qglVertexAttribPointerARB(ATTR_INDEX_POSITION, 3, GL_FLOAT, 0, vbo->stride_xyz, BUFFER_OFFSET(vbo->ofs_xyz + newFrame * vbo->size_xyz)); glState.vertexAttribPointersSet |= ATTR_POSITION; } @@ -1701,7 +1666,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_TEXCOORD )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_TEXCOORD0, 2, GL_FLOAT, 0, glState.currentVBO->stride_st, BUFFER_OFFSET(glState.currentVBO->ofs_st)); + qglVertexAttribPointerARB(ATTR_INDEX_TEXCOORD0, 2, GL_FLOAT, 0, vbo->stride_st, BUFFER_OFFSET(vbo->ofs_st)); glState.vertexAttribPointersSet |= ATTR_TEXCOORD; } @@ -1709,7 +1674,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_LIGHTCOORD )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_TEXCOORD1, 2, GL_FLOAT, 0, glState.currentVBO->stride_lightmap, BUFFER_OFFSET(glState.currentVBO->ofs_lightmap)); + qglVertexAttribPointerARB(ATTR_INDEX_TEXCOORD1, 2, GL_FLOAT, 0, vbo->stride_lightmap, BUFFER_OFFSET(vbo->ofs_lightmap)); glState.vertexAttribPointersSet |= ATTR_LIGHTCOORD; } @@ -1717,7 +1682,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_NORMAL )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_NORMAL, 3, GL_FLOAT, 0, glState.currentVBO->stride_normal, BUFFER_OFFSET(glState.currentVBO->ofs_normal + newFrame * glState.currentVBO->size_normal)); + qglVertexAttribPointerARB(ATTR_INDEX_NORMAL, 3, GL_UNSIGNED_BYTE, GL_TRUE, vbo->stride_normal, BUFFER_OFFSET(vbo->ofs_normal + newFrame * vbo->size_normal)); glState.vertexAttribPointersSet |= ATTR_NORMAL; } @@ -1726,24 +1691,16 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_TANGENT )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_TANGENT, 3, GL_FLOAT, 0, glState.currentVBO->stride_tangent, BUFFER_OFFSET(glState.currentVBO->ofs_tangent + newFrame * glState.currentVBO->size_normal)); // FIXME + qglVertexAttribPointerARB(ATTR_INDEX_TANGENT, 4, GL_UNSIGNED_BYTE, GL_TRUE, vbo->stride_tangent, BUFFER_OFFSET(vbo->ofs_tangent + newFrame * vbo->size_normal)); // FIXME glState.vertexAttribPointersSet |= ATTR_TANGENT; } - - if((attribBits & ATTR_BITANGENT) && (!(glState.vertexAttribPointersSet & ATTR_BITANGENT) || animated)) - { - GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_BITANGENT )\n"); - - qglVertexAttribPointerARB(ATTR_INDEX_BITANGENT, 3, GL_FLOAT, 0, glState.currentVBO->stride_bitangent, BUFFER_OFFSET(glState.currentVBO->ofs_bitangent + newFrame * glState.currentVBO->size_normal)); // FIXME - glState.vertexAttribPointersSet |= ATTR_BITANGENT; - } #endif if((attribBits & ATTR_COLOR) && !(glState.vertexAttribPointersSet & ATTR_COLOR)) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_COLOR )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_COLOR, 4, GL_FLOAT, 0, glState.currentVBO->stride_vertexcolor, BUFFER_OFFSET(glState.currentVBO->ofs_vertexcolor)); + qglVertexAttribPointerARB(ATTR_INDEX_COLOR, 4, GL_FLOAT, 0, vbo->stride_vertexcolor, BUFFER_OFFSET(vbo->ofs_vertexcolor)); glState.vertexAttribPointersSet |= ATTR_COLOR; } @@ -1751,7 +1708,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_LIGHTDIRECTION )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_LIGHTDIRECTION, 3, GL_FLOAT, 0, glState.currentVBO->stride_lightdir, BUFFER_OFFSET(glState.currentVBO->ofs_lightdir)); + qglVertexAttribPointerARB(ATTR_INDEX_LIGHTDIRECTION, 3, GL_FLOAT, 0, vbo->stride_lightdir, BUFFER_OFFSET(vbo->ofs_lightdir)); glState.vertexAttribPointersSet |= ATTR_LIGHTDIRECTION; } @@ -1759,7 +1716,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_POSITION2 )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_POSITION2, 3, GL_FLOAT, 0, glState.currentVBO->stride_xyz, BUFFER_OFFSET(glState.currentVBO->ofs_xyz + oldFrame * glState.currentVBO->size_xyz)); + qglVertexAttribPointerARB(ATTR_INDEX_POSITION2, 3, GL_FLOAT, 0, vbo->stride_xyz, BUFFER_OFFSET(vbo->ofs_xyz + oldFrame * vbo->size_xyz)); glState.vertexAttribPointersSet |= ATTR_POSITION2; } @@ -1767,7 +1724,7 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_NORMAL2 )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_NORMAL2, 3, GL_FLOAT, 0, glState.currentVBO->stride_normal, BUFFER_OFFSET(glState.currentVBO->ofs_normal + oldFrame * glState.currentVBO->size_normal)); + qglVertexAttribPointerARB(ATTR_INDEX_NORMAL2, 3, GL_UNSIGNED_BYTE, GL_TRUE, vbo->stride_normal, BUFFER_OFFSET(vbo->ofs_normal + oldFrame * vbo->size_normal)); glState.vertexAttribPointersSet |= ATTR_NORMAL2; } @@ -1776,17 +1733,9 @@ void GLSL_VertexAttribPointers(uint32_t attribBits) { GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_TANGENT2 )\n"); - qglVertexAttribPointerARB(ATTR_INDEX_TANGENT2, 3, GL_FLOAT, 0, glState.currentVBO->stride_tangent, BUFFER_OFFSET(glState.currentVBO->ofs_tangent + oldFrame * glState.currentVBO->size_normal)); // FIXME + qglVertexAttribPointerARB(ATTR_INDEX_TANGENT2, 4, GL_UNSIGNED_BYTE, GL_TRUE, vbo->stride_tangent, BUFFER_OFFSET(vbo->ofs_tangent + oldFrame * vbo->size_normal)); // FIXME glState.vertexAttribPointersSet |= ATTR_TANGENT2; } - - if((attribBits & ATTR_BITANGENT2) && (!(glState.vertexAttribPointersSet & ATTR_BITANGENT2) || animated)) - { - GLimp_LogComment("qglVertexAttribPointerARB( ATTR_INDEX_BITANGENT2 )\n"); - - qglVertexAttribPointerARB(ATTR_INDEX_BITANGENT2, 3, GL_FLOAT, 0, glState.currentVBO->stride_bitangent, BUFFER_OFFSET(glState.currentVBO->ofs_bitangent + oldFrame * glState.currentVBO->size_normal)); // FIXME - glState.vertexAttribPointersSet |= ATTR_BITANGENT2; - } #endif } diff --git a/code/renderergl2/tr_local.h b/code/renderergl2/tr_local.h index 368dd39e..1adfc361 100644 --- a/code/renderergl2/tr_local.h +++ b/code/renderergl2/tr_local.h @@ -116,7 +116,6 @@ typedef struct VBO_s uint32_t ofs_lightdir; #ifdef USE_VERT_TANGENT_SPACE uint32_t ofs_tangent; - uint32_t ofs_bitangent; #endif uint32_t stride_xyz; uint32_t stride_normal; @@ -126,7 +125,6 @@ typedef struct VBO_s uint32_t stride_lightdir; #ifdef USE_VERT_TANGENT_SPACE uint32_t stride_tangent; - uint32_t stride_bitangent; #endif uint32_t size_xyz; uint32_t size_normal; @@ -515,19 +513,17 @@ enum ATTR_INDEX_TEXCOORD0 = 1, ATTR_INDEX_TEXCOORD1 = 2, ATTR_INDEX_TANGENT = 3, - ATTR_INDEX_BITANGENT = 4, - ATTR_INDEX_NORMAL = 5, - ATTR_INDEX_COLOR = 6, - ATTR_INDEX_PAINTCOLOR = 7, - ATTR_INDEX_LIGHTDIRECTION = 8, - ATTR_INDEX_BONE_INDEXES = 9, - ATTR_INDEX_BONE_WEIGHTS = 10, + ATTR_INDEX_NORMAL = 4, + ATTR_INDEX_COLOR = 5, + ATTR_INDEX_PAINTCOLOR = 6, + ATTR_INDEX_LIGHTDIRECTION = 7, + ATTR_INDEX_BONE_INDEXES = 8, + ATTR_INDEX_BONE_WEIGHTS = 9, // GPU vertex animations - ATTR_INDEX_POSITION2 = 11, - ATTR_INDEX_TANGENT2 = 12, - ATTR_INDEX_BITANGENT2 = 13, - ATTR_INDEX_NORMAL2 = 14 + ATTR_INDEX_POSITION2 = 10, + ATTR_INDEX_TANGENT2 = 11, + ATTR_INDEX_NORMAL2 = 12 }; enum @@ -613,26 +609,23 @@ enum ATTR_TEXCOORD = 0x0002, ATTR_LIGHTCOORD = 0x0004, ATTR_TANGENT = 0x0008, - ATTR_BITANGENT = 0x0010, - ATTR_NORMAL = 0x0020, - ATTR_COLOR = 0x0040, - ATTR_PAINTCOLOR = 0x0080, - ATTR_LIGHTDIRECTION = 0x0100, - ATTR_BONE_INDEXES = 0x0200, - ATTR_BONE_WEIGHTS = 0x0400, + ATTR_NORMAL = 0x0010, + ATTR_COLOR = 0x0020, + ATTR_PAINTCOLOR = 0x0040, + ATTR_LIGHTDIRECTION = 0x0080, + ATTR_BONE_INDEXES = 0x0100, + ATTR_BONE_WEIGHTS = 0x0200, // for .md3 interpolation - ATTR_POSITION2 = 0x0800, - ATTR_TANGENT2 = 0x1000, - ATTR_BITANGENT2 = 0x2000, - ATTR_NORMAL2 = 0x4000, + ATTR_POSITION2 = 0x0400, + ATTR_TANGENT2 = 0x0800, + ATTR_NORMAL2 = 0x1000, ATTR_DEFAULT = ATTR_POSITION, ATTR_BITS = ATTR_POSITION | ATTR_TEXCOORD | ATTR_LIGHTCOORD | ATTR_TANGENT | - ATTR_BITANGENT | ATTR_NORMAL | ATTR_COLOR | ATTR_PAINTCOLOR | @@ -641,7 +634,6 @@ enum ATTR_BONE_WEIGHTS | ATTR_POSITION2 | ATTR_TANGENT2 | - ATTR_BITANGENT2 | ATTR_NORMAL2 }; @@ -986,8 +978,7 @@ typedef struct vec2_t lightmap; vec3_t normal; #ifdef USE_VERT_TANGENT_SPACE - vec3_t tangent; - vec3_t bitangent; + vec4_t tangent; #endif vec3_t lightdir; vec4_t vertexColors; @@ -998,7 +989,7 @@ typedef struct } srfVert_t; #ifdef USE_VERT_TANGENT_SPACE -#define srfVert_t_cleared(x) srfVert_t (x) = {{0, 0, 0}, {0, 0}, {0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0, 0}} +#define srfVert_t_cleared(x) srfVert_t (x) = {{0, 0, 0}, {0, 0}, {0, 0}, {0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0}, {0, 0, 0, 0}} #else #define srfVert_t_cleared(x) srfVert_t (x) = {{0, 0, 0}, {0, 0}, {0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0, 0}} #endif @@ -2094,10 +2085,9 @@ typedef struct shaderCommands_s { glIndex_t indexes[SHADER_MAX_INDEXES] QALIGN(16); vec4_t xyz[SHADER_MAX_VERTEXES] QALIGN(16); - vec4_t normal[SHADER_MAX_VERTEXES] QALIGN(16); + uint8_t normal[SHADER_MAX_VERTEXES][4] QALIGN(16); #ifdef USE_VERT_TANGENT_SPACE - vec4_t tangent[SHADER_MAX_VERTEXES] QALIGN(16); - vec4_t bitangent[SHADER_MAX_VERTEXES] QALIGN(16); + uint8_t tangent[SHADER_MAX_VERTEXES][4] QALIGN(16); #endif vec2_t texCoords[SHADER_MAX_VERTEXES][2] QALIGN(16); vec4_t vertexColors[SHADER_MAX_VERTEXES] QALIGN(16); diff --git a/code/renderergl2/tr_main.c b/code/renderergl2/tr_main.c index a198583c..e87bb8ee 100644 --- a/code/renderergl2/tr_main.c +++ b/code/renderergl2/tr_main.c @@ -454,6 +454,8 @@ qboolean R_CalcTangentVectors(srfVert_t * dv[3]) /* do each vertex */ for(i = 0; i < 3; i++) { + vec3_t bitangent, nxt; + // calculate s tangent vector s = dv[i]->st[0] + 10.0f; t = dv[i]->st[1]; @@ -475,12 +477,16 @@ qboolean R_CalcTangentVectors(srfVert_t * dv[3]) bary[1] = ((dv[2]->st[0] - s) * (dv[0]->st[1] - t) - (dv[0]->st[0] - s) * (dv[2]->st[1] - t)) / bb; bary[2] = ((dv[0]->st[0] - s) * (dv[1]->st[1] - t) - (dv[1]->st[0] - s) * (dv[0]->st[1] - t)) / bb; - dv[i]->bitangent[0] = bary[0] * dv[0]->xyz[0] + bary[1] * dv[1]->xyz[0] + bary[2] * dv[2]->xyz[0]; - dv[i]->bitangent[1] = bary[0] * dv[0]->xyz[1] + bary[1] * dv[1]->xyz[1] + bary[2] * dv[2]->xyz[1]; - dv[i]->bitangent[2] = bary[0] * dv[0]->xyz[2] + bary[1] * dv[1]->xyz[2] + bary[2] * dv[2]->xyz[2]; + bitangent[0] = bary[0] * dv[0]->xyz[0] + bary[1] * dv[1]->xyz[0] + bary[2] * dv[2]->xyz[0]; + bitangent[1] = bary[0] * dv[0]->xyz[1] + bary[1] * dv[1]->xyz[1] + bary[2] * dv[2]->xyz[1]; + bitangent[2] = bary[0] * dv[0]->xyz[2] + bary[1] * dv[1]->xyz[2] + bary[2] * dv[2]->xyz[2]; - VectorSubtract(dv[i]->bitangent, dv[i]->xyz, dv[i]->bitangent); - VectorNormalize(dv[i]->bitangent); + VectorSubtract(bitangent, dv[i]->xyz, bitangent); + VectorNormalize(bitangent); + + // store bitangent handedness + CrossProduct(dv[i]->normal, dv[i]->tangent, nxt); + dv[i]->tangent[3] = (DotProduct(nxt, bitangent) < 0.0f) ? -1.0f : 1.0f; // debug code //% Sys_FPrintf( SYS_VRB, "%d S: (%f %f %f) T: (%f %f %f)\n", i, @@ -1556,7 +1562,8 @@ static qboolean SurfIsOffscreen( const drawSurf_t *drawSurf, vec4_t clipDest[128 for ( i = 0; i < tess.numIndexes; i += 3 ) { - vec3_t normal; + vec3_t normal, tNormal; + float len; VectorSubtract( tess.xyz[tess.indexes[i]], tr.viewParms.or.origin, normal ); @@ -1567,7 +1574,11 @@ static qboolean SurfIsOffscreen( const drawSurf_t *drawSurf, vec4_t clipDest[128 shortest = len; } - if ( DotProduct( normal, tess.normal[tess.indexes[i]] ) >= 0 ) + tNormal[0] = tess.normal[tess.indexes[i]][0] / 127.5f - 1.0f; + tNormal[1] = tess.normal[tess.indexes[i]][1] / 127.5f - 1.0f; + tNormal[2] = tess.normal[tess.indexes[i]][2] / 127.5f - 1.0f; + + if ( DotProduct( normal, tNormal ) >= 0 ) { numTriangles--; } diff --git a/code/renderergl2/tr_model.c b/code/renderergl2/tr_model.c index a97dd6be..0b1500d6 100644 --- a/code/renderergl2/tr_model.c +++ b/code/renderergl2/tr_model.c @@ -689,11 +689,10 @@ static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize, for (i = 0; i < mdvModel->numSurfaces; i++, vboSurf++, surf++) { vec3_t *verts; - vec3_t *normals; vec2_t *texcoords; + uint8_t *normals; #ifdef USE_VERT_TANGENT_SPACE - vec3_t *tangents; - vec3_t *bitangents; + uint8_t *tangents; #endif byte *data; @@ -701,7 +700,7 @@ static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize, int ofs_xyz, ofs_normal, ofs_st; #ifdef USE_VERT_TANGENT_SPACE - int ofs_tangent, ofs_bitangent; + int ofs_tangent; #endif dataSize = 0; @@ -710,14 +709,11 @@ static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize, dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*verts); ofs_normal = dataSize; - dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*normals); + dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*normals) * 4; #ifdef USE_VERT_TANGENT_SPACE ofs_tangent = dataSize; - dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*tangents); - - ofs_bitangent = dataSize; - dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*bitangents); + dataSize += surf->numVerts * mdvModel->numFrames * sizeof(*tangents) * 4; #endif ofs_st = dataSize; @@ -729,18 +725,25 @@ static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize, normals = (void *)(data + ofs_normal); #ifdef USE_VERT_TANGENT_SPACE tangents = (void *)(data + ofs_tangent); - bitangents = (void *)(data + ofs_bitangent); #endif texcoords = (void *)(data + ofs_st); v = surf->verts; for ( j = 0; j < surf->numVerts * mdvModel->numFrames ; j++, v++ ) { + vec3_t nxt; + + CrossProduct(v->normal, v->tangent, nxt); VectorCopy(v->xyz, verts[j]); - VectorCopy(v->normal, normals[j]); + normals[j*4+0] = (uint8_t)(v->normal[0] * 127.5f + 128.0f); + normals[j*4+1] = (uint8_t)(v->normal[1] * 127.5f + 128.0f); + normals[j*4+2] = (uint8_t)(v->normal[2] * 127.5f + 128.0f); + normals[j*4+3] = 0; #ifdef USE_VERT_TANGENT_SPACE - VectorCopy(v->tangent, tangents[j]); - VectorCopy(v->bitangent, bitangents[j]); + tangents[j*4+0] = (uint8_t)(v->tangent[0] * 127.5f + 128.0f); + tangents[j*4+1] = (uint8_t)(v->tangent[1] * 127.5f + 128.0f); + tangents[j*4+2] = (uint8_t)(v->tangent[2] * 127.5f + 128.0f); + tangents[j*4+3] = (DotProduct(nxt, v->bitangent) < 0.0f) ? 0 : 255; #endif } @@ -765,20 +768,18 @@ static qboolean R_LoadMD3(model_t * mod, int lod, void *buffer, int bufferSize, vboSurf->vbo->ofs_normal = ofs_normal; #ifdef USE_VERT_TANGENT_SPACE vboSurf->vbo->ofs_tangent = ofs_tangent; - vboSurf->vbo->ofs_bitangent = ofs_bitangent; #endif vboSurf->vbo->ofs_st = ofs_st; vboSurf->vbo->stride_xyz = sizeof(*verts); - vboSurf->vbo->stride_normal = sizeof(*normals); + vboSurf->vbo->stride_normal = sizeof(*normals) * 4; #ifdef USE_VERT_TANGENT_SPACE - vboSurf->vbo->stride_tangent = sizeof(*tangents); - vboSurf->vbo->stride_bitangent = sizeof(*bitangents); + vboSurf->vbo->stride_tangent = sizeof(*tangents) * 4; #endif vboSurf->vbo->stride_st = sizeof(*st); vboSurf->vbo->size_xyz = sizeof(*verts) * surf->numVerts; - vboSurf->vbo->size_normal = sizeof(*normals) * surf->numVerts; + vboSurf->vbo->size_normal = sizeof(*normals) * surf->numVerts * 4; ri.Free(data); diff --git a/code/renderergl2/tr_model_iqm.c b/code/renderergl2/tr_model_iqm.c index 44c4a956..d41a8f52 100644 --- a/code/renderergl2/tr_model_iqm.c +++ b/code/renderergl2/tr_model_iqm.c @@ -1024,7 +1024,7 @@ void RB_IQMSurfaceAnim( surfaceType_t *surface ) { int i; vec4_t *outXYZ; - vec4_t *outNormal; + uint8_t *outNormal; vec2_t (*outTexCoord)[2]; vec4_t *outColor; @@ -1039,7 +1039,7 @@ void RB_IQMSurfaceAnim( surfaceType_t *surface ) { RB_CHECKOVERFLOW( surf->num_vertexes, surf->num_triangles * 3 ); outXYZ = &tess.xyz[tess.numVertexes]; - outNormal = &tess.normal[tess.numVertexes]; + outNormal = &tess.normal[tess.numVertexes][0]; outTexCoord = &tess.texCoords[tess.numVertexes]; outColor = &tess.vertexColors[tess.numVertexes]; @@ -1050,7 +1050,7 @@ void RB_IQMSurfaceAnim( surfaceType_t *surface ) { // transform vertexes and fill other data for( i = 0; i < surf->num_vertexes; - i++, outXYZ++, outNormal++, outTexCoord++, outColor++ ) { + i++, outXYZ++, outNormal+=4, outTexCoord++, outColor++ ) { int j, k; float vtxMat[12]; float nrmMat[9]; @@ -1116,19 +1116,22 @@ void RB_IQMSurfaceAnim( surfaceType_t *surface ) { vtxMat[11]; (*outXYZ)[3] = 1.0f; - (*outNormal)[0] = + (outNormal)[0] = (uint8_t)(( nrmMat[ 0] * data->normals[3*vtx+0] + nrmMat[ 1] * data->normals[3*vtx+1] + - nrmMat[ 2] * data->normals[3*vtx+2]; - (*outNormal)[1] = + nrmMat[ 2] * data->normals[3*vtx+2] + )* 127.5f + 128.0f); + (outNormal)[1] = (uint8_t)(( nrmMat[ 3] * data->normals[3*vtx+0] + nrmMat[ 4] * data->normals[3*vtx+1] + - nrmMat[ 5] * data->normals[3*vtx+2]; - (*outNormal)[2] = + nrmMat[ 5] * data->normals[3*vtx+2] + )* 127.5f + 128.0f); + (outNormal)[2] = (uint8_t)(( nrmMat[ 6] * data->normals[3*vtx+0] + nrmMat[ 7] * data->normals[3*vtx+1] + - nrmMat[ 8] * data->normals[3*vtx+2]; - (*outNormal)[3] = 0.0f; + nrmMat[ 8] * data->normals[3*vtx+2] + )* 127.5f + 128.0f); + (outNormal)[3] = 0; (*outColor)[0] = data->colors[4*vtx+0] / 255.0f; (*outColor)[1] = data->colors[4*vtx+1] / 255.0f; diff --git a/code/renderergl2/tr_shade.c b/code/renderergl2/tr_shade.c index 608de9d8..695b42fc 100644 --- a/code/renderergl2/tr_shade.c +++ b/code/renderergl2/tr_shade.c @@ -1027,7 +1027,6 @@ static unsigned int RB_CalcShaderVertexAttribs( shaderCommands_t *input ) vertexAttribs |= ATTR_NORMAL2; #ifdef USE_VERT_TANGENT_SPACE vertexAttribs |= ATTR_TANGENT2; - vertexAttribs |= ATTR_BITANGENT2; #endif } } diff --git a/code/renderergl2/tr_shade_calc.c b/code/renderergl2/tr_shade_calc.c index faab03e5..d77136e7 100644 --- a/code/renderergl2/tr_shade_calc.c +++ b/code/renderergl2/tr_shade_calc.c @@ -116,7 +116,7 @@ void RB_CalcDeformVertexes( deformStage_t *ds ) vec3_t offset; float scale; float *xyz = ( float * ) tess.xyz; - float *normal = ( float * ) tess.normal; + uint8_t *normal = ( uint8_t * ) tess.normal; float *table; if ( ds->deformationWave.frequency == 0 ) @@ -125,7 +125,9 @@ void RB_CalcDeformVertexes( deformStage_t *ds ) for ( i = 0; i < tess.numVertexes; i++, xyz += 4, normal += 4 ) { - VectorScale( normal, scale, offset ); + offset[0] = (normal[0] / 127.5f - 1.0f) * scale; + offset[1] = (normal[1] / 127.5f - 1.0f) * scale; + offset[2] = (normal[2] / 127.5f - 1.0f) * scale; xyz[0] += offset[0]; xyz[1] += offset[1]; @@ -145,7 +147,9 @@ void RB_CalcDeformVertexes( deformStage_t *ds ) ds->deformationWave.phase + off, ds->deformationWave.frequency ); - VectorScale( normal, scale, offset ); + offset[0] = (normal[0] / 127.5f - 1.0f) * scale; + offset[1] = (normal[1] / 127.5f - 1.0f) * scale; + offset[2] = (normal[2] / 127.5f - 1.0f) * scale; xyz[0] += offset[0]; xyz[1] += offset[1]; @@ -165,25 +169,35 @@ void RB_CalcDeformNormals( deformStage_t *ds ) { int i; float scale; float *xyz = ( float * ) tess.xyz; - float *normal = ( float * ) tess.normal; + uint8_t *normal = ( uint8_t * ) tess.normal; for ( i = 0; i < tess.numVertexes; i++, xyz += 4, normal += 4 ) { + vec3_t fNormal; + + fNormal[0] = normal[0] / 127.5f - 1.0f; + fNormal[1] = normal[1] / 127.5f - 1.0f; + fNormal[2] = normal[2] / 127.5f - 1.0f; + scale = 0.98f; scale = R_NoiseGet4f( xyz[0] * scale, xyz[1] * scale, xyz[2] * scale, tess.shaderTime * ds->deformationWave.frequency ); - normal[ 0 ] += ds->deformationWave.amplitude * scale; + fNormal[ 0 ] += ds->deformationWave.amplitude * scale; scale = 0.98f; scale = R_NoiseGet4f( 100 + xyz[0] * scale, xyz[1] * scale, xyz[2] * scale, tess.shaderTime * ds->deformationWave.frequency ); - normal[ 1 ] += ds->deformationWave.amplitude * scale; + fNormal[ 1 ] += ds->deformationWave.amplitude * scale; scale = 0.98f; scale = R_NoiseGet4f( 200 + xyz[0] * scale, xyz[1] * scale, xyz[2] * scale, tess.shaderTime * ds->deformationWave.frequency ); - normal[ 2 ] += ds->deformationWave.amplitude * scale; + fNormal[ 2 ] += ds->deformationWave.amplitude * scale; - VectorNormalizeFast( normal ); + VectorNormalizeFast( fNormal ); + + normal[0] = (uint8_t)(fNormal[0] * 127.5f + 128.0f); + normal[1] = (uint8_t)(fNormal[0] * 127.5f + 128.0f); + normal[2] = (uint8_t)(fNormal[0] * 127.5f + 128.0f); } } @@ -197,7 +211,7 @@ void RB_CalcBulgeVertexes( deformStage_t *ds ) { int i; const float *st = ( const float * ) tess.texCoords[0]; float *xyz = ( float * ) tess.xyz; - float *normal = ( float * ) tess.normal; + uint8_t *normal = ( uint8_t * ) tess.normal; float now; now = backEnd.refdef.time * ds->bulgeSpeed * 0.001f; @@ -210,9 +224,9 @@ void RB_CalcBulgeVertexes( deformStage_t *ds ) { scale = tr.sinTable[ off & FUNCTABLE_MASK ] * ds->bulgeHeight; - xyz[0] += normal[0] * scale; - xyz[1] += normal[1] * scale; - xyz[2] += normal[2] * scale; + xyz[0] += (normal[0] / 127.5f - 1.0f) * scale; + xyz[1] += (normal[1] / 127.5f - 1.0f) * scale; + xyz[2] += (normal[2] / 127.5f - 1.0f) * scale; } } @@ -262,11 +276,17 @@ void DeformText( const char *text ) { float color[4]; float bottom, top; vec3_t mid; + vec3_t fNormal; height[0] = 0; height[1] = 0; height[2] = -1; - CrossProduct( tess.normal[0], height, width ); + + fNormal[0] = tess.normal[0][0] / 127.5f - 1.0f; + fNormal[1] = tess.normal[0][1] / 127.5f - 1.0f; + fNormal[2] = tess.normal[0][2] / 127.5f - 1.0f; + + CrossProduct( fNormal, height, width ); // find the midpoint of the box VectorClear( mid ); diff --git a/code/renderergl2/tr_shader.c b/code/renderergl2/tr_shader.c index 65f0c5dc..308b0f14 100644 --- a/code/renderergl2/tr_shader.c +++ b/code/renderergl2/tr_shader.c @@ -1947,7 +1947,7 @@ static void ComputeVertexAttribs(void) #ifdef USE_VERT_TANGENT_SPACE if ((pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK) && !(r_normalMapping->integer == 0 && r_specularMapping->integer == 0)) { - shader.vertexAttribs |= ATTR_BITANGENT | ATTR_TANGENT; + shader.vertexAttribs |= ATTR_TANGENT; } #endif diff --git a/code/renderergl2/tr_surface.c b/code/renderergl2/tr_surface.c index 46f1c2fe..8978519c 100644 --- a/code/renderergl2/tr_surface.c +++ b/code/renderergl2/tr_surface.c @@ -124,10 +124,25 @@ void RB_AddQuadStampExt( vec3_t origin, vec3_t left, vec3_t up, float color[4], // constant normal all the way around VectorSubtract( vec3_origin, backEnd.viewParms.or.axis[0], normal ); - VectorCopy(normal, tess.normal[ndx]); - VectorCopy(normal, tess.normal[ndx+1]); - VectorCopy(normal, tess.normal[ndx+2]); - VectorCopy(normal, tess.normal[ndx+3]); + tess.normal[ndx][0] = (uint8_t)(normal[0] * 127.5f + 128.0f); + tess.normal[ndx][1] = (uint8_t)(normal[1] * 127.5f + 128.0f); + tess.normal[ndx][2] = (uint8_t)(normal[2] * 127.5f + 128.0f); + tess.normal[ndx][3] = 0; + + tess.normal[ndx+1][0] = (uint8_t)(normal[0] * 127.5f + 128.0f); + tess.normal[ndx+1][1] = (uint8_t)(normal[1] * 127.5f + 128.0f); + tess.normal[ndx+1][2] = (uint8_t)(normal[2] * 127.5f + 128.0f); + tess.normal[ndx+1][3] = 0; + + tess.normal[ndx+2][0] = (uint8_t)(normal[0] * 127.5f + 128.0f); + tess.normal[ndx+2][1] = (uint8_t)(normal[1] * 127.5f + 128.0f); + tess.normal[ndx+2][2] = (uint8_t)(normal[2] * 127.5f + 128.0f); + tess.normal[ndx+2][3] = 0; + + tess.normal[ndx+3][0] = (uint8_t)(normal[0] * 127.5f + 128.0f); + tess.normal[ndx+3][1] = (uint8_t)(normal[1] * 127.5f + 128.0f); + tess.normal[ndx+3][2] = (uint8_t)(normal[2] * 127.5f + 128.0f); + tess.normal[ndx+3][3] = 0; // standard square texture coordinates VectorSet2(tess.texCoords[ndx ][0], s1, t1); @@ -316,9 +331,10 @@ static void RB_SurfaceVertsAndIndexes( int numVerts, srfVert_t *verts, int numIn int i; glIndex_t *inIndex; srfVert_t *dv; - float *xyz, *normal, *texCoords, *lightCoords, *lightdir; + float *xyz, *texCoords, *lightCoords, *lightdir; + uint8_t *normal; #ifdef USE_VERT_TANGENT_SPACE - float *tangent, *bitangent; + uint8_t *tangent; #endif glIndex_t *outIndex; float *color; @@ -347,7 +363,12 @@ static void RB_SurfaceVertsAndIndexes( int numVerts, srfVert_t *verts, int numIn dv = verts; normal = tess.normal[ tess.numVertexes ]; for ( i = 0 ; i < numVerts ; i++, dv++, normal+=4 ) - VectorCopy(dv->normal, normal); + { + normal[0] = (uint8_t)(dv->normal[0] * 127.5f + 128.0f); + normal[1] = (uint8_t)(dv->normal[1] * 127.5f + 128.0f); + normal[2] = (uint8_t)(dv->normal[2] * 127.5f + 128.0f); + normal[3] = 0; + } } #ifdef USE_VERT_TANGENT_SPACE @@ -356,15 +377,12 @@ static void RB_SurfaceVertsAndIndexes( int numVerts, srfVert_t *verts, int numIn dv = verts; tangent = tess.tangent[ tess.numVertexes ]; for ( i = 0 ; i < numVerts ; i++, dv++, tangent+=4 ) - VectorCopy(dv->tangent, tangent); - } - - if ( tess.shader->vertexAttribs & ATTR_BITANGENT ) - { - dv = verts; - bitangent = tess.bitangent[ tess.numVertexes ]; - for ( i = 0 ; i < numVerts ; i++, dv++, bitangent+=4 ) - VectorCopy(dv->bitangent, bitangent); + { + tangent[0] = (uint8_t)(dv->tangent[0] * 127.5f + 128.0f); + tangent[1] = (uint8_t)(dv->tangent[1] * 127.5f + 128.0f); + tangent[2] = (uint8_t)(dv->tangent[2] * 127.5f + 128.0f); + tangent[3] = (uint8_t)(dv->tangent[3] * 127.5f + 128.0f); + } } #endif @@ -1127,7 +1145,8 @@ static void LerpMeshVertexes_scalar(mdvSurface_t *surf, float backlerp) VectorArrayNormalize((vec4_t *)tess.normal[tess.numVertexes], numVerts); } #endif - float *outXyz, *outNormal; + float *outXyz; + uint8_t *outNormal; mdvVertex_t *newVerts; int vertNum; @@ -1144,8 +1163,16 @@ static void LerpMeshVertexes_scalar(mdvSurface_t *surf, float backlerp) for (vertNum=0 ; vertNum < surf->numVerts ; vertNum++) { + vec3_t normal; + VectorCopy(newVerts->xyz, outXyz); - VectorCopy(newVerts->normal, outNormal); + VectorCopy(newVerts->normal, normal); + + outNormal[0] = (uint8_t)(normal[0] * 127.5f + 128.0f); + outNormal[1] = (uint8_t)(normal[1] * 127.5f + 128.0f); + outNormal[2] = (uint8_t)(normal[2] * 127.5f + 128.0f); + outNormal[3] = 0; + newVerts++; outXyz += 4; outNormal += 4; @@ -1163,15 +1190,22 @@ static void LerpMeshVertexes_scalar(mdvSurface_t *surf, float backlerp) for (vertNum=0 ; vertNum < surf->numVerts ; vertNum++) { + vec3_t normal; + VectorLerp(newVerts->xyz, oldVerts->xyz, backlerp, outXyz); - VectorLerp(newVerts->normal, oldVerts->normal, backlerp, outNormal); - //VectorNormalize(outNormal); + VectorLerp(newVerts->normal, oldVerts->normal, backlerp, normal); + VectorNormalize(normal); + + outNormal[0] = (uint8_t)(normal[0] * 127.5f + 128.0f); + outNormal[1] = (uint8_t)(normal[1] * 127.5f + 128.0f); + outNormal[2] = (uint8_t)(normal[2] * 127.5f + 128.0f); + outNormal[3] = 0; + newVerts++; oldVerts++; outXyz += 4; outNormal += 4; } - VectorArrayNormalize((vec4_t *)tess.normal[tess.numVertexes], surf->numVerts); } } @@ -1292,9 +1326,9 @@ static void RB_SurfaceGrid( srfBspSurface_t *srf ) { int i, j; float *xyz; float *texCoords, *lightCoords; - float *normal; + uint8_t *normal; #ifdef USE_VERT_TANGENT_SPACE - float *tangent, *bitangent; + uint8_t *tangent; #endif float *color, *lightdir; srfVert_t *dv; @@ -1382,7 +1416,6 @@ static void RB_SurfaceGrid( srfBspSurface_t *srf ) { normal = tess.normal[numVertexes]; #ifdef USE_VERT_TANGENT_SPACE tangent = tess.tangent[numVertexes]; - bitangent = tess.bitangent[numVertexes]; #endif texCoords = tess.texCoords[numVertexes][0]; lightCoords = tess.texCoords[numVertexes][1]; @@ -1403,22 +1436,22 @@ static void RB_SurfaceGrid( srfBspSurface_t *srf ) { if ( tess.shader->vertexAttribs & ATTR_NORMAL ) { - VectorCopy(dv->normal, normal); + normal[0] = (uint8_t)(dv->normal[0] * 127.5f + 128.0f); + normal[1] = (uint8_t)(dv->normal[1] * 127.5f + 128.0f); + normal[2] = (uint8_t)(dv->normal[2] * 127.5f + 128.0f); + normal[3] = 0; normal += 4; } #ifdef USE_VERT_TANGENT_SPACE if ( tess.shader->vertexAttribs & ATTR_TANGENT ) { - VectorCopy(dv->tangent, tangent); + tangent[0] = (uint8_t)(dv->tangent[0] * 127.5f + 128.0f); + tangent[1] = (uint8_t)(dv->tangent[1] * 127.5f + 128.0f); + tangent[2] = (uint8_t)(dv->tangent[2] * 127.5f + 128.0f); + tangent[3] = (uint8_t)(dv->tangent[3] * 127.5f + 128.0f); tangent += 4; } - - if ( tess.shader->vertexAttribs & ATTR_BITANGENT ) - { - VectorCopy(dv->bitangent, bitangent); - bitangent += 4; - } #endif if ( tess.shader->vertexAttribs & ATTR_TEXCOORD ) { diff --git a/code/renderergl2/tr_vbo.c b/code/renderergl2/tr_vbo.c index a75432f4..8c1a6779 100644 --- a/code/renderergl2/tr_vbo.c +++ b/code/renderergl2/tr_vbo.c @@ -142,20 +142,14 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert if(stateBits & ATTR_NORMAL) { vbo->ofs_normal = dataSize; - dataSize += sizeof(verts[0].normal); + dataSize += sizeof(uint8_t) * 4; } #ifdef USE_VERT_TANGENT_SPACE if(stateBits & ATTR_TANGENT) { vbo->ofs_tangent = dataSize; - dataSize += sizeof(verts[0].tangent); - } - - if(stateBits & ATTR_BITANGENT) - { - vbo->ofs_bitangent = dataSize; - dataSize += sizeof(verts[0].bitangent); + dataSize += sizeof(uint8_t) * 4; } #endif @@ -187,7 +181,6 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert vbo->stride_normal = dataSize; #ifdef USE_VERT_TANGENT_SPACE vbo->stride_tangent = dataSize; - vbo->stride_bitangent = dataSize; #endif vbo->stride_st = dataSize; vbo->stride_lightmap = dataSize; @@ -211,23 +204,31 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert // normal if(stateBits & ATTR_NORMAL) { - memcpy(data + dataOfs, &verts[i].normal, sizeof(verts[i].normal)); - dataOfs += sizeof(verts[i].normal); + uint8_t *p = data + dataOfs; + + p[0] = (uint8_t)(verts[i].normal[0] * 127.5f + 128.0f); + p[1] = (uint8_t)(verts[i].normal[1] * 127.5f + 128.0f); + p[2] = (uint8_t)(verts[i].normal[2] * 127.5f + 128.0f); + p[3] = 0; + + dataOfs += sizeof(uint8_t) * 4; } #ifdef USE_VERT_TANGENT_SPACE // tangent if(stateBits & ATTR_TANGENT) { - memcpy(data + dataOfs, &verts[i].tangent, sizeof(verts[i].tangent)); - dataOfs += sizeof(verts[i].tangent); - } + vec3_t nxt; + uint8_t *p = data + dataOfs; - // bitangent - if(stateBits & ATTR_BITANGENT) - { - memcpy(data + dataOfs, &verts[i].bitangent, sizeof(verts[i].bitangent)); - dataOfs += sizeof(verts[i].bitangent); + CrossProduct(verts[i].normal, verts[i].tangent, nxt); + + p[0] = (uint8_t)(verts[i].tangent[0] * 127.5f + 128.0f); + p[1] = (uint8_t)(verts[i].tangent[1] * 127.5f + 128.0f); + p[2] = (uint8_t)(verts[i].tangent[2] * 127.5f + 128.0f); + p[3] = (uint8_t)(verts[i].tangent[3] * 127.5f + 128.0f); + + dataOfs += sizeof(uint8_t) * 4; } #endif @@ -267,18 +268,13 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert if(stateBits & ATTR_NORMAL) { - dataSize += sizeof(verts[0].normal); + dataSize += sizeof(uint8_t) * 4; } #ifdef USE_VERT_TANGENT_SPACE if(stateBits & ATTR_TANGENT) { - dataSize += sizeof(verts[0].tangent); - } - - if(stateBits & ATTR_BITANGENT) - { - dataSize += sizeof(verts[0].bitangent); + dataSize += sizeof(uint8_t) * 4; } #endif @@ -311,7 +307,6 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert vbo->ofs_normal = 0; #ifdef USE_VERT_TANGENT_SPACE vbo->ofs_tangent = 0; - vbo->ofs_bitangent = 0; #endif vbo->ofs_st = 0; vbo->ofs_lightmap = 0; @@ -319,10 +314,9 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert vbo->ofs_lightdir = 0; vbo->stride_xyz = sizeof(verts[0].xyz); - vbo->stride_normal = sizeof(verts[0].normal); + vbo->stride_normal = sizeof(uint8_t) * 4; #ifdef USE_VERT_TANGENT_SPACE - vbo->stride_tangent = sizeof(verts[0].tangent); - vbo->stride_bitangent = sizeof(verts[0].bitangent); + vbo->stride_tangent = sizeof(uint8_t) * 4; #endif vbo->stride_vertexcolor = sizeof(verts[0].vertexColors); vbo->stride_st = sizeof(verts[0].st); @@ -345,8 +339,14 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert vbo->ofs_normal = dataOfs; for (i = 0; i < numVertexes; i++) { - memcpy(data + dataOfs, &verts[i].normal, sizeof(verts[i].normal)); - dataOfs += sizeof(verts[i].normal); + uint8_t *p = data + dataOfs; + + p[0] = (uint8_t)(verts[i].normal[0] * 127.5f + 128.0f); + p[1] = (uint8_t)(verts[i].normal[1] * 127.5f + 128.0f); + p[2] = (uint8_t)(verts[i].normal[2] * 127.5f + 128.0f); + p[3] = 0; + + dataOfs += sizeof(uint8_t) * 4; } } @@ -357,19 +357,17 @@ VBO_t *R_CreateVBO2(const char *name, int numVertexes, srfVert_t * vert vbo->ofs_tangent = dataOfs; for (i = 0; i < numVertexes; i++) { - memcpy(data + dataOfs, &verts[i].tangent, sizeof(verts[i].tangent)); - dataOfs += sizeof(verts[i].tangent); - } - } + vec3_t nxt; + uint8_t *p = data + dataOfs; - // bitangent - if(stateBits & ATTR_BITANGENT) - { - vbo->ofs_bitangent = dataOfs; - for (i = 0; i < numVertexes; i++) - { - memcpy(data + dataOfs, &verts[i].bitangent, sizeof(verts[i].bitangent)); - dataOfs += sizeof(verts[i].bitangent); + CrossProduct(verts[i].normal, verts[i].tangent, nxt); + + p[0] = (uint8_t)(verts[i].tangent[0] * 127.5f + 128.0f); + p[1] = (uint8_t)(verts[i].tangent[1] * 127.5f + 128.0f); + p[2] = (uint8_t)(verts[i].tangent[2] * 127.5f + 128.0f); + p[3] = (uint8_t)(verts[i].tangent[3] * 127.5f + 128.0f); + + dataOfs += sizeof(uint8_t) * 4; } } #endif @@ -691,7 +689,6 @@ void R_InitVBOs(void) dataSize += sizeof(tess.normal[0]); #ifdef USE_VERT_TANGENT_SPACE dataSize += sizeof(tess.tangent[0]); - dataSize += sizeof(tess.bitangent[0]); #endif dataSize += sizeof(tess.vertexColors[0]); dataSize += sizeof(tess.texCoords[0][0]) * 2; @@ -706,7 +703,6 @@ void R_InitVBOs(void) tess.vbo->ofs_normal = offset; offset += sizeof(tess.normal[0]) * SHADER_MAX_VERTEXES; #ifdef USE_VERT_TANGENT_SPACE tess.vbo->ofs_tangent = offset; offset += sizeof(tess.tangent[0]) * SHADER_MAX_VERTEXES; - tess.vbo->ofs_bitangent = offset; offset += sizeof(tess.bitangent[0]) * SHADER_MAX_VERTEXES; #endif // these next two are actually interleaved tess.vbo->ofs_st = offset; @@ -720,7 +716,6 @@ void R_InitVBOs(void) tess.vbo->stride_normal = sizeof(tess.normal[0]); #ifdef USE_VERT_TANGENT_SPACE tess.vbo->stride_tangent = sizeof(tess.tangent[0]); - tess.vbo->stride_bitangent = sizeof(tess.bitangent[0]); #endif tess.vbo->stride_vertexcolor = sizeof(tess.vertexColors[0]); tess.vbo->stride_st = sizeof(tess.texCoords[0][0]) * 2; @@ -878,12 +873,6 @@ void RB_UpdateVBOs(unsigned int attribBits) //ri.Printf(PRINT_ALL, "offset %d, size %d\n", tess.vbo->ofs_tangent, tess.numVertexes * sizeof(tess.tangent[0])); qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_tangent, tess.numVertexes * sizeof(tess.tangent[0]), tess.tangent); } - - if(attribBits & ATTR_BITANGENT) - { - //ri.Printf(PRINT_ALL, "offset %d, size %d\n", tess.vbo->ofs_bitangent, tess.numVertexes * sizeof(tess.bitangent[0])); - qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_bitangent, tess.numVertexes * sizeof(tess.bitangent[0]), tess.bitangent); - } #endif if(attribBits & ATTR_COLOR) @@ -905,7 +894,6 @@ void RB_UpdateVBOs(unsigned int attribBits) qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_normal, tess.numVertexes * sizeof(tess.normal[0]), tess.normal); #ifdef USE_VERT_TANGENT_SPACE qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_tangent, tess.numVertexes * sizeof(tess.tangent[0]), tess.tangent); - qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_bitangent, tess.numVertexes * sizeof(tess.bitangent[0]), tess.bitangent); #endif qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_vertexcolor, tess.numVertexes * sizeof(tess.vertexColors[0]), tess.vertexColors); qglBufferSubDataARB(GL_ARRAY_BUFFER_ARB, tess.vbo->ofs_lightdir, tess.numVertexes * sizeof(tess.lightdir[0]), tess.lightdir);