|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : Shadows.cpp 00004 * Part of : OpenGLEx / Shadows 00005 * 00006 * Copyright (c) 2004-2006 Nokia Corporation. 00007 * This material, including documentation and any related 00008 * computer programs, is protected by copyright controlled by 00009 * Nokia Corporation. 00010 * ============================================================================== 00011 */ 00012 00013 // INCLUDES 00014 #include <e32std.h> 00015 #include <e32math.h> 00016 00017 #include "Shadows.h" 00018 00019 // LOCAL MACROS 00020 #define MATERIAL_MAX (1 << 16) 00021 #define LIGHT_MAX (1 << 16) 00022 00023 #define MATERIALCOLOR(r, g, b, a) \ 00024 (GLfixed)(r * MATERIAL_MAX), \ 00025 (GLfixed)(g * MATERIAL_MAX), \ 00026 (GLfixed)(b * MATERIAL_MAX), \ 00027 (GLfixed)(a * MATERIAL_MAX) 00028 00029 #define LIGHTCOLOR(r, g, b, a) \ 00030 (GLfixed)(r * LIGHT_MAX), \ 00031 (GLfixed)(g * LIGHT_MAX), \ 00032 (GLfixed)(b * LIGHT_MAX), \ 00033 (GLfixed)(a * LIGHT_MAX) 00034 00035 00036 // CONSTANTS 00037 /** Vertice coordinates for the ground plane */ 00038 static const GLbyte planeVertices[4 * 3] = 00039 { 00040 1,-1,0, 00041 1,1,0, 00042 -1,1,0, 00043 -1,-1,0 00044 }; 00045 00046 /** Indices for drawing the triangles of the ground plane */ 00047 static const GLubyte planeTriangles[2 * 3] = 00048 { 00049 0,1,2, 00050 0,2,3 00051 }; 00052 00053 /** Vertice coordinates for the shadow object */ 00054 static const GLbyte objVertices[48 * 3] = 00055 { 00056 /* Front */ 00057 /* Outer ring */ 00058 0,0,0, // 0 00059 0,10,0, // 1 00060 0,10,10, // 2 00061 0,0,10, // 3 00062 /* Inner ring */ 00063 0,2,2, // 4 00064 0,8,2, // 5 00065 0,8,8, // 6 00066 0,2,8, // 7 00067 00068 /* Right */ 00069 /* Outer ring */ 00070 0,10,0, // 8 00071 -10,10,0, // 9 00072 -10,10,10, // 10 00073 0,10,10, // 11 00074 /* Inner ring */ 00075 -2,10,2, // 12 00076 -8,10,2, // 13 00077 -8,10,8, // 14 00078 -2,10,8, // 15 00079 00080 /* Back */ 00081 /* Outer ring */ 00082 -10,10,0, // 16 00083 -10,0,0, // 17 00084 -10,0,10, // 18 00085 -10,10,10, // 19 00086 /* Inner ring */ 00087 -10,8,2, // 20 00088 -10,2,2, // 21 00089 -10,2,8, // 22 00090 -10,8,8, // 23 00091 00092 /* Left */ 00093 /* Outer ring */ 00094 -10,0,0, // 24 00095 0,0,0, // 25 00096 0,0,10, // 26 00097 -10,0,10, // 27 00098 /* Inner ring */ 00099 -8,0,2, // 28 00100 -2,0,2, // 29 00101 -2,0,8, // 30 00102 -8,0,8, // 31 00103 00104 /* Top */ 00105 /* Outer ring */ 00106 0,0,10, // 32 00107 0,10,10, // 33 00108 -10,10,10, // 34 00109 -10,0,10, // 35 00110 /* Inner ring */ 00111 -2,2,10, // 36 00112 -2,8,10, // 37 00113 -8,8,10, // 38 00114 -8,2,10, // 39 00115 00116 /* Bottom */ 00117 /* Outer ring */ 00118 -10,0,0, // 40 00119 -10,10,0, // 41 00120 0,10,0, // 42 00121 0,0,0, // 43 00122 /* Inner ring */ 00123 -8,2,0, // 44 00124 -8,8,0, // 45 00125 -2,8,0, // 46 00126 -2,2,0 // 47 00127 }; 00128 00129 /** Indices for drawing the triangles of the shadow object */ 00130 static const GLubyte objTriangles[48 * 3] = 00131 { 00132 /* Front */ 00133 0,5,4, 00134 0,1,5, 00135 1,6,5, 00136 1,2,6, 00137 2,7,6, 00138 2,3,7, 00139 3,4,7, 00140 3,0,4, 00141 /* Right */ 00142 8,13,12, 00143 8,9,13, 00144 9,14,13, 00145 9,10,14, 00146 10,15,14, 00147 10,11,15, 00148 11,12,15, 00149 11,8,12, 00150 /* Back */ 00151 16,21,20, 00152 16,17,21, 00153 17,22,21, 00154 17,18,22, 00155 18,23,22, 00156 18,19,23, 00157 19,20,23, 00158 19,16,20, 00159 /* Left */ 00160 24,29,28, 00161 24,25,29, 00162 25,30,29, 00163 25,26,30, 00164 26,31,30, 00165 26,27,31, 00166 27,28,31, 00167 27,24,28, 00168 /* Top */ 00169 32,37,36, 00170 32,33,37, 00171 33,38,37, 00172 33,34,38, 00173 34,39,38, 00174 34,35,39, 00175 35,36,39, 00176 35,32,36, 00177 /* Bottom */ 00178 40,45,44, 00179 40,41,45, 00180 41,46,45, 00181 41,42,46, 00182 42,47,46, 00183 42,43,47, 00184 43,44,47, 00185 43,40,44 00186 00187 }; 00188 00189 /** Normals for the shadow object */ 00190 static const GLbyte objNormals[48 * 3] = 00191 { 00192 /* Front */ 00193 1,0,0, 00194 1,0,0, 00195 1,0,0, 00196 1,0,0, 00197 1,0,0, 00198 1,0,0, 00199 1,0,0, 00200 1,0,0, 00201 /* Right */ 00202 0,1,0, 00203 0,1,0, 00204 0,1,0, 00205 0,1,0, 00206 0,1,0, 00207 0,1,0, 00208 0,1,0, 00209 0,1,0, 00210 /* Back */ 00211 -1,0,0, 00212 -1,0,0, 00213 -1,0,0, 00214 -1,0,0, 00215 -1,0,0, 00216 -1,0,0, 00217 -1,0,0, 00218 -1,0,0, 00219 /* Left */ 00220 0,-1,0, 00221 0,-1,0, 00222 0,-1,0, 00223 0,-1,0, 00224 0,-1,0, 00225 0,-1,0, 00226 0,-1,0, 00227 0,-1,0, 00228 /* Top */ 00229 0,0,1, 00230 0,0,1, 00231 0,0,1, 00232 0,0,1, 00233 0,0,1, 00234 0,0,1, 00235 0,0,1, 00236 0,0,1, 00237 /* Bottom */ 00238 0,0,-1, 00239 0,0,-1, 00240 0,0,-1, 00241 0,0,-1, 00242 0,0,-1, 00243 0,0,-1, 00244 0,0,-1, 00245 0,0,-1 00246 }; 00247 00248 00249 /** Diffuse material definition for the shadow object. */ 00250 static const GLfixed objDiffuseFront[4] = { MATERIALCOLOR(1.0, 0.5, 0.0, 1.0) }; 00251 /** Ambient material definition for the shadow object. */ 00252 static const GLfixed objAmbientFront[4] = { MATERIALCOLOR(1.0, 0.5, 0.0, 1.0) }; 00253 00254 /** Global ambient light. */ 00255 static const GLfixed globalAmbient[4] = { LIGHTCOLOR(0.2, 0.2, 0.2, 1.0) }; 00256 00257 /** Lamp diffuse parameters. */ 00258 static const GLfixed lightDiffuseLamp[4] = { LIGHTCOLOR(0.7, 0.7, 0.7, 1.0) }; 00259 /** Lamp ambient parameters. */ 00260 static const GLfixed lightAmbientLamp[4] = { LIGHTCOLOR(0.3, 0.3, 0.3, 1.0) }; 00261 00262 00263 00264 // ============================ MEMBER FUNCTIONS =============================== 00265 00266 // ----------------------------------------------------------------------------- 00267 // CShadows::CShadows 00268 // C++ default constructor can NOT contain any code, that 00269 // might leave. 00270 // ----------------------------------------------------------------------------- 00271 // 00272 CShadows::CShadows( TUint aWidth, TUint aHeight ) 00273 :iScreenWidth(aWidth), iScreenHeight(aHeight) 00274 { 00275 } 00276 00277 00278 // ----------------------------------------------------------------------------- 00279 // CShadows::ConstructL 00280 // Symbian 2nd phase constructor can leave. 00281 // ----------------------------------------------------------------------------- 00282 // 00283 void CShadows::ConstructL( void ) 00284 { 00285 } 00286 00287 00288 // ----------------------------------------------------------------------------- 00289 // CShadows::NewL 00290 // Two-phased constructor. 00291 // ----------------------------------------------------------------------------- 00292 // 00293 CShadows* CShadows::NewL( TUint aWidth, TUint aHeight ) 00294 { 00295 /* Symbian 2-phase constructor. Calls both the default 00296 C++ constructor and Symbian ConstructL methods */ 00297 CShadows* self = new (ELeave) CShadows( aWidth, aHeight ); 00298 CleanupStack::PushL( self ); 00299 self->ConstructL(); 00300 CleanupStack::Pop(); 00301 00302 return self; 00303 } 00304 00305 00306 // Destructor. 00307 CShadows::~CShadows() 00308 { 00309 } 00310 00311 00312 // ----------------------------------------------------------------------------- 00313 // CShadows::AppInit 00314 // Initialize OpenGL ES. 00315 // ----------------------------------------------------------------------------- 00316 // 00317 void CShadows::AppInit( void ) 00318 { 00319 // Initialize viewport and projection. 00320 SetScreenSize( iScreenWidth, iScreenHeight ); 00321 00322 // Set the screen background color. 00323 glClearColor( 0.f, 0.4f, 0.7f, 1.f ); 00324 00325 // Enable auto normalization for object's normals. 00326 glEnable( GL_NORMALIZE ); 00327 00328 // Enable vertex and normal arrays. 00329 glEnableClientState( GL_VERTEX_ARRAY ); 00330 glEnableClientState( GL_NORMAL_ARRAY ); 00331 00332 // Set shadow object's front and back materials. 00333 glMaterialxv( GL_FRONT_AND_BACK, GL_AMBIENT, objAmbientFront ); 00334 glMaterialxv( GL_FRONT_AND_BACK, GL_DIFFUSE, objDiffuseFront ); 00335 00336 // Set the normal pointer. 00337 glNormalPointer( GL_BYTE, 0, objNormals ); 00338 00339 // Set up global ambient light. 00340 glLightModelxv( GL_LIGHT_MODEL_AMBIENT, globalAmbient ); 00341 00342 // Do not use perspective correction 00343 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00344 00345 // Set up lamp. 00346 glEnable( GL_LIGHT0 ); 00347 glLightxv( GL_LIGHT0, GL_DIFFUSE, lightDiffuseLamp ); 00348 glLightxv( GL_LIGHT0, GL_AMBIENT, lightAmbientLamp ); 00349 glLightxv( GL_LIGHT0, GL_SPECULAR, lightDiffuseLamp ); 00350 00351 glPointSize( 5.0 ); // For drawing the lamp object 00352 00353 // Initial position of the lamp 00354 iLightRadius = 10.0; 00355 iLightAngle = 0.0; 00356 iLightPosX = iLightRadius * cos(iLightAngle); // 10.0 00357 iLightPosY = iLightRadius * sin(iLightAngle); // 0.0 00358 iLightPosZ = 50.0; 00359 00360 iLightPos[0] = iLightPosX; 00361 iLightPos[1] = iLightPosY; 00362 iLightPos[2] = iLightPosZ; 00363 iLightPos[3] = 0.0; 00364 00365 iIsLightRotatedLeft = EFalse; 00366 iIsLightRotatedRight = EFalse; 00367 } 00368 00369 00370 // ----------------------------------------------------------------------------- 00371 // CShadows::AppExit 00372 // Release any allocations made in AppInit. 00373 // ----------------------------------------------------------------------------- 00374 // 00375 void CShadows::AppExit( void ) 00376 { 00377 } 00378 00379 00380 // ----------------------------------------------------------------------------- 00381 // CShadows::DrawPlane 00382 // Draw the ground plane. 00383 // ----------------------------------------------------------------------------- 00384 // 00385 void CShadows::DrawPlane( void ) 00386 { 00387 glColor4f( 255.f, 255.f, 255.f, 255.f ); // White 00388 glVertexPointer( 3, GL_BYTE, 0, planeVertices ); 00389 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles ); 00390 } 00391 00392 00393 // ----------------------------------------------------------------------------- 00394 // CShadows::DrawLamp 00395 // Draw the lamp. 00396 // ----------------------------------------------------------------------------- 00397 // 00398 void CShadows::DrawLamp( void ) 00399 { 00400 /* The lamp is drawn using one vertex from the shadow object 00401 vertex list. The lamp is drawn as a single point. */ 00402 00403 glColor4f( 255.f, 255.f, 0.f, 255.f ); // Yellow 00404 glVertexPointer( 3, GL_BYTE, 0, objVertices ); 00405 glDrawElements( GL_POINTS, 1, GL_UNSIGNED_BYTE, objTriangles ); 00406 } 00407 00408 00409 // ----------------------------------------------------------------------------- 00410 // CShadows::DrawShadowObject 00411 // Draw shadows. 00412 // ----------------------------------------------------------------------------- 00413 // 00414 void CShadows::DrawShadowObject( void ) 00415 { 00416 glColor4f( 0.f, 0.f, 0.f, 255.f ); // Black 00417 glVertexPointer( 3, GL_BYTE, 0, objVertices ); 00418 glDrawElements( GL_TRIANGLES, 48 * 3, GL_UNSIGNED_BYTE, objTriangles ); 00419 } 00420 00421 00422 // ----------------------------------------------------------------------------- 00423 // CShadows::DrawObject 00424 // Draw the shadow object. 00425 // ----------------------------------------------------------------------------- 00426 // 00427 void CShadows::DrawObject( void ) 00428 { 00429 glDrawElements( GL_TRIANGLES, 48 * 3, GL_UNSIGNED_BYTE, objTriangles ); 00430 } 00431 00432 00433 // ----------------------------------------------------------------------------- 00434 // CShadows::CalculateProjectionMatrix 00435 // Calculate proper shadow matrix. 00436 // ----------------------------------------------------------------------------- 00437 // 00438 void CShadows::CalculateProjectionMatrix( void ) 00439 { 00440 /* Shadows are calculated using appropriate projection matrix, which is 00441 defined, e.g., in OpenGL Programming Guide on page 584, or in Jim Blinn's 00442 Corner: A Trip Down the Graphics Pipeline on page 60. The latter also 00443 includes localized light sources as in our case. The general modelview 00444 matrix for creating a planar shadow can be expressed as: 00445 00446 shadowMat = 00447 00448 [ dtop-l[0]*g[0], -l[1]*g[0], -l[2]*g[0], -l[3]*g[0], 00449 -l[0]*g[1], dotp-l[1]*g[1], -l[2]*g[1], -l[3]*g[1], 00450 -l[0]*g[2], -l[1]*g[2], dotp-l[2]*g[2], -l[3]*g[2], 00451 -l[0]*g[3], -l[1]*g[3], -l[2]*g[3], dotp-l[3]*g[3] ], 00452 00453 where l - list(4) - light position 00454 g - list(4) - ground plane equation in form Ax + By + Cz + d = 0 00455 dotp = l[0]*g[0]+l[1]*g[1]+l[2]*g[2]+l[3]*g[3] 00456 00457 Note that in this example the ground plane is a x-y-plane in origin, i.e., 00458 the plane equation is g = [0,0,1,0]. Therefore the matrix can be written 00459 using only the light position parameters. 00460 00461 Note that if you have a local light source, then g[2] = 1. In the case of 00462 directional light g[2] = 0. 00463 */ 00464 00465 GLfloat shadowMat_local[16] = { iLightPosZ, 0.0, 0.0, 0.0, \ 00466 0.0, iLightPosZ, 0.0, 0.0, \ 00467 -iLightPosX, -iLightPosY, 0.0, -1.0, \ 00468 0.0, 0.0, 0.0, iLightPosZ }; 00469 00470 for ( int i=0;i<16;i++ ) 00471 { 00472 iShadowMat[i] = shadowMat_local[i]; 00473 } 00474 00475 } 00476 00477 00478 // ----------------------------------------------------------------------------- 00479 // CShadows::AppCycle 00480 // Draws and animates the objects. 00481 // ----------------------------------------------------------------------------- 00482 // 00483 void CShadows::AppCycle( TInt aFrame ) 00484 { 00485 /* The order of rendering in our case is following: 00486 1. Draw all the ground object, i.e., the plane 00487 2. Draw shadows. If we have multiple ground object, each would have 00488 to be rendered separately with each different light sources. 00489 3. Draw the shadow objects. */ 00490 00491 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 00492 00493 // Update light position 00494 if ( iIsLightRotatedLeft ) 00495 { 00496 iLightAngle += (GLfloat)0.1; 00497 } 00498 00499 if ( iIsLightRotatedRight ) 00500 { 00501 iLightAngle -= (GLfloat)0.1; 00502 } 00503 00504 iLightPosX = iLightRadius * cos( iLightAngle ); 00505 iLightPosY = iLightRadius * sin( iLightAngle ); 00506 iLightPos[0] = iLightPosX; 00507 iLightPos[1] = iLightPosY; 00508 00509 00510 // Move camera 00511 glLoadIdentity(); 00512 gluLookAt( 0.f, 20.f, 150.0, // Camera position 00513 0.f, 20.f, 0.f, // Look at point 00514 0.f, 1.f, 0.f ); // Up direction 00515 glRotatef( -75.f, 1.f, 0.f, 0.f ); 00516 00517 glDisable( GL_LIGHTING ); // Plane, lamp, and shadows don't need lighting 00518 glShadeModel( GL_FLAT ); // Plane, lamp, and shadows are single colored 00519 00520 // Draw plane 00521 glPushMatrix(); 00522 glScalef( 80.0, 60.0, 1.0 ); 00523 DrawPlane(); 00524 glPopMatrix(); 00525 00526 // Draw the lamp 00527 glLightfv( GL_LIGHT0, GL_POSITION, iLightPos ); // Update lamp position 00528 glPushMatrix(); 00529 glTranslatef( iLightPosX, iLightPosY, iLightPosZ ); 00530 DrawLamp(); 00531 glPopMatrix(); 00532 00533 glDisable( GL_DEPTH_TEST ); // Disable depth test while drawing shadows 00534 // To ensure that shadows are drawn on top 00535 // of the ground plane 00536 CalculateProjectionMatrix(); // Calculate/update shadowMat 00537 glPushMatrix(); // Store the current modelview for drawing 00538 // the actual objects after shadows are drawn 00539 // modelview = M_camera 00540 glMultMatrixf( iShadowMat ); // Multiply shadow matrix with current 00541 // modelview after camera xforms and before 00542 // model xforms 00543 00544 // Draw shadows 00545 glTranslatef( 0.0, 0.0, 30.0 ); // Translate object above the ground plane 00546 glRotatef( float(aFrame), 1.0, 0.0, 0.0 ); // Rotate object 00547 glRotatef( float(aFrame), 0.0, 1.0, 0.0 ); 00548 glRotatef( float(aFrame), 0.0, 0.0, 1.0 ); 00549 glTranslatef( 5.0, -5.0, -5.0 ); // Move origin to the middle of the 00550 // shadow object 00551 00552 DrawShadowObject(); // Draw shadows, i.e., draw the shadow object with the 00553 // correct modelview matrix: 00554 // modelview = M_camera * M_shadow * M_object 00555 glPopMatrix(); // modelview = M_camera 00556 00557 glEnable( GL_DEPTH_TEST ); // Shadows are drawn -> enable depth test 00558 glEnable( GL_LIGHTING ); // Enable lighting for drawing the objects 00559 glShadeModel( GL_SMOOTH ); // To make the lighting work properly 00560 00561 // Draw shadow object 00562 glTranslatef( 0.0, 0.0, 30.0 ); 00563 glRotatef( float(aFrame), 1.0, 0.0, 0.0 ); 00564 glRotatef( float(aFrame), 0.0, 1.0, 0.0 ); 00565 glRotatef( float(aFrame), 0.0, 0.0, 1.0 ); 00566 glTranslatef( 5.0, -5.0, -5.0 ); 00567 DrawObject(); 00568 00569 } 00570 00571 00572 //---------------------------------------------------------- 00573 // The following methods are called by the CShadowsAppUi 00574 // class when handling the incoming menu events. 00575 //---------------------------------------------------------- 00576 00577 00578 // ----------------------------------------------------------------------------- 00579 // CShadows::RotateLightLeft 00580 // Rotates light to left. 00581 // ----------------------------------------------------------------------------- 00582 // 00583 void CShadows::RotateLightLeft( void ) 00584 { 00585 iIsLightRotatedLeft = !iIsLightRotatedLeft; 00586 } 00587 00588 00589 // ----------------------------------------------------------------------------- 00590 // CShadows::RotateLightRight 00591 // Rotates light to right. 00592 // ----------------------------------------------------------------------------- 00593 // 00594 void CShadows::RotateLightRight( void ) 00595 { 00596 iIsLightRotatedRight = !iIsLightRotatedRight; 00597 } 00598 00599 // ----------------------------------------------------------------------------- 00600 // CShadows::SetScreenSize 00601 // Reacts to the dynamic screen size change during execution of this program. 00602 // ----------------------------------------------------------------------------- 00603 // 00604 void CShadows::SetScreenSize( TUint aWidth, TUint aHeight ) 00605 { 00606 iScreenWidth = aWidth; 00607 iScreenHeight = aHeight; 00608 00609 // Reinitialize viewport and projection. 00610 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00611 00612 // Recalculate the view frustrum 00613 glMatrixMode( GL_PROJECTION ); 00614 glLoadIdentity(); 00615 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00616 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00617 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00618 FRUSTUM_NEAR, FRUSTUM_FAR ); 00619 glMatrixMode( GL_MODELVIEW ); 00620 }
|
© Nokia 2009 |