S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CLensflare Class Reference

#include <lensflare.h>

List of all members.

Public Types

enum  { ELoadingTextures, ERunning }

Public Member Functions

virtual ~CLensflare ()
void AppInitL (void)
void AppExit (void)
void AppCycle (TInt aFrame)
void OnEnterStateL (TInt aState)
void OnStartLoadingTexturesL ()
void OnEndLoadingTexturesL ()
void DrawText (TBool aReflection)
void DrawPlane ()
TTime GetTimeTick ()
GLfloat GetElapsedTime ()
GLfloat GetFPSCount ()
void MoveCamera (GLint aDirection, GLfloat aTime=0)
void TurnCameraAzimuth (GLint aDirection, GLfloat aTime=0)
void TurnCameraElevation (GLint aDirection, GLfloat aTime=0)
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CLensflare * NewL (TUint aWidth, TUint aHeight)

Public Attributes

GLbyte iDirection

Protected Member Functions

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

Private Attributes

CTextureManager * iTextureManager
TUint iScreenWidth
TUint iScreenHeight
CLensFlareEffect * iLensFlareEffect
CLookUpTable * iLookUpTable
TCamera iCamera
TTime iT0
TTime iT1
GLfloat iElapsedTime
GLfloat iFPSCount
GLfloat iAzimuth
GLfloat iElevation
GLfloat iDistance
GLfloat iLensAngle
GLfloat iLensRadius


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 47 of file lensflare.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 184 of file lensflare.h.

00185                         {
00186                         ELoadingTextures, 
00187                         ERunning
00188                         };


Constructor & Destructor Documentation

