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
22 #include <GL/freeglut.h>
24 int g_LeaveGameMode = 0;
26 int g_mainwin1, g_mainwin2, g_sw1, g_sw2, g_gamemodewin;
29 * Call this function to have some text drawn at given coordinates
31 void PrintText( int nX, int nY, char* pszText )
37 * Prepare the OpenGL state
39 glDisable( GL_LIGHTING );
40 glDisable( GL_DEPTH_TEST );
41 glMatrixMode( GL_PROJECTION );
46 * Have an orthogonal projection matrix set
48 glOrtho( 0, glutGet( GLUT_WINDOW_WIDTH ),
49 0, glutGet( GLUT_WINDOW_HEIGHT ),
56 glMatrixMode( GL_MODELVIEW );
63 glColor3ub( 0, 0, 0 );
64 glRasterPos2i( nX, nY );
66 for( p=pszText, lines=0; *p; p++ )
71 glRasterPos2i( nX, nY-(lines*18) );
74 glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18, *p );
78 * Revert to the old matrix modes
80 glMatrixMode( GL_PROJECTION );
83 glMatrixMode( GL_MODELVIEW );
87 * Restore the old OpenGL states
89 glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
90 glEnable( GL_DEPTH_TEST );
91 glEnable( GL_LIGHTING );
95 * This is the display routine for our sample FreeGLUT windows
97 void SampleDisplay( void )
99 int win = glutGetWindow();
101 if (g_InGameMode && win!=g_gamemodewin)
102 /* Don't draw other windows when in gamemode, those aren't visible
103 * anyway. Drawing them continuously nonetheless can cause flicker trouble
104 * on my machine. This only seems to occur only when there are child windows
105 * among the non-visible windows
114 glClearColor(0.7f,0.7f,0.7f,1);
115 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
116 glutPostWindowRedisplay(g_mainwin2);
123 glClearColor(0.3f,0.3f,0.3f,1);
124 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
125 glutPostWindowRedisplay(g_mainwin2);
129 const GLfloat time = glutGet(GLUT_ELAPSED_TIME) / 1000.f * 40;
134 glClearColor( 0, 0.5, 1, 1 );
135 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
138 * Have the cube rotated
140 glMatrixMode( GL_MODELVIEW );
143 glRotatef( time, 0, 0, 1 );
144 glRotatef( time, 0, 1, 0 );
145 glRotatef( time, 1, 0, 0 );
150 glColor3f( 1, 1, 0 );
151 /* glutWireCube( 20.0 ); */
152 glutWireTeapot( 20.0 );
153 /* glutWireSphere( 15.0, 15, 15 ); */
154 /* glColor3f( 0, 1, 0 ); */
155 /* glutWireCube( 30.0 ); */
156 /* glutSolidCone( 10, 20, 10, 2 ); */
159 * Don't forget about the model-view matrix
166 if( g_InGameMode == 0 )
167 PrintText( 20, 20, "Hello there cruel world!" );
169 PrintText( 20, 20, "Press ESC to leave the game mode!" );
173 * And swap this context's buffers
176 glutPostWindowRedisplay(win);
180 * This is a sample idle function
182 void SampleIdle( void )
184 if( g_LeaveGameMode == 1 )
186 /* One could do all this just as well in SampleGameModeKeyboard... */
187 printf("leaving gamemode...\n");
188 glutLeaveGameMode( );
191 glutPostWindowRedisplay(g_mainwin1);
192 glutPostWindowRedisplay(g_mainwin2);
193 glutPostWindowRedisplay(g_sw1);
194 glutPostWindowRedisplay(g_sw2);
198 void SampleEntry(int state)
200 int window = glutGetWindow () ;
201 printf ( "Window %d Entry Callback: %d\n", window, state ) ;
205 * The reshape function
207 void SampleReshape( int nWidth, int nHeight )
209 GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
210 GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
211 GLfloat fCol[ 4 ] = { 0.5f, 1.0f, 0.0f, 1.0f };
214 * Update the viewport first
216 glViewport( 0, 0, nWidth, nHeight );
219 * Then the projection matrix
221 glMatrixMode( GL_PROJECTION );
223 glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
226 * Move back the camera a bit
228 glMatrixMode( GL_MODELVIEW );
230 glTranslatef( 0.0, 0.0, -40.0f );
233 * Enable some features...
235 glEnable( GL_CULL_FACE );
236 glEnable( GL_DEPTH_TEST );
237 glEnable( GL_NORMALIZE );
240 * Set up some lighting
242 glLightfv( GL_LIGHT0, GL_POSITION, fPos );
243 glEnable( GL_LIGHTING );
244 glEnable( GL_LIGHT0 );
247 * Set up a sample material
249 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
253 * A sample keyboard callback
255 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
257 printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
258 cChar, nMouseX, nMouseY );
262 * A sample keyboard callback (for game mode window)
264 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
272 * A sample special callback
274 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
276 printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
277 nSpecial, nMouseX, nMouseY );
281 * A sample menu callback
283 void SampleMenu( int menuID )
285 printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
289 * A sample menu status callback
291 void SampleMenuStatus( int status, int x, int y )
293 printf ( "SampleMenu() callback executed, MenuStatus is %i at (%i,%i)\n", status, x, y );
297 * The sample's entry point
299 int main( int argc, char** argv )
301 int menuID, subMenuA, subMenuB;
303 glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
304 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
305 glutInitWindowPosition( 100, 100 );
307 glutInit( &argc, argv );
309 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
311 subMenuA = glutCreateMenu( SampleMenu );
312 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
313 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
314 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
316 subMenuB = glutCreateMenu( SampleMenu );
317 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
318 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
319 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
320 glutAddSubMenu( "Going to sub menu A", subMenuA );
322 menuID = glutCreateMenu( SampleMenu );
323 glutAddMenuEntry( "Entry one", 1 );
324 glutAddMenuEntry( "Entry two", 2 );
325 glutAddMenuEntry( "Entry three", 3 );
326 glutAddMenuEntry( "Entry four", 4 );
327 glutAddMenuEntry( "Entry five", 5 );
328 glutAddSubMenu( "Enter sub menu A", subMenuA );
329 glutAddSubMenu( "Enter sub menu B", subMenuB );
331 g_mainwin1 = glutCreateWindow( "Hello world!" );
332 glutDisplayFunc( SampleDisplay );
333 glutReshapeFunc( SampleReshape );
334 glutKeyboardFunc( SampleKeyboard );
335 glutSpecialFunc( SampleSpecial );
336 glutIdleFunc( SampleIdle );
337 glutEntryFunc( SampleEntry );
338 glutMenuStatusFunc( SampleMenuStatus );
339 glutAttachMenu( GLUT_LEFT_BUTTON );
341 glutInitWindowPosition( 200, 200 );
342 g_mainwin2 = glutCreateWindow( "I am not Jan B." );
343 glutDisplayFunc( SampleDisplay );
344 glutReshapeFunc( SampleReshape );
345 glutKeyboardFunc( SampleKeyboard );
346 glutSpecialFunc( SampleSpecial );
347 glutIdleFunc( SampleIdle );
348 glutEntryFunc( SampleEntry );
349 glutMenuStatusFunc( SampleMenuStatus );
350 glutAttachMenu( GLUT_LEFT_BUTTON );
351 glutSetMenu(subMenuA);
352 glutAttachMenu( GLUT_RIGHT_BUTTON );
354 g_sw1=glutCreateSubWindow(g_mainwin2,200,0,100,100);
355 glutDisplayFunc( SampleDisplay );
356 glutSetMenu(subMenuB);
357 glutAttachMenu( GLUT_LEFT_BUTTON );
359 g_sw2=glutCreateSubWindow(g_sw1,50,0,50,50);
360 glutDisplayFunc( SampleDisplay );
362 glutAttachMenu( GLUT_RIGHT_BUTTON );
364 printf( "Testing game mode string parsing, don't panic!\n" );
365 glutGameModeString( "320x240:32@100" );
366 glutGameModeString( "640x480:16@72" );
367 glutGameModeString( "1024x768" );
368 glutGameModeString( ":32@120" );
369 glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
371 glutGameModeString( "640x480:37@300" ); /* this one should fail */
374 glutGameModeString( "800x600" ); /* this one is likely to succeed */
375 g_gamemodewin = glutEnterGameMode();
377 if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
379 glutDisplayFunc( SampleDisplay );
380 glutReshapeFunc( SampleReshape );
381 glutKeyboardFunc( SampleGameModeKeyboard );
382 glutIdleFunc( SampleIdle );
383 glutEntryFunc( SampleEntry );
384 glutMenuStatusFunc( SampleMenuStatus );
386 glutAttachMenu( GLUT_LEFT_BUTTON );
388 printf( "current window is %ix%i at (%i,%i)\n",
389 glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
390 glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
394 * Enter the main FreeGLUT processing loop
399 * returned from mainloop after window closed
400 * see glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS); above
402 printf( "glutMainLoop() termination works fine!\n" );
407 /*** END OF FILE ***/