OpenGL多视口分割

        OpenGL实现的多视口分割程序,同时也是OpenGL的一个简单的框架,可以根据自己的需要进行修改。

        VS2008的环境配置,代码如下:

// main.cpp

#include <GL/glut.h>
#include "glFrame.h"

int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize (800, 600); 
	glutInitWindowPosition (100, 100);

	glutCreateWindow ("Multiple Viewports");

	glutReshapeFunc(resizeGL);
	glutDisplayFunc(paintGL);
	glutMouseFunc(mousePress);
	glutMotionFunc(mouseMove);
	initGL();

	glutMainLoop();
	return 0;
}
// glFrame.h
void initGL();
void paintGL();
void resizeGL(int width, int height);
void mousePress(int button, int state, int width, int height);
void mouseMove(int x, int y);


#include <GL/glut.h>
#include "glFrame.h"

int width = 0;
int height = 0;
bool isVisible = true;	// whether the border is visible or not, by left button

void paintCenter();
void paintViewportA();
void paintViewportB();
void paintViewportC();

void initGL()
{
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glEnable(GL_POINT_SMOOTH);
	glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
	glEnable(GL_LINE_SMOOTH);
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

	//...
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

void paintGL()
{
	glClear(GL_COLOR_BUFFER_BIT);

	paintCenter();
	paintViewportA();
	paintViewportB();
	paintViewportC();

	glFlush();
}

void resizeGL(int w, int h)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, 1, 0, 1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	width = w;
	height = h;

	glViewport(0, 0, width, height);
}

void mousePress( int button, int state, int x, int y )
{
	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) 
	{
		isVisible = !isVisible;
	}

	glutPostRedisplay();
}

void mouseMove( int x, int y )
{
	glutPostRedisplay();
}

//
void paintCenter()
{
	// center viewport, cube 1
	glColor3f(1,0,0);
	glLoadIdentity();
	gluLookAt(0,0,4,0,0,0,0,1,0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(50, (GLfloat)width/(GLfloat)height,1.5,20.0);
	glMatrixMode(GL_MODELVIEW);

	int _width = width / 4;
	int _height = height / 3;
	glViewport(0, 0, 3*_width, 3*_height);
	glTranslatef(-0.3, 0.3 ,2);
	glRotatef(30,0,0,1);
	glRotatef(30,0,1,0);
	glutWireCube(0.5);

	// center viewport, cube 2
	glColor3f(0,1,0);
	glLoadIdentity();
	gluLookAt(0,0,4,0,0,0,0,1,0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(50.0,(GLfloat)width/(GLfloat)height,1.5,20.0);
	glMatrixMode(GL_MODELVIEW);
	glTranslatef(-0.5,-0.5,2);
	glRotatef(30,0,0,1);
	glRotatef(30,0,1,0);
	glutWireCube(0.1);

	// center viewport, cube 3
	glColor3f(0,0,1);
	glLoadIdentity();
	gluLookAt(0,0,4,0,0,0,0,1,0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(50.0,(GLfloat)width/(GLfloat)height,1.5,20.0);
	glMatrixMode(GL_MODELVIEW);
	glTranslatef(0.2,-0.5,2);
	glRotatef(30,0,0,1);
	glRotatef(30,0,1,0);
	glutWireCube(0.2);
}

void paintViewportA()
{
	// viewport 1, show cube 1
	glLoadIdentity();
	gluLookAt(0,0,4,0,0,0,0,1,0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0,(GLfloat)width/(GLfloat)height,1.5,20.0);
	glMatrixMode(GL_MODELVIEW);

	int _width = width / 4;
	int _height = height / 3;
	glViewport(3*_width, 2*_height, _width, _height);
	glColor3f(1,0,0);
	glTranslatef(0,0,2);
	glRotatef(30,0,0,1);
	glRotatef(30,0,1,0);
	glutWireCube(0.5);

	if (isVisible)
	{
		glLoadIdentity();
		gluLookAt(0,0,4,0,0,0,0,1,0);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60.0, 1.0,1.5,20.0);
		glMatrixMode(GL_MODELVIEW);

		glViewport(3*_width, 2*_height, _width, _height);
		glColor3f(1,1,1);
		glBegin(GL_LINES);
		glVertex3f(-2.15f, -2.3f,0.0f);
		glVertex3f(2.3f, -2.3f,0.0f); 
		glVertex3f(-2.15f, -2.5f,0.0f);
		glVertex3f(-2.15f, 2.5f,0.0f); 
		glEnd();
	}
}

void paintViewportB()
{
	// viewport 2, show cube 2
	glColor3f(0,1,0);
	glLoadIdentity();
	gluLookAt(0,0,4,0,0,0,0,1,0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(30.0,(GLfloat)width/(GLfloat)height,1.5,20.0);
	glMatrixMode(GL_MODELVIEW);

	int _width = width / 4;
	int _height = height / 3;
	glViewport(3*_width, _height, _width, _height);
	glColor3f(0,1,0);
	glTranslatef(0,0,2.15);
	glRotatef(30,0,0,1);
	glRotatef(30,0,1,0);
	glutWireCube(0.5);

	if (isVisible)
	{
		glLoadIdentity();
		gluLookAt(0,0,4,0,0,0,0,1,0);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(30.0,1.0,1.5,20.0);
		glMatrixMode(GL_MODELVIEW);

		glViewport(3*_width, _height, _width, _height);
		glColor3f(1,1,1);
		glBegin(GL_LINES);
		glVertex3f(-1.0f, -2.0f,0.0f);
		glVertex3f(-1.0f, 2.0f,0.0f); 
		glEnd();
	}
}

void paintViewportC()
{
	// viewport 3, show cube 3
	glColor3f(0,0,1);
	glLoadIdentity();
	gluLookAt(0,0,4,0,0,0,0,1,0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0,(GLfloat)width/(GLfloat)height,1.5,20.0);
	glMatrixMode(GL_MODELVIEW);

	int _width = width / 4;
	int _height = height / 3;
	glViewport(3*_width, 0, _width, _height);
	glColor3f(0,0,1);
	glTranslatef(0,0,2);
	glRotatef(30,0,0,1);
	glRotatef(30,0,1,0);
	glutWireCube(0.5);

	if (isVisible)
	{
		glLoadIdentity();
		gluLookAt(0,0,4,0,0,0,0,1,0);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluPerspective(60.0,1.0,1.5,20.0);
		glMatrixMode(GL_MODELVIEW);

		glViewport(3*_width, 0, _width, _height);
		glColor3f(1,1,1);
		glBegin(GL_LINES);
		glVertex3f(-2.15f, 2.3f,0.0f);
		glVertex3f(2.3f, 2.3f,0.0f); 
		glVertex3f(-2.15f, -2.5f,0.0f);
		glVertex3f(-2.15f, 2.5f,0.0f); 
		glEnd();
	}
}

        效果图如下:
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值