Skip to main content
gamedev.net
🔒 Locked

Quick Vertex Array Question

Started by Instruo Apr 18, 2004 at 4:05 PM 1 replies 650+ views
Original Post
Instruo
Instruo
Hi there, Been looking around the web for information on using vertex arrays and while I''ve found enough information to give me a general idea of how they work, most of the stuff I''ve found has been unclear and poorly written. Could someone please either just cleanly layout the usage of vertex arrays for vertex, normal and texture coord information, or perhaps point me somewhere that explains it decently? Thanks much!
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.
Ostsol
Ostsol
Fairly easy, actually:

glVertexPointer (...)
glTexCoordPointer (...)
glColorPointer (...)

The first parameter in these functions is the number of elements in the attribute. If it''s a 3 component colour or vertex position, then the value should be 3. If it''s a 2 component texture coordinate, then it should be 2.

The second parameter is the data type of the vertex array you''re entering. For example, if the values are floats, then you use GL_FLOAT.

The third parameter is the stride, or the bytes between the beginning of one element of the array and the beginning of the next element. Entering zero here means that the elements are adjacent to each other in memory. This is generally the case when you have multiple corresponding arrays, each containing a different vertex attribute (position coords, colour, normals, texcoords). However, if you have a structure that defines all the attributes for a single array. . .

For example:
struct MyVertex
{
float coords[3];
float colour[3];
float normal[3];
float texcoords[2];
}


. . . then you must enter the size of the structure (sizeof (MyVertex)) as the stride.

The last parameter is the vertex array itself.

glNormalArray (...) has all the same parameters except for the first. Normal arrays must always be made up of 3 component vectors.

All of them must first have their corresponding client state enabled for them to work. This is done via the glEnableClientState function.

Drawing arrays is usually accomplished via glDrawElements (...) or glDrawArrays (...). glDrawElements draws a list of vertices as by an array of vertex indices. glDrawArrays draws a specified number of vertices from the indicated start point.

The first parameter in both functions is the primative type (GL_TRIANGLES, GL_TRIANGLE_STRIP, etc. . .).

The second parameter in glDrawElements is the number of array elements you wish to draw -- think of it as the number of vertices.

The third parameter in glDrawElements is the data type of the indices in the index array. If the indices are unsigned integers, for example, then you''d use GL_UNSIGNED_INT.

The last parameter in glDrawElements is the actual array of indices.

The second parameter in glDrawArrays is the first vertex to be rendered. The last parameter is the number of vertices (including the first) to be rendered.

Here''s a full example using the MyVertex structure I defined above:

glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer (2, GL_FLOAT, sizeof (MyVertex), aVertices[0].texcoord);

glEnableClientState (GL_COLOR_ARRAY);
glColorPointer (4, GL_FLOAT, sizeof (MyVertex), aVertices[0].colour);

glEnableClientState (GL_NORMAL_ARRAY);
glNormalPointer (GL_FLOAT, sizeof (MyVertex), aVertices[0].normal);

glEnableClientState (GL_VERTEX_ARRAY);
glVertexPointer (3, GL_FLOAT, sizeof (MyVertex), aVertices[0].coords);


Now, assuming you want to draw with glDrawElements and have an index array prepared:

glDrawElements (GL_TRIANGLES, nIndices, GL_UNSIGNED_INT, anIndices);


Or for glDrawArrays:

glDrawArrays (GL_TRIANGLES, 0, 24);


Of course, standard vertex arrays only store vertex data in system memory. This means that every time you want to draw the vertex array, it is transfered up the AGP bus to the video card. For large meshes, this is very inefficient. It would be much better if the data could simply be stored in video memory. For this, we have vertex buffer objects (ARB_vertex_buffer_object). I''ve read that NVidia''s paper on the subject is very informative:

http://developer.nvidia.com/object/using_VBOs.html
-Ostsol
Instruo
Instruo
wow, that was an awesome post. thanks much : )
"Game Programming" in an of itself does not exist. We learn to program and then use that knowledge to make games.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.