Search code examples
c++openglglfwglm-mathopengl-compat

How do I change the viewing angle in OpenGL?


I need to create a 3d cube, and so far I've created all the vertices but when I run the program I can only see the cube (or what I hope is a cube, I can't tell) from one face, so it looks like a square. I want to know how to view my cube from above, so I can check whether or not it actually looks the way I want it to.

I created the 24 vertices using glVertex3f but like I said I can't tell if it is a cube or not because I cannot look at it from an angle other than the default.

I tried downloading GLM but I am very confused on how, if at all, to use that to change the viewing perspective.

glEnable(GL_DEPTH_TEST);
    // Loop until the user closes the window
    while (!glfwWindowShouldClose(window))
    {
        // Render here
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBegin(GL_QUADS);
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(0.5f, 0.5f, 0.5f);
        glVertex3f(-0.5f, 0.5f, 0.5f);
        glVertex3f(-0.5f, -0.5f, 0.5f);
        glVertex3f(0.5f, -0.5f, 0.5f);

        ... // Repeating drawing the vertices for each vertex of the cube

        glEnd();

        // Swap front and back buffers
        glfwSwapBuffers(window);

        // Poll for and process events
        glfwPollEvents();
    }

No error messages but I cant tell if its a cube or not.


Solution

  •     // Render here
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        // need the window width & height to compute aspect ratio
        int width, height;
        glfwGetWindowSize(window, &width, &height);
    
        // set up the camera projection (if you haven't done this in init)
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0f, float(width) / height, 0.1f, 100.0f);
    
        // set camera position & orientation
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(1, 1, -3, //< eye position
                  0, 0, 0,  //< aim position
                  0, 1, 0); //< up direction
    
        // now draw stuff
        glBegin(GL_QUADS);
        glEnd();