Originally posted by Suf:
[b]Could somebody please explain how the &n and &v work in the below example?
GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. /
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLfloat v[8][3]; / Will be filled in with X,Y,Z vertexes. */
for (j = 0; j < 6; j++) {
glBegin(GL_QUADS);
glNormal3fv(&n[j][0]);
glVertex3fv(&v[faces[j][0]][0]);
glVertex3fv(&v[faces[j][1]][0]);
glVertex3fv(&v[faces[j][2]][0]);
glVertex3fv(&v[faces[j][3]][0]);
glEnd();
}[/b]
A cube has 6 sides. Each side has a normal. A normal is a 3D vector so it has 3 components. n contains the normals for each side.
A cube has 8 corners. Each corner has a position. The position is a 3D point/vector so it has 3 components. v contains the positions of the corners.
The array not declared is “faces”. Each face has 4 corners. faces contains the indexes in v of the positions of the 4 corners for each of the 6 faces. faces should be declared “int faces[6][4]”
Each iteration of the for-loop draws a face.
glBegin(GL_QUADS) and glEnd() tell OpenGL to start and stop drawing quads. Note that glBegin and glEnd should be outside the for-loop for efficiency.
glNormal3fv tells OpenGL to use the 3 floats at the address given as the normal from now on. &n[j][0] is the address of the jth normal in n (which consists of 3 floats).
glVertex3fv tells OpenGL to the 3 floats at the address given as the position of a new vertex. faces[j][k] (f for short) is the index into v of vertex k in face j. So, &v[f][0] is the address of the position of vertex k in face j.
[This message has been edited by Jambolo (edited 07-16-2002).]