OpenGL VRML W3D            
  Home Tutorials Books Applications Tools Docs Models Textures  

 

              Bugs

GLUT Tutorial   

  GLUT Tutorial

Index
Setup

Basics
Initialization
Resizing the Window
Animation

Input
Keyboard
Moving the Camera
Advanced Keyboard
Moving the Camera II
Mouse

Pop-up Menus
Basics
Sub Menus
Modifying a Menu
Swapping Menus

Fonts
Bitmap Fonts
Bitmaps and the Ortho View
Stroke Fonts

Extras
Frames per Second
GLUT Game Mode

Subwindows
Creating and Destroying Subwindows
Resizing SubWindows
Rendering to SubWindows


Google

OpenGLTutorials @ Lighthouse3d.com

Led Shader
View Frustum Culling
GLSL Tutorial
Maths Tutorial
Billboarding Tutorial
Picking Tutorial
Terrain Tutorial
Display Lists Tutorial
GLUT Tutorial

   
[Previous: Resizing SubWindows] [Next]

GLUT Tutorial


Rendering to Multiple Subwindows


Before we start lets recall our callback definitions:
  • idle function - renderSceneAll
  • display func for main window - renderScene
  • display func for subwindow 1 - renderScenesw1
  • display func for subwindow 2 - renderScenesw2
  • display func for subwindow 3 - renderScenesw3
  • We'll start by the display functions for each window. The main window is covered with subwindows so we only want to paint it black. Since we are working with multiple windows the first thing we must do is call glutSetWindow with the proper window id. Then we just clear the color buffer with the default color, black.


        
    void renderScene() {
    	glutSetWindow(mainWindow);
    	glClear(GL_COLOR_BUFFER_BIT);
    	glutSwapBuffers();
    }
    
    


    We must define the display function for each subwindow. In our example, the geometry is the same for all windows, the only thing that differs is the viewpoint, or the camera, if you prefer.

    The function where the geometry is rendered is called renderScene2. However before we call this function we must set the current window to be the respective subwindow, load the identity matrix to clean the MODELVIEW matrix, and set the camera with gluLookAt.

    Some variables used in the following code require some description:
  • x,y,z : our current position in the world
  • lx,ly,lz : a vector specifying the line of sight
  • deltaMove, deltaAngle, angle : define the camera movements.
  • As mentioned before in the initial section covering subwindows we have three subwindows with different perspectives of the same scene. The first subwindow displays the scene from the current point of view. The second displays the same scene from the top, i.e. as if the camera was above the current position looking downwards, and with the same orientation as the line of sigh. The third subwindow behaves like a camera to the right of the current position, and pointing at the current position.

    The following code defines the display functions for every window. This code is an extended version of the previous versions. If you require more detail just explore the previous sections, namely Moving the Camera II for the keyboard movement, Bitmaps and the Orthogonal View for text display, or Frames per Second for the respective computation.


        
    //main window
    void renderScene() {
    	glutSetWindow(mainWindow);
    	glClear(GL_COLOR_BUFFER_BIT);
    	glutSwapBuffers();
    }
    
    //subwindow 1 - camera = current position
    void renderScenesw1() {
    	glutSetWindow(subWindow1);
    	glLoadIdentity();
    	gluLookAt(x, y, z, 
    		  x + lx,y + ly,z + lz,
    		  0.0f,1.0f,0.0f);
    
    	renderScene2(subWindow1);
    }
    
    // subwindow 2 - top view
    void renderScenesw2() {
    	glutSetWindow(subWindow2);
    	glLoadIdentity();
    	gluLookAt(x, y+15, z, 
    		  x ,y - 1,z,
    		  lx,0,lz);
    	renderScene2(subWindow2);
    }
    
    // subwindow 3 - right view
    void renderScenesw3() {
    	glutSetWindow(subWindow3);
    	glLoadIdentity();
    	gluLookAt(x-lz*10 , y, z+lx*10, 
    		  x ,y ,z ,
    		  0.0f,1.0f,0.0f);
    	renderScene2(subWindow3);
    }
    
    


    Now all its left to do is to define the global idle function. In our example, this function is renderSceneAll. This function checks if the variables deltaMove or deltaAngle are not zero, and updates the values of the current position, and the line of sight vector.

    Afterwards we call the display functions for each of the subwindows. Note that we are not calling the display function for the main window because its contents never change.


        
    void renderSceneAll() {
    	if (deltaMove)
    		moveMeFlat(deltaMove);
    	if (deltaAngle) {
    		angle += deltaAngle;
    		orientMe(angle);
    	}
    	renderScenesw1();
    	renderScenesw2();
    	renderScenesw3();
    }
    
    


    Thats it. Suggestions, bugs, or requests for more info are welcome. My e-mail is at the bottom of the page. Meanwhile the the source code and a VC project is available to download.

    [Previous: Resizing SubWindows] [Next]

           


    Site designed and maintained by António Ramires Fernandes
    Your comments, suggestions and references to further material are welcome!

    Lighthouse 3D privacy statement