首页 > 解决方案 > 如何在 OpenGL 中更改视角?

问题描述

我需要创建一个 3d 立方体,到目前为止,我已经创建了所有顶点,但是当我运行程序时,我只能从一个面上看到立方体(或者我希望是一个立方体,我无法分辨),所以它看起来像一个正方形。我想知道如何从上面查看我的立方体,这样我就可以检查它是否真的看起来像我想要的那样。

我使用 glVertex3f 创建了 24 个顶点,但就像我说的那样,我无法判断它是否是立方体,因为我无法从默认角度以外的角度查看它。

我尝试下载 GLM,但我很困惑如何使用它来改变视角。

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

没有错误消息,但我不知道它是否是立方体。

标签: c++openglglfwglm-mathopengl-compat

解决方案


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

推荐阅读