OpenGL改变背景而不改变先前绘制的图片的透视

时间:2015-02-06 05:10:18

标签: opengl

所以我绘制'I'并使用gluLookAt(0.f,0.f,3.f,0.f,0.f,0.f,0.f,1.f,0.f) ,我的身材适中。然后我添加一个drawScene()函数,用渐变颜色绘制背景,然后'I'变得超大。我想这是因为我在drawScene()中将矩阵模式更改为GL_PROJECTION和GL_MODELVIEW,那些可能会更改透视图?我想glPushMatrix()和glPopMatrix()需要保留矩阵状态,但我很难找到放置它们的位置。那么如何让'I'看起来正常呢?这是我的drawI()和drawScene():

void drawI(int format)
{
    glBegin(format);
    glColor3f(0, 0, 1);
    glVertex2f(point[3][0], point[3][1]);
    glVertex2f(point[2][0], point[2][1]);
    glVertex2f(point[1][0], point[1][1]);
    glVertex2f(point[12][0], point[12][1]);
    glVertex2f(point[10][0], point[10][1]);
    glEnd();

    glBegin(format);
    glVertex2f(point[10][0], point[10][1]);
    glVertex2f(point[11][0], point[11][1]);
    glVertex2f(point[12][0], point[12][1]);
    glEnd();

    glBegin(format);
    glVertex2f(point[9][0], point[9][1]);
    glVertex2f(point[10][0], point[10][1]);
    glVertex2f(point[3][0], point[3][1]);
    glVertex2f(point[4][0], point[4][1]);
    glVertex2f(point[6][0], point[6][1]);
    glColor3f(1, 0.5, 0);
    glVertex2f(point[7][0], point[7][1]);
    glVertex2f(point[8][0], point[8][1]);
    glEnd();

    glBegin(format);
    glColor3f(0, 0, 1);
    glVertex2f(point[5][0], point[5][1]);
    glVertex2f(point[6][0], point[6][1]);
    glVertex2f(point[4][0], point[4][1]);
    glEnd();

}

void drawScene()
{
    glBegin(GL_QUADS);
    //red color
    glColor3f(1.0,0.0,0.0);
    glVertex2f(-1.0,-1.0);
    glVertex2f(1.0,-1.0);
    //blue color
    glColor3f(0.0,0.0,1.0);
    glVertex2f(1.0, 1.0);
    glVertex2f(-1.0, 1.0);
    glEnd();


}

非常感谢!

所以我从drawScene()和drawI()中取出glMatrixMode()和glLoadIdentity()并将它们放在display()中。我上面改了drawScene()和drawI(),这是我的display()

void display()
{    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(70.f,1.f,0.001f,30.f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    drawScene();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    gluLookAt(0.f,0.f,3.f,0.f,0.f,0.f,0.f,1.f,0.f);

    drawI(GL_TRIANGLE_FAN);

    glutSwapBuffers();  
}

1 个答案:

答案 0 :(得分:0)

执行此操作(在3D模式下)的常规方法是在您的代码中,在调用drawI或drawScene之前:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far); // fov is camera angle in degrees, aspect is width/height of your viewing area, near and far are your near and far clipping planes.

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0)

在2D渲染中,您可能不需要调用gluPerspective,但是在调用drawI或drawScene之前,这些调用应该在您的代码中完成。执行此操作并从drawI和drawScene中删除glMatrixMode()和glLoadIdentity()调用。

编辑:

如果你的"我"仍然太大了,你可以做很多事情,但你应该用3D操作(也给出一个Z坐标)。

您可以缩放对象:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0)
glScalef(0.5, 0.5, 0.5);

您可以将相机向后移动(您还需要包含gluPerspective()调用):

gluLookAt(0.0,0.0,50.0,0.0,0.0,0.0,0.0,1.0,0.0);

在3D模式下控制渲染图像尺寸的最简单方法可能是将相机(眼睛)向后移动,然后通过改变相机光圈角度(gluPerspective()调用中的fov来控制图像大小) 。更宽的fov会缩小渲染图像;较小的fov会扩大它。

我不知道你的坐标值是什么,因为它们是变量,但相机位置为3.0,fov为70.0,方面为1应该给你左,右,在Z = 0时,顶部和底部剪切平面约为+/- 2.1。

如果你保持其他所有内容相同并将相机移动到50.0,剪裁平面将在+/- 35.0左右,那么你的" I"会占据观看区域的一小部分。

如果您将相机位置保持在50.0,但将fov更改为40.0,则剪裁平面将在+/- 18.2左右。你的"我"将填充比cameraZ = 50.0,fov = 70.0更大的区域,但是比相机Z = 3.0,fov = 70.0更小的区域。

您可以使用相机位置和fov来获得所需的图像尺寸,也可以缩放图像。我喜欢保持相机位置不变并改变fov。如果我提供的功能可以根据用户输入(可能是鼠标滚动)更改fov,那么它是提供放大/缩小效果的好方法。

BTW,在您的原始代码中,如果您致电:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,3.0,0.0,0.0,0.0,0.0,1.0,0.0)

然后在DrawI或drawScene中调用:

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

您已经删除了之前调用gluLookAt()所加载的矩阵。