OpenGL draw a star

10,148

First of all note, drawing by glBegin/glEnd sequences and the primitive type GL_POLYGON is deprecated since more than 10 years. Read about Fixed Function Pipeline and see Vertex Specification for a state of the art way of rendering.


You swapped the x coordinates of the the first 2 vertex coordinates of the polygon. Change your code like this, to solve the issue:

glColor3f(1, 0, 0);
glBegin(GL_POLYGON);

glVertex3f(-0.60, 0.77, 0); // <--- -0.60 instead of -0.68
glVertex3f(-0.68, 0.77, 0); // <--- -0.68 instead of -0.60
glVertex3f(-0.7, 0.68, 0);
glVertex3f(-0.64, 0.63, 0);
glVertex3f(-0.58, 0.68, 0);

glEnd();

Note, instead of the deprecated primitive type GL_POLYGON, GL_TRIANGLE_FAN can be used.

Share:
10,148

Related videos on Youtube

Luxemburg Manhattan
Author by

Luxemburg Manhattan

Updated on June 04, 2022

Comments

  • Luxemburg Manhattan
    Luxemburg Manhattan almost 2 years

    I am working on a 2D star. I was able to create 5 triangles for the points, and then the polygon for the middle, but when I ran the program, the polygon cut out a portion that was not colored. I tried to cover the non covered portion with a triangle and that also did not work.

    What is wrong with the code? Or what I can add to ensure that none of my shapes have missing coloring?

    Here is my code:

    void draw() {
    
    
        glBegin(GL_TRIANGLES);
    
        glColor3f(1, 0, 0);
        glVertex3f(-0.60, 0.77, 0);
    
        glColor3f(0, 1, 0);
        glVertex3f(-0.42, 0.77, 0);
    
        glColor3f(0, 0, 1);
        glVertex3f(-0.58, 0.68, 0);
    
        //second triangle top triangle
    
        glColor3f(1, 0, 0);
        glVertex3f(-0.64, 1, 0);
    
        glColor3f(0, 1, 0);
        glVertex3f(-0.68, 0.77, 0);
    
        glColor3f(0, 0, 1);
        glVertex3f(-0.60, 0.77, 0);
    
        //3rd Triangle
        glColor3f(1, 0, 0);
        glVertex3f(-0.68, 0.77, 0);
    
        glColor3f(0, 1, 0);
        glVertex3f(-0.7, 0.68, 0);
    
        glColor3f(0, 0, 1);
        glVertex3f(-0.86, 0.77, 0);
    
        //4th Triangle
        glColor3f(1, 0, 0);
        glVertex3f(-0.64, 0.63, 0);
    
        glColor3f(0, 1, 0);
        glVertex3f(-0.7, 0.68, 0);
    
        glColor3f(0, 0, 1);
        glVertex3f(-0.82, 0.43, 0);
    
        //5th Triangle
        glColor3f(1, 0, 0);
        glVertex3f(-0.64, 0.63, 0);
    
        glColor3f(0, 1, 0);
        glVertex3f(-0.58, 0.68, 0);
    
        glColor3f(0, 0, 1);
        glVertex3f(-0.51, 0.43, 0);
    
        glEnd();
    
        glBegin(GL_POLYGON);//code for the Polygon within the stars
        glColor3f(1, 0, 0);//sets color of Polygon
    
        glVertex3f(-0.68, 0.77, 0); //set the 5 vetices of polygon
    
        glColor3f(1, 0, 0);
        glVertex3f(-0.60, 0.77, 0);
    
        glColor3f(1, 0,0 );
        glVertex3f(-0.7, 0.68, 0);
    
        glColor3f(1, 0, 0);
        glVertex3f(-0.64, 0.63, 0);
    
        glColor3f(1, 0, 0);
        glVertex3f(-0.58, 0.68, 0);
    
    
    
    
        glEnd();
    
        //code for pentagaon
        glBegin(GL_POLYGON);
        glColor3f(1, 0, 0);//sets color of Polygon
        glVertex3f(0.45, 0.82, 0); //set the 5 vetices of polygon
    
        glColor3f(0, 1, 0);
        glVertex3f(0.8, 0.6, 0);
    
        glColor3f(0, 1, 0);
        glVertex3f(0.2, 0.6, 0);
    
        glColor3f(1, 0, 0);
        glVertex3f(0.2, 0.3, 0);
    
        glColor3f(1, 0, 0);
        glVertex3f(0.7, 0.3, 0);
    
    
        glEnd();
    }
    
    • BDL
      BDL almost 6 years
      Could you add an image to show the problem? Also, what is the pentagon polygon? The description of what you want to draw only includes one polygon.
    • meowgoesthedog
      meowgoesthedog almost 6 years
      The order of points matters if you have backface culling enabled. In your case some of the triangles have clockwise points and some anti-clockwise. Either use a consistent cyclic order or switch off backface culling by calling glCullFace(GL_FRONT_AND_BACK);.