代码如下:
#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;
}
运行效果如下: