# how to draw a pentagon?

It seems like translations and rotations are not allowed between the commands glBegin() and glEnd(). If not, how am I supposed to draw something like this (a pentagon):

``````
glBegin(GL_LINE_LOOP);
for (int i=0; i < 360; i += 360/5)
{
glPushMatrix();
angle = i;
glRotated(angle, 0.0, 0.0, 1.0);
glTranslatef(0.0, 10, 0.0);
glVertex2f(0.0, 0.0);
glPopMatrix();
}
glEnd();

``````

Calculate the positions of the vertices and emit those with glVertex2f.

You may also want to learn about OpenGL core profile (where there is no immediate mode - i.e. no glBegin()/glEnd()) and how to store vertex data in buffer objects.

How do I calculate the positions of the vertices? I thought rotation and translation are OpenGL’s primary ways of vertex transformations?

[QUOTE=mikeglaz;1249926]How do I calculate the positions of the vertices? I thought rotation and translation are OpenGL’s primary ways of vertex transformations?[/QUOTE]You’re going to have to use some trigonometry to calculate the coordinates of the pentagon vertices.
I’d do it in a loop before you actually draw it. Something like this -

``````int v;
float pent, ang, da = 6.2832 / 5.0;   // da is central angle between vertices in radians

for (v = 0; v < 5; v++)  {                  // Computes vertex coordinates.
ang = v * da;
pent[v] = cos (ang);
pent[v] = sin (ang);
}

glBegin (GL_LINE_LOOP);                                         // Draws pentagon.
for (v = 0; v < 5; v++)  glVertex2fv(pent[v]);
glEnd();

``````

This should draw a pentagon with vertices on the unit circle.
Haven’t tested this code. But it should be close to what you need.
Did you know that glVertex can only be called between glBegin and glEnd?

[QUOTE=Carmine;1249927]You’re going to have to use some trigonometry to calculate the coordinates of the pentagon vertices.
I’d do it in a loop before you actually draw it. Something like this -

``````int v;
float pent;

float ang, da = 6.2832 / 5.0;    // central angle between vertices in radians

// Compute vertex coordinates.

for (v = 0; v < 5; v++)  {
ang = v * da;
pent[v] = cos (ang);
pent[v] = sin (ang);
}

// Draw loop.

glBegin (GL_LINE_LOOP);
for (v = 0; v < 5; v++)  glVertex2fv(pent[v]);
glEnd();

``````

This should draw a pentagon with vertices on the unit circle.
Haven’t tested this code. But it should be close to what you need.
Did you know that glVertex can only be called between glBegin and glEnd?[/QUOTE]

Ok, I get it. Thanks