glNormalPointer()

Started by
8 comments, last by Kalidor 17 years, 8 months ago
I am asking myself latley how glNormalPointer**() is related to geometry that I define with glVertexPointer**() because I am new at openGL and I have to control flat and smooth shading in my program. How do you define the arrays so that openGL can make difference of what normal goes wher or is there a specific way for how glDrawElement() handle the normals? The problem is that everything on the screen looks smooth to me no matter what I try to do... even tried the good old, glBegin(TRIANGLES) and placed out all the normals manually but the results was still the same (to 'good'). Is there a good way (technique) for rendering your geometry on the screen that people commonly use that also gives good speed? Any tips on what I should read on the net?
Advertisement
Each array is read simultanuously. That is, with each next vertex position the next vertex normal is read. There is no "use the folowing normal for the next 4 vertices" or so. That is only possible in immediate mode. If you want sharp vertices then you have to duplicate the vertex position for each adjacent face and provide different vertex normals for them.

Perhaps, if all edges are sharp, then you may switch of smoothing and don't supply normals at all.
OK, now I am going insane here... yeah, it is my first time with lights but I am getting more confused now because I have played around the "light" & "material" settings, both seems to work fine when I was changing the colors.

So I took and drew some line to check my normals, it renderd as expected but! they regulary change between bright and dark colors as they rotate... I have tried with and without normals on the triangles though and nothing wants to happen :(

On the other hand, I look at Nehe's and other peoples turorials and .exe files and there the sides change brightness/darkness as their cube rotates, why not mine? The code are not so different.
Is there something I have to initialize? maybe something I have missed?

inline void simple_01_FLAT_Cube(const GLfloat RADIUS){    GLfloat tArray_MaterialSpecular[]  = { 0.6, 0.6, 0.2, 1.0 };    GLfloat tArray_MaterialShininess[] = { 20.0 };    GLfloat tArray_MaterialEmission[]  = { 0.0, 0.6, 0.0, 1.0 };    GLfloat tArray_Points[14][3] = {{ -0.4,  0.4,  0.4},     //0                                    {  0.4,  0.4,  0.4},     //1                                    {  0.4, -0.4,  0.4},     //2                                    { -0.4, -0.4,  0.4},     //3                                    { -0.4,  0.4, -0.4},     //4                                    {  0.4,  0.4, -0.4},     //5                                    {  0.4, -0.4, -0.4},     //6                                    { -0.4, -0.4, -0.4},     //7                                    {       0,       0,  0.4},     //8                                    {       0,       0, -0.4},     //9                                    { -0.4,       0,       0},     //10                                    {  0.4,       0,       0},     //11                                    {       0,  0.4,       0},     //12                                    {       0, -0.4,       0}};    //13    /** position 0 to 13**/    GLfloat tArray_Normals[6][3] = {{  0.0,           0.0,           1.0          }, //0                                     {  0.0,           0.0,          -1.0          }, //1                                     { -1.0,           0.0,           0.0          }, //2                                     {  1.0,           0.0,           0.0          }, //3                                     {  0.0,           1.0,           0.0          }, //4                                     {  0.0,          -1.0,           0.0          }};//5    glPolygonMode(GL_FRONT, GL_FILL);    glPolygonMode(GL_BACK, GL_LINE);    GLfloat tARRAY_RGB[3][4] = {{1.0, 0.0, 0.0, 1.0},                                {0.0, 1.0, 0.0, 1.0},                                {0.0, 0.0, 1.0, 1.0}};    glBegin(GL_LINES);        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[0]);        glVertex3fv(tArray_Normals[0]);        glVertex3fv(tArray_Normals[1]);        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[1]);        glVertex3fv(tArray_Normals[2]);        glVertex3fv(tArray_Normals[3]);        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[2]);        glVertex3fv(tArray_Normals[4]);        glVertex3fv(tArray_Normals[5]);    glEnd();    glBegin(GL_TRIANGLES);        //glPushMatrix();            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[0]);            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tArray_MaterialSpecular);            glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, tArray_MaterialShininess);          //  glEnable(GL_NORMALIZE);                /** FRONT ROW 1 (8, 3, 2) **/                glNormal3f( 0.0, 0.0, 1.0 );                glVertex3fv(tArray_Points[8]);                glVertex3fv(tArray_Points[3]);                glVertex3fv(tArray_Points[2]);                /** FRONT ROW 2 (8, 2, 1) **/                glVertex3fv(tArray_Points[8]);                glVertex3fv(tArray_Points[2]);                glVertex3fv(tArray_Points[1]);                /** FRONT ROW 3 (8, 1, 0) **/                glVertex3fv(tArray_Points[8]);                glVertex3fv(tArray_Points[1]);                glVertex3fv(tArray_Points[0]);                /** FRONT ROW 4 (8, 0, 3) **/                glVertex3fv(tArray_Points[8]);                glVertex3fv(tArray_Points[0]);                glVertex3fv(tArray_Points[3]);    glEnd();    glBegin(GL_TRIANGLES);            //glDisable(GL_NORMALIZE);        //glPopMatrix();        glPushMatrix();                glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[0]);                glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tArray_MaterialSpecular);                glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, tArray_MaterialShininess);                /** BACK ROW 1 (9, 4, 5) **/                glNormal3fv(tArray_Normals[0]);                glVertex3fv(tArray_Points[9]);                glVertex3fv(tArray_Points[4]);                glVertex3fv(tArray_Points[5]);                /** BACK ROW 2 (9, 5, 6) **/                glVertex3fv(tArray_Points[9]);                glVertex3fv(tArray_Points[5]);                glVertex3fv(tArray_Points[6]);                /** BACK ROW 3 (9, 6, 7) **/                glVertex3fv(tArray_Points[9]);                glVertex3fv(tArray_Points[6]);                glVertex3fv(tArray_Points[7]);                /** BACK ROW 4 (9, 7, 4) **/                glVertex3fv(tArray_Points[9]);                glVertex3fv(tArray_Points[7]);                glVertex3fv(tArray_Points[4]);        glPopMatrix();        glPushMatrix();            glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[1]);            glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tArray_MaterialSpecular);            glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, tArray_MaterialShininess);                /** LEFT ROW 1 (10, 3, 0) **/                glNormal3fv(tArray_Normals[1]);                glVertex3fv(tArray_Points[10]);                glVertex3fv(tArray_Points[3]);                glVertex3fv(tArray_Points[0]);                /** LEFT ROW 2 (10, 0, 4) **/                glVertex3fv(tArray_Points[10]);                glVertex3fv(tArray_Points[0]);                glVertex3fv(tArray_Points[4]);                /** LEFT ROW 3 (10, 4, 7) **/                glVertex3fv(tArray_Points[10]);                glVertex3fv(tArray_Points[4]);                glVertex3fv(tArray_Points[7]);                /** LEFT ROW 4 (10, 7, 3) **/                glVertex3fv(tArray_Points[10]);                glVertex3fv(tArray_Points[7]);                glVertex3fv(tArray_Points[3]);        glPopMatrix();        glPushMatrix();                glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[1]);                glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tArray_MaterialSpecular);                glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, tArray_MaterialShininess);                /** RIGHT ROW 1 (11, 2, 6) **/                glNormal3fv(tArray_Normals[2]);                glVertex3fv(tArray_Points[11]);                glVertex3fv(tArray_Points[2]);                glVertex3fv(tArray_Points[6]);                /** RIGHT ROW 2 (11, 6, 5) **/                glVertex3fv(tArray_Points[11]);                glVertex3fv(tArray_Points[6]);                glVertex3fv(tArray_Points[5]);                /** RIGHT ROW 3 (11, 5, 1) **/                glVertex3fv(tArray_Points[11]);                glVertex3fv(tArray_Points[5]);                glVertex3fv(tArray_Points[1]);                /** RIGHT ROW 4 (11, 1, 2) **/;                glVertex3fv(tArray_Points[11]);                glVertex3fv(tArray_Points[1]);                glVertex3fv(tArray_Points[2]);        glPopMatrix();        glPushMatrix();                glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[2]);                glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tArray_MaterialSpecular);                glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, tArray_MaterialShininess);                /** TOP ROW 1 (12, 0, 1) **/                glNormal3fv( tArray_Normals[3]);                glVertex3fv(tArray_Points[12]);                glVertex3fv(tArray_Points[0]);                glVertex3fv(tArray_Points[1]);                /** TOP ROW 2 (12, 1, 5) **/                glVertex3fv(tArray_Points[12]);                glVertex3fv(tArray_Points[1]);                glVertex3fv(tArray_Points[5]);                /** TOP ROW 3 (12, 5, 4) **/                glVertex3fv(tArray_Points[12]);                glVertex3fv(tArray_Points[5]);                glVertex3fv(tArray_Points[4]);                /** TOP ROW 4 (12, 4, 0) **/                glVertex3fv(tArray_Points[12]);                glVertex3fv(tArray_Points[4]);                glVertex3fv(tArray_Points[0]);        glPopMatrix();        glPushMatrix();                glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, tARRAY_RGB[2]);                glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, tArray_MaterialSpecular);                glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, tArray_MaterialShininess);                /** BOTTOM ROW 1 (13, 7, 6) **/                glNormal3fv(tArray_Normals[5]);                glVertex3fv(tArray_Points[13]);                glVertex3fv(tArray_Points[7]);                glVertex3fv(tArray_Points[6]);                /** BOTTOM ROW 2 (13, 6, 2) **/                glVertex3fv(tArray_Points[13]);                glVertex3fv(tArray_Points[6]);                glVertex3fv(tArray_Points[2]);                /** BOTTOM ROW 3 (13, 2, 3) **/                glVertex3fv(tArray_Points[13]);                glVertex3fv(tArray_Points[2]);                glVertex3fv(tArray_Points[3]);                /** BOTTOM ROW 4 (13, 3, 7) **/                glVertex3fv(tArray_Points[13]);                glVertex3fv(tArray_Points[3]);                glVertex3fv(tArray_Points[7]);        glPopMatrix();    glEnd();}


[Edited by - DeonCadme on July 28, 2006 9:06:08 AM]
Added another crazy test... changed glNormal3f(0.0, 0.0, 1.0) for the front side to glNormal3f(0.0, 0.0, -1.0) and then built the program... nothing changed! half the face on that square side should have behaved strange.
Have you called glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0)?

Also, remember that you should use [ source ] [ /source ] tags to enclose source code posted on this forum.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

No problem with glEnable's, is using them and the light seems to work just fine, but the more I play with the normals the more I also get suspicious that they are the problem... I just get the feeling that the normals I define have no effect on the final render that goes to the screen.
When you draw your normals with GL_LINE, disable lighting first, then enable it again once they have been drawn. That will stop the normals from fading between light and dark when the cube rotates.

As for the faces not lighting as they should, I'm not sure, but your drawing code looks a little overcomplicated if you are just drawing a cube. If you're sticking with immediate mode, you could just do the whole thing between one glBegin and glEnd. Also, I dont think the PushMatrix() and PopMatrix() are necessary inside the drawing code, since you're not really doing anything with it, you're just specifying vertex and normal coords.

Hope thats helpful
You can't call most OpenGL functions (ie: gl[Push/Pop]Matrix and glMaterial*) between a glBegin/glEnd pair. Read Chapter 2 in the Red Book for more information.

[Edited by - Kalidor on July 28, 2006 11:37:18 AM]
Quote:Original post by Kalidor
You can't call most OpenGL functions (ie: gl[Push/Pop]Matrix and glMaterial*) between a glBegin/glEnd pair. Read Chapter 2 in the Red Book for more information.


Actually, I know for a fact that glMaterial*() works under glBegin/glEnd without any glitches, glPush and glPop on the other hand... can not promise if they work, do not remember straight out of the head but yeah, can be looked up in the Red Book but I know they make a combination of glBegin, glMaterial and glPush later in a example around one of the more advanced topics of materials.

I was just using it while testing to see what I would end up with. The result is still not perfect but with luck, this will be enough for my homework ^^;
Quote:Original post by DeonCadme
Actually, I know for a fact that glMaterial*() works under glBegin/glEnd without any glitches...
Ah, my apologies. Don't know what I was thinking with glMaterial, it's even right on that page I linked to. [embarrass]
glPushMatrix and glPopMatrix are definitely not allowed though.

This topic is closed to new replies.

Advertisement