How to draw a 4 pointed star using glut openGL

1.3k views Asked by At

I want to draw a 4pointed star using GLUT and openGL in C++. Here is my code

glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0.0f,6.0f,0.0f);
    glVertex3f(1.0f,4.0f,0.0f);
    glVertex3f(3.0f,3.0f,0.0f);
    glVertex3f(1.0f,2.0f,0.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    glVertex3f(-1.0f,2.0f,0.0f);
    glVertex3f(-3.0f,3.0f,0.0f);
    glVertex3f(-1.0f,4.0f,0.0f);

glEnd();

The problem is the shape directly goes to 3,3 from 0,6

can anyone help me how to fix this, screenshot

I want something like this desired output

2

There are 2 answers

3
Rabbid76 On BEST ANSWER

The 1st point of the the GL_TRIANGLE_FAN primitiv is always held fixed (See Triangle primitives). Just start the GL_TRIANGLE_FAN primitiv at one of the "inner" points:

glBegin(GL_TRIANGLE_FAN);
    
    glVertex3f(1.0f,4.0f,0.0f);
    glVertex3f(3.0f,3.0f,0.0f);
    glVertex3f(1.0f,2.0f,0.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    glVertex3f(-1.0f,2.0f,0.0f);
    glVertex3f(-3.0f,3.0f,0.0f);
    glVertex3f(-1.0f,4.0f,0.0f);

    glVertex3f(0.0f,6.0f,0.0f);

glEnd();

0
Steven Lu On

You are creating a triangle fan but setting its central vertex (the initial one) at 0,6,0.

You probably want to change your geometry so that your central vertex is at the origin (for symmetry). It also works to move the first vertex down to the bottom as @Rabbid76 shows.