S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CRain Class Reference

#include <Rain.h>

List of all members.

Public Types

enum  { ELoadingTextures, ERunning }

Public Member Functions

virtual ~CRain ()
void AppInitL (void)
void AppExit (void)
void AppCycle (TInt aFrame)
void OnEnterStateL (TInt aState)
void OnStartLoadingTexturesL ()
void OnEndLoadingTexturesL ()
void DrawPlane ()
void DrawTree (GLfloat aX, GLfloat aZ)
void DrawCloud ()
CRainfall * GetRainfall ()
TTime GetTimeTick ()
GLfloat GetElapsedTime ()
GLfloat GetFPSCount ()
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CRain * NewL (TUint aWidth, TUint aHeight)

Protected Member Functions

 CRain (TUint aWidth, TUint aHeight)
void ConstructL (void)

Private Attributes

CTextureManager * iTextureManager
TUint iScreenWidth
TUint iScreenHeight
TTexture iTreeTexture
TTexture iGrassTexture
TTexture iCloudTexture
GLfloat iCloudTextureCoords [8]
GLfloat iTextureOffset
CRainfall * iRainfall
TCamera iCamera
TTime iT0
TTime iT1
GLfloat iElapsedTime
GLfloat iFPSCount
GLfloat iCloudVertices [12]


Detailed Description

Class that does the actual OpenGL ES rendering. Subclass of CFiniteStateMachine because the application needs to handle loading textures and running states.

Definition at line 43 of file Rain.h.


Member Enumeration Documentation

anonymous enum
 

Application states: ELoadingTextures - indicates that the app. is loading textures. ERunning - indicates that the app. is running.

Definition at line 169 of file Rain.h.

00170                 { 
00171                 ELoadingTextures, 
00172                 ERunning 
00173                 };


Constructor & Destructor Documentation

