Dmitry Kurtaev/ C++: Multiply GLUT windows

Problem: use separate GLUT windows with own callbacks and drawing functions.
Example: written on C++, freeglut.
Languages: EN, RU

Table of contents

Single classic window
Two classic windows
Single window as class object
Abstract views manager
GLUT subwindows
Useful links

Single classic window

GLUT is a library for OpenGL. It provides windows manipulations: create, reshape, receive user's callbacks and display OpenGL's drawings stored in buffers.
Minimal usage includes GLUT and window initialization, settings references to callback functions by specific events.

Created window inherits OS's windows style. At pictures it is running on Ubuntu 16.04LTS.
Single view
Addition: GLUT is a powerful but simple windows manager. We can not make some OS-like operations like hiding window's header or keep window always on top of other windows.

Two classic windows

We initializing two windows sequently. For each write own display function.

Two views

Single window as class object

Generally, problem is solved. But in future we may want to have more than two windows and with more harder callbacks logic. Let's try encapsulate window managment inside class for more scalability.
Make step backward to single window problem and implement class with same usage.

Header of our class has all that we need. User choose window title only. Now main function use single line for create similar window. In sight of class user it is easy to use without care about internal implementation.

In sight of class we just encpsulated existing usage inside corresponding methods. Note that functions is static. It is required for registering callbacks.

Abstract views manager

We need to provide few windows management. Due to display function is static, it is same for different class objects. But we need to separate displaying.
Let's make display as abstract function that will be implemented inside an inherited classes. As callback we calling each display of created window.

We will see that display_all function is registered as callback for each window i.e. in case of single display request we redisplay every other window.

Before we had different functions for different windows. Now we have problem with window identification. If unroll a loop inside display_all method, only last iteration is actual because it has own glClear(..) and glutSwapBuffers(..) calls that overwrites previous changes.
For solving problem we using glutSetWindow(..). It sets current window for which applying future commands.

For new window we implements class with own display.

GLUT subwindows

In addition let's try merge two windows inside single window. But each subwindow has own callbacks.
GLUT has method glutCreateSubWindow(..) which creates new window in space of existing parent window.

Subwindow

Addition: subwindow has constant shape although parent size changes. If needed, we can make own child reshape using relative coordinates and parent's reshape callback.

Useful links

OpenGL 2.1 Reference Pages
freeglut API