Wednesday, May 9, 2012

Creating multiple windows with OpenGL and GLUT

We may need the multiple OpenGL windows for our program for example we can show orthographic view of object (top view, side view,front view)  in separate  windows.
Steps for Creating Multiple Windows:-
1.Initialize drawing context and frame buffer using glutInit(), glutInitDisplayMode(), and glutInitWindowSize().
2.Create first window
3.Register callbacks for first window
4.Create second window
5.Position the second window
6.Register callbacks for second window
7.Register (shared) idle callback
8.Enter the main loop
Note: Callbacks can be shared between windows, if desired





Creating a Window:-
int glutCreateWindow(char *name);
Creates a window data structure and returns a small integer identifier.
Window is not displayed until the main loop is entered
Window becomes the current window. The current window
Can be set or obtained using glutSetWindow() and glutGetWindow().
MultipleWindow
Here is the example with complete source code. This code is compiled in codeblocks IDE and gcc compiler and also don’t forget to setup OpenGL for codeblocks to run this code.
//www.codeincodeblock.blogspot.com

#include<windows.h>
#include <GL/glut.h>
#include <stdlib.h>
using namespace std;
void handleKeypress(unsigned char key,int x,int y)
{
    switch(key)
    {
        case 27:
        exit(0);
    }
}
void initRendering()
{
    glEnable(GL_DEPTH_TEST);
}
void handleResize(int w,int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(45.0,(double)w/(double)h,1.0,200);
}
void drawScene1()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glBegin(GL_QUADS);


    glVertex3f(0.0f,0.0f,-5.0f);
    glVertex3f(0.0f,1.0f,-5.0f);
    glVertex3f(1.0f,1.0f,-5.0f);
    glVertex3f(1.0f,0.0f,-5.0f);

    glEnd();

    glutSwapBuffers();

}
void drawScene2()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glBegin(GL_QUADS);

    //rectangle

    glVertex3f(0.0f,0.0f,-5.0f);
    glVertex3f(0.0f,1.0f,-5.0f);
    glVertex3f(1.0f,1.0f,-5.0f);
    glVertex3f(1.0f,0.0f,-5.0f);

    glEnd();

    glutSwapBuffers();

}
int main(int argc,char** argv)
{
    glutInit(&argc,argv);
    initRendering();
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
    glutInitWindowSize(500,500);

    //window1=
     glutCreateWindow("First window :-www.codeincodeblock.blogspot.com");
    glutDisplayFunc(drawScene1);
    glutKeyboardFunc(handleKeypress);
    glutReshapeFunc(handleResize);

    //create the second  window
    //window2 =
    glutCreateWindow("Second Window :www.codeincodeblock.blogspot.com");
    //define a window position for second window
    glutPositionWindow(540,40);
    // register callbacks for second window, which is now current
    glutReshapeFunc(handleResize);
    glutDisplayFunc(drawScene2);
    glutKeyboardFunc(handleKeypress);

    glutMainLoop();




    return 0;

}


Resulting Display from Two Windows Code:

Result

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hello! Interesting post, however I have failed in using the keyHandle functions, I wanted the object to move in both windows at the same time, but it'll only move in the current window (the one that is selected), do you know how to fix this?

    ReplyDelete
    Replies
    1. I managed to update more than one window at same time by calling at end of rendering method glutSetWindow(int currentWindow).

      I did something like this in case of 2 windows:

      currentWindow = (currentWindow+1)%2;
      glutSetWindow(windows[currentWindow]);

      where windows[index] = glutCreateWindow("title");

      Delete