Search code examples
c++openglglutopengl-compat

How to draw a 4 pointed star using glut openGL


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


Solution

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