/************************************************************ * File: subwindow.cc * Author: C.Yap * Date: Sep 23, 2001. * Synopsis: * OpenGL program for lecture 2 * Illustrates the use of subwindows * ************************************************************/ #include // needed for use of cout #include // needed for math functions exp(x), etc #include #include ///////////////////////////////////////////////////////////// // constants ///////////////////////////////////////////////////////////// int screenWidth = 640; int screenHeight = 640; ///////////////////////////////////////////////////////////// // callback functions ///////////////////////////////////////////////////////////// void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT); // clear screen // Exercise: what would happen if you do not clear screen? glColor3f(0.0f, 0.0f, 1.0f); // pen is blue glBegin(GL_LINES); glVertex2d(10, 10) ; glVertex2d(900, 900) ; glEnd(); glFlush(); }//myDisplay() void displayButton(char* label, int state) { int len, i; glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glColor3f(1.0, 1.0, 0.0); glPushMatrix(); glTranslatef(-40, 0, 0); glScalef(8.0, 8.0, 1.0); if (state) drawButton(); else drawPressedButton(); glScalef(.125, .125, 1.0); glTranslatef(20, -5.0, 0.0); glScalef(0.1, 0.1, 0.1); len = (int) strlen(label); for (i = 0; i < len; i++) { glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, label[i]); } glPopMatrix(); glFlush(); glutSwapBuffers(); } void buttonDisplay(void) { displayButton(buttonLabel, OnOffFlag); } ///////////////////////////////////////////////////////////// // MAIN PROGRAM ///////////////////////////////////////////////////////////// void main(int argc, char** argv) { ///////////////////////////////////////////////////////////// // initialize main window ///////////////////////////////////////////////////////////// glutInit(&argc, argv); // initialize toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // display mode (single buffer) glutInitWindowSize(screenWidth, screenHeight); // window size glutInitWindowPosition(100, 150); // position of top right corner int mainWin; mainWin = glutCreateWindow("My First Window"); // open it ///////////////////////////////////////////////////////////// // initialize subwindows ///////////////////////////////////////////////////////////// glClearColor(1.0, 1.0, 1.0, 0.0); // background is white (default black) glColor3f(0.0f, 0.0f, 1.0f); // pen is blue (default black) glPointSize(2.0); // point is 2 pixels wide (default 1) glLineWidth(2.0); // line width is 2pixels wide glMatrixMode(GL_PROJECTION); // set camera shape glLoadIdentity(); // initialize projection matrix gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight); // set matrix for 2D graphics ///////////////////////////////////////////////////////////// // initialize subwindows ///////////////////////////////////////////////////////////// int button; int OnOffFlag = 0; // 0=off, 1=on button = glutCreateSubWindow(mainWin, 10, 10, 40, 40); // button size 40x40 glClearColor(0.0, 0.0, 0.5, 0.0); glutDisplayFunc( displayButton ); glutReshapeFunc( reshapeButton ); glutMouseFunc( mouseButton ); // end of subwins definitions // set the current window back to the top level one glutSetWindow(mainWin); ///////////////////////////////////////////////////////////// // register callback functions ///////////////////////////////////////////////////////////// glutDisplayFunc(myDisplay); // redraw window event ///////////////////////////////////////////////////////////// glutMainLoop(); // mandatory final call to glut ///////////////////////////////////////////////////////////// }//main ///////////////////////////////////////////////////////////// // The end /////////////////////////////////////////////////////////////