# glNormal3fv

Could somebody please explain how the &n and &v work in the below example?

GLfloat n = { /* 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; /
Will be filled in with X,Y,Z vertexes. */

for (i = 0; i < 6; i++) {
glNormal3fv(&n[i]);
glVertex3fv(&v[faces[i]]);
glVertex3fv(&v[faces[i]]);
glVertex3fv(&v[faces[i]]);
glVertex3fv(&v[faces[i]]);
glEnd();
}

Originally posted by Suf:
[b]Could somebody please explain how the &n and &v work in the below example?

GLfloat n = { /* 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; /
Will be filled in with X,Y,Z vertexes. */

for (j = 0; j < 6; j++) {
glNormal3fv(&n[j]);
glVertex3fv(&v[faces[j]]);
glVertex3fv(&v[faces[j]]);
glVertex3fv(&v[faces[j]]);
glVertex3fv(&v[faces[j]]);
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”

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] 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] is the address of the position of vertex k in face j.

[This message has been edited by Jambolo (edited 07-16-2002).]

The thing that I don’t understand is how to relate &n[j] with GLfloat n. When j=0,shouldn’t n=-1; and when j=1, n=0?

The & operator is how you get the memory address of a variable. glVertex3fv and glNormal3fv take pointers, which basically holds a memory address. &n[i] is getting the pointer to the normal array at n[i].

Also when you just specify an array’s name, you are actually referencing the address to the first element of that array. So in your case you should be able to change &n[i] to n[i] and get the same result.

If I were you, I’d look into some good C/C++ books and try and get an understanding of memory and how pointers work.