Apply normals

Hi, can someone tell me if is this the correct method to enable normal vectors of an object?

Step 1: Do the arrays of vertex and normals
Step 2: Create buffers:


    GLuint vertexs,normals;
    glGenBuffers(1,&vertexs);
    glBindBuffer(GL_ARRAY_BUFFER,vertexs);
    glBufferData(GL_ARRAY_BUFFER,sizeVertex*sizeof(GLfloat),vertexArray,GL_DYNAMIC_DRAW);
    glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),0);

    glGenBuffers(1,&normals);
    glBindBuffer(GL_ARRAY_BUFFER,normals);
    glBufferData(GL_ARRAY_BUFFER,sizeNormal*sizeof(GLfloat),normalArray,GL_DYNAMIC_DRAW);
    glNormalPointer(GL_FLOAT,3*sizeof(GL_FLOAT),0);

Step 3: Draw:


        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glDrawArrays(GL_TRIANGLES,0,sizeVertex);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);

EDIT: I put this on the forum because it doesn’t work but I cannot find the reason ^^.

Have test with glEnable(GL_LIGHTING) / glEnable(GL_LIGHT0) and others glLightfv() calls ?

Your glVertexPointer and glNormalPointer calls are in the wrong place. They should be with the draw code and use a null pointer since you have opted to use buffer objects as the source of the vertex attributes.

You’re saying to do this:


glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glVertexPointer(3,GL_FLOAT,3*sizeof(GL_FLOAT),NULL);
        glNormalPointer(GL_FLOAT,3*sizeof(GL_FLOAT),NULL);
        glDrawArrays(GL_TRIANGLES,0,sizeVertex);
        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);

It doesn’t work too. In fact it’s worst because the object doesn’t apear.

You are storing “sizeVertexsizeof(GLfloat)" bytes in your buffer object for positions + "sizeNormalsizeof(GLfloat)” bytes in the buffer object for normals, what do sizeVertex + sizeNormal represent?

You appear to be using sizeVertex for the vertex count in glDrawArrays, in which case you’d probably want to be using sizeVertex3sizeof(GLfloat) bytes in both glBufferData calls.

ps. You are using “sizeof(GL_FLOAT)” elsewhere, which may work but doesn’t make much sense, it should be “sizeof(GLfloat)”.

sizeNormal/sizeVertex represent the size of the respective arrays of normals and vertices.
For each 3 vertices coords there’s one normal vector.

You need 1 normal for each vertex, so will need to replicate the normal data so you have an equal number of each.

If sizeVertex is the number of vertices (size of array doesn’t really help, vertexArray/normalArray could be an array of floats, or be using an array of some vector type - which will give different lengths), then you will need sizeVertex3sizeof(GLfloat) bytes to store the position information in the buffer object.

Originally Posted By: BionicBytes
Your glVertexPointer and glNormalPointer calls are in the wrong place. They should be with the draw code and use a null pointer since you have opted to use buffer objects as the source of the vertex attributes.

You’re saying to do this:
Code:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3,GL_FLOAT,3sizeof(GL_FLOAT),NULL);
glNormalPointer(GL_FLOAT,3
sizeof(GL_FLOAT),NULL);
glDrawArrays(GL_TRIANGLES,0,sizeVertex);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);

It doesn’t work too. In fact it’s worst because the object doesn’t apear.

But you have not bound each buffer object before the vertex attribute pointers.