当前位置:   article > 正文

4.4 视口变换

视口变换

一、投影变换和视口变换
投影变换是将viewing frustum(视景体)变换为一个cuboid(立方体)
这里写图片描述

视口变换在投影变换之后,是将这个cuboid中的物体变换到视口中,见下图
这里写图片描述

二、视口变换
glViewport(GLint x,GLint y,GLsizei width,GLsizei height)

X,Y以像素为单位,指定了视口的左下角(在第一象限内,以(0,0)为原点的)位置。
width,height表示这个视口矩形的宽度和高度,根据窗口的实时变化重绘窗口。

glViewport在默认情况下,视口被设置为占据打开窗口的整个像素矩形,窗口大小和设置视口大小相同,所以为了选择一个更小的绘图区域,就可以用glViewport函数来实现这一变换,在窗口中定义一个像素矩形,最终将图像映射到这个矩形中。

可以对窗口区域进行划分,在同一个窗口中显示分割屏幕的效果,以显示多个视图。

示例1:

    //边界
    glLineWidth(3.0f);          //设置直线宽度

    glLineStipple(1, 0x3F07);   //设置虚线的模式
    glEnable(GL_LINE_STIPPLE);  //启用虚线

    glViewport(0,0,WIDTH,HEIGHT);
    glPushMatrix(); 
        glTranslatef(0.0f,0.0f,-4.0f);
        glBegin(GL_LINES);
            glColor3f(1.0f,1.0f,1.0f);
            glVertex3f(-WIDTH/2,0.0f,0.0f);
            glVertex3f(WIDTH/2,0.0f,0.0f);
            glVertex3f(0.0f,-HEIGHT/2,0.0f);
            glVertex3f(0.0f,HEIGHT/2,0.0f);
        glEnd();
    glPopMatrix();


    //视口1
    glViewport(0,0,WIDTH/2,HEIGHT/2);
    glPushMatrix(); 
        glTranslatef(0.0f,-0.5f,-3.0f);
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f,0.0f,0.0f);
            glVertex3f(0.0f,0.0f,0.0f);
            glColor3f(0.0f,0.0f,1.0f);
            glVertex3f(0.0f,1.0f,0.0f);
            glColor3f(0.0f,1.0f,0.0f);
            glVertex3f(-1.0f,0.0f,0.0f);
        glEnd();
    glPopMatrix();


    //视口2
    glViewport(WIDTH/2,0,WIDTH/2,HEIGHT/2);
    glPushMatrix(); 
        glTranslatef(0.0f,-0.5f,-3.0f);
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f,0.0f,0.0f);
            glVertex3f(0.0f,0.0f,0.0f);
            glColor3f(0.0f,0.0f,1.0f);
            glVertex3f(0.0f,1.0f,0.0f);
            glColor3f(0.0f,1.0f,0.0f);
            glVertex3f(-1.0f,0.0f,0.0f);
        glEnd();
    glPopMatrix();


    //视口3
    glViewport(WIDTH/2,HEIGHT/2,WIDTH/2,HEIGHT/2);
    glPushMatrix(); 
        glTranslatef(0.0f,-0.5f,-3.0f);
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f,0.0f,0.0f);
            glVertex3f(0.0f,0.0f,0.0f);
            glColor3f(0.0f,0.0f,1.0f);
            glVertex3f(0.0f,1.0f,0.0f);
            glColor3f(0.0f,1.0f,0.0f);
            glVertex3f(-1.0f,0.0f,0.0f);
        glEnd();
    glPopMatrix();


    //视口4
    glViewport(0,HEIGHT/2,WIDTH/2,HEIGHT/2);
    glPushMatrix(); 
        glTranslatef(0.0f,-0.5f,-3.0f);
        glBegin(GL_TRIANGLES);
            glColor3f(1.0f,0.0f,0.0f);
            glVertex3f(0.0f,0.0f,0.0f);
            glColor3f(0.0f,0.0f,1.0f);
            glVertex3f(0.0f,1.0f,0.0f);
            glColor3f(0.0f,1.0f,0.0f);
            glVertex3f(-1.0f,0.0f,0.0f);
        glEnd();
    glPopMatrix();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

效果图:
这里写图片描述

三、glViewport()函数和glOrtho()函数

glOrtho函数只是负责使用什么样的视景体来截取图像,并不负责使用某种规则把图像呈现在屏幕上。

glViewport负责把视景体截取的图像按照怎样的高和宽显示到屏幕上。图像会根据视口的大小拉伸变形。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/589670
推荐阅读
相关标签
  

闽ICP备14008679号