Thread: [OpenGL ]Dot3 bumpmapping weird results

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    2

    [OpenGL ]Dot3 bumpmapping weird results

    Ave
    I have a problem with the effect of DOT 3 The model is the MD2 format. The problem is that the effect can only be seen as the model is illuminated from the front. Just like in the screenshot. The code itself is simple but not working properly. And I do not know where the error is.

    Code:
    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, modelTexB->texID);
    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, modelTex->texID);
    glEnable (GL_TEXTURE_2D);
    glTexEnvi (GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
            
            glBegin(GL_TRIANGLES);
    
                for(i = 0; i < numTriangles; i++)  {
                  
                    for (GLint u = 0; u < 3; u++) {
                    
                        GLfloat Light_vector[3];
                    
                        Light_vector[0] = light_pos[0] - vList[triIndex[i].meshIndex[u]].point[0];
                        Light_vector[1] = light_pos[1] - vList[triIndex[i].meshIndex[u]].point[1];
                        Light_vector[2] = light_pos[2] - vList[triIndex[i].meshIndex[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);    
                        
                        glNormal3f           (0.0f, 0.0f ,1.0f); //?
                        
                        glVertex3fv (vList[triIndex[i].meshIndex[u]].point);
                   
                    }
                }
    front: http://img708.imageshack.us/img708/9136/99808516.png

    back: http://img401.imageshack.us/img401/1632/63897143.th.png

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's probably because you're calling glNormal3f with a fixed vector (0,0,1). Normals pointing straight in one direction often mess up the model from different angles. If I remember correctly, MD2 model files have normals included (in some kind of limited resolution normal-table format, but it should be good enough for your purposes). Alternatively, you could calculate the normals yourself.

    For very simple normal approximations, you can compute the centre-of-mass of the object (or use an arbitrary point), and draw lines from the centre through each vertex/face (and normalize, i.e. divide the vectors by their length). A much better method would be to compute face-normals with something like (A-B).dot(B-C).normalize(), assuming you have a vector class for A,B,C with the appropriate member functions. (It's also easy to implement this with straight arrays.) MD2 models will have consistent windings so this will always give you outwards-facing normals (of course, you have a 50% chance of getting it right the first time, unless you think it through, which I did not do in my example).

    Face-normals as described above let you do flat-shading. For smooth-shading you need vertex normals. One way to compute vertex normals is just to take the average of all adjacent face-normals (i.e. for each face that the vertex is touching, sum up that face's normal, and finally divide by the number of touching faces). The MD2 format with its indexing scheme makes it fairly straightforward to do this, fortunately. Don't forget to re-normalize the vectors. (Also you could get a zero vector this way, but that would be a pretty pathological MD2 file.)

    Flat shading can be enabled with glShadeModel(GL_FLAT), and then you should specify one glNormal before each face (e.g. for triangles, before each set of three vertices; triangle strips require a normal before each vertex after the initial triangle is specified). For smooth shading, go glShadeModel(GL_SMOOTH) and then specify a glNormal before each vertex, as you have done in your code above.

    Finally, there are a lot of existing MD2 loaders out there and programs that use them (I've written at least one myself...). It may be worth checking out their data structures etc to see how they work. Also, I've been pretty general here, feel free to ask if you have specific questions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    2
    Hello
    Thanks for the tips. I decided to write a function that calculates the normal for each triangle. It looks like this:

    Code:
    GLvoid CalculateMD2Normal (GLfloat *p1, GLfloat *p2, GLfloat *p3) {
    
       GLfloat a[3], b[3], result[3];
       GLfloat length;
    
       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]);
    
       glNormal3f(result[0]/length, result[1]/length, result[2]/length);
    }
    Code:
    glBegin(GL_TRIANGLES);
    
                for(i = 0; i < numTriangles; i++)  {
    
                    CalculateMD2Normal (vList[triIndex[i].meshIndex[0]].point, 
                                        vList[triIndex[i].meshIndex[1]].point, 
                                        vList[triIndex[i].meshIndex[2]].point); 
    
                    for (GLint u = 0; u < 3; u++) {
                    
                        GLfloat Light_vector[3];
    
                        Light_vector[0] = light_pos[0] - vList[triIndex[i].meshIndex[u]].point[0];
                        Light_vector[1] = light_pos[1] - vList[triIndex[i].meshIndex[u]].point[1];
                        Light_vector[2] = light_pos[2] - vList[triIndex[i].meshIndex[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          (vList[triIndex[i].meshIndex[u]].point);
                   
                    }
                }
    Unfortunately, or something I do not understand or do not know how, but the effect is the same.
    Last edited by W3D; 07-10-2012 at 04:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting weird results with isalpha
    By itsthemac in forum C Programming
    Replies: 7
    Last Post: 04-20-2011, 09:17 PM
  2. OpenGL Textures Doing Weird Things?
    By snipy3 in forum Game Programming
    Replies: 6
    Last Post: 05-05-2010, 01:08 PM
  3. Dot3 bumpmapping: weird results
    By psychopath in forum Game Programming
    Replies: 12
    Last Post: 02-09-2006, 01:40 PM
  4. Per-pixel dot3 lighting
    By VirtualAce in forum Game Programming
    Replies: 5
    Last Post: 01-20-2005, 08:05 PM
  5. OpenGL + BumpMapping
    By Sunny in forum Game Programming
    Replies: 8
    Last Post: 07-27-2002, 11:33 AM