Multiple Windows OpenGL/Glut

别等时光非礼了梦想. 提交于 2020-01-04 04:06:26

问题


I would like to know how to open multiple OpenGL/Glut windows. And I mean multiple windows at the same time not subwindows and not update the same window


回答1:


The same way as you would create one window, except you should do it multiple times :

#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
}

This example shows how to set the same callback to render in both windows. But you can use different functions for windows.




回答2:


While I believe the above answer is accurate, it is a little more complex then needed and it might be difficult when later having to deal with moving between the windows (say, for example, when drawing into them). This is what we've just done in class:

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

You will notice I'm using the same create window function but loading it into a GLint. That is because when we create a window this way, the function actually returns a unique GLint used by glut to identify windows.

We have to get and set windows to move between them and perform appropriate drawing functions. You can find the calls here.



来源:https://stackoverflow.com/questions/10465462/multiple-windows-opengl-glut

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!