View Full Version : how to draw a pentagon?

mikeglaz

04-15-2013, 08:47 AM

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();

carsten neumann

04-15-2013, 02:22 PM

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.

mikeglaz

04-15-2013, 02:55 PM

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

Carmine

04-15-2013, 03:09 PM

How do I calculate the positions of the vertices? I thought rotation and translation are OpenGL's primary ways of vertex transformations?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[5][2], 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][0] = cos (ang);

pent[v][1] = 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?

mikeglaz

04-15-2013, 09:30 PM

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[5][2];

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][0] = cos (ang);

pent[v][1] = 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?

Ok, I get it. Thanks

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.