您的位置:首页 > 其它

简单说说glutGet(GLUT_WINDOW_X/Y)与glViewport

2016-02-09 13:24 369 查看
glViewport(l,b,w,h);的作用是在窗口内划出一个宽为w,高为h的矩形区域。随后的绘制都发生在这个长方形里面。而l和b分别指的是这个长方形的左下角到窗口左边和下边的距离,单位是像素。

glutGet则不同。glutGet(GLUT_WINDOW_X/Y)获取的是窗口的左上角相对于屏幕的左边/上边的距离,单位是像素。

看下面的程序:它绘制了两个立方体,一红一蓝。立方体的前后左右的投影投射在窗口的四个glViewport里面。每当窗口的尺寸变化时,程序会调用glutGet(GLUT_WINDOW_X/Y)来获取窗口的位置。

#include <windows.h>
#include <gl/glut.h>

GLint gliW = 200, gliH = 200, gliLeft1 = 0, gliBottom1 = 0, gliLeft2 = 200, gliBottom2 = 0,
gliLeft3 = 400, gliBottom3 = 0, gliLeft4 = 0, gliBottom4 = 200, gliLeft5 = 200, gliBottom5 = 200,
gliLeft6 = 400, gliBottom6 = 200, gliLeft7 = 0, gliBottom7 = 400, gliLeft8 = 200, gliBottom8 = 400,
gliLeft9 = 400, gliBottom9 = 400;

void init()
{
glClearColor(0,0,0,0);//设定黑色为清空后的背景颜色
glShadeModel(GL_FLAT);
}

void display()
{
glClear( GL_COLOR_BUFFER_BIT );//清空窗口,只留背景

//从后看
glColor3f( 0.0, 0.0, 1.0 );

glLoadIdentity();

gluLookAt(0,0,-20,0,0,0,0,1,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, 10.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glViewport(gliLeft8,gliBottom8,gliW,gliH);

glTranslatef(0,0,2);
glRotatef(30,0,0,1);
glRotatef(30,0,1,0);
glutWireCube(5);
glTranslatef(0,0,2);
glColor3f( 1.0, 0.0, 0.0 );
glutSolidCube(1);

//从前看
glColor3f(0,0,1);
glLoadIdentity();
gluLookAt(0,0,20,0,0,0,0,1,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, 10.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glViewport(gliLeft2, gliBottom2, gliW,gliH);

glTranslatef(0,0,2);
glRotatef(30,0,0,1);
glRotatef(30,0,1,0);
glutWireCube(5);
glTranslatef(0,0,2);
glColor3f( 1.0, 0.0, 0.0 );
glutSolidCube(1);

//从左看
glColor3f(0,0,1);
glLoadIdentity();
gluLookAt(-20,0,0,0,0,0,0,1,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, 10.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glViewport(gliLeft4, gliBottom4, gliW,gliH);

glTranslatef(0,0,2);
glRotatef(30,0,0,1);
glRotatef(30,0,1,0);
glutWireCube(5);
glTranslatef(0,0,2);
glColor3f( 1.0, 0.0, 0.0 );
glutSolidCube(1);

//从右看
glColor3f(0,0,1);
glLoadIdentity();
gluLookAt(20,0,0,0,0,0,0,1,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0, 10.0, -10.0, 10.0, 10.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glViewport(gliLeft6, gliBottom6, gliW,gliH);

glTranslatef(0,0,2);
glRotatef(30,0,0,1);
glRotatef(30,0,1,0);
glutWireCube(5);
glTranslatef(0,0,2);
glColor3f( 1.0, 0.0, 0.0 );
glutSolidCube(1);

glFlush();
}
void reshape(int w, int h)
{
gliW = w / 3;
gliH = h / 3;

gliLeft2 = gliW;
gliLeft3 = gliLeft2 + gliW;

gliLeft5 = gliW;
gliLeft6 = gliLeft5 + gliW;

gliLeft8 = gliW;
gliLeft9 = gliLeft8 + gliW;

gliBottom4 = gliH;
gliBottom7 = gliBottom4 + gliH;
gliBottom5 = gliH;
gliBottom8 = gliBottom5 + gliH;
gliBottom6 = gliH;
gliBottom9 = gliBottom6 + gliH;
display();
printf("x = %d, y = %d\n", glutGet(GLUT_WINDOW_X), glutGet(GLUT_WINDOW_Y));
}

int main( int argc, char ** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize( gliW * 3, gliH * 3);
glutCreateWindow( "立方体" );

init();
glutDisplayFunc( display );
glutReshapeFunc(reshape);
glutMainLoop();

return 0;
}



命令行显示 x = 8, y = 31,应为图中红色箭头所指的位置。因此判断glutGet返回的是窗口的渲染区域的左上角的坐标。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: