注:源代码参照《computer graphics with opengl》英文版第四版272页。
1、关键是如何控制gluOrtho2D()的参数
gluOrtho2D(-size, size, -size, size);
2、改变参数
GLint flag = 0; //to control '+' or '-'
...
if (flag == 0)
size++;
else if (flag == 1)
size--;
if (size > 1000)
flag = 1;
if (size < 100)
flag = 0;
3、完整代码
#include "pch.h"
#include <GL/glut.h>
GLfloat size = 100.0;
GLsizei size1 = 100, size2 = 100;
GLint flag = 0; //to control '+' or '-'
class wcPt2D {
public:
GLfloat x, y;
};
void init(void) {
/* set color of display window to white */
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(-size, size, -size, size);
/*set parameters for world-cooridinate clipping window*/
glMatrixMode(GL_PROJECTION);
/*set mode for constructiong geometric transformation matrix*/
glMatrixMode(GL_MODELVIEW);
}
void triangle(wcPt2D *verts) {
GLint k;
glBegin(GL_TRIANGLES);
for (k = 0; k < 3; k++) {
glVertex2f(verts[k].x, verts[k].y);
}
glEnd();
}
void changeSize() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (flag == 0)
size++;
else if (flag == 1)
size--;
if (size > 1000)
flag = 1;
if (size < 100)
flag = 0;
gluOrtho2D(-size, size, -size, size);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay();
}
void displayFcn(void) {
/*define initial position for triangle*/
wcPt2D verts[3] = {
{-50.0,-25.0},
{50.0,-25.0},
{0.0,50.0}
};
/*clear diaplay window*/
glClear(GL_COLOR_BUFFER_BIT);
/*set fill color to blue*/
glColor3f(0.0, 0.0, 1.0);
/*set left viewport*/
glViewport(0, 0, 300, 300);
/*diplay triangle*/
triangle(verts);
/*rotate Triangle and display in right half of diaplay windows*/
/*set fill color to red*/
glColor3f(1.0, 0.0, 0.0);
/*set rught viewport*/
glViewport(300, 0, 300, 300);
/*rotate about a z axis*/
glRotatef(90.0, 0.0, 0.0, 1.0);
/*display red rotated triangle */
triangle(verts);
glFlush();
}
void main(int argc,char ** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 50);
glutInitWindowSize(600, 300);
glutCreateWindow("split-screen example");
init();
glutDisplayFunc(displayFcn);
glutIdleFunc(changeSize);
glutMainLoop();
}
4、实际效果
图一 图二
网友评论