OpenGL多视口分割

来源:互联网 发布:淘宝红色外套 编辑:程序博客网 时间:2024/05/14 16:05

        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.hvoid 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 buttonvoid 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 1glColor3f(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 2glColor3f(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 3glColor3f(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 1glLoadIdentity();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 2glColor3f(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 3glColor3f(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();}}

        效果图如下:
 

 

原创粉丝点击