Multiple Windows

So far, we have been working with OpenGL programs that use only a single window. To support multiple windows, OpenGL must know to which window a particular graphics command should be directed. The GLUT library maintains an internal pointer to the current window.

The glutCreateWindow function returns a unique integer identifier for each window that is created, which is automatically set as the current window.

  int  glutCreateWindow(char *title);   /* create new window */

After the current window has be set, all subsequent OpenGL and GLUT commands are directed to that window. The current window can be set and its identifier retrieved by the following functions:

  void glutSetWindow(int win);   /* set the current window */
  int  glutGetWindow(void);      /* return the current window identifier */

Each window can have its own callback functions and popup menus.

static int mainWindow,               /* main window identifier */
           secondWindow,             /* second window identifier */
           thirdWindow;              /* third window identifier */
  ...

  /* create main window */

  mainWindow = glutCreateWindow("OpenGL Example 11 - Multiple Windows");

  glutReshapeFunc(reshapeCB);        /* Register reshape callback for window */
  glutDisplayFunc(displayCB);        /* Register display callback for window */

  ... /* define main window menu */ ...

  /* create second window */

  secondWindow = glutCreateWindow("OpenGL Example 11 - Second Window");

  glutReshapeFunc(reshapeSecondCB);  /* Register reshape callback for window */
  glutDisplayFunc(displaySecondCB);  /* Register display callback for window */

  ... /* define second window menu */ ...

  /* create third window */

  thirdWindow = glutCreateWindow("OpenGL Example 11 - Third Window");

  glutReshapeFunc(reshapeThirdCB);   /* Register reshape callback for window */
  glutDisplayFunc(displayThirdCB);   /* Register display callback for window */

  ... /* define third window menu */ ...

  glutMainLoop();                    /* Enter the event loop */

  return 0;
}

void reshapeCB(int width, int height)
{
  glutSetWindow(mainWindow);          /* make main window current */
  glViewport(0, 0, width, height);    /* set the viewport to fill window */
}
void reshapeSecondCB(int width, int height)
{
  glutSetWindow(secondWindow);        /* make second window current */
  glViewport(0, 0, width, height);    /* set the viewport to fill window */
}
void reshapeThirdCB(int width, int height)
{
  glutSetWindow(thirdWindow);         /* make third window current */
  glViewport(0, 0, width, height);    /* set the viewport to fill window */
}

void displayCB(void)
{
  glutSetWindow(mainWindow);          /* make main window current */

  ... /* draw the main window */ ...

  glFlush();                          /* actually draw on screen */
}
void displaySecondCB(void)
{
  glutSetWindow(secondWindow);        /* make second window current */

  ... /* draw the second window */ ...

  glFlush();                          /* actually draw on screen */
}
void displayThirdCB(void)
{
  glutSetWindow(thirdWindow);         /* make third window current */

  ... /* draw the third window */ ...

  glFlush();                          /* actually draw on screen */
}

A complete working example is available as example11 in the Athena course locker, /mit/13.016/graphics/example11-multi.c.


Back to menus...
Back to the 13.016 overview...