ee62c812e996e62c762a50b139c8d3df13032272
[freeglut] / progs / demos / One / one.c
1 /*
2  * one.c
3  *
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 :)
6  *
7  * : This is a wrapper. I still have to figure out
8  * : how to build shared libraries under *nix :)
9  *
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
13  */
14
15 #ifdef HAVE_CONFIG_H
16 #include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22 #include <GL/freeglut.h>
23
24 int g_LeaveGameMode = 0;
25 int g_InGameMode = 0;
26
27 /*
28  * Call this function to have some text drawn at given coordinates
29  */
30 void PrintText( int nX, int nY, char* pszText )
31 {
32     int lines;
33     char *p;
34
35     /*
36      * Prepare the OpenGL state
37      */
38     glDisable( GL_LIGHTING );
39     glDisable( GL_DEPTH_TEST );
40     glMatrixMode( GL_PROJECTION );
41     glPushMatrix();
42     glLoadIdentity();
43
44     /*
45      * Have an orthogonal projection matrix set
46      */
47     glOrtho( 0, glutGet( GLUT_WINDOW_WIDTH ),
48              0, glutGet( GLUT_WINDOW_HEIGHT ),
49              -1, +1
50     );
51
52     /*
53      * Now the matrix mode
54      */
55     glMatrixMode( GL_MODELVIEW );
56     glPushMatrix();
57     glLoadIdentity();
58
59     /*
60      * Now the main text
61      */
62     glColor3ub( 0, 0, 0 );
63     glRasterPos2i( nX, nY );
64
65     for( p=pszText, lines=0; *p; p++ )
66     {
67         if( *p == '\n' )
68         {
69             lines++;
70             glRasterPos2i( nX, nY-(lines*18) );
71         }
72
73         glutBitmapCharacter( GLUT_BITMAP_HELVETICA_18,  *p );
74     }
75
76     /*
77      * Revert to the old matrix modes
78      */
79     glMatrixMode( GL_PROJECTION );
80     glPopMatrix();
81
82     glMatrixMode( GL_MODELVIEW );
83     glPopMatrix();
84
85     /*
86      * Restore the old OpenGL states
87      */
88     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );
89     glEnable( GL_DEPTH_TEST );
90     glEnable( GL_LIGHTING );
91 }
92
93 /*
94  * This is the display routine for our sample FreeGLUT windows
95  */
96 static float g_fTime = 0.0f;
97
98 void SampleDisplay( void )
99 {
100     /*
101      * Clear the screen
102      */
103     glClearColor( 0, 0.5, 1, 1 );
104     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
105
106     /*
107      * Have the cube rotated
108      */
109     glMatrixMode( GL_MODELVIEW );
110     glPushMatrix();
111
112     glRotatef( g_fTime, 0, 0, 1 );
113     glRotatef( g_fTime, 0, 1, 0 );
114     glRotatef( g_fTime, 1, 0, 0 );
115
116     /*
117      * And then drawn...
118      */
119     glColor3f( 1, 1, 0 );
120     /* glutWireCube( 20.0 ); */
121     glutWireTeapot( 20.0 );
122     /* glutWireSpher( 15.0, 15, 15 ); */
123     /* glColor3f( 0, 1, 0 ); */
124     /* glutWireCube( 30.0 ); */
125     /* glutSolidCone( 10, 20, 10, 2 ); */
126
127     /*
128      * Don't forget about the model-view matrix
129      */
130     glPopMatrix( );
131
132     /*
133      * Draw a silly text
134      */
135     if( g_InGameMode == 0 )
136         PrintText( 20, 20, "Hello there cruel world!" );
137     else
138         PrintText( 20, 20, "Press ESC to leave the game mode!" );
139
140     /*
141      * And swap this context's buffers
142      */
143     glutSwapBuffers( );
144 }
145
146 /*
147  * This is a sample idle function
148  */
149 void SampleIdle( void )
150 {
151     g_fTime += 0.5f;
152
153     if( g_LeaveGameMode == 1 )
154     {
155         glutLeaveGameMode( );
156         g_LeaveGameMode = 0;
157         g_InGameMode = 0;
158     }
159 }
160
161 /*
162  * The reshape function
163  */
164 void SampleReshape( int nWidth, int nHeight )
165 {
166     GLfloat fAspect = (GLfloat) nHeight / (GLfloat) nWidth;
167     GLfloat fPos[ 4 ] = { 0.0f, 0.0f, 10.0f, 0.0f };
168     GLfloat fCol[ 4 ] = { 0.5f, 1.0f,  0.0f, 1.0f };
169
170     /*
171      * Update the viewport first
172      */
173     glViewport( 0, 0, nWidth, nHeight );
174
175     /*
176      * Then the projection matrix
177      */
178     glMatrixMode( GL_PROJECTION );
179     glLoadIdentity();
180     glFrustum( -1.0, 1.0, -fAspect, fAspect, 1.0, 80.0 );
181
182     /*
183      * Move back the camera a bit
184      */
185     glMatrixMode( GL_MODELVIEW );
186     glLoadIdentity( );
187     glTranslatef( 0.0, 0.0, -40.0f );
188
189     /*
190      * Enable some features...
191      */
192     glEnable( GL_CULL_FACE );
193     glEnable( GL_DEPTH_TEST );
194     glEnable( GL_NORMALIZE );
195
196     /*
197      * Set up some lighting
198      */
199     glLightfv( GL_LIGHT0, GL_POSITION, fPos );
200     glEnable( GL_LIGHTING );
201     glEnable( GL_LIGHT0 );
202
203     /*
204      * Set up a sample material
205      */
206     glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, fCol );
207 }
208
209 /*
210  * A sample keyboard callback
211  */
212 void SampleKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
213 {
214     printf( "SampleKeyboard(): keypress '%c' at (%i,%i)\n",
215             cChar, nMouseX, nMouseY );
216 }
217
218 /*
219  * A sample keyboard callback (for game mode window)
220  */
221 void SampleGameModeKeyboard( unsigned char cChar, int nMouseX, int nMouseY )
222 {
223     if( cChar == 27 )
224         g_LeaveGameMode = 1;
225 }
226
227
228 /*
229  * A sample special callback
230  */
231 void SampleSpecial( int nSpecial, int nMouseX, int nMouseY )
232 {
233     printf( "SampleSpecial(): special keypress %i at (%i,%i)\n",
234             nSpecial, nMouseX, nMouseY );
235 }
236
237 /*
238  * A sample menu callback
239  */
240 void SampleMenu( int menuID )
241 {
242     /*
243      * Just print something funny
244      */
245     printf( "SampleMenu() callback executed, menuID is %i\n", menuID );
246 }
247
248 /*
249  * The sample's entry point
250  */
251 int main( int argc, char** argv )
252 {
253     int menuID, subMenuA, subMenuB;
254
255     glutInitDisplayString( "stencil~2 rgb double depth>=16 samples" );
256     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
257     glutInitWindowPosition( 100, 100 );
258
259     glutInit( &argc, argv );
260
261     glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);
262
263     subMenuA = glutCreateMenu( SampleMenu );
264     glutAddMenuEntry( "Sub menu A1 (01)", 1 );
265     glutAddMenuEntry( "Sub menu A2 (02)", 2 );
266     glutAddMenuEntry( "Sub menu A3 (03)", 3 );
267
268     subMenuB = glutCreateMenu( SampleMenu );
269     glutAddMenuEntry( "Sub menu B1 (04)", 4 );
270     glutAddMenuEntry( "Sub menu B2 (05)", 5 );
271     glutAddMenuEntry( "Sub menu B3 (06)", 6 );
272     glutAddSubMenu( "Going to sub menu A", subMenuA );
273
274     menuID = glutCreateMenu( SampleMenu );
275     glutAddMenuEntry( "Entry one",   1 );
276     glutAddMenuEntry( "Entry two",   2 );
277     glutAddMenuEntry( "Entry three", 3 );
278     glutAddMenuEntry( "Entry four",  4 );
279     glutAddMenuEntry( "Entry five",  5 );
280     glutAddSubMenu( "Enter sub menu A", subMenuA );
281     glutAddSubMenu( "Enter sub menu B", subMenuB );
282
283     glutCreateWindow( "Hello world!" );
284     glutDisplayFunc( SampleDisplay );
285     glutReshapeFunc( SampleReshape );
286     glutKeyboardFunc( SampleKeyboard );
287     glutSpecialFunc( SampleSpecial );
288     glutIdleFunc( SampleIdle );
289     glutAttachMenu( GLUT_LEFT_BUTTON );
290
291     glutInitWindowPosition( 200, 200 );
292     glutCreateWindow( "I am not Jan B." );
293     glutDisplayFunc( SampleDisplay );
294     glutReshapeFunc( SampleReshape );
295     glutKeyboardFunc( SampleKeyboard );
296     glutSpecialFunc( SampleSpecial );
297     glutIdleFunc( SampleIdle );
298     glutAttachMenu( GLUT_LEFT_BUTTON );
299     glutSetMenu(subMenuA);
300     glutAttachMenu( GLUT_RIGHT_BUTTON);
301
302     printf( "Testing game mode string parsing, don't panic!\n" );
303     glutGameModeString( "320x240:32@100" );
304     glutGameModeString( "640x480:16@72" );
305     glutGameModeString( "1024x768" );
306     glutGameModeString( ":32@120" );
307     glutGameModeString( "Toudi glupcze, Danwin bedzie moj!" );
308     
309     glutGameModeString( "640x480:37@300" );    /* this one should fail */
310     glutEnterGameMode();
311
312     glutGameModeString( "800x600" );    /* this one is likely to succeed */
313     glutEnterGameMode();
314
315     if (glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
316         g_InGameMode = 1;
317     glutDisplayFunc( SampleDisplay );
318     glutReshapeFunc( SampleReshape );
319     glutKeyboardFunc( SampleGameModeKeyboard );
320     glutIdleFunc( SampleIdle );
321
322     printf( "current window is %ix%i at (%i,%i)\n",
323         glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ),
324         glutGet( GLUT_WINDOW_X ), glutGet( GLUT_WINDOW_Y )
325     );
326
327     /*
328      * Enter the main FreeGLUT processing loop
329      */
330     glutMainLoop();
331
332     printf( "glutMainLoop() termination works fine!\n" );
333
334     /*
335      * This is never reached in FreeGLUT. Is that good?
336      */
337     return EXIT_SUCCESS;
338 }
339
340 /*** END OF FILE ***/