|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : Billboard.cpp 00004 * Part of : OpenGLEx / Billboard 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 // INCLUDE FILES 00014 #include "billboard.h" 00015 #include <aknnotewrappers.h> 00016 #include <e32math.h> 00017 00018 // EXTERNAL DATA STRUCTURES 00019 00020 // EXTERNAL FUNCTION PROTOTYPES 00021 00022 // CONSTANTS 00023 const GLint KNumberOfTreeInRow = 10; // Number of trees in a row. 00024 const GLint KNumberOfTreeInCol = 10; // Number of trees in a col. 00025 const GLint KTreeStep = 10; // Min. Distance between two trees. 00026 const GLfixed KCamRotationSpeed = FLOAT_2_FIXED( 2.5f ); // Speed at which the camera rotates. 00027 const GLfixed KCamMovementSpeed = FLOAT_2_FIXED( 2.5f ); // Speed at which the camera moves forwards/backwards 00028 const GLfixed KMaxCamDistance = INT_2_FIXED( 100 ); // Maximum allowed camera distance from the center of the scene 00029 const GLfixed KMinCamDistance = INT_2_FIXED( 10 ); // Minimum allowed camera distance from the center of the scene 00030 00031 /* Define vertice coordinates for the ground plane */ 00032 static const GLbyte planeVertices[4 * 3] = 00033 { 00034 +1,0,-1, 00035 +1,0,+1, 00036 -1,0,+1, 00037 -1,0,-1 00038 }; 00039 00040 /* Define texture coordinates for ground plane */ 00041 static const GLshort planeTextureCoords[4 * 2] = 00042 { 00043 0, 0, 00044 0, 1000, 00045 1000, 1000, 00046 1000, 0 00047 }; 00048 00049 00050 /* Define indices for drawing the triangles of the ground plane */ 00051 static const GLubyte planeTriangles[2 * 3] = 00052 { 00053 0,2,1, 00054 0,3,2 00055 }; 00056 00057 00058 /* Define vertice coordinates for drawing the trees */ 00059 static const GLbyte treeVertices[4 * 3] = 00060 { 00061 +1, 0, 0, 00062 +1, +1, 0, 00063 -1, +1, 0, 00064 -1, 0, 0 00065 }; 00066 00067 /* Define texture coordinates for drawing the trees */ 00068 static const GLbyte treeTextureCoords[4 * 2] = 00069 { 00070 0, 100, 00071 0, 0, 00072 100, 0, 00073 100, 100 00074 }; 00075 00076 /* Define indices for drawing the trees */ 00077 static const GLubyte treeTriangles[2 * 3] = 00078 { 00079 0,1,2, 00080 0,2,3 00081 }; 00082 00083 /* Define indices for drawing the clouds */ 00084 static const GLubyte cloudTriangles [] = 00085 { 00086 0,1,2, 00087 0,2,3 00088 }; 00089 00090 // MACROS 00091 00092 // LOCAL CONSTANTS AND MACROS 00093 00094 // MODULE DATA STRUCTURES 00095 00096 // LOCAL FUNCTION PROTOTYPES 00097 00098 // FORWARD DECLARATIONS 00099 00100 // ============================ MEMBER FUNCTIONS =============================== 00101 00102 // ----------------------------------------------------------------------------- 00103 // CBillboard::NewL 00104 // Symbian 2-phase constructor. Can leave. 00105 // ----------------------------------------------------------------------------- 00106 // 00107 CBillboard* CBillboard::NewL( TUint aWidth, TUint aHeight, CBillboardInput* aInputHandler ) 00108 { 00109 /* Symbian 2-phase constructor. Calls both the default 00110 C++ constructor and Symbian ConstructL methods */ 00111 CBillboard* self = new (ELeave) CBillboard( aWidth, aHeight, aInputHandler ); 00112 CleanupStack::PushL( self ); 00113 self->ConstructL(); 00114 CleanupStack::Pop(); 00115 00116 return self; 00117 } 00118 00119 // Destructor. 00120 CBillboard::~CBillboard() 00121 { 00122 } 00123 00124 // ----------------------------------------------------------------------------- 00125 // CBillboard::AppInit 00126 // Initializes the application. 00127 // ----------------------------------------------------------------------------- 00128 // 00129 void CBillboard::AppInitL( void ) 00130 { 00131 // Construct a texture manager that uses the application's private 00132 // directory as the source location for all textures. 00133 iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenHeight, 00134 FRUSTUM_TOP, FRUSTUM_BOTTOM, 00135 FRUSTUM_RIGHT, FRUSTUM_LEFT, 00136 FRUSTUM_NEAR, this ); 00137 00138 // Initialize viewport and projection. 00139 SetScreenSize( iScreenWidth, iScreenHeight ); 00140 00141 /* Set the screen background color. */ 00142 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00143 00144 /* Enable back face culling. */ 00145 glEnable( GL_CULL_FACE ); 00146 glEnable( GL_TEXTURE_2D ); 00147 glEnable( GL_DEPTH_TEST ); 00148 00149 /*Disable lighting */ 00150 glDisable( GL_LIGHTING ); 00151 00152 //Enable alpha test for color keying. 00153 glEnable( GL_ALPHA_TEST ); 00154 glAlphaFuncx( GL_GREATER, FLOAT_2_FIXED(0.0f)); 00155 00156 glMatrixMode( GL_TEXTURE ); 00157 glLoadIdentity(); 00158 glScalex( fixedDiv( FIXED_ONE, INT_2_FIXED(100) ), 00159 fixedDiv( FIXED_ONE, INT_2_FIXED(100) ), 00160 FIXED_ONE ); 00161 00162 00163 glMatrixMode( GL_MODELVIEW ); 00164 00165 /* Enable vertex arrays. */ 00166 glEnableClientState( GL_VERTEX_ARRAY ); 00167 /* Enable texture arrays. */ 00168 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00169 00170 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); 00171 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00172 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); 00173 00174 iCloudTextureCoords[0] = INT_2_FIXED( 0 ); iCloudTextureCoords[1] = INT_2_FIXED( 50 ); 00175 iCloudTextureCoords[2] = INT_2_FIXED( 0 ); iCloudTextureCoords[3] = INT_2_FIXED( 0 ); 00176 iCloudTextureCoords[4] = INT_2_FIXED( 50 ); iCloudTextureCoords[5] = INT_2_FIXED( 0 ); 00177 iCloudTextureCoords[6] = INT_2_FIXED( 50 ); iCloudTextureCoords[7] = INT_2_FIXED( 50 ); 00178 00179 // Initiallly distance is 40 and camera is only rotating counter-clockwise. 00180 iDistance = FLOAT_2_FIXED( 40.f ); 00181 00182 iLookUpTable = CLookUpTable::NewL( CLookUpTable::ESinx | CLookUpTable::ECosx ); 00183 00184 // Defining a color key interval for the tree texture. 00185 TUint8 MinColor[] = { 50, 0, 50}; 00186 TUint8 MaxColor[] = { 200, 0, 200}; 00187 00188 // Push the textures into the loading queue. 00189 _LIT( KTreeTexture, "tree.gif" ); 00190 _LIT( KGrassTexture, "grass.jpg" ); 00191 _LIT( KCloudTexture, "cloud.jpg" ); 00192 iTextureManager->RequestToLoad( KTreeTexture , &iTreeTexture, MinColor, MaxColor, false ); 00193 iTextureManager->RequestToLoad( KGrassTexture, &iGrassTexture, false ); 00194 iTextureManager->RequestToLoad( KCloudTexture, &iCloudTexture, false ); 00195 00196 //Start to load the textures. 00197 iTextureManager->DoLoadL(); 00198 } 00199 00200 // ----------------------------------------------------------------------------- 00201 // CBillboard::AppExit 00202 // Release any allocations made in AppInit. 00203 // ----------------------------------------------------------------------------- 00204 // 00205 void CBillboard::AppExit( void ) 00206 { 00207 delete iTextureManager; 00208 delete iLookUpTable; 00209 } 00210 00211 // ----------------------------------------------------------------------------- 00212 // CBillboard::DrawPlane 00213 // Draw the ground plane. 00214 // ----------------------------------------------------------------------------- 00215 // 00216 void CBillboard::DrawPlane() 00217 { 00218 glBindTexture( GL_TEXTURE_2D, iGrassTexture.iID ); 00219 00220 // Disable texture filtering 00221 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST); 00222 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST); 00223 00224 glLoadIdentity(); 00225 T3DModelx::MakeWorldViewMatrix( 00226 iCamera, 00227 TVectorx( INT_2_FIXED( 0 ), 00228 INT_2_FIXED( 0 ), 00229 INT_2_FIXED( 0 ) ) ); 00230 glScalex( INT_2_FIXED( 100 ), INT_2_FIXED( 0 ), INT_2_FIXED( 100 ) ); 00231 00232 glVertexPointer( 3, GL_BYTE, 0, planeVertices ); 00233 glTexCoordPointer( 2, GL_SHORT, 0, planeTextureCoords ); 00234 00235 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles ); 00236 00237 } 00238 00239 // ----------------------------------------------------------------------------- 00240 // CBillboard::DrawCloud 00241 // Draw clouds. 00242 // ----------------------------------------------------------------------------- 00243 // 00244 void CBillboard::DrawCloud( GLfloat aTimeSecs ) 00245 { 00246 //Moving the clouds. 00247 iTextureOffset = FLOAT_2_FIXED( aTimeSecs ); 00248 GLfixed leftEdge = iTextureOffset; 00249 GLfixed rightEdge = iTextureOffset + INT_2_FIXED( 50 ); 00250 iCloudTextureCoords[0] = iCloudTextureCoords[2] = leftEdge; 00251 iCloudTextureCoords[4] = iCloudTextureCoords[6] = rightEdge; 00252 00253 glLoadIdentity(); 00254 00255 glBindTexture( GL_TEXTURE_2D, iCloudTexture.iID ); 00256 // Enable texture filtering 00257 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); 00258 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00259 00260 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00261 glVertexPointer( 3, GL_FLOAT, 0, iCloudVertices ); 00262 glTexCoordPointer( 2, GL_FIXED, 0, iCloudTextureCoords ); 00263 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, cloudTriangles ); 00264 } 00265 00266 // ----------------------------------------------------------------------------- 00267 // CBillboard::DrawTree 00268 // Draw a tree. 00269 // ----------------------------------------------------------------------------- 00270 // 00271 void CBillboard::DrawTree( TInt aRow, TInt aCol ) 00272 { 00273 GLint x = KTreeStep * ( aCol - ( KNumberOfTreeInCol >> 1 ) ); 00274 GLint z = KTreeStep * ( aRow - ( KNumberOfTreeInRow >> 1 ) ); 00275 00276 glLoadIdentity(); 00277 00278 // Turns the tree so that it always faces the camera. (Billboarding technique) 00279 T3DModelx::MakeBillboardWorldViewMatrix( 00280 iCamera, 00281 TVectorx( INT_2_FIXED( x ), 00282 INT_2_FIXED( 0 ), 00283 INT_2_FIXED( z ) ) ); 00284 glScalex( INT_2_FIXED( 1 ), INT_2_FIXED( 3 ), INT_2_FIXED( 1 ) ); 00285 glBindTexture( GL_TEXTURE_2D, iTreeTexture.iID ); 00286 00287 // Disable texture filtering 00288 glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00289 glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00290 glVertexPointer( 3, GL_BYTE, 0, treeVertices ); 00291 glTexCoordPointer( 2, GL_BYTE, 0, treeTextureCoords ); 00292 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, treeTriangles ); 00293 } 00294 00295 00296 // ---------------------------------------------------------------------------- 00297 // CBillboard::AppCycle 00298 // Draws and animates the objects. 00299 // aFrame = Number of the frame to be rendered. 00300 // aTimeSecs = Seconds elapsed since the application started running. 00301 // aDeltaTimeSecs = Seconds elapsed since last call to AppCycle(). 00302 // ---------------------------------------------------------------------------- 00303 // 00304 void CBillboard::AppCycle( GLuint /*aFrame*/, GLfloat aTimeSecs, GLfloat aDeltaTimeSecs ) 00305 { 00306 glClear( GL_DEPTH_BUFFER_BIT ); 00307 00308 GLfixed fixedDeltaTimeSecs = FLOAT_2_FIXED( aDeltaTimeSecs ); 00309 00310 // Rotate the camera 00311 TurnCamera( fixedDeltaTimeSecs ); 00312 00313 // Move the camera forwards/backwards; 00314 MoveCamera( fixedDeltaTimeSecs ); 00315 00316 // Calculate the current camera transform 00317 GLfixed x = fixedMul( iDistance, iLookUpTable->Cosx( iAzimuth ) ); 00318 GLfixed z = fixedMul( iDistance, iLookUpTable->Sinx( iAzimuth ) ); 00319 iCamera.LookAt( TVectorx( x, INT_2_FIXED( 3 ), z ), 00320 TVectorx( INT_2_FIXED( 0 ), INT_2_FIXED( 0 ), INT_2_FIXED( 0 ) ), 00321 TVectorx( INT_2_FIXED( 0 ), INT_2_FIXED( 1 ), INT_2_FIXED( 0 ) ) ); 00322 00323 // Draws the clouds, the gound plane and the trees 00324 glDisable( GL_DEPTH_TEST ); 00325 DrawCloud( aTimeSecs ); 00326 DrawPlane(); 00327 glEnable( GL_DEPTH_TEST ); 00328 00329 for ( TInt row = 0;row < KNumberOfTreeInRow; row++ ) 00330 { 00331 for ( TInt column = 0;column < KNumberOfTreeInCol; column++ ) 00332 { 00333 DrawTree( row, column ); 00334 } 00335 } 00336 } 00337 00338 00339 // ----------------------------------------------------------------------------- 00340 // CBillboard::CBillBoard 00341 // Default C++ and Symbian ConstructL are placed in protected 00342 // area to force the use of NewL when creating an instance. 00343 // ----------------------------------------------------------------------------- 00344 // 00345 CBillboard::CBillboard( TUint aWidth, TUint aHeight, CBillboardInput* aInputHandler ) 00346 : CFiniteStateMachine() 00347 { 00348 iScreenWidth = aWidth; 00349 iScreenHeight = aHeight; 00350 iInputHandler = aInputHandler; 00351 iIsAutorotateOn = ETrue; 00352 } 00353 00354 // ----------------------------------------------------------------------------- 00355 // CBillboard::ConstructL 00356 // Symbian 2nd phase constructor can leave. 00357 // ----------------------------------------------------------------------------- 00358 // 00359 void CBillboard::ConstructL( void ) 00360 { 00361 } 00362 00363 // --------------------------------------------------------- 00364 // CBillboard::OnEnterState( TInt aState ) 00365 // Called by TFiniteStateMachine when the f.s.m enters a new state 00366 // --------------------------------------------------------- 00367 00368 void CBillboard::OnEnterStateL( TInt /*aState*/ ) 00369 { 00370 } 00371 00372 // ------------------------------------------------------------------------------------------------------- 00373 // CBillboard::OnStartLoadingTextures() 00374 // Called for a MTextureLoadingListener by the texture manager when texture loading operation starts 00375 // ------------------------------------------------------------------------------------------------------- 00376 00377 void CBillboard::OnStartLoadingTexturesL() 00378 { 00379 SetStateL( ELoadingTextures ); 00380 } 00381 00382 // ------------------------------------------------------------------------------------------------------------ 00383 // CBillboard::OnEndLoadingTextures() 00384 // Called for a MTextureLoadingListener by the texture manager when texture loading operation is completed 00385 // ------------------------------------------------------------------------------------------------------------ 00386 void CBillboard::OnEndLoadingTexturesL() 00387 { 00388 if ( GetState() == ELoadingTextures ) 00389 { 00390 SetStateL( ERunning ); 00391 } 00392 } 00393 00394 // ----------------------------------------------------------------------------- 00395 // CBillboard::SetScreenSize 00396 // Reacts to the dynamic screen size change during execution of this program. 00397 // ----------------------------------------------------------------------------- 00398 // 00399 void CBillboard::SetScreenSize( TUint aWidth, TUint aHeight ) 00400 { 00401 iScreenWidth = aWidth; 00402 iScreenHeight = aHeight; 00403 00404 // Notify the texture manager of screen size change 00405 iTextureManager->SetScreenSize( aWidth, aHeight ); 00406 00407 // Reinitialize viewport and projection. 00408 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00409 00410 // Recalculate the view frustrum 00411 glMatrixMode( GL_PROJECTION ); 00412 glLoadIdentity(); 00413 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00414 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00415 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00416 FRUSTUM_NEAR, FRUSTUM_FAR ); 00417 glMatrixMode( GL_MODELVIEW ); 00418 00419 // Recalculate cloud vertices based on the aspect ratio 00420 // Recalculate cloud vertices based on the aspect ratio 00421 iCloudVertices[ 0 ] = FRUSTUM_RIGHT * aspectRatio; 00422 iCloudVertices[ 1 ] = 0.f; 00423 iCloudVertices[ 2 ] = -FRUSTUM_NEAR; 00424 00425 iCloudVertices[ 3 ] = FRUSTUM_RIGHT * aspectRatio; 00426 iCloudVertices[ 4 ] = FRUSTUM_TOP; 00427 iCloudVertices[ 5 ] = -FRUSTUM_NEAR; 00428 00429 iCloudVertices[ 6 ] = FRUSTUM_LEFT * aspectRatio; 00430 iCloudVertices[ 7 ] = FRUSTUM_TOP; 00431 iCloudVertices[ 8 ] = -FRUSTUM_NEAR; 00432 00433 iCloudVertices[ 9 ] = FRUSTUM_LEFT * aspectRatio; 00434 iCloudVertices[ 10 ] = 0.f; 00435 iCloudVertices[ 11 ] = -FRUSTUM_NEAR; 00436 } 00437 00438 // ------------------------------------------------------------------------------------------------------------ 00439 // CBillboard::TurnCamera() 00440 // Turn the camera in azimuth. 00441 // ------------------------------------------------------------------------------------------------------------ 00442 void CBillboard::TurnCamera( GLfixed aDeltaTimeSecs ) 00443 { 00444 if( iIsAutorotateOn ) 00445 { 00446 // Autorotation is on: rotate the camera at constant speed 00447 iAzimuth += fixedMul( KCamRotationSpeed, aDeltaTimeSecs ); 00448 } 00449 // Autorotation is off: read joystick left/right movement and rotate the camera 00450 if( iInputHandler->IsInputPressed( EJoystickLeft ) ) 00451 { 00452 iIsAutorotateOn = EFalse; 00453 iAzimuth += fixedMul( KCamRotationSpeed, aDeltaTimeSecs ); 00454 } 00455 else if( iInputHandler->IsInputPressed( EJoystickRight ) ) 00456 { 00457 iIsAutorotateOn = EFalse; 00458 iAzimuth -= fixedMul( KCamRotationSpeed, aDeltaTimeSecs ); 00459 } 00460 else if( iInputHandler->IsInputPressed( EJoystickFire ) ) 00461 { 00462 // User pressed joystick button, turn autorotation on 00463 iIsAutorotateOn = ETrue; 00464 } 00465 00466 00467 // Keep the azimuth within range of [0, 359] 00468 while( iAzimuth < INT_2_FIXED( 0 ) ) 00469 { 00470 iAzimuth += INT_2_FIXED( 360 ); 00471 } 00472 00473 while( iAzimuth >= INT_2_FIXED( 360 ) ) 00474 { 00475 iAzimuth -= INT_2_FIXED( 360 ); 00476 } 00477 } 00478 00479 // ------------------------------------------------------------------------------------------------------------ 00480 // CBillboard::MoveCamera() 00481 // Move the camera forwards or backwards. 00482 // ------------------------------------------------------------------------------------------------------------ 00483 void CBillboard::MoveCamera( GLfixed aDeltaTimeSecs ) 00484 { 00485 if( iInputHandler->IsInputPressed( EJoystickUp ) ) 00486 { 00487 iDistance -= fixedMul( KCamMovementSpeed, aDeltaTimeSecs ); 00488 if ( iDistance < KMinCamDistance ) 00489 { 00490 iDistance = KMinCamDistance; 00491 } 00492 } 00493 if( iInputHandler->IsInputPressed( EJoystickDown ) ) 00494 { 00495 iDistance += fixedMul( KCamMovementSpeed, aDeltaTimeSecs ); 00496 if ( iDistance > KMaxCamDistance ) 00497 { 00498 iDistance = KMaxCamDistance; 00499 } 00500 } 00501 } 00502
|
© Nokia 2009 |