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

render.cpp File Reference

#include <GL/glut.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "config.h"
#include "heightfield.h"
#include "texture.h"
#include "scene.h"
#include "water.h"
#include "materials.h"

Go to the source code of this file.

Enumerations

enum  {
  MENU_LIGHTING = 1, MENU_POLYMODE, MENU_TEXTURING, MENU_FRAMERATE,
  MENU_EXIT
}

Functions

void __draw_fog ()
void glutString (char *pstr)
void setupCamera ()
void __drawModel (int model)
void renderScene (int pass)
void display ()
void reshape (GLint width, GLint height)
void initGL ()
void timerCallback (int)
void selectFromMenu (int idCommand)
void keyboard (unsigned char key, int, int)
void keyboardUp (unsigned char key, int, int)
void special (int key, int, int)
void specialUp (int key, int, int)
int buildPopupMenu ()
int main (int argc, char **argv)

Variables

GLdouble eqn [4] = {0, 0, 1, 0}
std::string model_path = "./render/models/"
std::string model_ext = ".model"
int g_nLastTime = 0
float g_fFPS = 0
const int g_visibility = g_cells * g_cellsize
float g_nearPlane = 1
float g_farPlane = g_visibility
const double VIEWING_DISTANCE_MIN = 3.0
float g_fViewDistance = 5*VIEWING_DISTANCE_MIN
int g_Width = 512
int g_Height = 512
bool g_bFillPolygons = true
bool go_Forward = false
bool go_Backward = false
bool go_Left = false
bool go_Right = false
float go_cells_per_step = 40


Enumeration Type Documentation

anonymous enum
 

Enumeration values:
MENU_LIGHTING 
MENU_POLYMODE 
MENU_TEXTURING 
MENU_FRAMERATE 
MENU_EXIT 

Definition at line 43 of file render.cpp.

00043      {
00044   MENU_LIGHTING = 1,
00045   MENU_POLYMODE,
00046   MENU_TEXTURING,
00047   MENU_FRAMERATE,
00048   MENU_EXIT
00049 };


Function Documentation

void __draw_fog  ) 
 

Definition at line 92 of file render.cpp.

References colorDarkGreen, colorLightGreen, day, and g_visibility.

Referenced by display().

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 }

void __drawModel int  model  ) 
 

Definition at line 171 of file render.cpp.

References drawModel(), g_CameraX, g_CameraY, g_CameraZ, MATERIAL_CERAMIC, MATERIAL_CRYPT, MATERIAL_GOLD, MATERIAL_RUBY, MATERIAL_SILVER, MATERIAL_SKIN, MATERIAL_WOOD, MODEL_CASKET_CLOSED, MODEL_CRYPT, MODEL_GALLOWS, MODEL_GALLOWS_ROPE, MODEL_GILJOTINA_1, MODEL_GILJOTINA_2, MODEL_GILJOTINA_3, MODEL_GILJOTINA_4, MODEL_GRAVEYARD, model_handle, MODEL_IRON_MAIDEN, MODEL_MANCAGE_1, MODEL_MANCAGE_2, MODEL_PLAYER, and setMaterial().

Referenced by initGL(), and renderScene().

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 }

int buildPopupMenu  ) 
 

Definition at line 881 of file render.cpp.

References MENU_EXIT, MENU_FRAMERATE, MENU_LIGHTING, MENU_POLYMODE, and selectFromMenu().

Referenced by main().

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 }

void display  ) 
 

Definition at line 313 of file render.cpp.

References __draw_fog(), colorDarkGreen, colorDarkGrey, colorWhite, draw_fog, draw_mirror, draw_mirror_plane, g_bFrameRate, g_CameraAngleX, g_CameraAngleY, g_CameraX, g_CameraY, g_CameraZ, g_cellsize, g_fFPS, g_Height, g_lightPos, g_Width, gethf(), glutString(), renderScene(), setDiffuseMaterialColor(), and setupCamera().

Referenced by main().

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 }

void glutString char *  pstr  ) 
 

Definition at line 114 of file render.cpp.

Referenced by display().

00115 {
00116   while (*pstr!=char(0)) {
00117     glutBitmapCharacter( GLUT_BITMAP_8_BY_13, int( *pstr ));
00118     pstr++;
00119   }
00120 }

void initGL  ) 
 

Definition at line 514 of file render.cpp.

References __drawModel(), addModel(), g_CameraX, g_CameraY, g_CameraZ, g_cellsize, g_hfH, g_hfW, g_length, g_strHeightFieldFile, gethf(), initHeightField(), initMaterials(), initTextures(), initWater(), model_ext, model_handle, model_in_use, model_index, MODEL_N, model_name, model_path, and model_use_list.

Referenced by main().

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 }

void keyboard unsigned char  key,
int  ,
int 
 

Definition at line 745 of file render.cpp.

References day, draw_fog, draw_height_field, draw_mirror_plane, g_CameraRotateDown, g_CameraRotateLeft, g_CameraRotateRight, g_CameraRotateUp, g_lightPos, g_show_axis, MENU_LIGHTING, MENU_POLYMODE, and selectFromMenu().

Referenced by main().

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 }

void keyboardUp unsigned char  key,
int  ,
int 
 

Definition at line 815 of file render.cpp.

References g_CameraRotateDown, g_CameraRotateLeft, g_CameraRotateRight, and g_CameraRotateUp.

Referenced by main().

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 }

int main int  argc,
char **  argv
 

Definition at line 913 of file render.cpp.

References buildPopupMenu(), deleteModels(), display(), g_Height, g_nLastTime, g_nTimerDelay, g_Width, initGL(), keyboard(), keyboardUp(), reshape(), special(), specialUp(), and timerCallback().

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 }

void renderScene int  pass  ) 
 

Definition at line 255 of file render.cpp.

References __drawModel(), colorDarkBrown, draw_height_field, draw_mirror_plane, drawHeightField(), eqn, g_bLighting, g_CameraX, g_CameraY, g_cells, g_cellsize, model_in_use, model_index, MODEL_N, model_use_list, and setDiffuseMaterialColor().

Referenced by display().

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 }

void reshape GLint  width,
GLint  height
 

Definition at line 506 of file render.cpp.

References g_Height, and g_Width.

Referenced by main().

00507 {
00508   g_Width = width;
00509   g_Height = height;
00510 }

void selectFromMenu int  idCommand  ) 
 

Definition at line 719 of file render.cpp.

References g_bFillPolygons, g_bFrameRate, g_bLighting, MENU_EXIT, MENU_FRAMERATE, MENU_LIGHTING, and MENU_POLYMODE.

Referenced by buildPopupMenu(), and keyboard().

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 }

void setupCamera  ) 
 

Definition at line 130 of file render.cpp.

References g_CameraAngleX, g_CameraAngleY, g_CameraX, g_CameraY, g_CameraZ, g_farPlane, g_Height, g_nearPlane, and g_Width.

Referenced by display().

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 }

void special int  key,
int  ,
int 
 

Definition at line 835 of file render.cpp.

References go_Backward, go_Forward, go_Left, and go_Right.

Referenced by main().

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 }

void specialUp int  key,
int  ,
int 
 

Definition at line 858 of file render.cpp.

References go_Backward, go_Forward, go_Left, and go_Right.

Referenced by main().

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 }

void timerCallback int   )  [static]
 

Definition at line 617 of file render.cpp.

References computeWater(), g_CameraAngleX, g_CameraAngleY, g_CameraRotateDown, g_CameraRotateLeft, g_CameraRotateRight, g_CameraRotateUp, g_CameraX, g_CameraY, g_CameraZ, g_cellsize, g_fFPS, g_length, g_nLastTime, g_nTimerDelay, gethf(), go_Backward, go_cells_per_step, go_Forward, go_Left, go_Right, and timerCallback().

Referenced by main(), and timerCallback().

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 }


Variable Documentation

GLdouble eqn[4] = {0, 0, 1, 0}
 

Definition at line 40 of file render.cpp.

Referenced by renderScene().

bool g_bFillPolygons = true [static]
 

Definition at line 81 of file render.cpp.

Referenced by selectFromMenu().

float g_farPlane = g_visibility [static]
 

Definition at line 70 of file render.cpp.

Referenced by setupCamera().

float g_fFPS = 0 [static]
 

Definition at line 63 of file render.cpp.

Referenced by display(), and timerCallback().

float g_fViewDistance = 5*VIEWING_DISTANCE_MIN
 

Definition at line 73 of file render.cpp.

int g_Height = 512 [static]
 

Definition at line 78 of file render.cpp.

Referenced by display(), main(), reshape(), and setupCamera().

float g_nearPlane = 1 [static]
 

Definition at line 69 of file render.cpp.

Referenced by setupCamera().

int g_nLastTime = 0 [static]
 

Definition at line 61 of file render.cpp.

Referenced by main(), and timerCallback().

const int g_visibility = g_cells * g_cellsize [static]
 

Definition at line 66 of file render.cpp.

Referenced by __draw_fog().

int g_Width = 512 [static]
 

Definition at line 77 of file render.cpp.

Referenced by display(), main(), reshape(), and setupCamera().

bool go_Backward = false [static]
 

Definition at line 86 of file render.cpp.

Referenced by special(), specialUp(), and timerCallback().

float go_cells_per_step = 40 [static]
 

Definition at line 90 of file render.cpp.

Referenced by timerCallback().

bool go_Forward = false [static]
 

Definition at line 85 of file render.cpp.

Referenced by special(), specialUp(), and timerCallback().

bool go_Left = false [static]
 

Definition at line 87 of file render.cpp.

Referenced by special(), specialUp(), and timerCallback().

bool go_Right = false [static]
 

Definition at line 88 of file render.cpp.

Referenced by special(), specialUp(), and timerCallback().

std::string model_ext = ".model" [static]
 

Definition at line 54 of file render.cpp.

Referenced by initGL().

std::string model_path = "./render/models/" [static]
 

Definition at line 53 of file render.cpp.

Referenced by initGL().

const double VIEWING_DISTANCE_MIN = 3.0 [static]
 

Definition at line 72 of file render.cpp.


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