00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <SDL/SDL.h>
00010 #include <GL/gl.h>
00011 #include <GL/glu.h>
00012
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015
00016 static GLboolean should_rotate = GL_TRUE;
00017
00018 static void quit_tutorial( int code )
00019 {
00020
00021
00022
00023
00024
00025 SDL_Quit( );
00026
00027
00028 exit( code );
00029 }
00030
00031 static void handle_key_down( SDL_keysym* keysym )
00032 {
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 switch( keysym->sym ) {
00043 case SDLK_ESCAPE:
00044 quit_tutorial( 0 );
00045 break;
00046 case SDLK_SPACE:
00047 should_rotate = !should_rotate;
00048 break;
00049 default:
00050 break;
00051 }
00052
00053 }
00054
00055 static void process_events( void )
00056 {
00057
00058 SDL_Event event;
00059
00060
00061 while( SDL_PollEvent( &event ) ) {
00062
00063 switch( event.type ) {
00064 case SDL_KEYDOWN:
00065
00066 handle_key_down( &event.key.keysym );
00067 break;
00068 case SDL_QUIT:
00069
00070 quit_tutorial( 0 );
00071 break;
00072 }
00073
00074 }
00075
00076 }
00077
00078 static void draw_screen( void )
00079 {
00080
00081 static float angle = 0.0f;
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 static GLfloat v0[] = { -1.0f, -1.0f, 1.0f };
00096 static GLfloat v1[] = { 1.0f, -1.0f, 1.0f };
00097 static GLfloat v2[] = { 1.0f, 1.0f, 1.0f };
00098 static GLfloat v3[] = { -1.0f, 1.0f, 1.0f };
00099 static GLfloat v4[] = { -1.0f, -1.0f, -1.0f };
00100 static GLfloat v5[] = { 1.0f, -1.0f, -1.0f };
00101 static GLfloat v6[] = { 1.0f, 1.0f, -1.0f };
00102 static GLfloat v7[] = { -1.0f, 1.0f, -1.0f };
00103 static GLubyte red[] = { 255, 0, 0, 255 };
00104 static GLubyte green[] = { 0, 255, 0, 255 };
00105 static GLubyte blue[] = { 0, 0, 255, 255 };
00106 static GLubyte white[] = { 255, 255, 255, 255 };
00107 static GLubyte yellow[] = { 0, 255, 255, 255 };
00108 static GLubyte black[] = { 0, 0, 0, 255 };
00109 static GLubyte orange[] = { 255, 255, 0, 255 };
00110 static GLubyte purple[] = { 255, 0, 255, 0 };
00111
00112
00113 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00114
00115
00116 glMatrixMode( GL_MODELVIEW );
00117 glLoadIdentity( );
00118
00119
00120 glTranslatef( 0.0, 0.0, -5.0 );
00121
00122
00123 glRotatef( angle, 0.0, 1.0, 0.0 );
00124
00125 if( should_rotate ) {
00126
00127 if( ++angle > 360.0f ) {
00128 angle = 0.0f;
00129 }
00130
00131 }
00132
00133
00134 glBegin( GL_TRIANGLES );
00135
00136 glColor4ubv( red );
00137 glVertex3fv( v0 );
00138 glColor4ubv( green );
00139 glVertex3fv( v1 );
00140 glColor4ubv( blue );
00141 glVertex3fv( v2 );
00142
00143 glColor4ubv( red );
00144 glVertex3fv( v0 );
00145 glColor4ubv( blue );
00146 glVertex3fv( v2 );
00147 glColor4ubv( white );
00148 glVertex3fv( v3 );
00149
00150 glColor4ubv( green );
00151 glVertex3fv( v1 );
00152 glColor4ubv( black );
00153 glVertex3fv( v5 );
00154 glColor4ubv( orange );
00155 glVertex3fv( v6 );
00156
00157 glColor4ubv( green );
00158 glVertex3fv( v1 );
00159 glColor4ubv( orange );
00160 glVertex3fv( v6 );
00161 glColor4ubv( blue );
00162 glVertex3fv( v2 );
00163
00164 glColor4ubv( black );
00165 glVertex3fv( v5 );
00166 glColor4ubv( yellow );
00167 glVertex3fv( v4 );
00168 glColor4ubv( purple );
00169 glVertex3fv( v7 );
00170
00171 glColor4ubv( black );
00172 glVertex3fv( v5 );
00173 glColor4ubv( purple );
00174 glVertex3fv( v7 );
00175 glColor4ubv( orange );
00176 glVertex3fv( v6 );
00177
00178 glColor4ubv( yellow );
00179 glVertex3fv( v4 );
00180 glColor4ubv( red );
00181 glVertex3fv( v0 );
00182 glColor4ubv( white );
00183 glVertex3fv( v3 );
00184
00185 glColor4ubv( yellow );
00186 glVertex3fv( v4 );
00187 glColor4ubv( white );
00188 glVertex3fv( v3 );
00189 glColor4ubv( purple );
00190 glVertex3fv( v7 );
00191
00192 glColor4ubv( white );
00193 glVertex3fv( v3 );
00194 glColor4ubv( blue );
00195 glVertex3fv( v2 );
00196 glColor4ubv( orange );
00197 glVertex3fv( v6 );
00198
00199 glColor4ubv( white );
00200 glVertex3fv( v3 );
00201 glColor4ubv( orange );
00202 glVertex3fv( v6 );
00203 glColor4ubv( purple );
00204 glVertex3fv( v7 );
00205
00206 glColor4ubv( green );
00207 glVertex3fv( v1 );
00208 glColor4ubv( red );
00209 glVertex3fv( v0 );
00210 glColor4ubv( yellow );
00211 glVertex3fv( v4 );
00212
00213 glColor4ubv( green );
00214 glVertex3fv( v1 );
00215 glColor4ubv( yellow );
00216 glVertex3fv( v4 );
00217 glColor4ubv( black );
00218 glVertex3fv( v5 );
00219
00220 glEnd( );
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239 SDL_GL_SwapBuffers( );
00240 }
00241
00242 static void setup_opengl( int width, int height )
00243 {
00244 float ratio = (float) width / (float) height;
00245
00246
00247 glShadeModel( GL_SMOOTH );
00248
00249
00250 glCullFace( GL_BACK );
00251 glFrontFace( GL_CCW );
00252 glEnable( GL_CULL_FACE );
00253
00254
00255 glClearColor( 0, 0, 0, 0 );
00256
00257
00258 glViewport( 0, 0, width, height );
00259
00260
00261
00262
00263
00264 glMatrixMode( GL_PROJECTION );
00265 glLoadIdentity( );
00266
00267
00268
00269
00270 gluPerspective( 60.0, ratio, 1.0, 1024.0 );
00271 }
00272
00273 int main( int argc, char* argv[] )
00274 {
00275
00276 const SDL_VideoInfo* info = NULL;
00277
00278 int width = 0;
00279 int height = 0;
00280
00281 int bpp = 0;
00282
00283 int flags = 0;
00284
00285
00286 if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
00287
00288 fprintf( stderr, "Video initialization failed: %s\n",
00289 SDL_GetError( ) );
00290 quit_tutorial( 1 );
00291 }
00292
00293
00294 info = SDL_GetVideoInfo( );
00295
00296 if( !info ) {
00297
00298 fprintf( stderr, "Video query failed: %s\n",
00299 SDL_GetError( ) );
00300 quit_tutorial( 1 );
00301 }
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312 width = 640;
00313 height = 480;
00314 bpp = info->vfmt->BitsPerPixel;
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
00333 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
00334 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
00335 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
00336 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 flags = SDL_OPENGL | SDL_FULLSCREEN;
00349
00350
00351
00352
00353 if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {
00354
00355
00356
00357
00358
00359 fprintf( stderr, "Video mode set failed: %s\n",
00360 SDL_GetError( ) );
00361 quit_tutorial( 1 );
00362 }
00363
00364
00365
00366
00367
00368 setup_opengl( width, height );
00369
00370
00371
00372
00373
00374 while( 1 ) {
00375
00376 process_events( );
00377
00378 draw_screen( );
00379 }
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389 return 0;
00390 }