S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CBillboard Class Reference

#include <Billboard.h>

List of all members.

Public Types

enum  { ELoadingTextures, ERunning }

Public Member Functions

virtual ~CBillboard ()
void AppInitL (void)
void AppExit (void)
void AppCycle (TInt aFrame)
void DrawPlane (TInt aFrame)
void DrawTree (TInt aFrame, TInt aI, TInt aJ)
void DrawCloud (TInt aFrame)
void OnEnterStateL (TInt aState)
void OnStartLoadingTexturesL ()
void OnEndLoadingTexturesL ()
TTime GetTimeTick ()
GLfixed GetElapsedTime ()
GLfixed GetFPSCount ()
void MoveCamera (GLint aDirection, GLfixed aTime=0)
void TurnCamera (GLint aDirection, GLfixed aTime=0)
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CBillboard * NewL (TUint aWidth, TUint aHeight)

Public Attributes

GLbyte iDirection

Protected Member Functions

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

Private Attributes

TUint iScreenWidth
TUint iScreenHeight
CTextureManager * iTextureManager
TTexture iTreeTexture
TTexture iGrassTexture
TTexture iCloudTexture
GLshort iCloudTextureCoords [8]
GLshort iTextureOffset
TCamerax iCamera
GLfixed iAzimuth
GLfixed iDistance
TTime iT0
TTime iT1
GLfixed iElapsedTime
GLfixed iFPSCount
GLfloat iCloudVertices [12]
CLookUpTable * iLookUpTable


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 39 of file Billboard.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 178 of file Billboard.h.

00179                         {
00180                         ELoadingTextures, 
00181                         ERunning
00182                         };


Constructor & Destructor Documentation

CBillboard::~CBillboard (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 119 of file Billboard.cpp.

00120         {
00121         }

CBillboard::CBillboard (  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 359 of file Billboard.cpp.

References iScreenHeight, and iScreenWidth.

00360         : CFiniteStateMachine()
00361         {
00362     iScreenWidth  = aWidth;
00363     iScreenHeight = aHeight;
00364         }


Member Function Documentation

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

Factory method for creating a new CBillboard object.

Definition at line 106 of file Billboard.cpp.

References CBillboard().

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

void CBillboard::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 Billboard.cpp.

References iCloudTexture, iCloudTextureCoords, iCloudVertices, iDirection, iDistance, iGrassTexture, iLookUpTable, iScreenHeight, iScreenWidth, iTextureManager, and iTreeTexture.

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     /*Disable lighting */
00139     glDisable( GL_LIGHTING );
00140 
00141     //Enable alpha test for color keying.
00142     glEnable( GL_ALPHA_TEST );
00143     glAlphaFuncx( GL_GREATER, FLOAT_2_FIXED(0.0f));
00144 
00145     /* Initialize viewport and projection. */
00146     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00147     glMatrixMode( GL_PROJECTION );
00148 
00149     // Calculate the view frustrum
00150     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00151     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00152                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00153                 FRUSTUM_NEAR, FRUSTUM_FAR );
00154 
00155     // Calculate cloud vertices based on the aspect ratio
00156     iCloudVertices[ 0 ] = FRUSTUM_RIGHT * aspectRatio;
00157     iCloudVertices[ 1 ] = 0.f;
00158     iCloudVertices[ 2 ] = -FRUSTUM_NEAR;
00159 
00160     iCloudVertices[ 3 ] = FRUSTUM_RIGHT * aspectRatio;
00161     iCloudVertices[ 4 ] = FRUSTUM_TOP;
00162     iCloudVertices[ 5 ] = -FRUSTUM_NEAR;
00163 
00164     iCloudVertices[ 6 ] = FRUSTUM_LEFT * aspectRatio;
00165     iCloudVertices[ 7 ] = FRUSTUM_TOP;
00166     iCloudVertices[ 8 ] = -FRUSTUM_NEAR;
00167 
00168     iCloudVertices[ 9 ] = FRUSTUM_LEFT * aspectRatio;
00169     iCloudVertices[ 10 ] = 0.f;
00170     iCloudVertices[ 11 ] = -FRUSTUM_NEAR;
00171 
00172 
00173     glMatrixMode( GL_TEXTURE );
00174     glLoadIdentity();
00175     glScalex(FIXED_DIV(FIXED_ONE, INT_2_FIXED(100)), FIXED_DIV(FIXED_ONE, INT_2_FIXED(100)), FIXED_ONE );
00176 
00177 
00178     glMatrixMode( GL_MODELVIEW );
00179 
00180     /* Enable vertex arrays. */
00181     glEnableClientState( GL_VERTEX_ARRAY );
00182     /* Enable texture arrays. */
00183     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00184 
00185     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00186     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00187 
00188     iCloudTextureCoords[0] =   0; iCloudTextureCoords[1] = 100; 
00189     iCloudTextureCoords[2] =   0; iCloudTextureCoords[3] =   0; 
00190     iCloudTextureCoords[4] = 100; iCloudTextureCoords[5] =   0; 
00191     iCloudTextureCoords[6] = 100; iCloudTextureCoords[7] = 100; 
00192 
00193     iDistance = INT_2_FIXED(40);
00194     iDirection = +1;
00195 
00196     iLookUpTable = CLookUpTable::NewL( CLookUpTable::ESinx | CLookUpTable::ECosx );
00197 
00198         // Construct a texture manager that uses the application's private
00199         // directory as the location for all textures.
00200     iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenWidth,
00201                                               FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR,                               
00202                                               this );
00203     
00204     // Defining a color key interval for the tree texture.
00205     TUint8 MinColor[] = {  50, 0,  50};
00206     TUint8 MaxColor[] = { 200, 0, 200};
00207     
00208     // Pushing the textures into the loading queue.
00209     _LIT(KTreeTexture, "tree.gif");
00210     _LIT(KGrassTexture, "grass.jpg");
00211     _LIT(KCloudTexture, "cloud.jpg");
00212     iTextureManager->RequestToLoad( KTreeTexture , &iTreeTexture, MinColor, MaxColor );
00213     iTextureManager->RequestToLoad( KGrassTexture, &iGrassTexture );
00214     iTextureManager->RequestToLoad( KCloudTexture, &iCloudTexture );
00215 
00216     //Start to load the textures.
00217     iTextureManager->DoLoadL();
00218         }