CRain::~CRain (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 119 of file Rain.cpp.

00120         {
00121         }

CRain::CRain (  TUint  aWidth,
TUint  aHeight
)  [protected]
 

Standard constructor that must never Leave. Stores the given screen width and height.

Parameters:
aWidth Width of the screen.
aHeight Height of the screen.

Definition at line 337 of file Rain.cpp.

References iScreenHeight, and iScreenWidth.

00338     : CFiniteStateMachine()
00339         {
00340         iScreenWidth  = aWidth;
00341         iScreenHeight = aHeight;
00342 
00343         }


Member Function Documentation

CRain * CRain::NewL (  TUint  aWidth,
TUint  aHeight
)  [static]
 

Factory method for creating a new CRain object.

Definition at line 104 of file Rain.cpp.

References CRain().

00105         {
00106         /* Symbian 2-phase constructor. Calls both the default 
00107            C++ constructor and Symbian ConstructL methods */
00108         CRain* self = new (ELeave) CRain( aWidth, aHeight );
00109         CleanupStack::PushL( self );
00110         self->ConstructL();
00111         CleanupStack::Pop();
00112 
00113         return self;
00114         }

void CRain::AppInitL (  void   ) 
 

Initializes OpenGL ES, sets the vertex and color arrays and pointers. Also selects the shading mode and creates the CTextureManager instance and starts loading of required textures.

Definition at line 128 of file Rain.cpp.

References iCamera, iCloudTexture, iCloudTextureCoords, iCloudVertices, iGrassTexture, iRainfall, iScreenHeight, iScreenWidth, iTextureManager, iTreeTexture, and CRainfall::NewL().

00129         {
00130         /* Set the screen background color. */
00131         glClearColor( 0.f, 0.f, 0.f, 1.f );
00132 
00133         /* Enable back face culling. */
00134         glEnable( GL_CULL_FACE  );
00135     glEnable( GL_TEXTURE_2D );
00136         glEnable( GL_DEPTH_TEST );
00137 
00138         //Enable alpha test for color keying.
00139         glEnable( GL_ALPHA_TEST );
00140         glAlphaFunc( GL_GREATER, 0.0f );
00141 
00142         /*Disable lighting */
00143         glDisable( GL_LIGHTING );
00144 
00145         /* Initialize viewport and projection. */
00146         glViewport( 0, 0, iScreenWidth, iScreenHeight );
00147     glMatrixMode( GL_PROJECTION );
00148     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00149     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00150                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00151                 FRUSTUM_NEAR, FRUSTUM_FAR );
00152 
00153 
00154     // Calculate cloud vertices based on the aspect ratio
00155     iCloudVertices[ 0 ] = FRUSTUM_RIGHT * aspectRatio;
00156     iCloudVertices[ 1 ] = 0.f;
00157     iCloudVertices[ 2 ] = -FRUSTUM_NEAR;
00158 
00159     iCloudVertices[ 3 ] = FRUSTUM_RIGHT * aspectRatio;
00160     iCloudVertices[ 4 ] = FRUSTUM_TOP;
00161     iCloudVertices[ 5 ] = -FRUSTUM_NEAR;
00162 
00163     iCloudVertices[ 6 ] = FRUSTUM_LEFT * aspectRatio;
00164     iCloudVertices[ 7 ] = FRUSTUM_TOP;
00165     iCloudVertices[ 8 ] = -FRUSTUM_NEAR;
00166 
00167     iCloudVertices[ 9 ] = FRUSTUM_LEFT * aspectRatio;
00168     iCloudVertices[ 10 ] = 0.f;
00169     iCloudVertices[ 11 ] = -FRUSTUM_NEAR;
00170 
00171 
00172         glMatrixMode( GL_MODELVIEW );
00173 
00174         /* Enable vertex arrays. */
00175         glEnableClientState( GL_VERTEX_ARRAY );
00176     /* Enable texture arrays. */
00177         glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00178 
00179     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00180     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00181 
00182         iCloudTextureCoords[0] = 0; iCloudTextureCoords[1] = 1; 
00183         iCloudTextureCoords[2] = 0; iCloudTextureCoords[3] = 0; 
00184         iCloudTextureCoords[4] = 1; iCloudTextureCoords[5] = 0; 
00185         iCloudTextureCoords[6] = 1; iCloudTextureCoords[7] = 1; 
00186 
00187         iRainfall = CRainfall::NewL(1000, TVector(0, 80, -(FRUSTUM_FAR+FRUSTUM_NEAR)/2), 120, (FRUSTUM_FAR-FRUSTUM_NEAR), 0);
00188         
00189         iCamera.LookAt(TVector(0, 30.0f, 0), TVector(0, 0, -(FRUSTUM_FAR+FRUSTUM_NEAR)/2), TVector(0, 1, 0));
00190 
00191         // Construct a texture manager that uses the application's private
00192         // directory as the source location for all textures.
00193         iTextureManager = CTextureManager::NewL( iScreenWidth, iScreenWidth,
00194                                                                                          FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR,                                                                
00195                                                      this );
00196 
00197     // Defining a color key interval for the tree texture.
00198         TUint8 MinColor[] = { 50, 0, 50};
00199         TUint8 MaxColor[] = { 200, 0, 200};
00200         
00201         // Pushing the textures into the loading queue.
00202         _LIT(KTreeName, "tree.gif");
00203         _LIT(KGrassName, "grass.jpg");
00204         _LIT(KCloudName, "cloud.jpg");
00205 
00206         iTextureManager->RequestToLoad(KTreeName , &iTreeTexture, MinColor, MaxColor );
00207         iTextureManager->RequestToLoad(KGrassName, &iGrassTexture );
00208         iTextureManager->RequestToLoad(KCloudName, &iCloudTexture );
00209 
00210         //Start to load the textures.
00211         iTextureManager->DoLoadL();
00212         }

void CRain::AppExit (  void   ) 
 

Called upon application exit. Destroys the CTextureManager instance that was created in AppInitL() method.

Definition at line 219 of file Rain.cpp.

References iRainfall, and iTextureManager.

00220         {
00221         delete iRainfall;
00222         delete iTextureManager;
00223         }

void CRain::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 300 of file Rain.cpp.

References DrawCloud(), DrawPlane(), DrawTree(), GetTimeTick(), iCamera, iElapsedTime, iFPSCount, iRainfall, iT0, iT1, CRainfall::RenderEngine(), and CRainfall::UpdateEngine().

00301         {
00302     glClear( GL_DEPTH_BUFFER_BIT );
00303 
00304     iT0 = iT1;
00305         do 
00306                 {
00307                 iT1 = GetTimeTick();
00308                 //Compute the Elapsed time (in sec.) between the current and the previous frame.
00309         iElapsedTime = I64LOW( iT1.MicroSecondsFrom( iT0 ).Int64() * ( 1 / 1000000.0f ) );
00310                 } while ( iElapsedTime == 0 );
00311         iFPSCount = 1 / (iElapsedTime);        //Compute the frame rate.
00312 
00313         /* Draws the clouds, and the ground plane */
00314     glDisable( GL_DEPTH_TEST );
00315         DrawCloud();
00316         DrawPlane();
00317 
00318         //Draw the trees.
00319         DrawTree( -TREE_STEP_X, -1*TREE_STEP_Z);
00320     DrawTree( +TREE_STEP_X, -1*TREE_STEP_Z);
00321         DrawTree( -TREE_STEP_X, -2*TREE_STEP_Z);
00322     DrawTree( +TREE_STEP_X, -2*TREE_STEP_Z);
00323 
00324         glEnable( GL_DEPTH_TEST );
00325 
00326         /*Update and render the particle engine */
00327         iRainfall->UpdateEngine(iElapsedTime);
00328     iRainfall->RenderEngine(iCamera);
00329         }

