PDA

View Full Version : problem in address passing

darshan
05-20-2010, 10:55 AM
hi i have a problem in understanding a code
for (i = 0; i < 6; ++i) {
glNormal3fv(&amp;cube_normals[i][0]);
glBegin(GL_POLYGON);
glVertex4fv(&amp;cube_vertexes[i][0][0]);
glVertex4fv(&amp;cube_vertexes[i][1][0]);
glVertex4fv(&amp;cube_vertexes[i][2][0]);
glVertex4fv(&amp;cube_vertexes[i][3][0]);
glEnd();

here address of the array has been passed in the glVertex4fv and the cube vertices r defined as shown below,wat is dat exactly happening here how the cube is drawn???

static float cube_vertexes[6][4][4] =
{
{
{-1.0, -1.0, -1.0, 1.0},
{-1.0, -1.0, 1.0, 1.0},
{-1.0, 1.0, 1.0, 1.0},
{-1.0, 1.0, -1.0, 1.0}},

{
{1.0, 1.0, 1.0, 1.0},
{1.0, -1.0, 1.0, 1.0},
{1.0, -1.0, -1.0, 1.0},
{1.0, 1.0, -1.0, 1.0}},

{
{-1.0, -1.0, -1.0, 1.0},
{1.0, -1.0, -1.0, 1.0},
{1.0, -1.0, 1.0, 1.0},
{-1.0, -1.0, 1.0, 1.0}},

{
{1.0, 1.0, 1.0, 1.0},
{1.0, 1.0, -1.0, 1.0},
{-1.0, 1.0, -1.0, 1.0},
{-1.0, 1.0, 1.0, 1.0}},

{
{-1.0, -1.0, -1.0, 1.0},
{-1.0, 1.0, -1.0, 1.0},
{1.0, 1.0, -1.0, 1.0},
{1.0, -1.0, -1.0, 1.0}},

{
{1.0, 1.0, 1.0, 1.0},
{-1.0, 1.0, 1.0, 1.0},
{-1.0, -1.0, 1.0, 1.0},
{1.0, -1.0, 1.0, 1.0}}
};

Yomboprime
05-20-2010, 11:51 AM
Each call toglVertex4fv specifies a point (it takes 4 floats: 3 coordinates and fourth is always 1.0, to multiply by the current 4x4 GL_MODELVIEW matrix)
So, every 4 calls define a polygon of the cube. It's simple.

darshan
05-20-2010, 08:41 PM
hi thanks for the reply but glVertex4fv(&amp;cube_vertexes[i][0][0]);
here how does it take the vertices ??? means it takes if i=0...[0][0][0] element of the array or takes the complete row of the 0th array??

darshan
05-20-2010, 08:55 PM
hi can u help me on another concept in the vertex array its [6][4][4] which represents 6 faces of the cube and each set of 4 vertices corresponds to 4 vertices of a face... can u tell me how those points are defined???

Dan Bartlett
05-21-2010, 04:18 AM
There are 6 faces of the cube, each containing 4 vertices, with each vertex's position stored as 4 floats, representing cartesian coordinates (X,Y,Z + W).

if you call

glVertex4fv(&amp;cube_vertexes[0][0][0]);

it will grab 4 floats from that address (hence the entire row)

{-1.0, -1.0, -1.0, 1.0}

and feed it into OpenGL as a new vertex.

calling glVertex## between glBegin(primitiveMode) + glEnd() causes all the vertices issued between glBegin + glEnd to be treated differently depending on the primitive mode chosen in the glBegin call. In your case, all the primitives issued between glBegin + glEnd are merged into one single polygon primitive. If you were to choose GL_TRIANGLES instead, then every 3 vertices you issue would be merged into one triangle primitive.

http://www.opengl.org/sdk/docs/man/xhtml/glBegin.xml

There are certain instructions that change the current values that will be issued alongside each glVertex## call, in your example

glNormal3fv(&amp;cube_normals[i][0]);

is used to set the normal direction per face of the cube, then all glVertex## calls following this will use the same value. You could also set the color per-vertex by calling glColor## before each glVertex## call.

More help on OpenGL instructions can be found at:
http://www.opengl.org/sdk/docs/man/