Main Page | Namespace List | Class List | File List | Class Members | File Members

render.cpp

Go to the documentation of this file.
00001 #include <GL/glut.h>
00002 #include <GL/glu.h>
00003 
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <math.h>
00007 
00008 #include "config.h"
00009 #include "heightfield.h"
00010 //#include "airplane.h"
00011 #include "texture.h"
00012 #include "scene.h"
00013 
00014 #include "water.h"
00015 
00016 #include "materials.h"
00017 
00018 
00019 
00020 //MODELS are taken from 
00021 //      http://www.3dcafe.com/asp/horror.asp
00022 //      and then converted to very simple format
00023 
00024 /* Despite the fact that all the models are drawn using glTriangles,
00025     framerate is at desired level. This is due to the usage of 
00026     glList's increasing performance for static objects.
00027    Application of depth of field or motion blur effects to the whole scene will
00028     decrease framerate dramatically. That is why they are presented in separate
00029     files on a rather simple scenes.
00030     */ 
00031 
00032 // Mirror Plane is made without stencil buffer.
00033 // The idea is taken from "Improving Shadows and Reflections via the Stencil Buffer".
00034 // Correct behaviour of the mirror plane can be seen on the water and lighting.
00035 // When mirror plane is disabled, all water flow goes in certain direction.
00036 // Enabling mirror plane we see that "reflection" in the mirror plane is correct.
00037 
00038 
00039 //equation for clipping plane (used in mirror plane)
00040 GLdouble eqn[4] = {0, 0, 1, 0};
00041 
00042 // Items in popup menu
00043 enum {
00044   MENU_LIGHTING = 1,
00045   MENU_POLYMODE,
00046   MENU_TEXTURING,
00047   MENU_FRAMERATE,
00048   MENU_EXIT
00049 };
00050 
00051 
00052 
00053 static std::string model_path = "./render/models/";
00054 static std::string model_ext  = ".model";
00055 
00056 
00057 
00058 
00059 
00060 // Time of last call to display timer
00061 static int g_nLastTime = 0;
00062 // Frames per second
00063 static float g_fFPS = 0;
00064 
00065 // Visibility range in GL coordinates
00066 static const int g_visibility = g_cells * g_cellsize;
00067 
00068 // Camera data
00069 static float g_nearPlane = 1;
00070 static float g_farPlane = g_visibility;
00071 
00072 static const double VIEWING_DISTANCE_MIN = 3.0;
00073 float g_fViewDistance = 5*VIEWING_DISTANCE_MIN;
00074 
00075 
00076 // Viewport parameters
00077 static int g_Width = 512;                          // Initial window width
00078 static int g_Height = 512;                         // Initial window height
00079 
00080 // Flags for drawing state
00081 static bool g_bFillPolygons = true;
00082 
00083 
00084 //Movement
00085 static bool go_Forward = false;
00086 static bool go_Backward = false;
00087 static bool go_Left = false;
00088 static bool go_Right = false;
00089 
00090 static float go_cells_per_step = 40;
00091 
00092 void __draw_fog ()
00093 {
00094     glFogi( GL_FOG_MODE, GL_LINEAR );
00095 if (day)
00096     glFogfv( GL_FOG_COLOR, colorLightGreen );
00097 else
00098     glFogfv( GL_FOG_COLOR, colorDarkGreen );
00099 //    glFogf( GL_FOG_DENSITY, 0.5f );
00100     glFogf( GL_FOG_START, g_visibility * 0.6f );
00101     glFogf( GL_FOG_END, g_visibility );
00102 //    glFogfv( GL_FOG_COLOR, colorSky );
00103 
00104 }
00105 
00106 
00107 
00108 /**********************************************
00109  **********************************************
00110    HELPER FUNCTIONS
00111  **********************************************
00112  *********************************************/
00113 
00114 void glutString( char *pstr )
00115 {
00116   while (*pstr!=char(0)) {
00117     glutBitmapCharacter( GLUT_BITMAP_8_BY_13, int( *pstr ));
00118     pstr++;
00119   }
00120 }
00121 
00122 
00123 /**********************************************
00124  **********************************************
00125    GL INITIALISATION / WINDOW REDRAWING
00126  **********************************************
00127  *********************************************/
00128 
00129 
00130 void setupCamera()
00131 {
00132   // Setup projection with field of view of 65 degrees
00133   glMatrixMode(GL_PROJECTION);
00134   glLoadIdentity();
00135   gluPerspective(65.0, (float)g_Width / g_Height, g_nearPlane, g_farPlane);
00136   glMatrixMode(GL_MODELVIEW);
00137   glLoadIdentity();
00138 
00139 
00140 glRotatef(g_CameraAngleX, 1, 0, 0);
00141 glRotatef(g_CameraAngleY, 0, 1, 0);
00142 
00143 glTranslatef(-g_CameraX,  -g_CameraZ, -g_CameraY);
00144 
00145 gluLookAt(0, 0, 0,
00146         0, 0, -1, 
00147         0, 1, 0);
00148 
00149 
00150 
00151 /*
00152   // Set up viewing transformation, looking down -Z axis
00153   gluLookAt(0, 0, -g_fViewDistance, 0, 0, -1, 0, 1, 0);
00154 
00155   if (g_bChaseCam) {
00156     // Put camera a bit upward
00157     glTranslatef( 0,-3,0 );
00158     glRotatef( -15, 1,0,0 );
00159     // Align viewing direction with plane X axis
00160     glRotatef( -90-g_fPlaneAngle, 0,1,0 );
00161   }
00162   else {
00163     // Free camera mode
00164     glRotatef( g_fCameraX, 1,0,0 );
00165     glRotatef( g_fCameraY, 0,1,0 );
00166   }
00167 */
00168 
00169 }
00170 
00171 void __drawModel(int model)
00172 {
00173 
00174 if (model == MODEL_GALLOWS) 
00175         {
00176         setMaterial( MATERIAL_WOOD );
00177         drawModel(model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, 15, 70, 10, -90, 0, 90, 0.25 );
00178         }
00179 
00180 if (model == MODEL_GALLOWS_ROPE) 
00181         {
00182         setMaterial( MATERIAL_RUBY );
00183         drawModel(model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, 15, 70, 10, -90, 0, 90, 0.25 );
00184         }
00185 
00186 if (model == MODEL_IRON_MAIDEN)
00187         {
00188         setMaterial( MATERIAL_SILVER );
00189         drawModel(model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, 17, 110, 12, -90, 0, 120., 1);
00190         }
00191 
00192 if (model == MODEL_CASKET_CLOSED)
00193         {
00194         setMaterial( MATERIAL_CRYPT );
00195         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, 30, 60, 18, -90, 0, 90, .05);
00196         }
00197 
00198 if (model == MODEL_CRYPT) 
00199         {
00200         setMaterial( MATERIAL_CRYPT );
00201         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, -40, 40, 8, -90, 0, -90, 0.025 );
00202         }
00203 
00204 if  (model == MODEL_GILJOTINA_1)
00205         {
00206         setMaterial( MATERIAL_WOOD );
00207         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ,  74, 70, 12, -90, 0, 90, 0.25 );
00208         }
00209 
00210 if (model == MODEL_GILJOTINA_2) 
00211         {
00212         setMaterial( MATERIAL_GOLD );
00213         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ,  74, 70, 12, -90, 0, 90, 0.25 );
00214         }
00215 if (model == MODEL_GILJOTINA_3)
00216         {//chelovek
00217         setMaterial( MATERIAL_SKIN );
00218         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ,  70, 70, 12, -90, 0, 90, 0.25 );
00219         }
00220 if (model == MODEL_GILJOTINA_4) 
00221         {
00222         setMaterial( MATERIAL_CERAMIC );
00223         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ,  74, 70, 12, -90, 0, 90, 0.25 );
00224         }
00225 
00226 if  (model == MODEL_MANCAGE_1)
00227         {
00228         setMaterial( MATERIAL_WOOD );
00229         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, 70, 25, 12, -90, 0, 90, 0.25 );
00230         }
00231 if  (model == MODEL_MANCAGE_2) 
00232         {
00233         setMaterial( MATERIAL_SILVER );
00234         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, 70, 25, 12, -90, 0, 90, 0.25 );
00235         }
00236 
00237 if  (model == MODEL_GRAVEYARD) 
00238         {
00239         setMaterial ( MATERIAL_SILVER );
00240         drawModel( model, model_handle[model], g_CameraX, g_CameraY, g_CameraZ, 100, 110, 17, -90, 0, -90, 0.04 );
00241         }
00242 
00243 if  (model == MODEL_PLAYER) 
00244         {
00245         setMaterial( MATERIAL_SILVER );
00246         drawModel( model, model_handle[model], 0,0,0,//g_CameraX, g_CameraY, g_CameraZ, 
00247              0  ,  0, 0,
00248             -90, 0, -90, 
00249             0.25 );
00250         }
00251 
00252 }
00253 
00254 
00255 void renderScene(int pass)
00256 {
00257   if (g_bLighting) {
00258     glEnable(GL_LIGHTING);
00259   }
00260   else {
00261     glDisable(GL_LIGHTING);
00262   }
00263 
00264 if ( (draw_mirror_plane))// && (pass != 1) )
00265     {
00266     glClipPlane(GL_CLIP_PLANE0, eqn);
00267     glEnable(GL_CLIP_PLANE0);
00268     }
00269     
00270   // Draw ground
00271 if  (draw_height_field)
00272     {
00273       glPushMatrix();
00274       glTranslatef( g_CameraX, 0, g_CameraY );//g_fHeight,0 );
00275           drawHeightField( g_CameraX/g_cellsize, g_CameraY/g_cellsize, g_cells, g_cells);//g_airplaneX,g_airplaneY, g_cells, g_cells );
00276       glPopMatrix();
00277     }
00278 
00279 
00280 if ( (draw_mirror_plane) )//&& (pass != 1) )
00281     {
00282     glDisable(GL_CLIP_PLANE0);
00283     }
00284 
00285     //Draw all models
00286 glDisable(GL_TEXTURE_2D);
00287 for (int i=0; i<MODEL_N; i++)
00288     {
00289     if ( model_in_use[i])
00290         {
00291         glPushMatrix();
00292         setDiffuseMaterialColor(colorDarkBrown);
00293     
00294         if ( model_use_list[i] )
00295             {
00296             glCallList ( model_index[i] );
00297             }
00298         else
00299             {
00300             __drawModel( i );
00301             }//endif
00302         glPopMatrix();
00303         }//endif
00304 
00305     }//endfor
00306 
00307 
00308   //draw environment map
00309   
00310 }
00311 
00312 
00313 void display()
00314 {
00315 //drawDinosaur();
00316 
00317 setDiffuseMaterialColor(colorWhite);
00318 //glColor4fv(colorWhite);
00319 
00320   // Clear frame buffer and depth buffer
00321 glEnable(GL_DEPTH_TEST);
00322   glClearColor( colorDarkGreen[0], colorDarkGreen[1], colorDarkGreen[2], colorDarkGreen[3] );
00323   
00324   if ((draw_mirror_plane) || (draw_mirror) )
00325     {
00326         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT );
00327         glEnable(GL_STENCIL_TEST);
00328     }
00329   else
00330     {
00331         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00332         glDisable(GL_STENCIL_TEST);
00333     }
00334   
00335   
00336   // Setup camera and initialize modelview matrix stack
00337   setupCamera();
00338   glViewport(0, 0, g_Width, g_Height);
00339   
00340 
00341   
00342   
00343   if (draw_mirror_plane)
00344     {
00345     if (draw_fog)
00346                 {
00347                 __draw_fog();
00348                 glEnable(GL_FOG);
00349                 }
00350                 else glDisable(GL_FOG);
00351                 
00352     glClearStencil(0);
00353     //step 2 -- push the modelview matrix
00354     glPushMatrix();
00355     //step 3 -- mirror lies in the z=0 plane.
00356     // Thus we negate the Z coordinate
00357     glScalef( 1.0f, 1.0f, -1.0f );
00358     
00359     //step 4 -- no culling
00360 //    glCullFace(GL_FRONT);
00361     //step 5
00362     // now drawing all the scene without care of objects behind it (there are no such!)
00363 
00364   // Set up the stationary light (after camera transform)
00365 glEnable(GL_LIGHT1);
00366 glDisable(GL_LIGHT0);
00367 /*  g_lightPos2[0] = g_lightPos[0];
00368   g_lightPos2[1] = g_lightPos[1];
00369   g_lightPos2[2] = g_lightPos[2];
00370   g_lightPos2[3] = g_lightPos[3];
00371 */
00372   glLightfv(GL_LIGHT1, GL_POSITION, g_lightPos);
00373 /*
00374 if (day)
00375   glLightfv(GL_LIGHT1, GL_DIFFUSE, colorVeryLightGreen);//Green );//White );
00376 else
00377   glLightfv(GL_LIGHT1, GL_DIFFUSE, colorLightGreen);//Green );//White );
00378 */
00379   glLightfv(GL_LIGHT1, GL_DIFFUSE, colorWhite );
00380   glLightfv(GL_LIGHT1, GL_AMBIENT, colorDarkGrey );
00381   glLightfv(GL_LIGHT1, GL_SPECULAR, colorWhite );
00382 
00383 
00384 /*  //draw the Sun
00385   glPushMatrix();
00386   glTranslatef(g_lightPos2[0], g_lightPos2[1], g_lightPos2[2]);
00387   glutWireSphere(3, 20, 20);
00388   glPopMatrix();
00389 */
00390 
00391 
00392   // Render the scene
00393 //glColor4f(1,1,1,1);
00394 
00395 
00396 //glClipPlane(GL_CLIP_PLANE0, eqn);
00397 //glEnable(GL_CLIP_PLANE0);
00398 
00399   g_CameraY = -g_CameraY;
00400 //Disabling the fog looks a little bit unnatural, 
00401 // but better then nothing
00402     
00403   renderScene(2);
00404   //glDisable(GL_FOG);
00405 g_CameraY = -g_CameraY;
00406   
00407 //glDisable(GL_CLIP_PLANE0);
00408     //step 6 -- no culling
00409 //    glCullFace(GL_BACK);
00410     glPopMatrix();
00411     //step 7
00412     glColorMask(0, 0, 0, 0);
00413         glBegin(GL_TRIANGLES);
00414             glVertex3d(-5, -5, 0);
00415             glVertex3d(-5, 5, 0);
00416             glVertex3d(5,5,0);
00417             
00418             glVertex3d(5, 5, 0);
00419             glVertex3d(5, -5, 0);
00420             glVertex3d(-5, -5, 0);
00421         glEnd();
00422         
00423     glColorMask(1,1,1,1);
00424 
00425     //step 8 -- no unreflected objects, except framerate
00426     glDisable(GL_CULL_FACE);
00427     
00428 //    glDisable(GL_STENCIL_TEST);
00429     }
00430     
00431 //RENDERING THE SCENE
00432   if (draw_fog)
00433     {
00434     __draw_fog();
00435     glEnable( GL_FOG );
00436     }
00437   else glDisable(GL_FOG);
00438 
00439   // Set up the stationary light (after camera transform)
00440 glEnable(GL_LIGHT0);
00441 glDisable(GL_LIGHT1);
00442   glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos);
00443 /*
00444 if (day)
00445   glLightfv(GL_LIGHT0, GL_DIFFUSE, colorVeryLightGreen);//Green );//White );
00446 else 
00447   glLightfv(GL_LIGHT0, GL_DIFFUSE, colorLightGreen);
00448 */
00449   glLightfv(GL_LIGHT0, GL_DIFFUSE, colorWhite );
00450   
00451   glLightfv(GL_LIGHT0, GL_AMBIENT, colorDarkGrey );
00452   glLightfv(GL_LIGHT0, GL_SPECULAR, colorWhite );
00453 
00454 //glColorMask(1,1,1,1);
00455 
00456   //draw the Sun
00457 /*  glPushMatrix();
00458   glTranslatef(g_lightPos[0], g_lightPos[1], g_lightPos[2]);
00459   glutWireSphere(3, 20, 20);
00460   glPopMatrix();
00461   */
00462   // Render the scene
00463 //if (draw_mirror_plane) glEnable(GL_CLIP_PLANE0);
00464 
00465 
00466   
00467   renderScene(1);
00468   
00469   
00470 //if (draw_mirror_plane) glDisable(GL_CLIP_PLANE0);
00471 
00472 
00473   // Display framerate
00474 glPushMatrix();
00475   if (g_bFrameRate) {
00476     char buf[200];
00477     sprintf( buf, "%0.2f fps,  X=%f, Y=%f, Z=%f, hf=%f, alpha=%f, beta=%f ", g_fFPS, g_CameraX, g_CameraY, g_CameraZ, 
00478         gethf( int(round(g_CameraX/g_cellsize)), int(round(g_CameraY/g_cellsize)) ),
00479         g_CameraAngleX, g_CameraAngleY );
00480     glMatrixMode( GL_PROJECTION );
00481     glLoadIdentity();
00482     glMatrixMode( GL_MODELVIEW );
00483     glLoadIdentity();
00484     glDisable( GL_DEPTH_TEST );
00485     glDisable( GL_LIGHTING );
00486     glColor3f( 1.0f, 1.0f, 1.0f );
00487     glRasterPos2f( -1,-1 );
00488     glutString( buf );
00489     glEnable( GL_DEPTH_TEST );
00490   }
00491 glPopMatrix();
00492 
00493 /*//displaySMoke
00494 glMatrixMode(GL_MODELVIEW);
00495 glPushMatrix();
00496 glTranslated( 5, 5, 5);
00497 displaySmoke();
00498 glPopMatrix();
00499 */
00500   // Make sure changes appear on screen
00501   glutSwapBuffers();
00502 }
00503 
00504 
00505 // Function which is called when the window size changes
00506 void reshape(GLint width, GLint height)
00507 {
00508   g_Width = width;
00509   g_Height = height;
00510 }
00511 
00512 
00513 // Function which is called once to initialize the GL
00514 void initGL()
00515 {
00516 GLenum performance = GL_FASTEST; //GL_NICEST
00517 glHint(GL_POINT_SMOOTH_HINT, performance );
00518 glHint(GL_LINE_SMOOTH_HINT, performance );
00519 glHint(GL_POLYGON_SMOOTH_HINT, performance );
00520 glHint(GL_FOG_HINT, performance );
00521 glHint(GL_PERSPECTIVE_CORRECTION_HINT, performance );
00522 
00523 //glEnable(GL_COLOR_MATERIAL);
00524   glEnable(GL_DEPTH_TEST);
00525   glDepthFunc(GL_LESS);
00526   glShadeModel(GL_SMOOTH);
00527   glEnable(GL_LIGHTING);
00528   glEnable(GL_LIGHT0);
00529 //  glEnable(GL_LIGHT1);
00530   glDisable( GL_CULL_FACE );
00531 //  glCullFace( GL_BACK );
00532   glEnable( GL_NORMALIZE );
00533 
00534 
00535   // Fog from 0.6*visibility to visibility range
00536 /*  if (draw_fog)
00537     {
00538     glFogi( GL_FOG_MODE, GL_LINEAR );
00539     glFogf( GL_FOG_START, g_visibility * 0.6f );
00540     glFogf( GL_FOG_END, g_visibility );
00541     glFogfv( GL_FOG_COLOR, colorSky );
00542     glEnable( GL_FOG );
00543     }
00544   else glDisable(GL_FOG);
00545   */
00546   
00547   // Initialize Textures
00548   initTextures();
00549   // Load the height field
00550     printf(" Loading the height field hf.raw_1024x1024_float...");
00551     std::string hffile = std::string(g_strHeightFieldFile);
00552   initHeightField(g_hfW, g_hfH, hffile.c_str());
00553 
00554   
00555   // Initialize Materials
00556   initMaterials();
00557   
00558   // init bastian's water
00559   initWater( 32, 32 );
00560   
00561   // Load all models
00562 for (int i=0; i<MODEL_N; i++)
00563     {
00564     if ( model_in_use[i] )
00565         {
00566         model_handle[i] = addModel( model_path + model_name[i] + model_ext);
00567         if (model_handle[i] != -1)
00568                 {
00569             printf(" Models: %s is added with handle %d. \n ", model_name[i].c_str(), model_handle[i]);
00570                 }
00571         else
00572                 {
00573             printf(" Models: %s is not added due to some errors. \n ", model_name[i].c_str() );
00574                 continue;
00575                 }
00576             
00577     
00578         // Try to use Lists for drawing the complex models
00579         model_index[i] = glGenLists(1);
00580         if ( model_index[i] != 0)
00581             {
00582             model_use_list[i] = true;
00583             glNewList ( model_index[i], GL_COMPILE );
00584                 __drawModel( i );
00585             glEndList();    
00586             }
00587         else 
00588             {
00589             printf(" Can't create new display list for model %s. \n  Will use simple rendering. \n", model_name[i].c_str());
00590             model_use_list[i] = false;
00591             }//endif
00592         }//endif
00593     }//endfor
00594 
00595 
00596   // Initialize position of the camera
00597   g_CameraX = 27;
00598   g_CameraY = 80;
00599   g_CameraZ =  g_length  + gethf( int (round (g_CameraX/g_cellsize)), int (round (g_CameraY/g_cellsize)) );
00600 
00601 //init smoke
00602 //initSmoke();
00603 
00604 
00605 }
00606 
00607 
00608 
00609 
00610 
00611 /**********************************************
00612  **********************************************
00613    TIMER AND ANIMATION
00614  **********************************************
00615  *********************************************/
00616 
00617 static void timerCallback(int)
00618 {
00619 
00620 //animate();
00621   // Get time in seconds after last call to the timer
00622   int nc = glutGet(GLUT_ELAPSED_TIME);
00623   float fSec = float(nc-g_nLastTime) / 1000.0f;
00624   // Compute average frame rate from the last twenty frames
00625   g_fFPS = (19.0f*g_fFPS + 1.0f / fSec) / 20.0f;
00626   g_nLastTime = nc;
00627 
00628   // Perform all animations
00629   // Camera movement at 1/2 rounds per second
00630 /*  float fCamArc = fSec * 180.0f;
00631   if (g_bCameraUp && g_fCameraX < 90) g_fCameraX += fCamArc;
00632   if (g_bCameraDown && g_fCameraX > -90) g_fCameraX -= fCamArc;
00633   if (g_bCameraLeft) g_fCameraY += fCamArc;
00634   if (g_bCameraRight) g_fCameraY -= fCamArc;
00635 */
00636   float fCamArc = fSec * 180.0f / 1;
00637   if (g_CameraRotateUp && g_CameraAngleX > -90) g_CameraAngleX -= fCamArc;  
00638   if (g_CameraRotateDown && g_CameraAngleX < 90) g_CameraAngleX += fCamArc;  
00639   if (g_CameraRotateLeft) g_CameraAngleY -= fCamArc;  
00640   if (g_CameraRotateRight) g_CameraAngleY += fCamArc;  
00641 
00642     //water
00643     computeWater ( float(nc) / 1000.0f );
00644 
00645 
00646 /*
00647   // Rotor movement
00648   g_fPropellerAngle += float(g_nPropellerRPS) * fSec * 360.0f/2.0f;
00649 */
00650 /*
00651   // Plane rotation
00652   if (g_bRotateLeft) {
00653     g_fPlaneAngle += fSec * 360.0f / 5.0f;
00654     g_fRudderAngle = -60.0f;
00655   }
00656   else if (g_bRotateRight) {
00657     g_fPlaneAngle -= fSec * 360.0f / 5.0f;
00658     g_fRudderAngle = 60.0f;
00659   }
00660   else {
00661     g_fRudderAngle = 0.0f;
00662   }
00663 */
00664 
00665     //Movement
00666     if (go_Forward || go_Backward || go_Left || go_Right)
00667         {
00668         if (go_Right)
00669             {
00670             g_CameraY += g_cellsize * go_cells_per_step * fSec * cos (M_PI * (g_CameraAngleY - 90.0f) / 180.0f );
00671             g_CameraX -= g_cellsize * go_cells_per_step * fSec * sin (M_PI * (g_CameraAngleY - 90.0f) / 180.0f);
00672             }
00673         if (go_Left)
00674             {
00675             g_CameraY -= g_cellsize * go_cells_per_step * fSec * cos (M_PI * (g_CameraAngleY - 90.0f ) / 180.0f );
00676             g_CameraX += g_cellsize * go_cells_per_step * fSec * sin (M_PI * (g_CameraAngleY - 90.0f) / 180.0f);
00677             }
00678         if (go_Forward)
00679             {
00680             g_CameraY -= g_cellsize * go_cells_per_step * fSec * cos (M_PI * (g_CameraAngleY ) / 180.0f );
00681             g_CameraX += g_cellsize * go_cells_per_step * fSec * sin (M_PI * (g_CameraAngleY ) / 180.0f);
00682             }
00683         if (go_Backward)
00684             {
00685             g_CameraY += g_cellsize * go_cells_per_step * fSec * cos (M_PI * (g_CameraAngleY ) / 180.0f );
00686             g_CameraX -= g_cellsize * go_cells_per_step * fSec * sin (M_PI * (g_CameraAngleY ) / 180.0f);
00687             }
00688 //          if (g_CameraY < 0) g_CameraY = 0;
00689             
00690             g_CameraZ = g_length + gethf( int(round (g_CameraX/g_cellsize)) , int(round (g_CameraY/g_cellsize)) );
00691 
00692 
00693 //      g_airplaneX = g_CameraX;
00694 //      g_airplaneY = g_CameraY;
00695         }
00696 /*
00697   // Plane movement
00698   g_airplaneX += g_cellsize * g_nPropellerRPS * fSec * cos( M_PI * g_fPlaneAngle / 180.0f );
00699   g_airplaneY -= g_cellsize * g_nPropellerRPS * fSec * sin( M_PI * g_fPlaneAngle / 180.0f );
00700 */
00701   // Redraw and restart timer
00702   glutPostRedisplay();
00703   glutTimerFunc( g_nTimerDelay, timerCallback, 0);
00704 }
00705 
00706 
00707 
00708 
00709 
00710 /**********************************************
00711  **********************************************
00712    MENU AND KEYBOARD HANDLING
00713  **********************************************
00714  *********************************************/
00715 
00716 
00717 // Just to illustrate how menus are used
00718 // This callback is called when a menu item was selected
00719 void selectFromMenu(int idCommand)
00720 {
00721   switch (idCommand) {
00722   case MENU_LIGHTING:
00723     g_bLighting = !g_bLighting;
00724     break;
00725   case MENU_FRAMERATE:
00726     g_bFrameRate = !g_bFrameRate;
00727     break;
00728   case MENU_POLYMODE:
00729     g_bFillPolygons = !g_bFillPolygons;
00730     glPolygonMode (GL_FRONT_AND_BACK, g_bFillPolygons ? GL_FILL : GL_LINE);
00731     break;      
00732   case MENU_EXIT:
00733     printf( "Exiting.\n" );
00734     glutSetKeyRepeat( GLUT_KEY_REPEAT_ON );
00735     exit (0);
00736     break;
00737   }
00738 
00739   // Almost any menu selection requires a redraw
00740   glutPostRedisplay();
00741 }
00742 
00743 
00744 // Callback function called when a key was pressed
00745 void keyboard(unsigned char key, int, int )
00746 {
00747   switch (key) {
00748   case 27:             // ESCAPE key
00749     exit (0);
00750     break;
00751   case 32:
00752     //g_bChaseCam = !g_bChaseCam;
00753     break;
00754   case 'l':
00755     selectFromMenu(MENU_LIGHTING);
00756     break;
00757   case 'h':
00758     draw_height_field = !draw_height_field;
00759     break;
00760   case 'p':
00761     selectFromMenu(MENU_POLYMODE);
00762     break;
00763   case 'a':
00764   g_CameraRotateLeft = true;
00765   g_CameraRotateRight = false;
00766 //    g_bRotateLeft = true;
00767     break;
00768   case 'd':
00769       g_CameraRotateRight = true;
00770       g_CameraRotateLeft = false;
00771 //      g_bRotateRight = true;
00772         break;
00773   case 'x': 
00774         g_show_axis = !g_show_axis;
00775         break;
00776   case 's':
00777       g_CameraRotateDown = true;
00778       g_CameraRotateUp = false;
00779       break;
00780   case 'w':
00781       g_CameraRotateUp = true;
00782       g_CameraRotateDown = false;
00783       break;
00784   case 'm':
00785         draw_mirror_plane = !draw_mirror_plane;
00786         break;
00787   case 'q':
00788     printf(" Quit...\n");
00789      exit(0);
00790      break;
00791   case 'f':
00792     draw_fog = !draw_fog;
00793     break;
00794   case 'n':
00795     day = !day;
00796     
00797     if (day)
00798         {
00799         g_lightPos[0] = 30;
00800         g_lightPos[1] = 30;
00801         g_lightPos[2] = 30;
00802         }
00803     else
00804         {
00805         g_lightPos[0] = 30;
00806         g_lightPos[1] = -30;
00807         g_lightPos[2] = 30;
00808         }
00809     
00810     break;    
00811 
00812   }
00813 }
00814 
00815 void keyboardUp(unsigned char key, int,int)
00816 {
00817   switch( key ) {
00818   case 'a':
00819     g_CameraRotateLeft = false;
00820 //    g_bRotateLeft = false;
00821     break;
00822   case 'd':
00823     g_CameraRotateRight = false;
00824 //    g_bRotateRight = false;
00825     break;
00826   case 's':
00827     g_CameraRotateDown = false;
00828     break;
00829   case 'w':
00830     g_CameraRotateUp = false;
00831     break;    
00832   }
00833 }
00834 
00835 void special( int key, int,int )
00836 {
00837   switch( key ) {
00838   case GLUT_KEY_UP:
00839     go_Forward = true;
00840     go_Backward = false;
00841     break;
00842   case GLUT_KEY_DOWN:
00843     go_Forward = false;
00844     go_Backward = true;
00845     break;
00846   case GLUT_KEY_LEFT:
00847     go_Left = true;
00848     go_Right = false;
00849     break;
00850   case GLUT_KEY_RIGHT:
00851     go_Left = false;
00852     go_Right = true;
00853     break;
00854   }
00855 }
00856 
00857 
00858 void specialUp( int key, int,int )
00859 {
00860   switch( key ) {
00861   case GLUT_KEY_UP:
00862     go_Forward = false;
00863     break;
00864   case GLUT_KEY_DOWN:
00865     go_Backward = false;
00866     break;
00867   case GLUT_KEY_LEFT:
00868     go_Left = false;
00869     break;
00870   case GLUT_KEY_RIGHT:
00871     go_Right = false;
00872     break;
00873   }
00874 }
00875 
00876 
00877 
00878 
00879 
00880 // This function initializes the popup menu
00881 int buildPopupMenu()
00882 {
00883   printf( "Keys:\n" );
00884   printf( "  l - Toggle lighting\n" );
00885   printf( "  p - Toggle polygon fill\n" );
00886   printf( "  f - Display frame rate\n" );
00887   printf( "  h - Toggle heigth field\n");
00888   printf( "  m - Draw mirror plane\n");
00889   printf( "  x - Show axes\n");
00890   printf( "  f - Draw fog \n");
00891   printf( "  n - Toggle day/night\n");
00892   printf( "  ESC,q - Exit demo\n" );
00893 
00894 
00895   int menu = glutCreateMenu (selectFromMenu);
00896   glutAddMenuEntry ("Toggle lighting\tl", MENU_LIGHTING);
00897   glutAddMenuEntry ("Toggle polygon fill\tp", MENU_POLYMODE);
00898   glutAddMenuEntry ("Display frame rate\tf", MENU_FRAMERATE);
00899   glutAddMenuEntry ("Exit demo\tEsc", MENU_EXIT);
00900   return menu;
00901 }
00902 
00903 
00904 
00905 
00906 
00907 /**********************************************
00908  **********************************************
00909    MAIN FUNCTION
00910  **********************************************
00911  *********************************************/
00912 
00913 int main(int argc, char** argv)
00914 {
00915   // GLUT Window Initialization:
00916   glutInit(&argc, argv);
00917   glutInitWindowSize(g_Width, g_Height);
00918   glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
00919   glutCreateWindow("CG-I GLUT example");
00920 
00921   glutSetKeyRepeat( GLUT_KEY_REPEAT_ON );
00922   glutIgnoreKeyRepeat(0);
00923 
00924   // Initialize OpenGL graphics state
00925   initGL();
00926 
00927   // Register callbacks:
00928   glutDisplayFunc(display);
00929   glutReshapeFunc(reshape);
00930   glutKeyboardFunc(keyboard);
00931   glutKeyboardUpFunc(keyboardUp);
00932   glutSpecialFunc(special);
00933   glutSpecialUpFunc(specialUp);
00934 
00935 
00936   // Create popup menu
00937   buildPopupMenu();
00938   glutAttachMenu(GLUT_RIGHT_BUTTON);
00939 
00940   // Start display timer
00941   g_nLastTime = glutGet( GLUT_ELAPSED_TIME );
00942   glutTimerFunc( g_nTimerDelay, timerCallback, 0);
00943 
00944   // Turn the flow of control over to GLUT
00945   glutMainLoop();
00946 
00947 deleteModels();
00948 
00949   return 0;
00950 }

Generated on Thu Jan 20 02:46:58 2005 for Main_Demo by doxygen 1.3.6