void CRain::OnEnterStateL (  TInt  aState  ) 
 

Called when the finite state machine enters a new state. Does nothing in this implementation.

Parameters:
aState State that is about to be entered.

Definition at line 358 of file Rain.cpp.

References GetTimeTick(), and iT1.

00359         {
00360         switch ( aState )
00361                 {
00362                 case ERunning:
00363                     iT1 = GetTimeTick(); 
00364                         break;
00365                 default:
00366                         break;
00367                 }
00368         }

void CRain::OnStartLoadingTexturesL (   ) 
 

Called when texture manager starts loading the textures. Sets the current state to "loading textures".

Definition at line 375 of file Rain.cpp.

00376         {
00377         SetStateL( ELoadingTextures );
00378         }

void CRain::OnEndLoadingTexturesL (   ) 
 

Called when texture manager has completed texture loading. Changes the current state to "running".

Definition at line 385 of file Rain.cpp.

00386         {
00387         if ( GetState() == ELoadingTextures )
00388                 {
00389                 SetStateL( ERunning );
00390                 }
00391         }

void CRain::DrawPlane (   ) 
 

Draws the ground plane.

Definition at line 230 of file Rain.cpp.

References iCamera, and iGrassTexture.

00231         {
00232         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE );
00233 
00234     glBindTexture(  GL_TEXTURE_2D, iGrassTexture.iID );
00235 
00236     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00237         glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00238         
00239     glLoadIdentity();
00240         T3DModel::MakeWorldViewMatrix(iCamera, TVector(0.0f, 0.0f, 0.0f));
00241         glScalef( 1000, 0, 1000 );
00242 
00243     glVertexPointer( 3, GL_BYTE, 0, planeVertices );
00244         glTexCoordPointer( 2, GL_BYTE, 0, planeTextureCoords );
00245     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles );
00246 
00247     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00248         glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00249 
00250         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00251         }

void CRain::DrawTree (  GLfloat  aX,
GLfloat  aZ
) 
 

Draws a tree at the given location.

Parameters:
aX X-coordinate for the tree.
aZ Z-coordinate for the tree.

Definition at line 278 of file Rain.cpp.

References iCamera, and iTreeTexture.

00279         {
00280     glLoadIdentity();
00281         
00282         //Turns the tree so that it always faces the camera.
00283         T3DModel::MakeBillboardWorldViewMatrix(iCamera, TVector(aX, 0.0f, aZ));
00284         glScalef( 10, 30, 10 );
00285 
00286     glBindTexture(  GL_TEXTURE_2D, iTreeTexture.iID );
00287     
00288         glVertexPointer( 3, GL_BYTE, 0, treeVertices );
00289         glTexCoordPointer( 2, GL_BYTE, 0, treeTextureCoords );
00290     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, treeTriangles );
00291 
00292         }

void CRain::DrawCloud (   ) 
 

Draws the clouds.

Definition at line 258 of file Rain.cpp.

References iCloudTexture, iCloudTextureCoords, iCloudVertices, iElapsedTime, and iTextureOffset.

00259         {
00260     iTextureOffset += (GLfloat)(0.01 * iElapsedTime);
00261 
00262         iCloudTextureCoords[0] = iCloudTextureCoords[2]= iTextureOffset;
00263     iCloudTextureCoords[4] = iCloudTextureCoords[6]= iTextureOffset+0.4f;
00264 
00265     glLoadIdentity();
00266         glBindTexture(  GL_TEXTURE_2D, iCloudTexture.iID );
00267 
00268         glVertexPointer( 3, GL_FLOAT, 0, iCloudVertices );
00269         glTexCoordPointer( 2, GL_FLOAT, 0, iCloudTextureCoords );
00270     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, cloudTriangles );
00271         }

CRainfall * CRain::GetRainfall (   ) 
 

Returns pointer to the particle-engine effect

Returns:
Effect

Definition at line 412 of file Rain.cpp.

References iRainfall.

00413         { 
00414         return iRainfall; 
00415         }

TTime CRain::GetTimeTick (   ) 
 

Returns the current system time.

Returns:
System time.

Definition at line 399 of file Rain.cpp.

00400         {
00401         TTime time;
00402         time.UniversalTime();
00403         
00404         return time;
00405         }

