S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CSnow Class Reference

#include <Snow.h>

List of all members.

Public Types

enum  { ELoadingTextures, ERunning }

Public Member Functions

virtual ~CSnow ()
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 ()
CSnowfall * GetSnowfall ()
TTime GetTimeTick ()
GLfloat GetElapsedTime ()
GLfloat GetFPSCount ()
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CSnow * NewL (TUint aWidth, TUint aHeight)

Protected Member Functions

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

Private Attributes

CTextureManager * iTextureManager
TUint iScreenWidth
TUint iScreenHeight
TTexture iTreeTexture
TTexture iGroundTexture
TTexture iCloudTexture
GLfloat iCloudTextureCoords [8]
GLfloat iTextureOffset
CSnowfall * iSnowfall
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 Snow.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 171 of file Snow.h.

00172                 { 
00173                 ELoadingTextures, 
00174                 ERunning 
00175                 };


Constructor & Destructor Documentation

CSnow::~CSnow (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 133 of file Snow.cpp.

00134         {
00135         }

CSnow::CSnow (  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 390 of file Snow.cpp.

References iScreenHeight, and iScreenWidth.

00391         : CFiniteStateMachine()
00392         {
00393     iScreenWidth  = aWidth;
00394     iScreenHeight = aHeight;
00395 
00396         }


Member Function Documentation

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

Factory method for creating a new CRain object.

Definition at line 120 of file Snow.cpp.

References CSnow().

00121         {
00122     /* Symbian 2-phase constructor. Calls both the default 
00123        C++ constructor and Symbian ConstructL methods */
00124     CSnow* self = new (ELeave) CSnow( aWidth, aHeight );
00125     CleanupStack::PushL( self );
00126     self->ConstructL();
00127     CleanupStack::Pop();
00128 
00129     return self;
00130         }

void CSnow::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 175 of file Snow.cpp.

References iCamera, iCloudTexture, iCloudTextureCoords, iCloudVertices, iGroundTexture, iScreenHeight, iScreenWidth, iSnowfall, iTextureManager, iTreeTexture, and CSnowfall::NewL().

00176         {
00177     /* Set the screen background color. */
00178     glClearColor( 0.f, 0.f, 0.f, 1.f );
00179 
00180     /* Enable back face culling. */
00181     glEnable( GL_CULL_FACE  );
00182     glEnable( GL_TEXTURE_2D );
00183     glEnable( GL_DEPTH_TEST );
00184 
00185     //Enable alpha test for color keying.
00186     glEnable( GL_ALPHA_TEST );
00187     glAlphaFunc( GL_GREATER, 0.0f );
00188 
00189     /*Disable lighting */
00190     glDisable( GL_LIGHTING );
00191 
00192     /* Initialize viewport and projection. */
00193     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00194     glMatrixMode( GL_PROJECTION );
00195 
00196     // Calculate the view frustrum
00197     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00198     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00199                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00200                 FRUSTUM_NEAR, FRUSTUM_FAR );
00201 
00202     // Calculate cloud vertices based on the aspect ratio
00203     iCloudVertices[ 0 ] = FRUSTUM_RIGHT * aspectRatio;
00204     iCloudVertices[ 1 ] = 0.f;
00205     iCloudVertices[ 2 ] = -FRUSTUM_NEAR;
00206 
00207     iCloudVertices[ 3 ] = FRUSTUM_RIGHT * aspectRatio;
00208     iCloudVertices[ 4 ] = FRUSTUM_TOP;
00209     iCloudVertices[ 5 ] = -FRUSTUM_NEAR;
00210 
00211     iCloudVertices[ 6 ] = FRUSTUM_LEFT * aspectRatio;
00212     iCloudVertices[ 7 ] = FRUSTUM_TOP;
00213     iCloudVertices[ 8 ] = -FRUSTUM_NEAR;
00214 
00215     iCloudVertices[ 9 ] = FRUSTUM_LEFT * aspectRatio;
00216     iCloudVertices[ 10 ] = 0.f;
00217     iCloudVertices[ 11 ] = -FRUSTUM_NEAR;
00218 
00219     glMatrixMode( GL_MODELVIEW );
00220 
00221     /* Enable vertex arrays. */
00222     glEnableClientState( GL_VERTEX_ARRAY );
00223     /* Enable texture arrays. */
00224     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00225 
00226     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00227     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00228 
00229 
00230     iCloudTextureCoords[0] = 0; iCloudTextureCoords[1] = 1; 
00231     iCloudTextureCoords[2] = 0; iCloudTextureCoords[3] = 0; 
00232     iCloudTextureCoords[4] = 1; iCloudTextureCoords[5] = 0; 
00233     iCloudTextureCoords[6] = 1; iCloudTextureCoords[7] = 1; 
00234 
00235     iCamera.LookAt(TVector(0, 30.0f, 0), TVector(0, 0, -(FRUSTUM_FAR+FRUSTUM_NEAR)/2), TVector(0, 1, 0));
00236  
00237     // Construct a texture manager that uses the application's private
00238         // directory as the source location for all textures.
00239     iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenWidth,
00240                                               FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR,                               
00241                                               this );
00242     iSnowfall = CSnowfall::NewL(600, TVector(0, 80, -(FRUSTUM_FAR+FRUSTUM_NEAR)/2), 120, (FRUSTUM_FAR-FRUSTUM_NEAR), 0, iTextureManager);
00243 
00244     // Defining a color key interval for the tree texture.
00245     TUint8 MinColor[] = { 50, 0, 50 };
00246     TUint8 MaxColor[] = { 255, 75, 255 };
00247     
00248     // Pushing the textures into the loading queue.
00249         _LIT(KSnowyTreeName, "snowy_tree.gif");
00250         _LIT(KSnowyGroundName, "snowy_ground.jpg");
00251         _LIT(KCloudName, "cloud.jpg");
00252     iTextureManager->RequestToLoad(KSnowyTreeName, &iTreeTexture, MinColor, MaxColor );
00253     iTextureManager->RequestToLoad(KSnowyGroundName, &iGroundTexture );
00254     iTextureManager->RequestToLoad(KCloudName, &iCloudTexture );
00255 
00256     //Start to load the textures.
00257     iTextureManager->DoLoadL();
00258         }

void CSnow::AppExit (  void   ) 
 

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

Definition at line 265 of file Snow.cpp.

References iSnowfall, and iTextureManager.

00266         {
00267     delete iSnowfall;
00268     delete iTextureManager;
00269         }

void CSnow::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 349 of file Snow.cpp.

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

00350         {
00351     glClear( GL_DEPTH_BUFFER_BIT );
00352 
00353     iT0 = iT1;
00354     do 
00355                 {
00356         iT1 = GetTimeTick();
00357         //Compute the elapsed time (in sec.) between the current and the previous frame.
00358         iElapsedTime = I64LOW( iT1.MicroSecondsFrom( iT0 ).Int64() * ( 1 / 1000000.0f ) );
00359                 } while ( iElapsedTime == 0 );                   
00360     
00361         iFPSCount = 1 / (iElapsedTime);        //Compute the frame rate.
00362 
00363     /* Draws the clouds, and the ground plane */
00364     glDisable( GL_DEPTH_TEST );
00365     DrawCloud();
00366     DrawPlane();
00367 
00368     const TReal32 treeStepX = 30.0f;
00369     const TReal32 treeStepZ = 100.0f;
00370 
00371     //Draw the trees.
00372     DrawTree( -treeStepX, -1*treeStepZ);
00373     DrawTree( +treeStepX, -1*treeStepZ);
00374     DrawTree( -treeStepX, -2*treeStepZ);
00375     DrawTree( +treeStepX, -2*treeStepZ);
00376 
00377     glEnable( GL_DEPTH_TEST );
00378 
00379     /*Update and render the particle engine */
00380     iSnowfall->UpdateEngine(iElapsedTime);
00381     iSnowfall->RenderEngine(iCamera);
00382         }

void CSnow::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 411 of file Snow.cpp.

References GetTimeTick(), and iT1.

00412         {
00413     if ( aState == ERunning )
00414                 {
00415         iT1 = GetTimeTick();
00416                 }
00417         }

void CSnow::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 424 of file Snow.cpp.

00425         {
00426     SetStateL( ELoadingTextures );
00427         }

void CSnow::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 433 of file Snow.cpp.

00434         {
00435         if ( GetState() == ELoadingTextures )
00436                 {
00437         SetStateL( ERunning );
00438                 }
00439         }

void CSnow::DrawPlane (   ) 
 

Draws the ground plane

Definition at line 276 of file Snow.cpp.

References iCamera, and iGroundTexture.

00277         {
00278     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE );
00279 
00280     glBindTexture(  GL_TEXTURE_2D, iGroundTexture.iID );
00281     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00282     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00283 
00284     glLoadIdentity();
00285     T3DModel::MakeWorldViewMatrix(iCamera, TVector(0.0f, 0.0f, 0.0f));
00286     glScalef( 1000, 0, 1000 );
00287 
00288 
00289     glVertexPointer( 3, GL_BYTE, 0, planeVertices );
00290     glTexCoordPointer( 2, GL_BYTE, 0, planeTextureCoords );
00291     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles );
00292 
00293     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00294     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00295 
00296     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00297         }

void CSnow::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 329 of file Snow.cpp.

References iCamera, and iTreeTexture.

00330         {
00331     glLoadIdentity();
00332 
00333     //Turns the tree so that it always faces the camera.
00334     T3DModel::MakeBillboardWorldViewMatrix(iCamera, TVector(aX, 0.0f, aZ));
00335     glScalef( 10, 30, 10 );
00336 
00337     glBindTexture(  GL_TEXTURE_2D, iTreeTexture.iID );
00338     
00339     glVertexPointer( 3, GL_BYTE, 0, treeVertices );
00340     glTexCoordPointer( 2, GL_BYTE, 0, treeTextureCoords );
00341     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, treeTriangles );
00342         }

void CSnow::DrawCloud (   ) 
 

Draws the clouds.

Definition at line 304 of file Snow.cpp.

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

00305         {
00306     iTextureOffset += (GLfloat)(0.01*iElapsedTime);
00307 
00308     iCloudTextureCoords[0] = iCloudTextureCoords[2] = iTextureOffset;
00309     iCloudTextureCoords[4] = iCloudTextureCoords[6] = iTextureOffset + 0.4f;
00310 
00311     glDisable( GL_DEPTH_TEST );
00312 
00313     glLoadIdentity();
00314 
00315     glBindTexture(  GL_TEXTURE_2D, iCloudTexture.iID );
00316 
00317     glVertexPointer( 3, GL_FLOAT, 0, iCloudVertices );
00318     glTexCoordPointer( 2, GL_FLOAT, 0, iCloudTextureCoords );
00319     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, cloudTriangles );
00320 
00321     glEnable( GL_DEPTH_TEST );
00322         }

CSnowfall * CSnow::GetSnowfall (   ) 
 

Returns pointer to the particle-engine effect

Returns:
Effect

Definition at line 165 of file Snow.cpp.

References iSnowfall.

00166         { 
00167         return iSnowfall;
00168         }

TTime CSnow::GetTimeTick (   ) 
 

Returns the current system time.

Returns:
System time.

Definition at line 447 of file Snow.cpp.

00448 {
00449    TTime time;
00450    time.UniversalTime();
00451    
00452    return time;
00453 }

GLfloat CSnow::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 143 of file Snow.cpp.

References iElapsedTime.

00144         { 
00145         return iElapsedTime; 
00146         }

GLfloat CSnow::GetFPSCount (   ) 
 

Returns current frames-per-second count.

Returns:
Current frames per second.

Definition at line 154 of file Snow.cpp.

References iFPSCount.

00155         { 
00156         return iFPSCount; 
00157         }

void CSnow::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 460 of file Snow.cpp.

References iCloudVertices, iScreenHeight, iScreenWidth, and iTextureManager.

00461     {
00462     iScreenWidth  = aWidth;
00463     iScreenHeight = aHeight;
00464     
00465     // Notify the texture manager of screen size change
00466     iTextureManager->SetScreenSize( aWidth, aHeight );
00467 
00468     // Reinitialize viewport and projection.
00469     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00470 
00471     // Recalculate the view frustrum
00472     glMatrixMode( GL_PROJECTION );
00473     glLoadIdentity();
00474     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00475     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00476                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00477                 FRUSTUM_NEAR, FRUSTUM_FAR );
00478     glMatrixMode( GL_MODELVIEW );
00479 
00480     // Recalculate cloud vertices based on the aspect ratio
00481     iCloudVertices[ 0 ] = FRUSTUM_RIGHT * aspectRatio;
00482     iCloudVertices[ 1 ] = 0.f;
00483     iCloudVertices[ 2 ] = -FRUSTUM_NEAR;
00484 
00485     iCloudVertices[ 3 ] = FRUSTUM_RIGHT * aspectRatio;
00486     iCloudVertices[ 4 ] = FRUSTUM_TOP;
00487     iCloudVertices[ 5 ] = -FRUSTUM_NEAR;
00488 
00489     iCloudVertices[ 6 ] = FRUSTUM_LEFT * aspectRatio;
00490     iCloudVertices[ 7 ] = FRUSTUM_TOP;
00491     iCloudVertices[ 8 ] = -FRUSTUM_NEAR;
00492 
00493     iCloudVertices[ 9 ] = FRUSTUM_LEFT * aspectRatio;
00494     iCloudVertices[ 10 ] = 0.f;
00495     iCloudVertices[ 11 ] = -FRUSTUM_NEAR;
00496     }

void CSnow::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 403 of file Snow.cpp.

00404         {
00405         }


Member Data Documentation

CTextureManager* CSnow::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 180 of file Snow.h.

TUint CSnow::iScreenWidth [private]
 

Width of the screen

Definition at line 182 of file Snow.h.

TUint CSnow::iScreenHeight [private]
 

Height of the screen

Definition at line 184 of file Snow.h.

TTexture CSnow::iTreeTexture [private]
 

Tree texture.

Definition at line 187 of file Snow.h.

TTexture CSnow::iGroundTexture [private]
 

Ground texture.

Definition at line 189 of file Snow.h.

TTexture CSnow::iCloudTexture [private]
 

Cloud texture.

Definition at line 191 of file Snow.h.

GLfloat CSnow::iCloudTextureCoords[8] [private]
 

Cloud texture coordinates.

Definition at line 193 of file Snow.h.

GLfloat CSnow::iTextureOffset [private]
 

Cloud texture offset.

Definition at line 195 of file Snow.h.

CSnowfall* CSnow::iSnowfall [private]
 

Snowfall particle effect.

Definition at line 198 of file Snow.h.

TCamera CSnow::iCamera [private]
 

Camera.

Definition at line 201 of file Snow.h.

TTime CSnow::iT0 [private]
 

Reference time.

Definition at line 203 of file Snow.h.

TTime CSnow::iT1 [private]
 

Reference time + time delta.

Definition at line 205 of file Snow.h.

GLfloat CSnow::iElapsedTime [private]
 

Elapsed time = iT1 - iT0

Definition at line 207 of file Snow.h.

GLfloat CSnow::iFPSCount [private]
 

Current frames per second.

Definition at line 209 of file Snow.h.

GLfloat CSnow::iCloudVertices[12] [private]
 

Vertice coordinates drawing the clouds.

Definition at line 212 of file Snow.h.


© Nokia 2006

Back to top