OpenGL 透视

标签 opengl 3d projection

我正在尝试使用透视投影描绘一个立方体,但我得到的只是一个正方形的角。正方形的面设置在原点并向正方向扩展。使用 glOrtho 我可以设置坐标系,但我在使用 glPerspective 做同样的事情时遇到了麻烦。

#include <gl/glut.h>

void mesh(void) {
float v[8][3] = { /* Vertices for 8 corners of a cube. */
{0.0, 0.0, 0.0}, {100.0, 0.0, 0.0}, {100.0, 100.0, 0.0}, {0.0, 100.0, 0.0},
{0.0, 0.0, -100.0}, {100.0, 0.0, -100.0}, {100.0, 100.0, -100.0}, {0.0, 100.0, -100.0} };
float n[6][3] = { /* Normals for the 6 faces of a cube. */
{0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0},
{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0} };
int f[6][4] = { /* Indexes of the vertices in v that make the 6 faces of a cube. */
{0, 1, 2, 3}, {1, 5, 6, 2}, {3, 2, 6, 7}, 
{0, 4, 7, 3}, {0, 1, 5, 4}, {4, 5, 6, 7} };



for (int j = 0; j < 6; j++) {
    glBegin(GL_QUADS);
    glNormal3fv(&n[j][0]);
    glVertex3fv(&v[f[j][0]][0]);
    glVertex3fv(&v[f[j][1]][0]);
    glVertex3fv(&v[f[j][2]][0]);
    glVertex3fv(&v[f[j][3]][0]);
    glEnd();
    glFlush();
}
}

void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
mesh();
}

void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB |GLUT_DEPTH |GLUT_SINGLE);
glutInitWindowSize(400, 300);
glutInitWindowPosition(200, 200);
glutCreateWindow("Mesh");
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glRotatef(15, 0.0, 0.0, 1.0);
//glOrtho(-400.0, 400.0, -300.0, 300.0, 200.0, -200.0);
gluPerspective(120,1,0,600);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutDisplayFunc(display);
glutMainLoop();
}

最佳答案

你说你只能看到立方体的角?那么你的视野太宽了。你正在使用 gluPerspective() 并提供你的计算是正确的。我的值有点偏离,函数参数是:

void gluPerspective(GLdouble fovy,
                    GLdouble aspect_ratio,
                    GLdouble zNear,
                    GLdouble zFar);

我建议将其更改为类似

gluPerspective(45.0f,
               width_of_window / height_of_window,    //aspect ratio
               0.1f, 
               500.0f);

关于OpenGL 透视,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11562135/

相关文章:

java - 在 OpenGL 中绘制多个对象

java - 如何在 JavaFX 中将 GUI 覆盖在 3D 场景上?

c++ - Equirectangular 到 Cubic 与点对点映射?

matrix - 投影矩阵在平面上投影一个点

algorithm - 没有获得关于 Kinect 的正确用户方向

c++ - 为什么我在带有 OpenGL 核心配置文件的窗口上看不到红色三角形?

opengl - GLSL 纹理大小

c++ - 现代 OpenGL 投影 View 模型转换不起作用

c++ - GLM opengl 裁剪问题 - Game Engine c++

r - 在 R 中制作 3D 图