GLfloat CRain::GetElapsedTime (   ) 
 

Returns elapsed time (in sec.) between the current and the previous frame

Returns:
Elapsed time (in sec.) between the current and the previous frame

Definition at line 422 of file Rain.cpp.

References iElapsedTime.

00423         { 
00424         return iElapsedTime; 
00425         }

GLfloat CRain::GetFPSCount (   ) 
 

Returns current frames-per-second count.

Returns:
Current frames per second.

Definition at line 432 of file Rain.cpp.

References iFPSCount.

00433         { 
00434         return iFPSCount; 
00435         }

void CRain::SetScreenSize (  TUint  aWidth,
TUint  aHeight
) 
 

Notifies that the screen size has dynamically changed during execution of this program. Resets the viewport to this new size and sets this size to the CTextureManager.

Parameters:
aWidth New width of the screen.
aHeight New height of the screen.

Definition at line 442 of file Rain.cpp.

References iCloudVertices, iScreenHeight, iScreenWidth, and iTextureManager.

00443     {
00444     iScreenWidth  = aWidth;
00445     iScreenHeight = aHeight;
00446     
00447     // Notify the texture manager of screen size change
00448     iTextureManager->SetScreenSize( aWidth, aHeight );
00449 
00450     // Reinitialize viewport and projection.
00451     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00452     
00453     // Recalculate the view frustrum
00454     glMatrixMode( GL_PROJECTION );
00455     glLoadIdentity();
00456     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00457     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00458                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00459                 FRUSTUM_NEAR, FRUSTUM_FAR );
00460     glMatrixMode( GL_MODELVIEW );
00461 
00462     // Recalculate cloud vertices based on the aspect ratio
00463     iCloudVertices[ 0 ] = FRUSTUM_RIGHT * aspectRatio;
00464     iCloudVertices[ 1 ] = 0.f;
00465     iCloudVertices[ 2 ] = -FRUSTUM_NEAR;
00466 
00467     iCloudVertices[ 3 ] = FRUSTUM_RIGHT * aspectRatio;
00468     iCloudVertices[ 4 ] = FRUSTUM_TOP;
00469     iCloudVertices[ 5 ] = -FRUSTUM_NEAR;
00470 
00471     iCloudVertices[ 6 ] = FRUSTUM_LEFT * aspectRatio;
00472     iCloudVertices[ 7 ] = FRUSTUM_TOP;
00473     iCloudVertices[ 8 ] = -FRUSTUM_NEAR;
00474 
00475     iCloudVertices[ 9 ] = FRUSTUM_LEFT * aspectRatio;
00476     iCloudVertices[ 10 ] = 0.f;
00477     iCloudVertices[ 11 ] = -FRUSTUM_NEAR;
00478     }

void CRain::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 350 of file Rain.cpp.

00351         {
00352         }


Member Data Documentation

CTextureManager* CRain::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 180 of file Rain.h.

TUint CRain::iScreenWidth [private]
 

Width of the screen

Definition at line 182 of file Rain.h.

TUint CRain::iScreenHeight [private]
 

Height of the screen

Definition at line 184 of file Rain.h.

TTexture CRain::iTreeTexture [private]
 

Tree texture.

Definition at line 187 of file Rain.h.

TTexture CRain::iGrassTexture [private]
 

Ground texture.

Definition at line 189 of file Rain.h.

TTexture CRain::iCloudTexture [private]
 

Cloud texture.

Definition at line 191 of file Rain.h.

GLfloat CRain::iCloudTextureCoords[8] [private]
 

Cloud texture coordinates.

Definition at line 193 of file Rain.h.

GLfloat CRain::iTextureOffset [private]
 

Cloud texture offset.

Definition at line 195 of file Rain.h.

CRainfall* CRain::iRainfall [private]
 

Rainfall particle effect.

Definition at line 198 of file Rain.h.

TCamera CRain::iCamera [private]
 

Camera.

Definition at line 201 of file Rain.h.

TTime CRain::iT0 [private]
 

Reference time.

Definition at line 203 of file Rain.h.

TTime CRain::iT1 [private]
 

Reference time + time delta.

Definition at line 205 of file Rain.h.

GLfloat CRain::iElapsedTime [private]
 

Elapsed time = iT1 - iT0

Definition at line 207 of file Rain.h.

GLfloat CRain::iFPSCount [private]
 

Current frames per second.

Definition at line 209 of file Rain.h.

GLfloat CRain::iCloudVertices[12] [private]
 

Vertice coordinates drawing the clouds.

Definition at line 212 of file Rain.h.


© Nokia 2006

Back to top