|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : Rain.cpp 00004 * Part of : OpenGLEx / Rain 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 00015 #include "rain.h" 00016 #include <aknnotewrappers.h> 00017 #include <e32math.h> 00018 00019 // CONSTANTS 00020 00021 /* Define vertice coordinates for rain */ 00022 static const GLbyte rainVertices[2 * 3] = 00023 { 00024 0,3,0, 00025 0,0,0, 00026 }; 00027 00028 /* Define vertice coordinates for the ground plane */ 00029 static const GLbyte planeVertices[4 * 3] = 00030 { 00031 +1,0,-1, 00032 +1,0,+1, 00033 -1,0,+1, 00034 -1,0,-1 00035 }; 00036 00037 /* Define texture coordinates for ground plane */ 00038 static const GLbyte planeTextureCoords[4 * 2] = 00039 { 00040 0, 10, 00041 0, 0, 00042 10, 0, 00043 10, 10 00044 }; 00045 00046 00047 /* Define indices for drawing the triangles of the ground plane */ 00048 static const GLubyte planeTriangles[2 * 3] = 00049 { 00050 0,2,1, 00051 0,3,2 00052 }; 00053 00054 00055 /* Define vertice coordinates for tree */ 00056 static const GLbyte treeVertices[4 * 3] = 00057 { 00058 +1, 0,0, 00059 +1,+1,0, 00060 -1,+1,0, 00061 -1, 0,0 00062 }; 00063 00064 /* Define texture coordinates for tree */ 00065 static const GLbyte treeTextureCoords[4 * 2] = 00066 { 00067 0, 1, 00068 0, 0, 00069 1, 0, 00070 1, 1 00071 }; 00072 00073 /* Define indices for drawing the tree */ 00074 static const GLubyte treeTriangles[2 * 3] = 00075 { 00076 0,1,2, 00077 0,2,3 00078 }; 00079 00080 00081 /* Define indices for drawing the clouds */ 00082 static const GLubyte cloudTriangles [] = 00083 { 00084 0,1,2, 00085 0,2,3 00086 }; 00087 00088 // LOCAL CONSTANTS AND MACROS 00089 00090 #define TREE_STEP_X 30.0f 00091 #define TREE_STEP_Z 100.0f 00092 00093 00094 // ============================= MEMBER FUNCTIONS =============================== 00095 00096 // ----------------------------------------------------------------------------- 00097 // CRain::NewL 00098 // Symbian 2-phase constructor. Can leave. 00099 // ----------------------------------------------------------------------------- 00100 // 00101 CRain* CRain::NewL( TUint aWidth, TUint aHeight ) 00102 { 00103 /* Symbian 2-phase constructor. Calls both the default 00104 C++ constructor and Symbian ConstructL methods */ 00105 CRain* self = new (ELeave) CRain( aWidth, aHeight ); 00106 CleanupStack::PushL( self ); 00107 self->ConstructL(); 00108 CleanupStack::Pop(); 00109 00110 return self; 00111 } 00112 00113 00114 // Destructor. 00115 00116 CRain::~CRain() 00117 { 00118 } 00119 00120 // ----------------------------------------------------------------------------- 00121 // CRain::AppInit 00122 // Application initialization code. 00123 // ----------------------------------------------------------------------------- 00124 // 00125 void CRain::AppInitL( void ) 00126 { 00127 // Construct a texture manager that uses the application's private 00128 // directory as the location for all textures. 00129 iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenHeight, 00130 FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR, 00131 this ); 00132 00133 // Initialize viewport and projection. 00134 SetScreenSize( iScreenWidth, iScreenHeight ); 00135 00136 /* Set the screen background color. */ 00137 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00138 00139 /* Enable back face culling. */ 00140 glEnable( GL_CULL_FACE ); 00141 glEnable( GL_TEXTURE_2D ); 00142 glEnable( GL_DEPTH_TEST ); 00143 00144 //Enable alpha test for color keying. 00145 glEnable( GL_ALPHA_TEST ); 00146 glAlphaFunc( GL_GREATER, 0.0f ); 00147 00148 /*Disable lighting */ 00149 glDisable( GL_LIGHTING ); 00150 00151 /* Enable vertex arrays. */ 00152 glEnableClientState( GL_VERTEX_ARRAY ); 00153 /* Enable texture arrays. */ 00154 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00155 00156 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); 00157 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00158 00159 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); 00160 00161 iCloudTextureCoords[0] = 0; iCloudTextureCoords[1] = 1; 00162 iCloudTextureCoords[2] = 0; iCloudTextureCoords[3] = 0; 00163 iCloudTextureCoords[4] = 1; iCloudTextureCoords[5] = 0; 00164 iCloudTextureCoords[6] = 1; iCloudTextureCoords[7] = 1; 00165 00166 iRainfall = CRainfall::NewL( 1000, TVector(0, 80, -(FRUSTUM_FAR+FRUSTUM_NEAR)/2), 120, (FRUSTUM_FAR-FRUSTUM_NEAR), 0); 00167 00168 iCamera.LookAt(TVector(0, 30.0f, 0), TVector(0, 0, -(FRUSTUM_FAR+FRUSTUM_NEAR)/2), TVector(0, 1, 0)); 00169 00170 // Defining a color key interval for the tree texture. 00171 TUint8 MinColor[] = { 50, 0, 50}; 00172 TUint8 MaxColor[] = { 200, 0, 200}; 00173 00174 // Pushing the textures into the loading queue. 00175 _LIT(KTreeName, "tree.gif"); 00176 _LIT(KGrassName, "grass.jpg"); 00177 _LIT(KCloudName, "cloud.jpg"); 00178 00179 iTextureManager->RequestToLoad(KTreeName , &iTreeTexture, MinColor, MaxColor ); 00180 iTextureManager->RequestToLoad(KGrassName, &iGrassTexture ); 00181 iTextureManager->RequestToLoad(KCloudName, &iCloudTexture ); 00182 00183 //Start to load the textures. 00184 iTextureManager->DoLoadL(); 00185 } 00186 00187 // ----------------------------------------------------------------------------- 00188 // CRain::AppExit 00189 // Application cleanup code. 00190 // ----------------------------------------------------------------------------- 00191 // 00192 void CRain::AppExit( void ) 00193 { 00194 delete iRainfall; 00195 delete iTextureManager; 00196 } 00197 00198 // ----------------------------------------------------------------------------- 00199 // CRain::DrawPlane 00200 // Draws the ground plane. 00201 // ----------------------------------------------------------------------------- 00202 // 00203 void CRain::DrawPlane() 00204 { 00205 glBindTexture( GL_TEXTURE_2D, iGrassTexture.iID ); 00206 glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00207 glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00208 00209 glLoadIdentity(); 00210 T3DModel::MakeWorldViewMatrix(iCamera, TVector(0.0f, 0.0f, 0.0f)); 00211 glScalef( 1000, 0, 1000 ); 00212 00213 glVertexPointer( 3, GL_BYTE, 0, planeVertices ); 00214 glTexCoordPointer( 2, GL_BYTE, 0, planeTextureCoords ); 00215 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles ); 00216 } 00217 00218 // ----------------------------------------------------------------------------- 00219 // CRain::DrawCloud 00220 // Draws the clouds. 00221 // ----------------------------------------------------------------------------- 00222 // 00223 void CRain::DrawCloud( GLfloat aDeltaTimeSecs ) 00224 { 00225 iTextureOffset += (GLfloat)(0.01f * aDeltaTimeSecs); 00226 00227 iCloudTextureCoords[0] = iCloudTextureCoords[2]= iTextureOffset; 00228 iCloudTextureCoords[4] = iCloudTextureCoords[6]= iTextureOffset+0.4f; 00229 00230 glLoadIdentity(); 00231 glBindTexture( GL_TEXTURE_2D, iCloudTexture.iID ); 00232 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00233 glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00234 00235 glVertexPointer( 3, GL_FLOAT, 0, iCloudVertices ); 00236 glTexCoordPointer( 2, GL_FLOAT, 0, iCloudTextureCoords ); 00237 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, cloudTriangles ); 00238 } 00239 00240 // ----------------------------------------------------------------------------- 00241 // CRain::DrawTree 00242 // Draws a tree. 00243 // ----------------------------------------------------------------------------- 00244 // 00245 void CRain::DrawTree(GLfloat aX, GLfloat aZ) 00246 { 00247 glLoadIdentity(); 00248 00249 //Turns the tree so that it always faces the camera. 00250 T3DModel::MakeBillboardWorldViewMatrix(iCamera, TVector(aX, 0.0f, aZ)); 00251 glScalef( 10, 30, 10 ); 00252 00253 glBindTexture( GL_TEXTURE_2D, iTreeTexture.iID ); 00254 glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00255 glTexParameterf( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00256 00257 glVertexPointer( 3, GL_BYTE, 0, treeVertices ); 00258 glTexCoordPointer( 2, GL_BYTE, 0, treeTextureCoords ); 00259 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, treeTriangles ); 00260 00261 } 00262 00263 00264 // ---------------------------------------------------------------------------- 00265 // C#Name#::AppCycle 00266 // Draws and animates the objects. 00267 // aFrame = Number of the frame to be rendered. 00268 // aTimeSecs = Seconds elapsed since the application started running. 00269 // aDeltaTimeSecs = Seconds elapsed since last call to AppCycle(). 00270 // ---------------------------------------------------------------------------- 00271 // 00272 void CRain::AppCycle( GLuint /*aFrame*/, GLfloat /*aTimeSecs*/, GLfloat aDeltaTimeSecs ) 00273 { 00274 glClear( GL_DEPTH_BUFFER_BIT ); 00275 00276 /* Draws the clouds, and the ground plane */ 00277 glDisable( GL_DEPTH_TEST ); 00278 DrawCloud( aDeltaTimeSecs ); 00279 DrawPlane(); 00280 00281 //Draw the trees. 00282 DrawTree( -TREE_STEP_X, -1*TREE_STEP_Z); 00283 DrawTree( +TREE_STEP_X, -1*TREE_STEP_Z); 00284 DrawTree( -TREE_STEP_X, -2*TREE_STEP_Z); 00285 DrawTree( +TREE_STEP_X, -2*TREE_STEP_Z); 00286 00287 glEnable( GL_DEPTH_TEST ); 00288 00289 /*Update and render the particle engine */ 00290 iRainfall->UpdateEngine(aDeltaTimeSecs); 00291 iRainfall->RenderEngine(iCamera); 00292 } 00293 00294 // ---------------------------------------------------------------------- 00295 // CRain::CRain 00296 // C++ default constructor can NOT contain any code, that 00297 // might leave. 00298 // ---------------------------------------------------------------------- 00299 // 00300 CRain::CRain( TUint aWidth, TUint aHeight ) 00301 : CFiniteStateMachine() 00302 { 00303 iScreenWidth = aWidth; 00304 iScreenHeight = aHeight; 00305 00306 } 00307 00308 // ---------------------------------------------------------------------- 00309 // CRain::ConstructL 00310 // Symbian 2nd phase constructor can leave. 00311 // ---------------------------------------------------------------------- 00312 // 00313 void CRain::ConstructL( void ) 00314 { 00315 } 00316 00317 // --------------------------------------------------------- 00318 // CRain::OnEnterState( TInt aState ) 00319 // Called by TFiniteStateMachine when the f.s.m enters a new state 00320 // --------------------------------------------------------- 00321 void CRain::OnEnterStateL( TInt /*aState*/ ) 00322 { 00323 } 00324 00325 // ------------------------------------------------------------------------------------------------------- 00326 // CRain::OnStartLoadingTextures() 00327 // Called for a MTextureLoadingListener by the texture manager when texture loading operation starts 00328 // ------------------------------------------------------------------------------------------------------- 00329 00330 void CRain::OnStartLoadingTexturesL() 00331 { 00332 SetStateL( ELoadingTextures ); 00333 } 00334 00335 // ------------------------------------------------------------------------------------------------------------ 00336 // CRain::OnEndLoadingTextures() 00337 // Called for a MTextureLoadingListener by the texture manager when texture loading operation is completed 00338 // ------------------------------------------------------------------------------------------------------------ 00339 00340 void CRain::OnEndLoadingTexturesL() 00341 { 00342 if ( GetState() == ELoadingTextures ) 00343 { 00344 SetStateL( ERunning ); 00345 } 00346 } 00347 00348 // ---------------------------------------------------------------------- 00349 // CRain::GetRainfall 00350 // Get the particle effect 00351 // ---------------------------------------------------------------------- 00352 // 00353 CRainfall* CRain::GetRainfall() 00354 { 00355 return iRainfall; 00356 } 00357 00358 // ----------------------------------------------------------------------------- 00359 // CRain::SetScreenSize 00360 // Reacts to the dynamic screen size change during execution of this program. 00361 // ----------------------------------------------------------------------------- 00362 // 00363 void CRain::SetScreenSize( TUint aWidth, TUint aHeight ) 00364 { 00365 iScreenWidth = aWidth; 00366 iScreenHeight = aHeight; 00367 00368 // Notify the texture manager of screen size change 00369 iTextureManager->SetScreenSize( aWidth, aHeight ); 00370 00371 // Reinitialize viewport and projection. 00372 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00373 00374 // Recalculate the view frustrum 00375 glMatrixMode( GL_PROJECTION ); 00376 glLoadIdentity(); 00377 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00378 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00379 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00380 FRUSTUM_NEAR, FRUSTUM_FAR ); 00381 glMatrixMode( GL_MODELVIEW ); 00382 00383 // Recalculate cloud vertices based on the aspect ratio 00384 iCloudVertices[ 0 ] = FRUSTUM_RIGHT * aspectRatio; 00385 iCloudVertices[ 1 ] = 0.f; 00386 iCloudVertices[ 2 ] = -FRUSTUM_NEAR; 00387 00388 iCloudVertices[ 3 ] = FRUSTUM_RIGHT * aspectRatio; 00389 iCloudVertices[ 4 ] = FRUSTUM_TOP; 00390 iCloudVertices[ 5 ] = -FRUSTUM_NEAR; 00391 00392 iCloudVertices[ 6 ] = FRUSTUM_LEFT * aspectRatio; 00393 iCloudVertices[ 7 ] = FRUSTUM_TOP; 00394 iCloudVertices[ 8 ] = -FRUSTUM_NEAR; 00395 00396 iCloudVertices[ 9 ] = FRUSTUM_LEFT * aspectRatio; 00397 iCloudVertices[ 10 ] = 0.f; 00398 iCloudVertices[ 11 ] = -FRUSTUM_NEAR; 00399 } 00400 00401 // ---------------------------------------------------------------------- 00402 // CRainfall::CRainfall 00403 // C++ default constructor can NOT contain any code, that can leave 00404 // ---------------------------------------------------------------------- 00405 // 00406 CRainfall::CRainfall() 00407 { 00408 } 00409 00410 // ---------------------------------------------------------------------- 00411 // CRainfall::NewL 00412 // 2nd Two-phased constructor. 00413 // Creates and initializes a CRainfall object: 00414 // raindrops are released from within 00415 // the rectangle (aPosition.iX-aWidth/2, aPosition.iY, aPosition.iX-aWidth/2) 00416 // (aPosition.iX+aWidth/2, aPosition.iY, aPosition.iX+aWidth/2). 00417 // Once their height is < iGroundLevel, they are reset.// Returns: The current time. 00418 // ---------------------------------------------------------------------- 00419 // 00420 CRainfall* CRainfall::NewL(GLint aParticlesCount, TVector aPosition, 00421 GLfloat aWidth, GLfloat aDepth, GLfloat aGroundLevel) 00422 { 00423 /* Symbian 2-phase constructor. Calls both the default 00424 C++ constructor and Symbian ConstructL methods */ 00425 CRainfall* self = new (ELeave) CRainfall; 00426 CleanupStack::PushL( self ); 00427 self->ConstructL( aParticlesCount, aPosition, aWidth, aDepth, aGroundLevel); 00428 CleanupStack::Pop(); 00429 00430 return self; 00431 } 00432 00433 // ---------------------------------------------------------------------- 00434 // CRainfall::ConstructL 00435 // 2nd phase constructor 00436 // Initializes a CRainfall object: 00437 // raindrops are released from within 00438 // the rectangle (aPosition.iX-aWidth/2, aPosition.iY, aPosition.iX-aWidth/2) 00439 // (aPosition.iX+aWidth/2, aPosition.iY, aPosition.iX+aWidth/2). 00440 // Once their height is < iGroundLevel, they are reset. 00441 // ---------------------------------------------------------------------- 00442 // 00443 void CRainfall::ConstructL(GLint aParticlesCount, TVector aPosition, 00444 GLfloat aWidth, GLfloat aDepth, GLfloat aGroundLevel) 00445 { 00446 CParticleEngine::ConstructL(aParticlesCount, aPosition); 00447 00448 iWidth = aWidth; 00449 iDepth = aDepth; 00450 iGroundLevel = aGroundLevel; 00451 00452 TTime now; 00453 now.HomeTime(); 00454 iSeed = now.Int64(); 00455 00456 ResetEngine(); 00457 } 00458 00459 // Destructor. 00460 CRainfall::~CRainfall() 00461 { 00462 } 00463 00464 // ---------------------------------------------------------------------- 00465 // CRainfall::ResetParticle 00466 // Implementation of CParticleEngine::ResetParticle(GLint) 00467 // ---------------------------------------------------------------------- 00468 // 00469 void CRainfall::ResetParticle(GLint aIndex) 00470 { 00471 00472 TParticle & Particle = iParticles[aIndex]; 00473 00474 Particle.iPosition = TVector( iWidth * (Random() - 0.5f), 0, iDepth * (Random() - 0.5f)) + iPosition; 00475 Particle.iVelocity = TVector( 0, randrange( -40.f, -20.f, iSeed ), 0); 00476 } 00477 00478 // ---------------------------------------------------------------------- 00479 // CRainfall::UpdateEngine 00480 // Implementation of CParticleEngine::UpdateEngine(GLfloat aElapsedTime) 00481 // ---------------------------------------------------------------------- 00482 // 00483 void CRainfall::UpdateEngine(GLfloat aDeltaTimeSecs) 00484 { 00485 for ( GLint i = 0; i < iParticlesCount; i++ ) 00486 { 00487 iParticles[i].iPosition += (iParticles[i].iVelocity * aDeltaTimeSecs); 00488 if ( iParticles[i].iPosition.iY < iGroundLevel ) 00489 { 00490 ResetParticle(i); 00491 } 00492 } 00493 } 00494 00495 00496 // ---------------------------------------------------------------------- 00497 // CRainfall::UpdateEngine 00498 // Implementation of CParticleEngine::RenderEngine(TCamera &) 00499 // ---------------------------------------------------------------------- 00500 // 00501 void CRainfall::RenderEngine( TCamera & aCamera ) 00502 { 00503 glDisable( GL_TEXTURE_2D ); 00504 glEnable(GL_BLEND); 00505 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 00506 glColor4f( 1.0f, 1.0f, 1.0f, 0.5f ); 00507 00508 TVector CameraPos = aCamera.GetPosition(); 00509 00510 glVertexPointer( 3, GL_BYTE, 0, rainVertices ); 00511 for ( GLint i = 0; i < iParticlesCount; i++ ) 00512 { 00513 glLoadIdentity(); 00514 glTranslatef( iParticles[i].iPosition.iX - CameraPos.iX, 00515 iParticles[i].iPosition.iY - CameraPos.iY, 00516 iParticles[i].iPosition.iZ - CameraPos.iZ ); 00517 00518 glDrawArrays( GL_LINES, 0, 2 ); 00519 } 00520 00521 glEnable( GL_TEXTURE_2D ); 00522 glDisable(GL_BLEND); 00523 } 00524 00525 // ---------------------------------------------------------------------- 00526 // CRainfall::Random 00527 // Implementation of CParticleEngine::Random 00528 // Create a random float number. 00529 // ---------------------------------------------------------------------- 00530 // 00531 GLfloat CRainfall::Random() 00532 { 00533 return ((GLfloat)(Math::FRand(iSeed))); 00534 } 00535
|
© Nokia 2009 |