Nokia N97 SDK
Example Applications Guide

CRain Class Reference

#include <Rain.h>

Inheritance diagram for CRain:

List of all members.

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 41 of file Rain.h.


Public Types

enum   { ELoadingTextures, ERunning }

Public Member Functions

virtual  ~CRain ()
void  AppInitL (void)
void  AppExit (void)
void  AppCycle (GLuint aFrame, GLfloat aTimeSecs, GLfloat aDeltaTimeSecs)
void  OnEnterStateL (TInt aState)
void  OnStartLoadingTexturesL ()
void  OnEndLoadingTexturesL ()
void  DrawPlane ()
void  DrawTree (GLfloat aX, GLfloat aZ)
void  DrawCloud (GLfloat aTimeSecs)
CRainfall *  GetRainfall ()
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
GLfloat  iCloudVertices [12]


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 152 of file Rain.h.

00153                 {
00154                 ELoadingTextures,
00155                 ERunning
00156                 };


Constructor & Destructor Documentation

CRain::~CRain (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 116 of file Rain.cpp.

00117         {
00118         }

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 300 of file Rain.cpp.

References iScreenHeight, and iScreenWidth.

Referenced by NewL().

00301     : CFiniteStateMachine()
00302         {
00303         iScreenWidth  = aWidth;
00304         iScreenHeight = aHeight;
00305 
00306         }


Member Function Documentation

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

Factory method for creating a new CRain object.

Definition at line 101 of file Rain.cpp.

References CRain().

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         }

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 125 of file Rain.cpp.

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

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         }

void CRain::AppExit (  void   ) 
 

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

Definition at line 192 of file Rain.cpp.

References iRainfall, and iTextureManager.

Referenced by CRainContainer::~CRainContainer().

00193         {
00194         delete iRainfall;
00195         delete iTextureManager;
00196         }

void CRain::AppCycle (  GLuint  aFrame,
GLfloat  aTimeSecs,
GLfloat  aDeltaTimeSecs
) 
 

Renders one frame.

Parameters:
aFrame  Number of the frame to be rendered.
aTimeSecs  Seconds elapsed since the application started running.
aDeltaTimeSecs  Seconds elapsed since last call to AppCycle().

Definition at line 272 of file Rain.cpp.

References DrawCloud(), DrawPlane(), DrawTree(), iCamera, iRainfall, CRainfall::RenderEngine(), and CRainfall::UpdateEngine().

Referenced by CRainContainer::DrawCallBack().

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         }

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 321 of file Rain.cpp.

00322         {
00323         }

void CRain::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 330 of file Rain.cpp.

00331         {
00332         SetStateL( ELoadingTextures );
00333         }

void CRain::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 340 of file Rain.cpp.

00341         {
00342         if ( GetState() == ELoadingTextures )
00343                 {
00344                 SetStateL( ERunning );
00345                 }
00346         }

void CRain::DrawPlane (   ) 
 

Draws the ground plane.

Definition at line 203 of file Rain.cpp.

References iCamera, and iGrassTexture.

Referenced by AppCycle().

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         }

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 245 of file Rain.cpp.

References iCamera, and iTreeTexture.

Referenced by AppCycle().

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         }

void CRain::DrawCloud (  GLfloat  aTimeSecs  ) 
 

Draws the clouds.

Parameters:
aDeltaTimeSecs  Time that has elapsed since last frame.

Definition at line 223 of file Rain.cpp.

References iCloudTexture, iCloudTextureCoords, iCloudVertices, and iTextureOffset.

Referenced by AppCycle().

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         }

CRainfall * CRain::GetRainfall (   ) 
 

Returns pointer to the particle-engine effect

Returns:
Effect

Definition at line 353 of file Rain.cpp.

References iRainfall.

00354         {
00355         return iRainfall;
00356         }

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 363 of file Rain.cpp.

References iCloudVertices, iScreenHeight, iScreenWidth, and iTextureManager.

Referenced by AppInitL(), and CRainContainer::SizeChanged().

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     }

void CRain::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 313 of file Rain.cpp.

00314         {
00315         }


Member Data Documentation

CTextureManager* CRain::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 163 of file Rain.h.

Referenced by AppExit(), AppInitL(), and SetScreenSize().

TUint CRain::iScreenWidth [private]
 

Width of the screen

Definition at line 165 of file Rain.h.

Referenced by AppInitL(), CRain(), and SetScreenSize().

TUint CRain::iScreenHeight [private]
 

Height of the screen

Definition at line 167 of file Rain.h.

Referenced by AppInitL(), CRain(), and SetScreenSize().

TTexture CRain::iTreeTexture [private]
 

Tree texture.

Definition at line 170 of file Rain.h.

Referenced by AppInitL(), and DrawTree().

TTexture CRain::iGrassTexture [private]
 

Ground texture.

Definition at line 172 of file Rain.h.

Referenced by AppInitL(), and DrawPlane().

TTexture CRain::iCloudTexture [private]
 

Cloud texture.

Definition at line 174 of file Rain.h.

Referenced by AppInitL(), and DrawCloud().

GLfloat CRain::iCloudTextureCoords[8] [private]
 

Cloud texture coordinates.

Definition at line 176 of file Rain.h.

Referenced by AppInitL(), and DrawCloud().

GLfloat CRain::iTextureOffset [private]
 

Cloud texture offset.

Definition at line 178 of file Rain.h.

Referenced by DrawCloud().

CRainfall* CRain::iRainfall [private]
 

Rainfall particle effect.

Definition at line 181 of file Rain.h.

Referenced by AppCycle(), AppExit(), AppInitL(), and GetRainfall().

TCamera CRain::iCamera [private]
 

Camera.

Definition at line 184 of file Rain.h.

Referenced by AppCycle(), AppInitL(), DrawPlane(), and DrawTree().

GLfloat CRain::iCloudVertices[12] [private]
 

Vertice coordinates drawing the clouds.

Definition at line 187 of file Rain.h.

Referenced by DrawCloud(), and SetScreenSize().


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top