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_mainwin, 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 /* Dont draw other windows when in gamemode, those aren't visible
103 * anyway. Drawing them continuously anyway can cause flicker trouble
104 * on my machine. This only seems to occur 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_mainwin);
123 glClearColor(0.3f,0.3f,0.3f,1);
124 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
125 glutPostWindowRedisplay(g_mainwin);
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 glutLeaveGameMode( );
193 void SampleEntry(int state)
195 int window = glutGetWindow () ;
196 printf ( "Window %d Entry Callback: %d\n", window, state ) ;
200 * The reshape function
202 void SampleReshape( int nWidth, int nHeight )
204 GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
205 GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
206 GLfloat fCol[ 4 ] = { 0.5f, 1.0f, 0.0f, 1.0f };
209 * Update the viewport first
211 glViewport( 0, 0, nWidth, nHeight );
214 * Then the projection matrix
216 glMatrixMode( GL_PROJECTION );
218 glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
221 * Move back the camera a bit
223 glMatrixMode( GL_MODELVIEW );
225 glTranslatef( 0.0, 0.0, -40.0f );
228 * Enable some features...
230 glEnable( GL_CULL_FACE );
231 glEnable( GL_DEPTH_TEST );
232 glEnable( GL_NORMALIZE );
235 * Set up some lighting
237 glLightfv( GL_LIGHT0, GL_POSITION, fPos );
238 glEnable( GL_LIGHTING );
239 glEnable( GL_LIGHT0 );
242 * Set up a sample material
244 glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
248 * A sample keyboard callback
250 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
252 printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
253 cChar, nMouseX, nMouseY );
257 * A sample keyboard callback (for game mode window)
259 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
267 * A sample special callback
269 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
271 printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
272 nSpecial, nMouseX, nMouseY );
276 * A sample menu callback
278 void SampleMenu( int menuID )
280 printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
284 * A sample menu status callback
286 void SampleMenuStatus( int status, int x, int y )
288 printf ( "SampleMenu() callback executed, MenuStatus is %i at (%i,%i)\n", status, x, y );
292 * The sample's entry point
294 int main( int argc, char** argv )
296 int menuID, subMenuA, subMenuB;
298 glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
299 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
300 glutInitWindowPosition( 100, 100 );
302 glutInit( &argc, argv );
304 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
306 subMenuA = glutCreateMenu( SampleMenu );
307 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
308 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
309 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
311 subMenuB = glutCreateMenu( SampleMenu );
312 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
313 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
314 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
315 glutAddSubMenu( "Going to sub menu A", subMenuA );
317 menuID = glutCreateMenu( SampleMenu );
318 glutAddMenuEntry( "Entry one", 1 );
319 glutAddMenuEntry( "Entry two", 2 );
320 glutAddMenuEntry( "Entry three", 3 );
321 glutAddMenuEntry( "Entry four", 4 );
322 glutAddMenuEntry( "Entry five", 5 );
323 glutAddSubMenu( "Enter sub menu A", subMenuA );
324 glutAddSubMenu( "Enter sub menu B", subMenuB );
326 glutCreateWindow( "Hello world!" );
327 glutDisplayFunc( SampleDisplay );
328 glutReshapeFunc( SampleReshape );
329 glutKeyboardFunc( SampleKeyboard );
330 glutSpecialFunc( SampleSpecial );
331 glutIdleFunc( SampleIdle );
332 glutEntryFunc( SampleEntry );
333 glutMenuStatusFunc( SampleMenuStatus );
334 glutAttachMenu( GLUT_LEFT_BUTTON );
336 glutInitWindowPosition( 200, 200 );
337 g_mainwin = glutCreateWindow( "I am not Jan B." );
338 glutDisplayFunc( SampleDisplay );
339 glutReshapeFunc( SampleReshape );
340 glutKeyboardFunc( SampleKeyboard );
341 glutSpecialFunc( SampleSpecial );
342 glutIdleFunc( SampleIdle );
343 glutEntryFunc( SampleEntry );
344 glutMenuStatusFunc( SampleMenuStatus );
345 glutAttachMenu( GLUT_LEFT_BUTTON );
346 glutSetMenu(subMenuA);
347 glutAttachMenu( GLUT_RIGHT_BUTTON );
349 g_sw1=glutCreateSubWindow(g_mainwin,200,0,100,100);
350 glutDisplayFunc( SampleDisplay );
351 glutSetMenu(subMenuB);
352 glutAttachMenu( GLUT_LEFT_BUTTON );
354 g_sw2=glutCreateSubWindow(g_sw1,50,0,50,50);
355 glutDisplayFunc( SampleDisplay );
357 glutAttachMenu( GLUT_RIGHT_BUTTON );
359 printf( "Testing game mode string parsing, don't panic!\n" );
360 glutGameModeString( "320x240:32@100" );
361 glutGameModeString( "640x480:16@72" );
362 glutGameModeString( "1024x768" );
363 glutGameModeString( ":32@120" );
364 glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
366 glutGameModeString( "640x480:37@300" ); /* this one should fail */
369 glutGameModeString( "800x600" ); /* this one is likely to succeed */
370 g_gamemodewin = glutEnterGameMode();
372 if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
374 glutDisplayFunc( SampleDisplay );
375 glutReshapeFunc( SampleReshape );
376 glutKeyboardFunc( SampleGameModeKeyboard );
377 glutIdleFunc( SampleIdle );
378 glutEntryFunc( SampleEntry );
379 glutMenuStatusFunc( SampleMenuStatus );
381 glutAttachMenu( GLUT_LEFT_BUTTON );
383 printf( "current window is %ix%i at (%i,%i)\n",
384 glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
385 glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
389 * Enter the main FreeGLUT processing loop
394 * returned from mainloop after window closed
395 * see glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS); above
397 printf( "glutMainLoop() termination works fine!\n" );
402 /*** END OF FILE ***/