Search code examples
c++openglglutopengl-compat

Show glutSolidCone on its side


I am trying to look at a cone lying on its side from above. For this I wrote the following code:

void display(void)
{   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(250, 250, 0);
    glRotatef(90, 1, 0, 0);
    glutSolidCone(20, 20, 20, 20);
    glTranslatef(-250, -250, 0);
    glRotatef(-90, 1, 0, 0);

    glFlush();
    glutSwapBuffers(); 
}

int main(int argc, char **argv)
{
    float x, y;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutCreateWindow("Cone");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(drag);
    glutMouseFunc(click);
    glutSpecialFunc(catchKey);   

    glEnable(GL_DEPTH_TEST);

    glutMainLoop();         
    return 0;
}

I translate to the middle of the screen and then rotate 90 degrees around the x-axis, which in my idea should produce the cone on its side. The result however is a not a cone but a line which is not tilted 90 but 45 degrees. What should I change in order for the cone to show up as I want it to?


Solution

  • Where is the projection matrix?

    If you don't set a projection matrix, then the coordiantes have to be set in normalize device space. In NDC all the coordinates are in range [-1.0, 1.0]:

    e.g.

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    glTranslatef(0.5, 0.5, 0);
    glRotatef(90, 1, 0, 0);
    glutSolidCone(1.0, 1.0, 20, 20);
    glTranslatef(-0.5, -0.5, 0);
    glRotatef(-90, 1, 0, 0);
    

    Alternatively you can setup an orthographic projection, which projects the world coordinates 1:1 to the window coordinates. The projection matrix can be set by glOrtho:

    e.g.

    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     
    
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0, 500.0, 500.0, 0.0, -100.0, 100.0);
    
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    
        glTranslatef(250, 250, 0);
        glRotatef(90, 1, 0, 0);
        glutSolidCone(20, 20, 20, 20);
        glTranslatef(-250, -250, 0);
        glRotatef(-90, 1, 0, 0);
    
        glFlush();
        glutSwapBuffers(); 
    }
    

    The projection matrix describes the mapping from 3D points of the view on a scene, to 2D points on the viewport. At Orthographic Projection, the view space coordinates are linearly mapped to the clip space coordinates. The clip space coordinates are equal to the normalized device coordinates.
    The normalized device coordinates are linearly mapped to the viewport rectangle. The viewport rectangle can be defined by glViewport. Initially it is defined by the size of the window.


    Note, that drawing by glBegin/glEnd sequences and the fixed function matrix stack is deprecated since decades. See Fixed Function Pipeline and Legacy OpenGL. Read about Vertex Specification and Shader for a state of the art way of rendering.