Luxemburg Manhattan
Luxemburg Manhattan

Reputation: 31

OpenGL draw a star

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

Upvotes: 2

Views: 14490

Answers (1)

Rabbid76
Rabbid76

Reputation: 210889

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.

Upvotes: 1

Related Questions