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

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返回的是窗口的渲染区域的左上角的坐标。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值