我想知道如何打开多个OpenGL/Glut窗口。我指的是同时打开多个窗口,而不是子窗口,也不是更新同一个窗口。
虽然我认为上面的回答是正确的,但它比必要的更复杂,当以后需要在窗口之间移动时(例如在绘图过程中),可能会很困难。以下是我们在课堂上所做的:
GLint WindowID1, WindowID2; // window ID numbers
glutInitWindowSize(250.0, 250.0); // set a window size
glutInitWindowPosition(50,50); // set a window position
WindowID1 = glutCreateWindow("Window One"); // Create window 1
glutInitWindowSize(500.0, 250.0); // set a window size
glutInitWindowPosition(500,50); // set a window position
WindowID2 = glutCreateWindow("Window Two"); // Create window 2
与创建一个窗口的方式相同,只是您需要多次执行此操作:
#include <cstdlib>
#include <GL/glut.h>
// Display callback ------------------------------------------------------------
float clr = 0.2;
void display()
{
// clear the draw buffer .
glClear(GL_COLOR_BUFFER_BIT); // Erase everything
// set the color to use in draw
clr += 0.1;
if ( clr>1.0)
{
clr=0;
}
// create a polygon ( define the vertexs)
glBegin(GL_POLYGON); {
glColor3f(clr, clr, clr);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f( 0.5, 0.5);
glVertex2f( 0.5, -0.5);
} glEnd();
glFlush();
}
// Main execution function
int main(int argc, char *argv[])
{
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("win1"); // Create a window 1
glutDisplayFunc(display); // Register display callback
glutCreateWindow("win2"); // Create a window 2
glutDisplayFunc(display); // Register display callback
glutMainLoop(); // Enter main event loop
}
这个例子展示了如何设置相同的回调函数来渲染两个窗口,但是你可以为不同的窗口使用不同的函数。