赞
踩
一、投影变换和视口变换
投影变换是将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();
效果图:
三、glViewport()函数和glOrtho()函数
glOrtho函数只是负责使用什么样的视景体来截取图像,并不负责使用某种规则把图像呈现在屏幕上。
glViewport负责把视景体截取的图像按照怎样的高和宽显示到屏幕上。图像会根据视口的大小拉伸变形。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。