void CBillboard::AppExit (  void   ) 
 

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

Definition at line 225 of file Billboard.cpp.

References iLookUpTable, and iTextureManager.

00226         {
00227     delete iTextureManager;
00228     delete iLookUpTable;
00229         }

void CBillboard::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 310 of file Billboard.cpp.

References DrawCloud(), DrawPlane(), DrawTree(), GetTimeTick(), iAzimuth, iCamera, iDirection, iDistance, iElapsedTime, iFPSCount, iLookUpTable, iT0, iT1, and TurnCamera().

00311         {
00312         glClear( GL_DEPTH_BUFFER_BIT );
00313 
00314     iT0 = iT1;
00315     do 
00316                 {
00317         iT1 = GetTimeTick();
00318         //Compute the ellapsed time (in sec.) between the current and the previous frame.
00319         iElapsedTime = FLOAT_2_FIXED( I64LOW( iT1.MicroSecondsFrom( iT0 ).Int64() ) * ( 1 / 1000000.0f ) );
00320                 } while ( iElapsedTime == 0 );   
00321 
00322         //Compute the frame rate
00323     iFPSCount = FIXED_DIV(FIXED_ONE, iElapsedTime);
00324 
00325     /* Move the camera. */
00326     TurnCamera(iDirection);
00327 
00328     GLfixed sin = iLookUpTable->Sinx(iAzimuth),
00329             cos = iLookUpTable->Cosx(iAzimuth);
00330 
00331     GLfixed x = FIXED_MUL(iDistance, cos);
00332     GLfixed z = FIXED_MUL(iDistance, sin);
00333 
00334     iCamera.LookAt(TVectorx(x, INT_2_FIXED(3), z),
00335                    TVectorx(INT_2_FIXED(0), INT_2_FIXED(0),  INT_2_FIXED(0)),
00336                    TVectorx(INT_2_FIXED(0), INT_2_FIXED(1),  INT_2_FIXED(0)));
00337 
00338     /* Draws the clouds, the gound plane and the trees */
00339     glDisable( GL_DEPTH_TEST );
00340     DrawCloud( aFrame );
00341     DrawPlane( aFrame );
00342     glEnable( GL_DEPTH_TEST );
00343 
00344     for ( TInt i = 0;i < KNumberOfTreeInRow; i++ )
00345         {
00346         for ( TInt j = 0;j < KNumberOfTreeInCol; j++ )
00347                         {
00348                         DrawTree( aFrame, i, j);
00349                         }
00350         }
00351         }

void CBillboard::DrawPlane (  TInt  aFrame  ) 
 

Draws the ground plane.

Parameters:
aFrame Current frame number.

