4 * Hey! This was the original file where freeglut development started. Just
5 * note what I have written here at the time. And see the creation date :)
7 * : This is a wrapper. I still have to figure out
8 * : how to build shared libraries under *nix :)
10 * Copyright (c) 1999 by Pawel W. Olszta
11 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
12 * Creation date: czw gru 2 11:58:41 CET 1999
18 #include <GL/freeglut.h>
20 int g_LeaveGameMode = 0;
22 int g_mainwin1, g_mainwin2, g_sw1, g_sw2, g_gamemodewin;
25 * Call this function to have some text drawn at given coordinates
27 void PrintText( int nX, int nY, char* pszText )
33 * Prepare the OpenGL state
35 glDisable( GL_LIGHTING );
36 glDisable( GL_DEPTH_TEST );
37 glMatrixMode( GL_PROJECTION );
42 * Have an orthogonal projection matrix set
44 glOrtho( 0, glutGet( GLUT_WINDOW_WIDTH ),
45 0, glutGet( GLUT_WINDOW_HEIGHT ),
52 glMatrixMode( GL_MODELVIEW );
59 glColor3ub( 0, 0, 0 );
60 glRasterPos2i( nX, nY );
62 for( p=pszText, lines=0; *p; p++ )
67 glRasterPos2i( nX, nY-(lines*18) );
70 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, *p );
74 * Revert to the old matrix modes
76 glMatrixMode( GL_PROJECTION );
79 glMatrixMode( GL_MODELVIEW );
83 * Restore the old OpenGL states
85 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
86 glEnable( GL_DEPTH_TEST );
87 glEnable( GL_LIGHTING );
91 * This is the display routine for our sample FreeGLUT windows
93 void SampleDisplay( void )
95 int win = glutGetWindow();
97 if (g_InGameMode && win!=g_gamemodewin)
98 /* Don't draw other windows when in gamemode, those aren't visible
99 * anyway. Drawing them continuously nonetheless can cause flicker trouble
100 * on my machine. This only seems to occur only when there are child windows
101 * among the non-visible windows
110 glClearColor(0.7f,0.7f,0.7f,1);
111 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
112 glutPostWindowRedisplay(g_mainwin2);
119 glClearColor(0.3f,0.3f,0.3f,1);
120 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
121 glutPostWindowRedisplay(g_mainwin2);
125 const GLfloat time = glutGet(GLUT_ELAPSED_TIME) / 1000.f * 40;
130 glClearColor( 0, 0.5, 1, 1 );
131 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
134 * Have the cube rotated
136 glMatrixMode( GL_MODELVIEW );
139 glRotatef( time, 0, 0, 1 );
140 glRotatef( time, 0, 1, 0 );
141 glRotatef( time, 1, 0, 0 );
146 glColor3f( 1, 1, 0 );
147 /* glutWireCube( 20.0 ); */
148 glutWireTeapot( 20.0 );
149 /* glutWireSphere( 15.0, 15, 15 ); */
150 /* glColor3f( 0, 1, 0 ); */
151 /* glutWireCube( 30.0 ); */
152 /* glutSolidCone( 10, 20, 10, 2 ); */
155 * Don't forget about the model-view matrix
162 if( g_InGameMode == 0 )
163 PrintText( 20, 20, "Hello there cruel world!" );
165 PrintText( 20, 20, "Press ESC to leave the game mode!" );
169 * And swap this context's buffers
172 glutPostWindowRedisplay(win);
176 * This is a sample idle function
178 void SampleIdle( void )
180 if( g_LeaveGameMode == 1 )
182 /* One could do all this just as well in SampleGameModeKeyboard... */
183 printf("leaving gamemode...\n");
184 glutLeaveGameMode( );
187 glutPostWindowRedisplay(g_mainwin1);
188 glutPostWindowRedisplay(g_mainwin2);
189 glutPostWindowRedisplay(g_sw1);
190 glutPostWindowRedisplay(g_sw2);
194 void SampleEntry(int state)
196 int window = glutGetWindow () ;
197 printf ( "Window %d Entry Callback: %d\n", window, state ) ;
201 * The reshape function
203 void SampleReshape( int nWidth, int nHeight )
205 GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
206 GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
207 GLfloat fCol[ 4 ] = { 0.5f, 1.0f, 0.0f, 1.0f };
210 * Update the viewport first
212 glViewport( 0, 0, nWidth, nHeight );
215 * Then the projection matrix
217 glMatrixMode( GL_PROJECTION );
219 glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
222 * Move back the camera a bit
224 glMatrixMode( GL_MODELVIEW );
226 glTranslatef( 0.0, 0.0, -40.0f );
229 * Enable some features...
231 glEnable( GL_CULL_FACE );
232 glEnable( GL_DEPTH_TEST );
233 glEnable( GL_NORMALIZE );
236 * Set up some lighting
238 glLightfv( GL_LIGHT0, GL_POSITION, fPos );
239 glEnable( GL_LIGHTING );
240 glEnable( GL_LIGHT0 );
243 * Set up a sample material
245 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
249 * A sample keyboard callback
251 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
253 printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
254 cChar, nMouseX, nMouseY );
258 * A sample keyboard callback (for game mode window)
260 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
268 * A sample special callback
270 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
272 printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
273 nSpecial, nMouseX, nMouseY );
277 * A sample menu callback
279 void SampleMenu( int menuID )
281 printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
285 * A sample menu status callback
287 void SampleMenuStatus( int status, int x, int y )
289 printf ( "SampleMenu() callback executed, MenuStatus is %i at (%i,%i)\n", status, x, y );
293 * The sample's entry point
295 int main( int argc, char** argv )
297 int menuID, subMenuA, subMenuB;
299 glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
300 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
301 glutInitWindowPosition( 100, 100 );
303 glutInit( &argc, argv );
305 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
306 glutMenuStatusFunc( SampleMenuStatus );
307 glutIdleFunc( SampleIdle );
309 subMenuA = glutCreateMenu( SampleMenu );
310 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
311 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
312 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
314 subMenuB = glutCreateMenu( SampleMenu );
315 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
316 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
317 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
318 glutAddSubMenu( "Going to sub menu A", subMenuA );
320 menuID = glutCreateMenu( SampleMenu );
321 glutAddMenuEntry( "Entry one", 1 );
322 glutAddMenuEntry( "Entry two", 2 );
323 glutAddMenuEntry( "Entry three", 3 );
324 glutAddMenuEntry( "Entry four", 4 );
325 glutAddMenuEntry( "Entry five", 5 );
326 glutAddSubMenu( "Enter sub menu A", subMenuA );
327 glutAddSubMenu( "Enter sub menu B", subMenuB );
329 g_mainwin1 = glutCreateWindow( "Hello world!" );
330 glutDisplayFunc( SampleDisplay );
331 glutReshapeFunc( SampleReshape );
332 glutKeyboardFunc( SampleKeyboard );
333 glutSpecialFunc( SampleSpecial );
334 glutEntryFunc( SampleEntry );
335 glutAttachMenu( GLUT_LEFT_BUTTON );
337 glutInitWindowPosition( 200, 200 );
338 g_mainwin2 = glutCreateWindow( "I am not Jan B." );
339 glutDisplayFunc( SampleDisplay );
340 glutReshapeFunc( SampleReshape );
341 glutKeyboardFunc( SampleKeyboard );
342 glutSpecialFunc( SampleSpecial );
343 glutEntryFunc( SampleEntry );
344 glutAttachMenu( GLUT_LEFT_BUTTON );
345 glutSetMenu(subMenuA);
346 glutAttachMenu( GLUT_RIGHT_BUTTON );
348 g_sw1=glutCreateSubWindow(g_mainwin2,200,0,100,100);
349 glutDisplayFunc( SampleDisplay );
350 glutSetMenu(subMenuB);
351 glutAttachMenu( GLUT_LEFT_BUTTON );
353 g_sw2=glutCreateSubWindow(g_sw1,50,0,50,50);
354 glutDisplayFunc( SampleDisplay );
356 glutAttachMenu( GLUT_RIGHT_BUTTON );
358 printf( "Testing game mode string parsing, don't panic!\n" );
359 glutGameModeString( "320x240:32@100" );
360 glutGameModeString( "640x480:16@72" );
361 glutGameModeString( "1024x768" );
362 glutGameModeString( ":32@120" );
363 glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
365 glutGameModeString( "640x480:37@300" ); /* this one should fail */
368 glutGameModeString( "800x600" ); /* this one is likely to succeed */
369 g_gamemodewin = glutEnterGameMode();
371 if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
373 glutDisplayFunc( SampleDisplay );
374 glutReshapeFunc( SampleReshape );
375 glutKeyboardFunc( SampleGameModeKeyboard );
376 glutEntryFunc( SampleEntry );
378 glutAttachMenu( GLUT_LEFT_BUTTON );
380 printf( "current window is %ix%i at (%i,%i)\n",
381 glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
382 glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
386 * Describe pixel format
388 printf("The current window has %i red bits, %i green bits, %i blue bits and %i alpha bits for a total buffer size of %i bits\n",glutGet(GLUT_WINDOW_RED_SIZE),glutGet(GLUT_WINDOW_GREEN_SIZE),glutGet(GLUT_WINDOW_BLUE_SIZE),glutGet(GLUT_WINDOW_ALPHA_SIZE),glutGet(GLUT_WINDOW_BUFFER_SIZE));
389 printf("It furthermore has %i depth bits and %i stencil bits\n",glutGet(GLUT_WINDOW_DEPTH_SIZE),glutGet(GLUT_WINDOW_STENCIL_SIZE));
392 * Enter the main FreeGLUT processing loop
397 * returned from mainloop after window closed
398 * see glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS); above
400 printf( "glutMainLoop() termination works fine!\n" );
405 /*** END OF FILE ***/