PDA

View Full Version : Apply normals



Cucus
05-09-2011, 12:59 PM
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(GLf loat),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(GLf loat),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 ^^.

The Little Body
05-09-2011, 01:11 PM
Have test with glEnable(GL_LIGHTING) / glEnable(GL_LIGHT0) and others glLightfv() calls ?

BionicBytes
05-09-2011, 01:42 PM
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.

Cucus
05-09-2011, 01:50 PM
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.

Dan Bartlett
05-09-2011, 05:56 PM
You are storing "sizeVertex*sizeof(GLfloat)" bytes in your buffer object for positions + "sizeNormal*sizeof(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 sizeVertex*3*sizeof(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)".

Cucus
05-09-2011, 07:29 PM
sizeNormal/sizeVertex represent the size of the respective arrays of normals and vertices.
For each 3 vertices coords there's one normal vector.

Dan Bartlett
05-10-2011, 03:54 PM
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 sizeVertex*3*sizeof(GLfloat) bytes to store the position information in the buffer object.

BionicBytes
05-11-2011, 01:49 AM
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,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.


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