PDA

View Full Version : glNormal3fv



Suf
07-16-2002, 01:31 PM
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 (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}

Jambolo
07-16-2002, 02:00 PM
Originally posted by Suf:
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();
}

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).]

Suf
07-17-2002, 05:58 AM
The thing that I don't understand is how to relate &n[j][0] with GLfloat n[6][3]. When j=0,shouldn't n[0][0]=-1; and when j=1, n[1][0]=0?

Deiussum
07-17-2002, 06:25 AM
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][0] 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][0] 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.