Definition at line 236 of file Billboard.cpp.

References iCamera, and iGrassTexture.

00237         {
00238     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_DONT_CARE );
00239 
00240     glBindTexture(  GL_TEXTURE_2D, iGrassTexture.iID );
00241 
00242     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00243     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00244     
00245     glLoadIdentity();
00246     T3DModelx::MakeWorldViewMatrix(iCamera, TVectorx(INT_2_FIXED(0), INT_2_FIXED(0), INT_2_FIXED(0)));
00247     glScalex( INT_2_FIXED(100), INT_2_FIXED(0), INT_2_FIXED(100) );
00248 
00249     glVertexPointer( 3, GL_BYTE, 0, planeVertices );
00250     glTexCoordPointer( 2, GL_SHORT, 0, planeTextureCoords );
00251     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles );
00252 
00253     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00254     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00255 
00256     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00257         }

void CBillboard::DrawTree (  TInt  aFrame,
TInt  aI,
TInt  aJ
) 
 

Draws a tree.

Parameters:
aFrame Current frame number.
aI Counter.
aJ Counter.

Definition at line 285 of file Billboard.cpp.

References iCamera, and iTreeTexture.

00286         {
00287     GLint x = KTreeStep * (aJ - (KNumberOfTreeInCol >> 1));
00288     GLint z = KTreeStep * (aI - (KNumberOfTreeInRow >> 1));
00289 
00290     glLoadIdentity();
00291     
00292     // Turns the tree so that it always faces the camera. (Billboarding technique)
00293     T3DModelx::MakeBillboardWorldViewMatrix(iCamera, TVectorx(INT_2_FIXED(x), INT_2_FIXED(0), INT_2_FIXED(z)));
00294     glScalex( INT_2_FIXED(1), INT_2_FIXED(3), INT_2_FIXED(1) );
00295 
00296     glBindTexture(  GL_TEXTURE_2D, iTreeTexture.iID );
00297     
00298     glVertexPointer(   3, GL_BYTE, 0, treeVertices );
00299     glTexCoordPointer( 2, GL_BYTE, 0, treeTextureCoords );
00300     glDrawElements(    GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, treeTriangles );
00301         }

void CBillboard::DrawCloud (  TInt  aFrame  ) 
 

Draws clouds.

Parameters:
aFrame Current frame number

Definition at line 264 of file Billboard.cpp.

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

00265         {
00266     //Moving the clouds.
00267     iTextureOffset = (GLshort)(iTextureOffset + 10 * iDirection * FIXED_2_FLOAT(iElapsedTime));
00268     iCloudTextureCoords[0] = iCloudTextureCoords[2] = iTextureOffset;
00269     iCloudTextureCoords[4] = iCloudTextureCoords[6] = (GLshort)(iTextureOffset+40);
00270 
00271     glLoadIdentity();
00272 
00273     glBindTexture( GL_TEXTURE_2D, iCloudTexture.iID );
00274 
00275         glVertexPointer( 3, GL_FLOAT, 0, iCloudVertices );
00276     glTexCoordPointer( 2, GL_SHORT, 0, iCloudTextureCoords );
00277     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, cloudTriangles );
00278         }

void CBillboard::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 380 of file Billboard.cpp.

References GetTimeTick(), and iT1.

00381         {
00382     switch ( aState )
00383                 {
00384                 case ERunning:
00385                         iT1 = GetTimeTick();
00386                         break;
00387 
00388                 default:
00389                   break;
00390                 }
00391         }

void CBillboard::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 398 of file Billboard.cpp.

00399         {
00400     SetStateL( ELoadingTextures );
00401         }

void CBillboard::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 407 of file Billboard.cpp.

00408         {
00409         if ( GetState() == ELoadingTextures )
00410                 {
00411                 SetStateL( ERunning );
00412                 }
00413         }

TTime CBillboard::GetTimeTick (   ) 
 

Returns the system time using TTime.

Returns:
Current system time.

Definition at line 419 of file Billboard.cpp.

00420         {
00421         TTime time;
00422         time.UniversalTime();
00423         
00424         return time;
00425         }

GLfixed CBillboard::GetElapsedTime (   ) 
 

Returns the elapsed time between the current and the previous frame.

Returns:
Elapsed time between the current and the previous frame in seconds.

Definition at line 431 of file Billboard.cpp.

References iElapsedTime.

00432         {
00433         return iElapsedTime; 
00434         }

GLfixed CBillboard::GetFPSCount (   ) 
 

Returns the current frame rate in frames per second.

Returns:
Current f.p.s.

