Jump to content
  • Advertisement
Sign in to follow this  
Stompy9999

OpenGL Vertex Array question

This topic is 5028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
err, oops http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/drawarrays.html

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!