OpenGL: 多视口

代码如下:

#include <iostream>

#include <gl/glut.h>
#pragma comment(lib, "glut32.lib")

int window_width = 1024;
int window_height = 800;
float rtri = 0.0;
float rquad = 0.0;

void init()
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
}

void display()
{  
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	for (int loop = 0; loop < 4; ++loop)
	{
		if (0 == loop)
		{
			glViewport(0, 0, window_width / 2, window_height / 2);
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			gluOrtho2D(-20, 20, -20, 20);
		}
		if (1 == loop)
		{
			glViewport(window_width / 2, 0, window_width, window_height / 2);
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			gluOrtho2D(-20, 20, -20, 20);
		}
		if (2 == loop)
		{
			glViewport(0, window_height / 2, window_width / 2, window_height);
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			gluOrtho2D(-20, 20, -20, 20);
		}
		if (3 == loop)
		{
			glViewport(window_width / 2, window_height / 2, window_width, window_height) ;
			glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			gluOrtho2D(-20, 20, -20, 20);
		}
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glClear(GL_DEPTH_BUFFER_BIT);
		if (0 == loop)
		{
			glBegin(GL_QUADS);
			{
				glVertex2f(-10, -10);
				glVertex2f(10, -10);
				glVertex2f(10, 10);
				glVertex2f(-10, 10);
			}
			glEnd();
		}
		if (1 == loop)
		{
			glBegin(GL_QUADS);
			{
				glVertex2f(-10, -10);
				glVertex2f(10, -10);
				glVertex2f(10, 10);
				glVertex2f(-10, 10);
			}
			glEnd();
		}
		if (2 == loop)
		{
			glBegin(GL_QUADS);
			{
				glVertex2f(-10, -10);
				glVertex2f(10, -10);
				glVertex2f(10, 10);
				glVertex2f(-10, 10);
			}
			glEnd();
		}
		if (3 == loop)
		{
			glBegin(GL_QUADS);
			{
				glVertex2f(-10, -10);
				glVertex2f(10, -10);
				glVertex2f(10, 10);
				glVertex2f(-10, 10);
			}
			glEnd();
		}
	}
	glutSwapBuffers();
}

void resize(int w, int h)
{
	window_height = h; 
	window_width = w;
}
int main(int argc, char ** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
	glutInitWindowSize(1024, 800);
	glutCreateWindow("OpenGL");

	init();
	glutDisplayFunc(display);
	glutReshapeFunc(resize);
	glutMainLoop();
	return EXIT_SUCCESS;
}

运行效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值