Definition at line 440 of file Billboard.cpp.

References iFPSCount.

00441         { 
00442         return iFPSCount; 
00443         }

void CBillboard::MoveCamera (  GLint  aDirection,
GLfixed  aTime = 0
) 
 

Move the camera from/to the origin.

Parameters:
aDirection Direction to move the camera.
aTime Time interval that is used as the basis for calculating the movement.

Definition at line 492 of file Billboard.cpp.

References iDistance, and iElapsedTime.

00493         {
00494         if ( aTime == 0 )
00495                 {
00496                 aTime = iElapsedTime;
00497                 }
00498         iDistance += FIXED_MUL(INT_2_FIXED(aDirection * 3), aTime);
00499         if ( iDistance < INT_2_FIXED(-100) )
00500                 {
00501                 iDistance = INT_2_FIXED(-100);
00502                 }
00503         if ( iDistance > INT_2_FIXED(+100) )
00504                 {
00505                 iDistance = INT_2_FIXED(+100);
00506                 }
00507     }

void CBillboard::TurnCamera (  GLint  aDirection,
GLfixed  aTime = 0
) 
 

Turn the camera in azimuth.

Parameters:
aDirection Direction to turn the camera.
aTime Time interval that is used as the basis for calculating the movement.

Definition at line 513 of file Billboard.cpp.

References iAzimuth, and iElapsedTime.

00514         {
00515         if ( aTime == 0 )
00516                 {
00517                 aTime = iElapsedTime;
00518                 }
00519     iAzimuth += FIXED_MUL(INT_2_FIXED(aDirection * 10), aTime);
00520      //Keeping the azimuth within [0, 359]
00521     if ( iAzimuth < 0 )
00522         {
00523         iAzimuth = INT_2_FIXED(+359);
00524         }
00525     else if ( iAzimuth > INT_2_FIXED(+359) )
00526         {
00527         iAzimuth = 0; 
00528         }
00529     else 
00530         {
00531         }
00532         }

void CBillboard::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 450 of file Billboard.cpp.

References iCloudVertices, iScreenHeight, iScreenWidth, and iTextureManager.

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

void CBillboard::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 371 of file Billboard.cpp.

00372         {
00373         }


Member Data Documentation

GLbyte CBillboard::iDirection
 

Animation direction and speed for moving the camera.

Definition at line 185 of file Billboard.h.

TUint CBillboard::iScreenWidth [private]
 

Width of the screen

Definition at line 190 of file Billboard.h.

TUint CBillboard::iScreenHeight [private]
 

Height of the screen

Definition at line 192 of file Billboard.h.

CTextureManager* CBillboard::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 195 of file Billboard.h.

TTexture CBillboard::iTreeTexture [private]
 

Tree texture.

Definition at line 198 of file Billboard.h.

TTexture CBillboard::iGrassTexture [private]
 

Grass texture.

Definition at line 200 of file Billboard.h.

TTexture CBillboard::iCloudTexture [private]
 

Cloud texture.

Definition at line 202 of file Billboard.h.

GLshort CBillboard::iCloudTextureCoords[8] [private]
 

Cloud texture coordinates.

Definition at line 204 of file Billboard.h.

GLshort CBillboard::iTextureOffset [private]
 

Cloud texture offset. Used to animate the clouds in the sky.

Definition at line 206 of file Billboard.h.

TCamerax CBillboard::iCamera [private]
 

Camera.

Definition at line 209 of file Billboard.h.

GLfixed CBillboard::iAzimuth [private]
 

Camera azimuth.

Definition at line 211 of file Billboard.h.

GLfixed CBillboard::iDistance [private]
 

Camera distance.

Definition at line 213 of file Billboard.h.

TTime CBillboard::iT0 [private]
 

Time begin.

Definition at line 216 of file Billboard.h.

TTime CBillboard::iT1 [private]
 

Time end.

Definition at line 218 of file Billboard.h.

GLfixed CBillboard::iElapsedTime [private]
 

Time delta = elapsed time

Definition at line 220 of file Billboard.h.

GLfixed CBillboard::iFPSCount [private]
 

Counter for frames per second.

Definition at line 222 of file Billboard.h.

GLfloat CBillboard::iCloudVertices[12] [private]
 

Vertice coordinates drawing the clouds.

Definition at line 225 of file Billboard.h.

CLookUpTable* CBillboard::iLookUpTable [private]
 

Lookup-table for sin, cos, tan.

Definition at line 228 of file Billboard.h.


© Nokia 2006

Back to top