c++ - 如何在 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();
}
没有错误消息,但我不知道它是否是立方体。
解决方案
// 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();
推荐阅读
- django - Django获取嵌套相关对象的计数
- python - 如何将选择动态传递到 django 表单中
- wiki - 通过 Wiki Rest API 向 Confluence 页面添加特殊字段
- google-cloud-firestore - Cloud Firestore 带宽耗尽错误
- mysql - Rails 5 - 间歇性错误 - ActiveRecord::NotNullViolation(Mysql2::Error: 字段'created_at'没有默认值
- firebase - firestore触发的java-script google功能代码无法正常工作
- javascript - 如何在moment.js中四舍五入到最接近的上15分钟
- mysql - 在mysql上以最快的方式查找数字序列,
- couchbase - FindAndModify 使用 Couchbase
- python - 如何使用 c++ 中的引用实现类似 python 的行为?