# How to draw N sided polygon using VBOs?

Hi,

I am looking into resources, sample code, anything related on how to draw a N sided polygon using VBOs in 2D

Any help will be much appreciated

Cheers
rS

Well, it looks like the best thing for me is to learn how to draw using vertex array first instead of jumping straight to VBO, so I did but I have a problem, the code draw a n side polygon, but there is always a vertice at [0,0] and I am not sure how to fix it

``````
#define NUM_SIDES 4

typedef struct {
GLfloat x;
GLfloat y;
GLfloat z;
} Vertex3D;

Vertex3D vertices[NUM_SIDES];

GLfloat colors[] = {
1.0, 0.0, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0
};

//--------------------------------------------------------------
void testApp::setup(){
ofBackground(0.0, 0.0, 0.0);

// set vertices values
const float K = TWO_PI / NUM_SIDES;
for (int i = 0; i < NUM_SIDES; i++) {
vertices[i].x = cos(i * K) * radius;
vertices[i].y = sin(i * K) * radius;
vertices[i].z = 0.0;
}
}

//--------------------------------------------------------------
void testApp::draw(){
glPushMatrix();
glTranslatef(ofGetWidth() * 0.5, ofGetHeight() * 0.5, 0.0);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, &vertices);
glColorPointer(4, GL_FLOAT, 0, &colors);
glDrawArrays(GL_LINE_LOOP, 0, ((sizeof vertices) / 2) * NUM_SIDES);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);

glPopMatrix();
}

``````

Any help will be much appreciated

Cheers
rS

I think your call to glDrawArrays is wrong –

http://www.opengl.org/sdk/docs/man/xhtm-…-Arrays.xml

seems to me that the number of vertices to draw should be 4, not ((sizeof vertices) / 2) * NUM_SIDES.

If I plug in this, it seems to work fine for me:

``````
glDrawArrays(GL_LINE_LOOP, 0, NUM_SIDES);

``````

take care!
zach

BOOMBASTIC!