Sign in to follow this  
w3g

[OpenGL] MD2 bumpmapping weird results - newbie question

Recommended Posts

hello
I have a problem with bump mapping, when displaying the model MD2. The effect works fine if the model is illuminated from the front. If the light is behind it is not illuminated back side of the model. The code is simple to me. I think that the problem lies in determining the normal triangle. I wrote this function using the materials on the forum but got no satisfactory result.

[source lang="cpp"]glActiveTextureARB (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_CUBE_MAP, NORMALIZATION_CUBE_MAP);
glEnable (GL_TEXTURE_CUBE_MAP);
glTexEnvi (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
glTexEnvi (GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_REPLACE);
glTexEnvi (GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_TEXTURE);

glActiveTextureARB (GL_TEXTURE1);
glBindTexture (GL_TEXTURE_2D, NORMAL_TEXTURE);
glEnable (GL_TEXTURE_2D);
glTexEnvi (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE);
glTexEnvi (GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_DOT3_RGB);
glTexEnvi (GL_TEXTURE_ENV,GL_SOURCE0_RGB,GL_PREVIOUS);
glTexEnvi (GL_TEXTURE_ENV,GL_SOURCE1_RGB,GL_TEXTURE);

glActiveTextureARB (GL_TEXTURE2);
glBindTexture (GL_TEXTURE_2D, BASE_TEXTURE);
glEnable (GL_TEXTURE_2D);
glTexEnvi (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);

glBegin(GL_TRIANGLES);

for(i = 0; i < numTriangles; i++) {

MD2Normal (List[tindex[i].Index[0]].point,
List[tindex[i].Index[1]].point,
List[tindex[i].Index[2]].point);

GLfloat Light_vector[3];

for (GLint u = 0; u < 3; u++) {

Light_vector[0] = light_pos[0] - List[tindex[i].Index[u]].point[0];
Light_vector[1] = light_pos[1] - List[tindex[i].Index[u]].point[1];
Light_vector[2] = light_pos[2] - List[tindex[i].Index[u]].point[2];

glMultiTexCoord3fv (GL_TEXTURE0, Light_vector);
glMultiTexCoord2fARB (GL_TEXTURE1, bst[triIndex[i].stIndex[u]].s, bst[triIndex[i].stIndex[u]].t);
glMultiTexCoord2fARB (GL_TEXTURE2, st[triIndex[i].stIndex[u]].s, st[triIndex[i].stIndex[u]].t);

glVertex3fv (List[tindex[i].Index[u]].point);
}
}[/source]
Calculating the normal vector for each triangle

[source lang="cpp"]GLvoid MD2Normal (GLfloat *p1, GLfloat *p2, GLfloat *p3) {

GLfloat a[3], b[3], result[3];
GLfloat length;
GLfloat normal[3];

a[0] = p1[0] - p2[0];
a[1] = p1[1] - p2[1];
a[2] = p1[2] - p2[2];

b[0] = p1[0] - p3[0];
b[1] = p1[1] - p3[1];
b[2] = p1[2] - p3[2];

result[0] = a[1] * b[2] - b[1] * a[2];
result[1] = b[0] * a[2] - a[0] * b[2];
result[2] = a[0] * b[1] - b[0] * a[1];

length = (GLfloat)sqrt(result[0]*result[0] + result[1]*result[1] + result[2]*result[2]);

normal[0] = result[0]/length;
normal[1] = result[1]/length;
normal[2] = result[2]/length;

glNormal3f(normal[0], normal[1], normal[2]);
}[/source]

So as I wrote the code itself is simple - trivial. I wonder just where is the problem here?
Thank you for your help. Edited by w3g

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this