CLensflare::~CLensflare (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 99 of file LensFlare.cpp.

00100         {
00101         }

CLensflare::CLensflare (  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 109 of file LensFlare.cpp.

References iScreenHeight, and iScreenWidth.

00110         : CFiniteStateMachine()
00111         {
00112     iScreenWidth  = aWidth;
00113     iScreenHeight = aHeight;
00114 
00115         }


Member Function Documentation

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

Factory method for creating a new CLensflare object.

Definition at line 84 of file LensFlare.cpp.

References CLensflare().

00085         {
00086     /* Symbian 2-phase constructor. Calls both the default 
00087        C++ constructor and Symbian ConstructL methods */
00088     CLensflare* self = new (ELeave) CLensflare( aWidth, aHeight );
00089     CleanupStack::PushL( self );
00090     self->ConstructL();
00091     CleanupStack::Pop();
00092 
00093     return self;
00094         }

void CLensflare::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 131 of file LensFlare.cpp.

References iAzimuth, iDirection, iDistance, iElevation, iLensFlareEffect, iLookUpTable, iScreenHeight, iScreenWidth, and iTextureManager.

00132         {
00133     /* Set the screen background color. */
00134     glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
00135 
00136     /* Enable back face culling. */
00137     glEnable( GL_CULL_FACE  );
00138     glEnable( GL_TEXTURE_2D );
00139     glEnable( GL_DEPTH_TEST );
00140 
00141     /*Disable lighting */
00142     glDisable( GL_LIGHTING );
00143 
00144     /* In this example, speed is  more important than quality */
00145     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00146 
00147     /* Initialize viewport and projection. */
00148     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00149     glMatrixMode( GL_PROJECTION );
00150 
00151     // Calculate the view frustrum
00152     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00153     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00154                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00155                 FRUSTUM_NEAR, FRUSTUM_FAR );
00156 
00157     glMatrixMode( GL_MODELVIEW );
00158 
00159     /* Enable vertex arrays. */
00160     glEnableClientState( GL_VERTEX_ARRAY );
00161     /* Enable texture arrays. */
00162     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00163 
00164     glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00165 
00166     iDistance = 250;
00167     iElevation = 8;
00168     iAzimuth = 90;
00169     iDirection = +1;
00170 
00171     iLookUpTable = CLookUpTable::NewL( CLookUpTable::ESin | CLookUpTable::ECos );
00172 
00173         // Construct a texture manager that uses the application's private
00174         // directory as the source location for all textures.
00175     iTextureManager = CTextureManager::NewL( iScreenWidth, iScreenWidth,
00176                                              FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR,                                
00177                                              this );
00178 
00179     TBufC<MAX_TEXTURE_NAME> LensFlareTextureNames[4];
00180 
00181     //Set of textures that will be used by the lens flare effect.
00182     _LIT(KLens1, "lens1.jpg");
00183     _LIT(KLens2, "lens2.jpg");
00184     _LIT(KLens3, "lens3.jpg");
00185     LensFlareTextureNames[0] = KLens1;
00186     LensFlareTextureNames[1] = KLens2;
00187     LensFlareTextureNames[2] = KLens3;
00188 
00189     iLensFlareEffect = CLensFlareEffect::NewL(  LensFlareTextureNames, 3,
00190                                                 LensFlareConfigs, LensFlareConfigCount, 
00191                                                 iTextureManager,
00192                                                 iScreenWidth, iScreenHeight);
00193     
00194     //Start to load the textures.
00195     iTextureManager->DoLoadL();
00196         }

void CLensflare::AppExit (  void   ) 
 

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

Definition at line 203 of file LensFlare.cpp.

References iLensFlareEffect, iLookUpTable, and iTextureManager.

00204         {
00205     delete iTextureManager;
00206     delete iLensFlareEffect;
00207     delete iLookUpTable;
00208         }

void CLensflare::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 269 of file LensFlare.cpp.

References DrawPlane(), DrawText(), GetTimeTick(), iAzimuth, iCamera, iDirection, iDistance, iElapsedTime, iElevation, iFPSCount, iLensAngle, iLensFlareEffect, iLensRadius, iLookUpTable, iScreenHeight, iScreenWidth, iT0, iT1, and TurnCameraAzimuth().

00270         {
00271 
00272     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00273 
00274     iT0 = iT1;
00275     do 
00276                 {
00277         iT1 = GetTimeTick();
00278         //Compute the ellapsed time (in sec.) between the current and the previous frame.
00279         iElapsedTime = I64LOW( iT1.MicroSecondsFrom( iT0 ).Int64() * ( 1 / 1000000.0f ) );
00280                 } while ( iElapsedTime == 0 );                   
00281     
00282         iFPSCount = 1 / (iElapsedTime);        //Compute the frame rate.
00283 
00284     /* Move the camera. */
00285     TurnCameraAzimuth(iDirection);
00286 
00287     GLfloat sinA, cosA, sinE, cosE;
00288 
00289     sinE = iLookUpTable->Sin((GLushort)iElevation);
00290     cosE = iLookUpTable->Cos((GLushort)iElevation);
00291     sinA = iLookUpTable->Sin((GLushort)iAzimuth);
00292     cosA = iLookUpTable->Cos((GLushort)iAzimuth);
00293 
00294     GLfloat x = iDistance * cosE * cosA;
00295     GLfloat y = iDistance * sinE;
00296     GLfloat z = iDistance * cosE * sinA;
00297 
00298     iCamera.LookAt(TVector(x, y, z ), TVector(0.0f, 0.0f, 0.0f ), TVector(0.0f, 1.0f, 0.0f));
00299 
00300     /*Draws the gound plane and the 'NokiaGL' text in the normal and the reflected form.*/
00301     DrawText(ETrue);
00302     DrawPlane();
00303     DrawText(EFalse);
00304 
00305     /* Animate and draw the lens flare. */
00306     iLensAngle += (20 * iDirection * iElapsedTime);
00307     if ( iLensAngle < 0 )
00308         {
00309         iLensAngle = 359;
00310         }
00311     else if ( iLensAngle > 359 ) 
00312         {
00313         iLensAngle = 0;
00314         }
00315     else
00316         {
00317         }
00318     
00319     GLfloat sin = iLookUpTable->Sin((GLushort)iLensAngle);
00320     GLfloat cos = iLookUpTable->Cos((GLushort)iLensAngle);
00321 
00322     iLensRadius = 65 * (sin > 0 ? sin : -sin);
00323     TReal xx = iLensRadius * cos + iScreenWidth / 2;
00324     TReal yy = iLensRadius * sin + iScreenHeight / 2;
00325 
00326     iLensFlareEffect->DrawAt((TInt)xx, (TInt)yy);
00327         }

void CLensflare::OnEnterStateL (  TInt  aState  ) 
 

Called when the finite state machine enters a new state. Gets the current time that is then used as the starting time.

Parameters:
aState State that is about to be entered.

Definition at line 449 of file LensFlare.cpp.

References GetTimeTick(), and iT1.

00450         {
00451     switch ( aState )
00452                 {
00453                 case ERunning:
00454                         iT1 = GetTimeTick();
00455                         break;
00456                 default:
00457               break;
00458                 }
00459         }

void CLensflare::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 426 of file LensFlare.cpp.

00427         {
00428     SetStateL( ELoadingTextures );
00429         }

void CLensflare::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 436 of file LensFlare.cpp.

00437         {
00438         if ( GetState() == ELoadingTextures )
00439                 {
00440                 SetStateL( ERunning );
00441                 }
00442         }

void CLensflare::DrawText (  TBool  aReflection  ) 
 

Draw the 'NokiaGL' text.

Parameters:
aReflection Flag that specifies if we are drawing the reflected text or not.

Definition at line 216 of file LensFlare.cpp.

References iCamera.

00217         {
00218     glDisable( GL_DEPTH_TEST );
00219 
00220     glLoadIdentity();
00221 
00222     T3DModel::MakeWorldViewMatrix( iCamera, TVector(0.0f, 0.0f, 0.0f));
00223 
00224     if ( aReflection )
00225         {
00226         glScalef( 1.0f , -1.0f, 1.0f);// reflect across Y=0 plane. 
00227         }
00228     /* Scale the model to fit the screen. */
00229     glScalef( 0.01f, 0.01f, 0.01f );
00230 
00231     glDisable( GL_TEXTURE_2D );
00232     glColor4f( 0, 0, 1.f, 1.0f ); // Blue (nokia's logo color)
00233     glVertexPointer( 3, GL_SHORT, 0, TextVertices );
00234     glDrawElements( GL_TRIANGLES, TextIndicesCount, GL_UNSIGNED_SHORT, TextIndices );
00235     glEnable( GL_TEXTURE_2D );
00236 
00237     glEnable( GL_DEPTH_TEST );
00238         }

void CLensflare::DrawPlane (   ) 
 

Draw the ground plane.

Definition at line 245 of file LensFlare.cpp.

References iCamera.

00246         {
00247     glLoadIdentity();
00248     T3DModel::MakeWorldViewMatrix(iCamera, TVector(0.0f, 0.0f, 0.0f));
00249     glScalef( 250, 0, 250 );
00250 
00251     //Enable alpha blending in order to blend the reflected text with the groud plane.
00252     glEnable(GL_BLEND);
00253     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00254     glColor4f( 0.7f, 0.0f, 0.0f, 0.70f ); // 70% of groud plane color - 30% of text color.
00255 
00256     glDisable( GL_TEXTURE_2D );
00257     glVertexPointer( 3, GL_BYTE, 0, planeVertices );
00258     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles );
00259     glEnable( GL_TEXTURE_2D );
00260 
00261     glDisable(GL_BLEND);
00262         }

