glDrawArrays & glDrawElements GL_POLYGON Problems

Hey,

I’m having some problems drawing a polygon using a vertex array :frowning:

If I put the vertex data into a glBegin(GL_POLYGON), glEnd() it will work fine but the below code refuses to work. I have enabled the vertex array in another function, this code works if you tell it to draw points or lines but not polygons.

Any ideas?

Thanks

Paul

static GLint vertices[] = { 0.25, 0.25, 0.0,
0.75, 0.25, 0.0,
0.75, 0.75, 0.0,
0.25, 0.75, 0.0 };

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

static GLubyte indices[] = { 0, 1, 2, 3 };

glVertexPointer (3, GL_INT, 0, vertices);
glColorPointer (3, GL_FLOAT, 0, colors);

glDrawElements (GL_POLYGON, 4, GL_UNSIGNED_BYTE, indices);

//glDrawArrays (GL_POLYGON, 0, 4);

Originally posted by DragonSpawn:
[b]
static GLint vertices = { 0.25, 0.25, 0.0,
0.75, 0.25, 0.0,
0.75, 0.75, 0.0,
0.25, 0.75, 0.0 };

[/b]
Oups ! I didn’t see if the other things are right. But that is wrong. You say you put GLint inside vertices but you use float values. If I’m not wrong, this simply cast values, all to zero. So you can not see anything at all.

Oops, that was a typo :slight_smile:

But this code does not work, the only difference being that instead of creating a square, it creates a 8-sided polygon.

/-
| |
-/

Any ideas?

static GLfloat vertices[] = { -1.00, 0.00, 0.00,
							  -2.00, 0.00, -1.00,
							  -2.00, 0.00, -2.00,
							  -1.00, 0.00, -3.00, 
							  1.00, 0.00, -3.00,
							  2.00, 0.00, -2.00,
							  2.00, 0.00, -1.00, 
							  1.00, 0.00, 0.00 };

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

static GLubyte indices[] = { 0, 1, 2, 3, 4, 5, 6, 7 };


glVertexPointer (3, GL_FLOAT, 0, vertices);
glColorPointer (3, GL_FLOAT, 0, colors);

glDrawElements (GL_POLYGON, 8, GL_UNSIGNED_BYTE, indices);

for drawing squares, try GL_QUADS instead of GL_POLYGON. The last draws only one polygon whatever how many vertices you specify in the array.

I’m not trying to draw a square :stuck_out_tongue: I’m trying to draw a polygon shaped like:

/-
| |
-/

but the above code is not drawing anything :frowning:

OK. I didn’t noticed that.

It’s simply because you culled the back faces and you draw it so it can be seen from bottom only.

Ah…forgive me, its been one of those days.

Thankyou :slight_smile: