Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: how to draw a pentagon?

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2010
    Posts
    28

    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):

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

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    741
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2010
    Posts
    28
    How do I calculate the positions of the vertices? I thought rotation and translation are OpenGL's primary ways of vertex transformations?

  4. #4
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    185
    Quote Originally Posted by mikeglaz View Post
    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 -

    Code :
    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?
    Last edited by Carmine; 04-16-2013 at 11:44 AM.

  5. #5
    Junior Member Newbie
    Join Date
    Jun 2010
    Posts
    28
    Quote Originally Posted by Carmine View Post
    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 -

    Code :
    [FONT=Courier New]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();
    [/FONT]

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •