Help with Vertex/Normal Arrays

Hello

After solving my surface normal problem fairly recently (calculated using an external program lol) I have noticed that they aren’t appearing correctly with lighting.

What I mean by this is that some faces are light, and some are dark where they shouldn’t be.

I’m using vertex arrays and normal arrays to create objects (there is a colour array in there too, but its left over from a previous implementation).

Plus, I’m pretty sure that the OpenGL coding style that I am using is an older much simpler style than most of you use, so please, help me with layman terms xD This is not the entire code either, that would be far too long to post. This is the object code, which I’m pretty sure the problem is occurring. I think that it is my inexperience with the normal array shown below, we were never shown how to format it correctly and searching the internet has not helped at all.

Thankyou :slight_smile:

void headPartTwo()
{
	// This part of the model is a component of the head.

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);

	typedef GLfloat point3[3];
	typedef GLfloat point1[1];
	point3 vertices[12] =	{{-0.6,-1.5,-0.8},{0.6,-1.5,-0.8}, // 0,1
							{0.6,1.0,-1.2},{-0.6,1.0,-1.2}, // 2,3
							{0.8,-1.5,-0.2},{1.0,1.0,-0.6}, // 4,5
							{1.0,0.0,1.2},{0.8,1.0,1.2}, // 6,7
							{-0.8,0.0,1.2},{-1.0,1.0,1.2}, // 8,9
							{-0.8,-1.5,-0.2},{-1.0,1.0,-0.6}};	// 10,11 
	point1 normals[24] = {0,0,1, 1,0,0, 1,0,0, 1,0,0, 1,0,0, 0,1,0, 0,1,0, 0,0,1};
	point3 color[8] =	{{0.0,0.0,0.0},{1.0,0.0,0.0},
						{1.0,1.0,0.0},{0.0,1.0,0.0},
						{0.0,0.0,1.0},{1.0,0.0,1.0},
						{1.0,1.0,1.0},{0.0,1.0,1.0}};

	GLubyte cubeIndices[32]={0,1,2,3, 1,4,5,2, 4,6,7,5, 8,10,11,9, 10,0,3,11, 10,4,1,0, 8,6,4,10, 6,8,9,7};

	glVertexPointer(3,GL_FLOAT,0,vertices);
	glColorPointer(3,GL_FLOAT,0,color);
	glNormalPointer(GL_FLOAT,0,normals);
	glDrawElements(GL_QUADS, 32, GL_UNSIGNED_BYTE, cubeIndices);

	glDisableClientState(GL_NORMAL_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
}

To debug this, what about drawing little lines representing your normals ? From vertex1 to vertex1+normal1, etc ?

I tried that on most of the shapes, as they are quite basic primitives. Whatever way the arrow points “out” of the face I set the normal to 1 on that axis right?

If so then it still doesn’t work :stuck_out_tongue:

Or else I’ve really messed up somewhere down the line.

EDIT: Thought I’d add this little picture, to show you my understanding of the normals.

lol.

Quickly seen, I just can see you have 12 vertices, 12 colors but only 8 normals. Couldn’t this be the issue ?

It could be, is it possible that the normals that I am trying to achieve are per-vertex and not per-face?

But then I honestly have no idea how to fix it. I was under the assumption that the values in the normal array are called after glDrawElements draws a face (using GL_QUADS).

arts is right : Normals, colors, vertex position, are all per index :slight_smile:

Oh I see.

So how would I go about changing them? I’m such an idiot when it comes to these things lol. Sucks that they are essential in my project -.-

Yes, normals are only per vertex with using vertex array. If you have only one normal per face, then repeat your normal for each vertex of each face. You don’t need to change them at all.

Ah, that seems to work so far. Though with shapes that have angled edges it’s proving a little problem. But, overall, its working out quite well :slight_smile:

Thanks :smiley: