#include <GL/glut.h>#include <GL/glu.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#include "config.h"#include "torus.h"#include "texture.h"#include "heightfield.h"Go to the source code of this file.
Enumerations | |
| enum | { MENU_LIGHTING = 1, MENU_POLYMODE, MENU_TEXTURING, MENU_FRAMERATE, MENU_EXIT } |
Functions | |
| void | glutString (char *pstr) |
| void | computeCameraPosition (float *eyePoint) |
| void | computeLightPosition (const float *lightPosWorld, float *lightPosEye) |
| void | setupCamera () |
| void | renderScene () |
| void | display () |
| void | reshape (GLint width, GLint height) |
| void | initGL () |
| void | timerCallback (int) |
| void | selectFromMenu (int idCommand) |
| float | sExpFunc (int n) |
| 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 | |
| int | g_nLastTime = 0 |
| float | g_fFPS = 0 |
| float | g_nearPlane = 1 |
| float | g_farPlane = 10 |
| const double | VIEWING_DISTANCE_MIN = 1 |
| float | g_fViewDistance = 5*VIEWING_DISTANCE_MIN |
| int | g_Width = 256 |
| int | g_Height = 256 |
| bool | g_bFillPolygons = true |
| bool | g_bRotateLeft = false |
| bool | g_bRotateRight = false |
| bool | g_bRotateDown = false |
| bool | g_bRotateUp = false |
| bool | g_bCameraUp = false |
| bool | g_bCameraDown = false |
| bool | g_bCameraLeft = false |
| bool | g_bCameraRight = false |
| float | g_fCameraX = 0 |
| float | g_fCameraY = 0 |
| float | g_eyePoint [4] |
|
|
Definition at line 21 of file sheet06.cpp.
00021 {
00022 MENU_LIGHTING = 1,
00023 MENU_POLYMODE,
00024 MENU_TEXTURING,
00025 MENU_FRAMERATE,
00026 MENU_EXIT
00027 };
|
|
|
Definition at line 497 of file sheet06.cpp. References MENU_EXIT, MENU_FRAMERATE, MENU_POLYMODE, and selectFromMenu(). Referenced by main().
00498 {
00499 printf( "Keys:\n" );
00500
00501 // printf( " F1 - Per Pixel Lighting ON\n" );
00502 // printf( " F2 - Per Pixel Lighting OFF\n" );
00503
00504 // printf( " F3 - Specular Term ON\n" );
00505 // printf( " F4 - Specular Tern OFF\n\n" );
00506
00507 // printf( " 0-9 - Set specular exponent to n^2/4\n\n" );
00508
00509 // printf( " Cursor keys - rotate camera\n" );
00510 // printf( " A,S,D,W - rotate Torus\n\n" );
00511
00512 printf( " p - Toggle polygon fill\n" );
00513 printf( " f - Display frame rate\n" );
00514 printf( " ESC - Exit demo\n" );
00515
00516
00517 int menu = glutCreateMenu (selectFromMenu);
00518 glutAddMenuEntry ("Toggle polygon fill\tp", MENU_POLYMODE);
00519 glutAddMenuEntry ("Display frame rate\tf", MENU_FRAMERATE);
00520 glutAddMenuEntry ("Exit demo\tEsc", MENU_EXIT);
00521 return menu;
00522 }
|
|
|
Definition at line 89 of file sheet06.cpp. References g_farPlane, and g_nearPlane.
00090 {
00091 // Projected eye position:
00092 // In the projection step (gluProject(), the eye position is mapped from (0,0,0) to
00093 // (0,0, - (n*f) / (f-n)), where n and f are near and far plane.
00094 float eyeProject[4];
00095 eyeProject[0] = 0;
00096 eyeProject[1] = 0;
00097 eyeProject[2] = - (g_nearPlane * g_farPlane) / (g_farPlane - g_nearPlane);
00098 eyeProject[3] = 0;
00099
00100 // Multiply projected eye position with inverse projection to get
00101 // eye point of the camera in world coordinates
00102 float modelviewT[16];
00103 glGetFloatv( GL_TRANSPOSE_MODELVIEW_MATRIX, modelviewT );
00104 float projectionT[16];
00105 glGetFloatv( GL_TRANSPOSE_PROJECTION_MATRIX, projectionT );
00106
00107 // Compute inverse matrices
00108 float inverse_projection[16];
00109 float inverse_modelview[16];
00110 Invert<float> (projectionT, inverse_projection);
00111 Invert<float> (modelviewT, inverse_modelview);
00112
00113 float eyeCamera[4];
00114 Multiply<float> (inverse_projection, eyeProject, eyeCamera );
00115 Multiply<float> (inverse_modelview, eyeCamera, eyePoint );
00116 }
|
|
||||||||||||
|
Definition at line 122 of file sheet06.cpp.
00123 {
00124 // Now that the camera transformation is set up, the current modelview
00125 // matrix transforms world coordinates to eye coordinates, and is
00126 // the correct transformation matrix for the light sources
00127 float projection[16];
00128 glGetFloatv( GL_TRANSPOSE_MODELVIEW_MATRIX, projection );
00129 Multiply<float> (projection, lightPosWorld, lightPosEye );
00130 }
|
|
|
Definition at line 191 of file sheet06.cpp. References colorDarkGrey, colorSky, colorWhite, g_bFrameRate, g_farPlane, g_fFPS, g_Height, g_lightPos, g_Width, glutString(), ModelViewMatrix, renderScene(), and setupCamera(). Referenced by main().
00192 {
00193 // Clear frame buffer and depth buffer
00194 glClearColor( colorSky[0], colorSky[1], colorSky[2], colorSky[3] );
00195 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00196
00197 // Setup camera and initialize modelview matrix stack
00198 setupCamera();
00199 glGetFloatv(GL_MODELVIEW_MATRIX, ModelViewMatrix);
00200
00201 glViewport(0, 0, g_Width, g_Height);
00202
00203 // Set up the stationary light (after camera transform)
00204 glLightfv(GL_LIGHT0, GL_POSITION, g_lightPos);
00205 glLightfv(GL_LIGHT0, GL_DIFFUSE, colorWhite );
00206 glLightfv(GL_LIGHT0, GL_AMBIENT, colorDarkGrey );
00207 glLightfv(GL_LIGHT0, GL_SPECULAR, colorWhite );
00208
00209 // Fog from 0.6*visibility to visibility range
00210 glFogi( GL_FOG_MODE, GL_LINEAR );
00211 glFogf( GL_FOG_START, g_farPlane * 0.6f );
00212 glFogf( GL_FOG_END, g_farPlane );
00213 glFogfv( GL_FOG_COLOR, colorSky );
00214 glEnable( GL_FOG );
00215
00216 // Render the scene
00217 renderScene();
00218
00219
00220 // Display framerate
00221 if (g_bFrameRate) {
00222 char buf[100];
00223 sprintf( buf, "%0.2f fps", g_fFPS );
00224 glMatrixMode( GL_PROJECTION );
00225 glLoadIdentity();
00226 glMatrixMode( GL_MODELVIEW );
00227 glLoadIdentity();
00228 glDisable( GL_DEPTH_TEST );
00229 glDisable( GL_LIGHTING );
00230 glColor3f( 1.0f, 1.0f, 1.0f );
00231 glRasterPos2f( -1,-1 );
00232 glutString( buf );
00233 glEnable( GL_DEPTH_TEST );
00234 }
00235
00236 // Make sure changes appear onscreen
00237 glutSwapBuffers();
00238 }
|
|
|
Definition at line 75 of file sheet06.cpp. Referenced by display().
00076 {
00077 while (*pstr!=char(0)) {
00078 glutBitmapCharacter( GLUT_BITMAP_8_BY_13, int( *pstr ));
00079 pstr++;
00080 }
00081 }
|
|
|
Definition at line 250 of file sheet06.cpp. References g_hfH, g_hfW, g_strHeightFieldFile, initHeightField(), initTextures(), and initTorus(). Referenced by main().
00251 {
00252 glEnable(GL_DEPTH_TEST);
00253 glDepthFunc(GL_LESS);
00254 glShadeModel(GL_SMOOTH);
00255 glEnable(GL_LIGHTING);
00256 glEnable(GL_LIGHT0);
00257 glEnable( GL_CULL_FACE );
00258 glCullFace( GL_BACK );
00259 glEnable( GL_NORMALIZE );
00260
00261 // Init Cg
00262 initTorus();
00263 // Load the textures
00264 initTextures();
00265
00266 // Load the height field
00267 printf( "Loading height field %s, size %i x %i ... ",
00268 g_strHeightFieldFile, g_hfW, g_hfH );
00269 initHeightField();
00270 printf( "success.\n" );
00271 }
|
|
||||||||||||||||
|
Definition at line 365 of file sheet06.cpp. References g_bRotateDown, g_bRotateLeft, g_bRotateRight, g_bRotateUp, g_specularExponent, MENU_FRAMERATE, MENU_LIGHTING, MENU_POLYMODE, selectFromMenu(), and sExpFunc(). Referenced by main().
00366 {
00367 switch (key) {
00368 case 27: // ESCAPE key
00369 exit (0);
00370 break;
00371 case 'l':
00372 selectFromMenu(MENU_LIGHTING);
00373 break;
00374 case 'f':
00375 selectFromMenu(MENU_FRAMERATE);
00376 break;
00377 case 'p':
00378 selectFromMenu(MENU_POLYMODE);
00379 break;
00380 case '1':
00381 g_specularExponent = sExpFunc( 1 );
00382 break;
00383 case '2':
00384 g_specularExponent = sExpFunc( 2 );
00385 break;
00386 case '3':
00387 g_specularExponent = sExpFunc( 3 );
00388 break;
00389 case '4':
00390 g_specularExponent = sExpFunc( 4 );
00391 break;
00392 case '5':
00393 g_specularExponent = sExpFunc( 5 );
00394 break;
00395 case '6':
00396 g_specularExponent = sExpFunc( 6 );
00397 break;
00398 case '7':
00399 g_specularExponent = sExpFunc( 7 );
00400 break;
00401 case '8':
00402 g_specularExponent = sExpFunc( 8 );
00403 break;
00404 case '9':
00405 g_specularExponent = sExpFunc( 9 );
00406 break;
00407 case '0':
00408 g_specularExponent = sExpFunc( 0 );
00409 break;
00410 case 'a':
00411 g_bRotateLeft = true;
00412 break;
00413 case 'd':
00414 g_bRotateRight = true;
00415 break;
00416 case 's':
00417 g_bRotateUp = true;
00418 break;
00419 case 'w':
00420 g_bRotateDown = true;
00421 break;
00422 }
00423 }
|
|
||||||||||||||||
|
Definition at line 425 of file sheet06.cpp. References g_bRotateDown, g_bRotateLeft, g_bRotateRight, and g_bRotateUp. Referenced by main().
00426 {
00427 switch( key ) {
00428 case 'a':
00429 g_bRotateLeft = false;
00430 break;
00431 case 'd':
00432 g_bRotateRight = false;
00433 break;
00434 case 's':
00435 g_bRotateUp = false;
00436 break;
00437 case 'w':
00438 g_bRotateDown = false;
00439 break;
00440 }
00441 }
|
|
||||||||||||
|
Definition at line 534 of file sheet06.cpp. References buildPopupMenu(), display(), g_Height, g_nLastTime, g_nTimerDelay, g_Width, initGL(), keyboard(), keyboardUp(), reshape(), special(), specialUp(), and timerCallback().
00535 {
00536 // GLUT Window Initialization:
00537 glutInit(&argc, argv);
00538 glutInitWindowSize(g_Width, g_Height);
00539 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
00540 glutCreateWindow("CG-I GLUT example");
00541
00542 glutSetKeyRepeat( GLUT_KEY_REPEAT_ON );
00543 glutIgnoreKeyRepeat(0);
00544
00545 // Initialize OpenGL graphics state
00546 initGL();
00547
00548 // Register callbacks:
00549 glutDisplayFunc(display);
00550 glutReshapeFunc(reshape);
00551 glutKeyboardFunc(keyboard);
00552 glutKeyboardUpFunc(keyboardUp);
00553 glutSpecialFunc(special);
00554 glutSpecialUpFunc(specialUp);
00555
00556
00557 // Create popup menu
00558 buildPopupMenu();
00559 glutAttachMenu(GLUT_RIGHT_BUTTON);
00560
00561 // Start display timer
00562 g_nLastTime = glutGet( GLUT_ELAPSED_TIME );
00563 glutTimerFunc( g_nTimerDelay, timerCallback, 0);
00564
00565 // Turn the flow of control over to GLUT
00566 glutMainLoop();
00567 return 0;
00568 }
|
|
|
Definition at line 164 of file sheet06.cpp. References drawHeightField(), drawTorus(), g_bLighting, g_eyePoint, and g_farPlane. Referenced by display().
00165 {
00166 if (g_bLighting) {
00167 glEnable(GL_LIGHTING);
00168 }
00169 else {
00170 glDisable(GL_LIGHTING);
00171 }
00172
00173 // Draw torus
00174 glPushMatrix();
00175 // glRotatef( g_fTorusX, 0,1,0 );
00176 // glRotatef( g_fTorusY, 1,0,0 );
00177 drawTorus( g_eyePoint );
00178 glPopMatrix();
00179
00180
00181
00182 // Draw ground
00183 glPushMatrix();
00184 glTranslatef( 0,-5,0 );
00185 glRotatef( -90, 1,0,0 );
00186 drawHeightField( 0,0, (int)g_farPlane, (int)g_farPlane );
00187 glPopMatrix();
00188 }
|
|
||||||||||||
|
Definition at line 242 of file sheet06.cpp. References g_Height, and g_Width. Referenced by main().
|
|
|
Definition at line 333 of file sheet06.cpp. References g_bFillPolygons, g_bFrameRate, g_bLighting, MENU_EXIT, MENU_FRAMERATE, MENU_LIGHTING, and MENU_POLYMODE. Referenced by buildPopupMenu(), and keyboard().
00334 {
00335 switch (idCommand) {
00336 case MENU_LIGHTING:
00337 g_bLighting = !g_bLighting;
00338 break;
00339 case MENU_FRAMERATE:
00340 g_bFrameRate = !g_bFrameRate;
00341 break;
00342 case MENU_POLYMODE:
00343 g_bFillPolygons = !g_bFillPolygons;
00344 glPolygonMode (GL_FRONT_AND_BACK, g_bFillPolygons ? GL_FILL : GL_LINE);
00345 break;
00346 case MENU_EXIT:
00347 printf( "Exiting.\n" );
00348 glutSetKeyRepeat( GLUT_KEY_REPEAT_ON );
00349 exit (0);
00350 break;
00351 }
00352
00353 // Almost any menu selection requires a redraw
00354 glutPostRedisplay();
00355 }
|
|
|
Definition at line 140 of file sheet06.cpp. References computeLightPosition(), g_farPlane, g_fCameraX, g_fCameraY, g_fViewDistance, g_Height, g_lightPos, g_lightPosEye, g_nearPlane, and g_Width. Referenced by display().
00141 {
00142 // Setup projection with field of view of 65 degrees
00143 glMatrixMode(GL_PROJECTION);
00144 glLoadIdentity();
00145 gluPerspective(65.0, (float)g_Width / g_Height, g_nearPlane, g_farPlane);
00146
00147 // Initializa modelview
00148 glMatrixMode(GL_MODELVIEW);
00149 glLoadIdentity();
00150
00151 // Set up viewing transformation, looking down -Z axis
00152 gluLookAt(0, 0, -g_fViewDistance, 0, 0, -1, 0, 1, 0);
00153
00154 // Free camera mode
00155 glRotatef( g_fCameraX, 1,0,0 );
00156 glRotatef( g_fCameraY, 0,1,0 );
00157
00158 // Compute the position of the light source in eye coordinates
00159 computeLightPosition( g_lightPos, g_lightPosEye );
00160 }
|
|
|
Definition at line 358 of file sheet06.cpp. Referenced by keyboard().
00359 {
00360 return float(n*n) / 2.0f + 0.01f;
00361 }
|
|
||||||||||||||||
|
Definition at line 443 of file sheet06.cpp. References g_bBlinnPhong, g_bCameraDown, g_bCameraLeft, g_bCameraRight, g_bCameraUp, and g_bPerPixelLighting. Referenced by main().
00444 {
00445 switch( key ) {
00446 case GLUT_KEY_F1:
00447 g_bPerPixelLighting = true;
00448 break;
00449 case GLUT_KEY_F2:
00450 g_bPerPixelLighting = false;
00451 break;
00452 case GLUT_KEY_F3:
00453 g_bBlinnPhong = true;
00454 break;
00455 case GLUT_KEY_F4:
00456 g_bBlinnPhong = false;
00457 break;
00458 case GLUT_KEY_UP:
00459 g_bCameraUp = true;
00460 break;
00461 case GLUT_KEY_DOWN:
00462 g_bCameraDown = true;
00463 break;
00464 case GLUT_KEY_LEFT:
00465 g_bCameraLeft = true;
00466 break;
00467 case GLUT_KEY_RIGHT:
00468 g_bCameraRight = true;
00469 break;
00470 }
00471 }
|
|
||||||||||||||||
|
Definition at line 474 of file sheet06.cpp. References g_bCameraDown, g_bCameraLeft, g_bCameraRight, and g_bCameraUp. Referenced by main().
00475 {
00476 switch( key ) {
00477 case GLUT_KEY_UP:
00478 g_bCameraUp = false;
00479 break;
00480 case GLUT_KEY_DOWN:
00481 g_bCameraDown = false;
00482 break;
00483 case GLUT_KEY_LEFT:
00484 g_bCameraLeft = false;
00485 break;
00486 case GLUT_KEY_RIGHT:
00487 g_bCameraRight = false;
00488 break;
00489 }
00490 }
|
|
|
Definition at line 283 of file sheet06.cpp. References g_bRotateDown, g_bRotateLeft, g_bRotateRight, g_bRotateUp, g_fFPS, g_fTorusX, g_fTorusY, g_nLastTime, g_nTimerDelay, and timerCallback(). Referenced by main(), and timerCallback().
00284 {
00285 // Get time in seconds after last call to the timer
00286 int nc = glutGet(GLUT_ELAPSED_TIME);
00287 float fSec = float(nc-g_nLastTime) / 1000.0f;
00288 // Compute average frame rate from the last twenty frames
00289 g_fFPS = (19.0f*g_fFPS + 1.0f / fSec) / 20.0f;
00290 g_nLastTime = nc;
00291
00292 // Perform all animations
00293 // Camera movement at 1/2 rounds per second
00294 /* float fCamArc = fSec * 180.0f;
00295 if (g_bCameraUp && g_fCameraX < 90) g_fCameraX += fCamArc;
00296 if (g_bCameraDown && g_fCameraX > -90) g_fCameraX -= fCamArc;
00297 if (g_bCameraLeft) g_fCameraY += fCamArc;
00298 if (g_bCameraRight) g_fCameraY -= fCamArc;
00299 */
00300 // Plane rotation
00301 if (g_bRotateLeft) {
00302 g_fTorusX += fSec * 360.0f / 5.0f;
00303 }
00304 else if (g_bRotateRight) {
00305 g_fTorusX -= fSec * 360.0f / 5.0f;
00306 }
00307 if (g_bRotateUp) {
00308 g_fTorusY += fSec * 360.0f / 5.0f;
00309 }
00310 else if (g_bRotateDown) {
00311 g_fTorusY -= fSec * 360.0f / 5.0f;
00312 }
00313
00314
00315 // Redraw and restart timer
00316 glutPostRedisplay();
00317 glutTimerFunc( g_nTimerDelay, timerCallback, 0);
00318 }
|
|
|
Definition at line 58 of file sheet06.cpp. Referenced by special(), and specialUp(). |
|
|
Definition at line 59 of file sheet06.cpp. Referenced by special(), and specialUp(). |
|
|
Definition at line 60 of file sheet06.cpp. Referenced by special(), and specialUp(). |
|
|
Definition at line 57 of file sheet06.cpp. Referenced by special(), and specialUp(). |
|
|
Definition at line 47 of file sheet06.cpp. Referenced by selectFromMenu(). |
|
|
Definition at line 53 of file sheet06.cpp. Referenced by keyboard(), keyboardUp(), and timerCallback(). |
|
|
Definition at line 51 of file sheet06.cpp. Referenced by keyboard(), keyboardUp(), and timerCallback(). |
|
|
Definition at line 52 of file sheet06.cpp. Referenced by keyboard(), keyboardUp(), and timerCallback(). |
|
|
Definition at line 54 of file sheet06.cpp. Referenced by keyboard(), keyboardUp(), and timerCallback(). |
|
|
Definition at line 65 of file sheet06.cpp. Referenced by renderScene(). |
|
|
Definition at line 36 of file sheet06.cpp. Referenced by computeCameraPosition(), display(), renderScene(), and setupCamera(). |
|
|
Definition at line 61 of file sheet06.cpp. Referenced by setupCamera(). |
|
|
Definition at line 62 of file sheet06.cpp. Referenced by setupCamera(). |
|
|
Definition at line 32 of file sheet06.cpp. Referenced by display(), and timerCallback(). |
|
|
Definition at line 39 of file sheet06.cpp. Referenced by setupCamera(). |
|
|
Definition at line 44 of file sheet06.cpp. Referenced by display(), main(), reshape(), and setupCamera(). |
|
|
Definition at line 35 of file sheet06.cpp. Referenced by computeCameraPosition(), and setupCamera(). |
|
|
Definition at line 30 of file sheet06.cpp. Referenced by main(), and timerCallback(). |
|
|
Definition at line 43 of file sheet06.cpp. Referenced by display(), main(), reshape(), and setupCamera(). |
|
|
Definition at line 38 of file sheet06.cpp. |
1.3.6