Hello, everyone.
I am trying to render out a simple GL_FLAT cube using a vertex buffer as shown here:
The vertices I'm using are in the following array, which is fine (and it renders fine):
GLfloat vertices[24] = {0.5, 0.5, 0.5,
-0.5, 0.5, 0.5,
-0.5, -0.5, 0.5,
0.5, -0.5, 0.5,
0.5, -0.5, -0.5,
0.5, 0.5, -0.5,
-0.5, 0.5, -0.5,
-0.5, -0.5, -0.5};
The index array, which is also fine, is as follows:
GLubyte indices[24] = {0,1,2,3, // 24 indices
0,3,4,5,
0,5,6,1,
1,6,7,2,
7,4,3,2,
4,7,6,5};
The normals array is being calculated automatically (for each face here, not each vertex, since the model is faceted, not smooth). It also
seems to be fine:
normals 0x0012fcbc float [18]
[0] 0.00000000 float
[1] 0.00000000 float
[2] 1.0000000 float
[3] 1.0000000 float
[4] 0.00000000 float
[5] 0.00000000 float
[6] 0.00000000 float
[7] 1.0000000 float
[8] 0.00000000 float
[9] -1.0000000 float
[10] 0.00000000 float
[11] 0.00000000 float
[12] 0.00000000 float
[13] -1.0000000 float
[14] 0.00000000 float
[15] 0.00000000 float
[16] 0.00000000 float
[17] -1.0000000 float
And here is the code I have for the actual rendering:
//...
glTranslatef(0.0f, 0.0f, -3.0f);
glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
// bind vertex and index buffers
g_VBE.glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertBuffer);
g_VBE.glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indBuffer);
glEnableClientState(GL_VERTEX_ARRAY); // activate vertex coords array
glVertexPointer(3, GL_FLOAT, 0, 0); // last param is offset, not ptr
// bind the normals buffer
g_VBE.glBindBufferARB(GL_ARRAY_BUFFER_ARB, normBuffer);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, 0);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, 0);
glDisableClientState(GL_NORMAL_ARRAY); // deactivate normals array
glDisableClientState(GL_VERTEX_ARRAY); // deactivate vertex array
//...
I have a light set up at 0.0f, 0.0f, 1.0f, 1.0f and it seems to be working fine. It is right behind the camera, and since the cube is right in front, it should be dead on.
However, here is what I'm seeing (this is the cube turned to 45 degree angle):
As you can see, the two sides aren't lit equally bright, which means either the normals or something about the way I'm using them is messed up. If I try to do a 360-degree turnaround, the faces are not lit the same, and the lighting changes from face to face in a weird way.
Any suggestions?