如何在OpenGL中更改视图透视图?

问题描述 投票:1回答:1

我画了许多线以形成网格。我想看到网格在其X轴上旋转,但是我从未得到预期的结果。我尝试了glRotatefgluLookAt,但它们并没有按照我想要的方式工作。请看下面的图片。

this is the grid

this is how I want to see it

编辑:geez,在这里发布代码也很困难,哈哈,反正在这里。Edit2:已删除,仅保留有问题的代码。

无论我如何设置gluLookAt,请找到以下代码,网格结果将不在我想要的透视图中。

#include <GL/glut.h>

void display() {

    ...

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_LINES);
    for (int i = 0; i < 720; i += 3)
    {   
        glColor3f(0, 1, 1);
        glVertex3f(linePoints[i], linePoints[i + 1], linePoints[i + 2]);
    }
    glEnd();

    glFlush();
}
void init() {

    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60.0, 4.0 / 3.0, 1, 40);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, -2, 1.25, 0, 0, 0, 0, 1, 0);
}
opengl camera rotation perspective viewpoint
1个回答
0
投票

让我们假设您在世界的xy平面中有一个网格:

glColor3f(0, 1, 1);
glBegin(GL_LINES);
for (int i = 0; i <= 10; i ++)
{   
    // horizontal
    glVertex3f(-50.0f + i*10.0f, -50.0f, 0.0f);
    glVertex3f(-50.0f + i*10.0f,  50.0f, 0.0f);

    // vertical
    glVertex3f(-50.0f, -50.0f + i*10.0f, 0.0f);
    glVertex3f( 50.0f, -50.0f + i*10.0f, 0.0f);
}
glEnd();

确保到投影的远平面的距离足够大(请参见gluPerspective)。裁剪不在gluPerspective远平面附近的所有几何图形。进一步确保纵横比(4.0 / 3.0)与视口矩形(窗口)的比例匹配。

Viewing frustum

为了使用glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, 4.0 / 3.0, 1, 200); ,视图的向上矢量必须为网格的gluLookAt。如果栅格平行于xy平面布置,则向上矢量为z轴(0、0、1)。目标(中心)是网格(0、0、0)的中心。视点(眼睛位置)应该在网格的上方和前面,例如(0,-55,50)。请注意,该视图用于网格,其左下角为(-50,-50,0),右上角为(50,50,0)。

gluLookAt

perpendicular

© www.soinside.com 2019 - 2024. All rights reserved.