简单说说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)来获取窗口的位置。
命令行显示 x = 8, y = 31,应为图中红色箭头所指的位置。因此判断glutGet返回的是窗口的渲染区域的左上角的坐标。
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返回的是窗口的渲染区域的左上角的坐标。
相关文章推荐
- 微信红包随机算法 OC
- Android逆向分析(一) - 反编译看看手Q口令红包的实现原理
- 常用meta整理
- 安装rx10udp1
- NoSQL之Redis(三) --- Redis在项目中的运用
- 使用Bundle在Activity之间交换数据及数据返回
- ubuntu下修改读写权限
- 每天读点“重口味”心理学
- hdu2112 HDU Today (floyd+stl-map)
- POJ 1698 Alice's Chance
- Android开发学习之路-抢红包助手开发全攻略
- 支付宝与微信新年红包战之我见
- NYOJ14会场安排问题
- Docker常用命令
- Dockerfile初探
- 【第二章】 IoC 之 2.1 IoC基础 ——跟我学Spring3
- JS产生随机数的几个用法!
- 数据结构: 栈
- scala入门教程:scala中的面向对象定义类,构造函数,继承
- 多线程 (五)NSOperation