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 )
281 * Just print something funny
283 printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
287 * The sample's entry point
289 int main( int argc, char** argv )
291 int menuID, subMenuA, subMenuB;
293 glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
294 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
295 glutInitWindowPosition( 100, 100 );
297 glutInit( &argc, argv );
299 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
301 subMenuA = glutCreateMenu( SampleMenu );
302 glutAddMenuEntry( "Sub menu A1 (01)", 1 );
303 glutAddMenuEntry( "Sub menu A2 (02)", 2 );
304 glutAddMenuEntry( "Sub menu A3 (03)", 3 );
306 subMenuB = glutCreateMenu( SampleMenu );
307 glutAddMenuEntry( "Sub menu B1 (04)", 4 );
308 glutAddMenuEntry( "Sub menu B2 (05)", 5 );
309 glutAddMenuEntry( "Sub menu B3 (06)", 6 );
310 glutAddSubMenu( "Going to sub menu A", subMenuA );
312 menuID = glutCreateMenu( SampleMenu );
313 glutAddMenuEntry( "Entry one", 1 );
314 glutAddMenuEntry( "Entry two", 2 );
315 glutAddMenuEntry( "Entry three", 3 );
316 glutAddMenuEntry( "Entry four", 4 );
317 glutAddMenuEntry( "Entry five", 5 );
318 glutAddSubMenu( "Enter sub menu A", subMenuA );
319 glutAddSubMenu( "Enter sub menu B", subMenuB );
321 glutCreateWindow( "Hello world!" );
322 glutDisplayFunc( SampleDisplay );
323 glutReshapeFunc( SampleReshape );
324 glutKeyboardFunc( SampleKeyboard );
325 glutSpecialFunc( SampleSpecial );
326 glutIdleFunc( SampleIdle );
327 glutEntryFunc( SampleEntry );
328 glutAttachMenu( GLUT_LEFT_BUTTON );
330 glutInitWindowPosition( 200, 200 );
331 g_mainwin = glutCreateWindow( "I am not Jan B." );
332 glutDisplayFunc( SampleDisplay );
333 glutReshapeFunc( SampleReshape );
334 glutKeyboardFunc( SampleKeyboard );
335 glutSpecialFunc( SampleSpecial );
336 glutIdleFunc( SampleIdle );
337 glutAttachMenu( GLUT_LEFT_BUTTON );
338 glutSetMenu(subMenuA);
339 glutAttachMenu( GLUT_RIGHT_BUTTON );
341 g_sw1=glutCreateSubWindow(g_mainwin,200,0,100,100);
342 glutDisplayFunc( SampleDisplay );
343 glutSetMenu(subMenuB);
344 glutAttachMenu( GLUT_LEFT_BUTTON );
346 g_sw2=glutCreateSubWindow(g_sw1,50,0,50,50);
347 glutDisplayFunc( SampleDisplay );
349 glutAttachMenu( GLUT_RIGHT_BUTTON );
351 printf( "Testing game mode string parsing, don't panic!\n" );
352 glutGameModeString( "320x240:32@100" );
353 glutGameModeString( "640x480:16@72" );
354 glutGameModeString( "1024x768" );
355 glutGameModeString( ":32@120" );
356 glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
358 glutGameModeString( "640x480:37@300" ); /* this one should fail */
361 glutGameModeString( "800x600" ); /* this one is likely to succeed */
362 g_gamemodewin = glutEnterGameMode();
364 if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
366 glutDisplayFunc( SampleDisplay );
367 glutReshapeFunc( SampleReshape );
368 glutKeyboardFunc( SampleGameModeKeyboard );
369 glutIdleFunc( SampleIdle );
371 glutAttachMenu( GLUT_LEFT_BUTTON );
373 printf( "current window is %ix%i at (%i,%i)\n",
374 glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
375 glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
379 * Enter the main FreeGLUT processing loop
384 * returned from mainloop after window closed
385 * see glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS); above
387 printf( "glutMainLoop() termination works fine!\n" );
392 /*** END OF FILE ***/