TTime CLensflare::GetTimeTick (   ) 
 

Returns the current system time.

Returns:
Current system time

Definition at line 334 of file LensFlare.cpp.

00335         {
00336         TTime time;
00337         time.UniversalTime();
00338    
00339         return time;
00340         }

GLfloat CLensflare::GetElapsedTime (   ) 
 

Returns the ellapsed time (in seconds) between the current and the previous frame.

Returns:
iElapsedTime Elapsed time between frames in seconds.

Definition at line 346 of file LensFlare.cpp.

References iElapsedTime.

00347         { 
00348         return iElapsedTime;
00349         }

GLfloat CLensflare::GetFPSCount (   ) 
 

Returns the current frames per second rate.

Returns:
iFPSCount Current frame rate.

Definition at line 355 of file LensFlare.cpp.

References iFPSCount.

00356         {
00357         return iFPSCount;
00358         }

void CLensflare::MoveCamera (  GLint  aDirection,
GLfloat  aTime = 0
) 
 

Move the camera from/to the origin.

Parameters:
aDirection Direction to move camera.
aTime Time.

Definition at line 364 of file LensFlare.cpp.

References iDistance, and iElapsedTime.

00365         {
00366         if ( aTime == 0 )
00367                 {
00368                 aTime = iElapsedTime;
00369                 }
00370         iDistance += (aDirection * 20 * aTime);
00371         }

void CLensflare::TurnCameraAzimuth (  GLint  aDirection,
GLfloat  aTime = 0
) 
 

