• Advertisement
Sign in to follow this  

Reflective bumpmapping (now a reflections problem in GLSL :) )

This topic is 2765 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have very little experience with shaders and this is actually the first time I'm trying to get Cg stuff running in my code. As an example I chose the reflective bumpmapping demo from CodeSamplers.

I'm so far that I have the shader running, save for the bumpmapping part which, when enabled, simply distorts the reflection and doesn't apply any actual bumpmapping - and for the world of me I can't figure out why.

I'm posting some code which I'm not particularly certain of, as the rest of the code I've either not touched or I've managed to test properly.

TBN space calculation:

TVector3D vTangent;
TVector3D c1 = VecCrossProduct(face->vVertexNormals[v], TVector3D(0.0, 0.0, 1.0));
TVector3D c2 = VecCrossProduct(face->vVertexNormals[v], TVector3D(0.0, 1.0, 0.0));

if(VecLength(c1) > VecLength(c2))
{ vTangent = c1; }
{ vTangent = c2; }

face->vVertexTangents[v] = VecNormalize(vTangent);
face->vVertexBinormals[v] = VecNormalize(VecCrossProduct(face->vVertexNormals[v], vTangent));

Rendering code (should be identical to the tutorial):

// Bind and enable shaders
cgGLBindProgram( cgVertexProgram );
cgGLBindProgram( cgFragmentProgram );
cgGLEnableProfile( cgVertexProfile );
cgGLEnableProfile( cgFragmentProfile );

// Set tweakable shader parameters
cgGLSetParameter1f( cgReflectivity, g_fReflectIndex );
cgGLSetParameter1f( cgBumpScale, g_fBumpScale );
if( g_bModulate )
cgGLSetParameter1f( cgModulate, 1.0f );
cgGLSetParameter1f( cgModulate, 0.0f );

// Toggle normal mapping
if( g_fBumpScale < 0.0f || g_fBumpScale > 0.0f )
cgGLSetParameter1f( cgBumpy, 1.0f );
cgGLSetParameter1f( cgBumpy, 0.0f );

// Enable all textures
cgGLEnableTextureParameter( cgBaseMap );
cgGLEnableTextureParameter( cgNormalMap );
cgGLEnableTextureParameter( cgEnvironmentMap );

// Set all matrices
cgGLSetStateMatrixParameter( cgModelView, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_IDENTITY );
cgGLSetStateMatrixParameter( cgModelViewI, CG_GL_MODELVIEW_MATRIX, CG_GL_MATRIX_INVERSE );

// Set vertex shader inputs:
// Model must supply tangent, binormal and normal data per vertex which
// is used to define surface local space (tangent space) for TBN matrix
// calculation to transform per fragment normal to cube space.
cgGLEnableClientState( cgNormal );
cgGLEnableClientState( cgPosition );
cgGLEnableClientState( cgBinormal );
cgGLEnableClientState( cgTangent );
cgGLEnableClientState( cgTexcoords );

// Draw primitives
DrawBox2Cg(TVector3D(-2, 2, -2), TVector3D(2, 4, 2));

cgGLDisableClientState( cgNormal );
cgGLDisableClientState( cgPosition );
cgGLDisableClientState( cgBinormal );
cgGLDisableClientState( cgTangent );
cgGLDisableClientState( cgTexcoords );

// Disable all textures
cgGLDisableTextureParameter( cgBaseMap );
cgGLDisableTextureParameter( cgNormalMap );
cgGLDisableTextureParameter( cgEnvironmentMap );

// Disable profiles
cgGLDisableProfile( cgVertexProfile );
cgGLDisableProfile( cgFragmentProfile );

The rest of the code is unchanged from any functional point of view (this includes shader code).

What I also cannot understand from this sample is the meaning of the line in the tutorial:

cgGLSetParameter3f( cgEyePosition, 0.0f, 0.0f, 0.0f );

which seems to set a static camera position. Can anyone tell what this variable does? Setting this every frame to the actual eye position seems to completely mess up the entire scene.

Tutorials would really benefit from a more rigorous explanation for the beginners involved :).

[Edited by - irreversible on August 23, 2010 2:46:05 AM]

Share this post

Link to post
Share on other sites
I think the main problem was me using GLSL and Cg at the same time - though I'm not 100% sure. By some line-by-line testing I managed to get the Cg version of the shader to work.

However, then I used HLSL2GLSL to convert it to GLSL and now I can get the bumpmapping to work, but the reflections are off. The primary symptom seems to be that the reflections seem to remain the same size regardless of how far I move the camera from the reflective surface.

I think the reason is in how I set up my matrices. The original Cg code that sets them up is:



In GLSL I can see several solutions to setting up the same matrices. Right now I have this inside the shader:

ModelViewProj = gl_ModelViewProjectionMatrix;
ModelView = gl_ModelViewMatrix;

and I set up the modelview inverse and inverse transpose in my renderer:

glGetFloatv(GL_MODELVIEW_MATRIX, mat);
shader->sendUniformMatrix4fv("ModelViewI", 1, false, mat);
shader->sendUniformMatrix4fv("ModelViewIT", 1, false, mat);

Is there anything I'm doing wrong here or missing here? Or could I be looking for the problem in the wrong place altogether?


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement