//*************************************************************************
//
// File Name: SubWindow Template
// Author : Ali Baderedine
//
// Description: SubWindow Template
//
//*************************************************************************
#include <gl/glut.h>
// Link the lib files to the program. This is not necessary
// if you have added the lib names using Project/Settings/Link
#pragma comment (lib, "opengl32.lib")
#pragma comment (lib, "glut32.lib")
#pragma comment (lib, "glu32.lib")
// ***** Main Window Variables *****//
#define GAP 25 /* gap between subwindows */
// define the window position on screen
float main_window_x;
float main_window_y;
// variables representing the window size
float main_window_w = 256 + GAP * 2;
float main_window_h = 256 + 64 + GAP * 3;
// variable representing the window title
char *window_title = "SubWindow Template";
// ***** Sub Window 1 Variables *****//
// define the window position on screen
float subwindow1_x;
float subwindow1_y;
// variables representing the window size
float subwindow1_w = 256;
float subwindow1_h = 256;
// ***** Sub Window 2 Variables *****//
// define the window position on screen
float subwindow2_x;
float subwindow2_y;
// variables representing the window size
float subwindow2_w = 256;
float subwindow2_h = 64;
// ***** Main Window callback functions *****//
void main_display (void);
void main_reshape (int w, int h);
// ***** SubWindow 1 callback functions *****//
void subwindow1_display (void);
// ***** SubWindow 2 callback functions *****//
void subwindow2_display (void);
// Redisplay all
void redisplay_all (void);
// A program with two subwindows
int main_window, subwindow_1, subwindow_2;
// Tells whether to display the window full screen or not
int fullScreen = 0;
// This function centers your window on the screen
void centerOnScreen (void);
// ***** Main Window callback functions *****//
void main_reshape (int width, int height)
{
// Just take the case when the user tries
// to make the size of the window very small...
if (width < GAP * 4 || height < GAP * 6)
{
glutSetWindow (main_window);
glutReshapeWindow (main_window_w, main_window_h);
return;
}
glViewport(0, 0, width, height);
// Change the subwindow dimensions as window dimensions change
// Note (Keep up the ratio!)
// main_window_w ---> subwindow1_w
// main_window_w' (width) ---> ??
// ==>
subwindow1_w = (subwindow1_w * (width-GAP*2.0))/(main_window_w-GAP*2.0);
subwindow1_h = (subwindow1_h * (height-GAP*3.0))/(main_window_h-GAP*3.0);
glutSetWindow (subwindow_1);
glutPositionWindow (GAP, GAP);
glutReshapeWindow (subwindow1_w, subwindow1_h);
subwindow2_w = (subwindow2_w * (width-GAP*2.0))/(main_window_w-GAP*2.0);
subwindow2_h = (subwindow2_h * (height-GAP*3.0))/(main_window_h-GAP*3.0);
glutSetWindow (subwindow_2);
glutPositionWindow (GAP, GAP+subwindow1_h+GAP);
glutReshapeWindow (subwindow2_w, subwindow2_h);
main_window_w = width;
main_window_h = height;
}
void main_display (void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}
// ***** SubWindow 1 callback functions *****//
void subwindow1_display (void)
{
glClearColor(0.0, 0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (1, 1, 0);
glutWireTeapot (0.5);
glutSwapBuffers ();
}
// ***** SubWindow 2 callback functions *****//
void subwindow2_display (void)
{
glClearColor(1.0, 1.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (0, 0, 1);
glutWireTeapot (0.5);
glutSwapBuffers ();
}
// Redisplay All
void redisplay_all (void)
{
glutSetWindow(subwindow_1);
glutPostRedisplay();
glutSetWindow(subwindow_2);
glutPostRedisplay();
}
void centerOnScreen (void)
{
// Set the window position such that the window becomes centered
main_window_x = (glutGet (GLUT_SCREEN_WIDTH) - main_window_w)/2;
main_window_y = (glutGet (GLUT_SCREEN_HEIGHT) - main_window_h)/2;
}
void main (int argc, char **argv)
{
centerOnScreen ();
/**** Main Window *****/
glutInit(&argc, argv);
glutInitWindowSize (main_window_w, main_window_h);
glutInitWindowPosition (main_window_x, main_window_y);
glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE);
main_window = glutCreateWindow (window_title);
glutReshapeFunc (main_reshape);
glutDisplayFunc (main_display);
// Subwindow 1 (Notice that x, y, w, h are just dummy values here
// The reason is that they're gonna be reset in main_reshape
// since the reshape function is called as the program starts.
subwindow_1 = glutCreateSubWindow (main_window, 0, 0, 0, 0);
glutDisplayFunc (subwindow1_display);
/**** Subwindow 2 *****/
subwindow_2 = glutCreateSubWindow (main_window, 0,0 , 0, 0);
glutDisplayFunc (subwindow2_display);
if (fullScreen)
glutFullScreen ();
glutMainLoop();
}