PDA

View Full Version : drawArrays



LordFrost
02-01-2001, 01:59 PM
I think I quite understand the idea behind the glDrawArrays function, but how does a proper array have to look like?

Thaellin
02-01-2001, 05:07 PM
Hullo,

You have several options, really.

The easiest thing to do is something like the following:

// enable the 'vertex' and 'color' arrays
float vertices[3][3]={{-1,0,0}, {1,0,0}, {0,1,0}};
float colors[3][3]={{1,0,0}, {0,1,0}, {0,0,1}};

// The first arg is the number of components per element. We have 3 components
// (X,Y,Z) in our vertices. The second arg tells what data type we're using.
// We have an array of floats. Third is the stride - you can use this to insert
// space that will be skipped over in your array (if you had data alignment
// constraints, you might use this). Finally is our data pointer.
glVertexPointer(3,GL_FLOAT,0,vertices);

// Pretty much the same story for colors.
glColorPointer(3,GL_FLOAT,0,colors);

// The DrawArrays call lets the driver know what to do with the data and how
// much data it should do it with. Arg #1 is the type of OpenGL primitive
// represented by the data you previously pointed to with the gl*Pointer calls.
// You can use any of the same values you would use in a begin/end pair.
// Arg 2 is the starting offset... (90% certain). Arg 3 is the number of array
// elements you wish to process. DrawArrays processes a consecutive set
// of elements...
glDrawArrays(GL_TRIANGLES,0,3);

glFlush();
// done.

There are a number of good tutorials out there, and the redbook (plus sample code) is a positively wonderful resource.

Have fun,
-- Jeff