Hi,
I read through a few demos, that implement the simplest form of DOT3 Bump Mapping (only diffuse with N.L) via the GL_ARB_texture_env_combine and GL_ARB_texture_env_dot3 Extensions.
I tried to play arround with Matrix math, too.
I never really did a deep look into it (I´m ashamed for that g), because the things I did with OpenGL worked without big Matrix knowledge .
This is the code I´m currently using (big parts are based on the simple DOT3 ATI demo, but I do some things in another way):
// light position in object space
float lpOS[3] = {-20.0f, 0.0f, 10.0f};
// light vector
float lv[3];
// light vector in tangent space
float lv_ts[3];float modelViewMatrix[16];
float tangentMatrix[16] =
{1.0f, 0.0f, 0.0f, 0.0f,
0.0f,-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f};while(bExitMainLoop == false)
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);glLoadIdentity();
glTranslatef(0.0f, 0.0f, -20.0f);
glRotatef(fRotate, 0.0f, 1.0f, 0.0f);glPushAttrib(GL_TEXTURE_BIT);
// TEX UNIT 0
glActiveTextureARB(GL_TEXTURE0_ARB);
glBindTexture(GL_TEXTURE_2D, TEXTURES.Return_TextureIDs(4));
glEnable(GL_TEXTURE_2D);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_DOT3_RGBA_ARB);glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PRIMARY_COLOR_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);// TEX UNIT 1
glActiveTextureARB(GL_TEXTURE1_ARB);
glBindTexture(GL_TEXTURE_2D, TEXTURES.Return_TextureIDs(2));
glEnable(GL_TEXTURE_2D);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS_ARB);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);glTexEnvf(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);glGetFloatv(GL_MODELVIEW_MATRIX, modelViewMatrix);
glBegin(GL_TRIANGLE_STRIP);
// compute L and store as vertex color
lv[0] = (lpOS[0] + 5.0f);
lv[1] = (lpOS[1] + 5.0f);
lv[2] = (lpOS[2] - 0.0f);
Vector_3D_Matrix_3x3_Multiply(lv, modelViewMatrix, lv);
Vector_3D_Normalize(lv);// convert to tangent space
Vector_3D_Matrix_3x3_Multiply(lv, tangentMatrix, lv_ts);// scale and bias
lv_ts[0] = lv_ts[0] * 0.5 + 0.5;
lv_ts[1] = lv_ts[1] * 0.5 + 0.5;
lv_ts[2] = lv_ts[2] * 0.5 + 0.5;glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 0.0);
glColor3fv(lv_ts);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-5.0, -5.0, 0.0);
// compute L and store as vertex color
lv[0] = (lpOS[0] - 5.0f);
lv[1] = (lpOS[1] + 5.0f);
lv[2] = (lpOS[2] - 0.0f);
Vector_3D_Matrix_3x3_Multiply(lv, modelViewMatrix, lv);
Vector_3D_Normalize(lv);// convert to tangent space
Vector_3D_Matrix_3x3_Multiply(lv, tangentMatrix, lv_ts);//scale and bias
lv_ts[0] = lv_ts[0] * 0.5 + 0.5;
lv_ts[1] = lv_ts[1] * 0.5 + 0.5;
lv_ts[2] = lv_ts[2] * 0.5 + 0.5;glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 0.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 0.0);
glColor3fv(lv_ts);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(5.0, -5.0, 0.0);
// compute L and store as vertex color
lv[0] = (lpOS[0] + 5.0f);
lv[1] = (lpOS[1] - 5.0f);
lv[2] = (lpOS[2] - 0.0f);
Vector_3D_Matrix_3x3_Multiply(lv, modelViewMatrix, lv);
Vector_3D_Normalize(lv);// convert to tangent space
Vector_3D_Matrix_3x3_Multiply(lv, tangentMatrix, lv_ts);// scale and bias
lv_ts[0] = lv_ts[0] * 0.5 + 0.5;
lv_ts[1] = lv_ts[1] * 0.5 + 0.5;
lv_ts[2] = lv_ts[2] * 0.5 + 0.5;glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 0.0, 1.0);
glColor3fv(lv_ts);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-5.0, 5.0, 0.0);
// compute L and store as vertex color
lv[0] = (lpOS[0] - 5.0f);
lv[1] = (lpOS[1] - 5.0f);
lv[2] = (lpOS[2] - 0.0f);
Vector_3D_Matrix_3x3_Multiply(lv, modelViewMatrix, lv);
Vector_3D_Normalize(lv);// convert to tangent space
Vector_3D_Matrix_3x3_Multiply(lv, tangentMatrix, lv_ts);// scale and bias
lv_ts[0] = lv_ts[0] * 0.5 + 0.5;
lv_ts[1] = lv_ts[1] * 0.5 + 0.5;
lv_ts[2] = lv_ts[2] * 0.5 + 0.5;glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1.0, 1.0);
glMultiTexCoord2fARB(GL_TEXTURE1_ARB, 1.0, 1.0);
glColor3fv(lv_ts);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(5.0, 5.0, 0.0);glEnd();
glPopAttrib();fRotate += 0.05f;
SwapBuffers(GLINIT.Return_HDC());
GetGLError();
}
Now I´ve got a few question on my code and I really would like to get some feedback from the “professionals” .
One thing before I ask some “general” questions.
Am I calculating the light vector in the right way?
light_vector = (light_position_OS + vertex_position_OS) * current modelviel matrix
In the ATI demo it was:
light_vector[0] = light_position_OS + (-vertex_position_OS)
light_vector[1] = light_position_OS + (- vertex_position_OS)
light_vector[2] = light_position_OS + (- vertex_position_OS)
But there were some additional matrix calculations in, so that the Bump Mapping worked, if the bump mapped object rotated.
That didn´t work for me, because I left the additional matrix math out (didn´t understand everything).
And so I thought it should work, if I multiply ligth position + vertex position with the current modelviel matrix, after doing my object translations and rotations, but perhaps my math is wrong .
Here the general questions:
- Are there errors in the code (I assume the Matrix / Vector Math functions work correctly)?
- Do I really need a glNormal3f call or is it just a cosmetical thing?
- What could be optimized?
- Any other suggestions for me ?
Diapolo
[This message has been edited by Diapolo (edited 02-25-2002).]