PDA

View Full Version : OpenGL Redbook Example 2-13



Banangroda
02-12-2009, 09:08 AM
It's just very strange. Three calls to glVertex3fv, but they use only a number as arguments instead of an array?!

Example 2-13 : Drawing an Icosahedron

#define X .525731112119133606
#define Z .850650808352039932
static GLfloat vdata[12][3] = {
{-X, 0.0, Z}, {X, 0.0, Z}, {-X, 0.0, -Z}, {X, 0.0, -Z},
{0.0, Z, X}, {0.0, Z, -X}, {0.0, -Z, X}, {0.0, -Z, -X},
{Z, X, 0.0}, {-Z, X, 0.0}, {Z, -X, 0.0}, {-Z, -X, 0.0}
};
static GLuint tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11} };
int i;
glBegin(GL_TRIANGLES);
for (i = 0; i < 20; i++) {
/* color information here */
glVertex3fv(&amp;vdata[tindices[i][0]][0]);
glVertex3fv(&amp;vdata[tindices[i][1]][0]);
glVertex3fv(&amp;vdata[tindices[i][2]][0]);
}
glEnd();

The strange numbers X and Z are chosen so that the distance from the origin to any of the vertices of the
icosahedron is 1.0. The coordinates of the twelve vertices are given in the array vdata[][], where the
zeroth vertex is {- &Xgr; , 0.0, &Zgr; }, the first is {X, 0.0, Z}, and so on. The array tindices[][] tells
how to link the vertices to make triangles. For example, the first triangle is made from the zeroth, fourth,
and first vertex. If you take the vertices for triangles in the order given, all the triangles have the same
orientation.

Overmind
02-12-2009, 09:22 AM
These calls are using an array as argument, not just a single number.

glVertex3fv takes a pointer to an array of three elements as argument. But a pointer to an array is the same as a pointer to the first element of the array.

So these two expressions are equivalent:

&amp;vdata[...][0]


vdata[...]

Banangroda
02-12-2009, 10:28 AM
Ah. Thanks.