View Full Version : Displaying Vertex Arrays

ToddAtWSU

01-31-2006, 05:31 PM

I am trying to display an object with Vertex Arrays. I have the data read from a PLY file into the vertices. If the faces take 3 vertices I draw them as GL_TRIANGLES and 4 vertices as GL_QUADS. I have a file that wants to draw 5 vertices per polygon, thus making pentagons. I assume this could be drawn with GL_POLYGON as could any other shapes such as triangles and quads. My problem comes up with calling the glDrawElements function to display the image from the array. I have read that glDrawElements can take GL_POLYGON as a data type, but how do I specify how many points define the polygon? Thank you for your help!

RigidBody

01-31-2006, 11:17 PM

GLfloat coord[][2] = {{-.75, -.75},{0., -1.0},{.75, -.75},{.75, .75},{0., 0.5}};

unsigned short indices[] = {0, 1, 2, 3, 4};

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-1., 1., -1., 1., 1., 100.);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0., 0., 10., 0., 0., 0., 0., 1., 0.);

glEnableClientState(GL_VERTEX_ARRAY);

glVertexPointer(2, GL_FLOAT, 0, coord);

glDrawElements(GL_POLYGON, 5, GL_UNSIGNED_SHORT, indices); the number of vertices (5) is passed to glDrawElements, not the number of polygons. note that you can only draw one polygon with each call to glDrawElements.

if you draw quads or triangles, a single call to glDrawElements may render many quads/trias, the number of vertices you pass is divided by 4 (for quads) or by 3 (for triangles).

ToddAtWSU

02-01-2006, 05:37 AM

That would work fine if my array of indices was only 5 indices long. But my array contains at least 50 indices, if not many more. I am thinking maybe I could divide this polygon up into many triangles, maybe using a GL_TRIANGLE_FAN. Would this work, or would I need to modify my array of vertices first? Thanks again!

RigidBody

02-01-2006, 05:55 AM

maybe my answer wasn't clear enough...you can pass an arbitrary number of indices to glDrawElements.

if you use GL_POLYGON, only one poly is drawn with each call to glDrawElements; the poly is converted to triangles internally by the ogl driver. if you use a color array with different colors for each vertex, like in my example, you will notice that the color interpolation is not smooth at the edges of the internally generated trias.

you can use the same vertex array, no matter which type of primitive- trias, quads, polys- you draw. the vertex array defines a set of points in space; and the index array defines the connectivity between these vertices.

ToddAtWSU

02-01-2006, 06:08 AM

Doesn't the 2nd argument of glDrawElements take in the total number of items in the indices array? So if I have 50 items in my array, my second argument would need to be 50, right? But wouldn't that try to draw a 50-sided polygon? Or do I not understand what the 2nd argument for glDrawElements is? Thanks again!

songho

02-01-2006, 06:35 AM

RigidBody said glDrawElements() draws a sequence of primitives by hopping around vertex arrays with the associated array indices.

So you can pick any limited number of vertices from array, and the number of vertices becomes a param of glDrawElements(), for example, 5.

I think you are thinking about glDrawArrays(). Yes it only reads the array straight through.

Here is an example to draw 2 pentagons using glDrawElements():

GLfloat vertices[] = { ... };

GLushort indices[] = {0,1,2,3,4, 5,6,7,8,9,...};

// draw first poly using index 0 to 4

glDrawElements(GL_POLYGON, 5, GL_UNSIGNED_SHORT, &indices[0]);

// draw second poly using index 5 to 9

glDrawElements(GL_POLYGON, 5, GL_UNSIGNED_SHORT, &indices[5]);

EDIT:

To use glDrawArrays(), all shared vertices must duplicated in the arrays. The last param(5) is the number of vertices per polygon and the second param is starting offset of arrays.

To draw 2 pentagons with glDrawArrays():

glDrawArrays(GL_POLYGON, 0, 5);

glDrawArrays(GL_POLYGON, 5, 5);

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.