Turn the camera in azimuth.

Parameters:
aDirection Direction to turn camera
aTime Time

Definition at line 377 of file LensFlare.cpp.

References iAzimuth, and iElapsedTime.

00378         {
00379         if ( aTime == 0 )
00380                 {
00381                 aTime = iElapsedTime;
00382                 }
00383         iAzimuth += (aDirection * 20 * aTime);
00384         if ( iAzimuth < 0 )
00385                 {
00386                 iAzimuth = 359;
00387                 }
00388         else if ( iAzimuth > 359 )
00389                 {
00390                 iAzimuth = 0;
00391                 }
00392         else
00393                 {
00394                 }
00395         }

void CLensflare::TurnCameraElevation (  GLint  aDirection,
GLfloat  aTime = 0
) 
 

Change the camera elevation.

Parameters:
aDirection Direction to turn camera
aTime Time.

Definition at line 401 of file LensFlare.cpp.

References iElapsedTime, and iElevation.

00402         {
00403         if ( aTime == 0 )
00404                 {
00405                 aTime = iElapsedTime;
00406                 }
00407         iElevation += ( aDirection * 20 * aTime);
00408         if ( iElevation < 5 )
00409                 {
00410                 iElevation = 5;
00411                 }
00412         else if ( iElevation > 175 )
00413                 {
00414                 iElevation = 175;
00415                 }
00416         else
00417                 {
00418                 }
00419         }

void CLensflare::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 466 of file LensFlare.cpp.

References iScreenHeight, iScreenWidth, and iTextureManager.

00467     {
00468     iScreenWidth  = aWidth;
00469     iScreenHeight = aHeight;
00470     
00471     // Notify the texture manager of screen size change
00472     iTextureManager->SetScreenSize( aWidth, aHeight );
00473 
00474     // Reinitialize viewport and projection.
00475     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00476 
00477     // Recalculate the view frustrum
00478     glMatrixMode( GL_PROJECTION );
00479     glLoadIdentity();
00480     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00481     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00482                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00483                 FRUSTUM_NEAR, FRUSTUM_FAR );
00484     glMatrixMode( GL_MODELVIEW );
00485     }

void CLensflare::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 122 of file LensFlare.cpp.

00123         {
00124         }


Member Data Documentation

GLbyte CLensflare::iDirection
 

Camera movement direction.

Definition at line 192 of file lensflare.h.

CTextureManager* CLensflare::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 197 of file lensflare.h.

TUint CLensflare::iScreenWidth [private]
 

Width of the screen

Definition at line 199 of file lensflare.h.

TUint CLensflare::iScreenHeight [private]
 

Height of the screen

Definition at line 201 of file lensflare.h.

CLensFlareEffect* CLensflare::iLensFlareEffect [private]
 

Lens-flare effect.

Definition at line 204 of file lensflare.h.

CLookUpTable* CLensflare::iLookUpTable [private]
 

Trigonometric functions lookup-table.

Definition at line 207 of file lensflare.h.

TCamera CLensflare::iCamera [private]
 

Camera.

Definition at line 210 of file lensflare.h.

TTime CLensflare::iT0 [private]
 

Delta time measurement start.

Definition at line 213 of file lensflare.h.

TTime CLensflare::iT1 [private]
 

Delta time measurement end.

Definition at line 215 of file lensflare.h.

GLfloat CLensflare::iElapsedTime [private]
 

Elapsed time == delta.

Definition at line 217 of file lensflare.h.

GLfloat CLensflare::iFPSCount [private]
 

Rendered frames per second.

Definition at line 219 of file lensflare.h.

GLfloat CLensflare::iAzimuth [private]
 

Azimuth angle for camera.

Definition at line 221 of file lensflare.h.

GLfloat CLensflare::iElevation [private]
 

Elevation angle for camera.

Definition at line 223 of file lensflare.h.

GLfloat CLensflare::iDistance [private]
 

Camera distance.

Definition at line 225 of file lensflare.h.

GLfloat CLensflare::iLensAngle [private]
 

Angle of the lensflare effect.

Definition at line 227 of file lensflare.h.

GLfloat CLensflare::iLensRadius [private]
 

Lens radius.

Definition at line 229 of file lensflare.h.


© Nokia 2006

Back to top