Vertex Array question

Started by
16 comments, last by Slaru 19 years, 1 month ago
I have just started to learn about OpenGL vertex arrays. I have a question about actually drawing the array. Can you draw an array without knowing the actual size of the array? For instance, if you had array with no predefined size like this:


float Vertices[] = {

// data

}


Can this array be used for drawing purposes?
-----------------------------Play Stompy's Revenge! Now!
Advertisement
Yes. The glDrawArrays command takes a parameter which defines how many primitives to draw. Overshooting the end of the array on accident, though, is probably very scary.
I probably am not understanding the glDrawArrays function. I know the first parameter is the render mode such as GL_LINES, GL_POINTS, etc. What is the significance of the last two parameters and how do I get the data necessary to fill those parameters?
-----------------------------Play Stompy's Revenge! Now!
glDrawArrays spec

Anyway, the second parameter is start, the last is count. For example, you want to draw an array of 32 triangles.

glDrawArrays(GL_TRIANGLES, 0, 32);

Tells OpenGL to draw triangles, from the array pointer you proved with glVertexPointer (and friends), starting at index 0 in the array, and drawing 32 triangles.
err, oops http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/drawarrays.html
First parameter is a GLenum. It is the kind of primitive to render. eg. GL_TRIANGLES, GL_POINTS...
Second parameter is a GLint. It is the first index of the enabled arrays to use.
Third parameter is a GLsizei. It is the number of indices to use.

Example for drawing points:
glDrawArrays(GL_POINTS, 0, NUM_OF_POINTS);


Slaru
Well, I am trying to draw a model. I have an array containing all of the vertices positions, but I don't know how many vertices or triangles I will be rendering. So how can I use glDrawArrays to draw this? I can post some code if necessary.
-----------------------------Play Stompy's Revenge! Now!
Quote:Original post by Anonymous Poster
... For example, you want to draw an array of 32 triangles.

glDrawArrays(GL_TRIANGLES, 0, 32);

Tells OpenGL to draw triangles, from the array pointer you proved with glVertexPointer (and friends), starting at index 0 in the array, and drawing 32 triangles.


Wouldn't it be:

glDrawArrays(GL_TRIANGLES, 0, 32*3);

Since the third parameter is just the number of indices and a triangle needs three vertices per face. So 32 triangles times 3 vertices per face? I'm not sure, but I though it is times 3. Correct me if I'm wrong.

Slaru
You would need to know how many vertices you are drawing and what kind of primitive type they represent. If they represent triangles where every three vertices in the array are the vertices of one triangle, you can use glDrawArrays().

Example:
// Enable vertes array, color array, texture coord array...glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(    /*Any other arrays of data to go along with the vertices*/);// Set vertex pointer, color pointer...glVertexPointer(3, GL_FLOAT, 0, PointerToVerticesArray);glColorPointer();  // Any other array pointers set...// Draw the arrays// You need to know how many of the primitive type you are// going to draw for this part (NUM_OF_TRIANGLES in the code)glDrawArrays(GL_TRIANGLES, 0, NUM_OF_TRIANGLES*3);


I think that is all that is needed.

EDIT: The first parameter in glVertexPointer is what tells OpenGL the number of elements that make up that coordinate type. eg. 3D point would be 3, 2D point would be 2...

Slaru
If your vertex array specifies a triangle list where no vertices are shared, then the last parameter to glDrawArrays would be 3 * numTriangles.

From the man page:

When glDrawArrays is called, it uses 'count' sequential elements from each enabled array to construct a sequence of geometric primitives, beginning with element 'first.'


-SirKnight

This topic is closed to new replies.

Advertisement