George
George

Reputation: 5681

opengl regular polygon

i have the following code which draws nothing. If i use glBegin(GL_POINTS) it draws a circle but with polygon mode it doesn't.

int WXSIZE=500,WYSIZE=500;

//Coordinate system
float Xmin=-8, Xmax=8, Ymin=-8, Ymax=8;

void setupmywindow()
{
    glClearColor(0,0,0,0);  
    gluOrtho2D(Xmin, Xmax, Ymin, Ymax);
}


void mypolygon(float radius) //test object
{

   glColor3f(1,0,0);
   int numPoints=20;
   float x,y;
   float centerx,centery=0;

    for (int i = 0; i < numPoints; i++)
{
    x = centerx + radius * sin(2.0*PI*i/numPoints);
    y = centery + radius * cos(2.0*PI*i/numPoints);

     glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
     glBegin(GL_POLYGON);
     glVertex2f(x, y);
     glEnd();

}

}


void myDisplay()
//single object
{
    glClear(GL_COLOR_BUFFER_BIT);
    mypolygon(2.0);
    glutSwapBuffers();
}


int main(int argc, char **argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(WXSIZE,WYSIZE);
    glutCreateWindow("My graphic window");
    setupmywindow();
    glutDisplayFunc(myDisplay);
    glutMainLoop();
} 

Any suggestions?

EDIT----------------------

glBegin(GL_POLYGON);

    for (int i = 0; i < numPoints; i++)
{
    x = centerx + radius * sin(2.0*PI*i/numPoints);
    y = centery + radius * cos(2.0*PI*i/numPoints);

    glVertex2f(x, y);

}
   glEnd();

I messed it with the loop.

Upvotes: 0

Views: 2543

Answers (3)

Davido
Davido

Reputation: 2931

When you call glBegin with GL_POLYGON, it is expecting I believe a minimum of three vertices. Standard drawing protocol is to draw using triangles, witch vertices in sets of three, since you need three vertices for each triangle face. You are only feeding it a single vertex, so you're not going to see anything. Try changing it to this:

glBegin(GL_TRIANGLES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glVertex2f(x3, y3);
glEnd();

Upvotes: 0

Christian Rau
Christian Rau

Reputation: 45948

In every loop you are drawing a polygon, that consists of a single vertex, so nothing. Just put the glBegin/glEnd (and the glPolygonMode) outside of the for loop and only draw glVertex in the loop. Of course it works with points, as a n times a single point is the same as n points. But n polygon consisting of one point each is not the same as one polygon consisting of n points.

Upvotes: 4

tibur
tibur

Reputation: 11636

Your polygon seems to be on the wrong side. By default, OpenGL only shows front faces, which need to be specified counterclockwise. You can:

  • Invert the order of your primitives (for (int i = numPoints-1; i >= 0 ; i--))
  • Invert the front face (glFrontFace(GL_CW))
  • Disable back face culling (glDisable(GL_CULL_FACE)).

Upvotes: 3

Related Questions