S60 3rd Edition SDK for Symbian OS
Example Applications Guide

Snow.h

00001 /*
00002 * ==============================================================================
00003 *  Name        : Snow.h
00004 *  Part of     : OpenGLEx / Snow
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 #ifndef __SNOWH__
00017 #define __SNOWH__
00018 
00019 //  INCLUDES
00020 
00021 #include <e32base.h> // for CBase definition
00022 #include <GLES\gl.h>      // OpenGL ES header file
00023 #include "utils.h"
00024 
00025 // MACROS
00026 
00027 #define FRUSTUM_LEFT   -1.f     //left vertical clipping plane
00028 #define FRUSTUM_RIGHT  +1.f     //right vertical clipping plane
00029 #define FRUSTUM_BOTTOM -1.f     //bottom horizontal clipping plane
00030 #define FRUSTUM_TOP    +1.f     //top horizontal clipping plane
00031 #define FRUSTUM_NEAR   +3.f     //near depth clipping plane
00032 #define FRUSTUM_FAR    +1000.f  //far depth clipping plane
00033 
00034 // FORWARD DECLARATIONS
00035 
00036 class CSnowfall;
00037 
00038 /**
00039  * Class that does the actual OpenGL ES rendering.
00040  * Subclass of CFiniteStateMachine because the application
00041  * needs to handle loading textures and running states.
00042  */
00043 class CSnow 
00044         : public CFiniteStateMachine, public MTextureLoadingListener
00045         {
00046         public: // Constructors and destructor
00047 
00048         /**
00049          * Factory method for creating a new CRain object.
00050          */
00051                 static CSnow* NewL( TUint aWidth, TUint aHeight );
00052 
00053         /**
00054          * Destructor. Does nothing.
00055          */
00056                 virtual ~CSnow();
00057 
00058 
00059         public: // New functions.
00060 
00061         /**
00062          * Initializes OpenGL ES, sets the vertex and color
00063          * arrays and pointers. Also selects the shading mode and
00064          * creates the CTextureManager instance and starts loading
00065          * of required textures.
00066          */
00067                 void AppInitL( void );
00068                 
00069         /**
00070          * Called upon application exit. Destroys the CTextureManager
00071          * instance that was created in AppInitL() method.
00072          */
00073                 void AppExit( void );
00074                 
00075         /**
00076          * Renders one frame.
00077          * @param aFrame Number of the frame to be rendered.
00078          */
00079                 void AppCycle( TInt aFrame );
00080 
00081         /**
00082          * Called when the finite state machine enters a new state.
00083          * Does nothing in this implementation.
00084          * @param aState State that is about to be entered.
00085          */
00086                 void OnEnterStateL( TInt aState);
00087                 
00088         /**
00089          * Called when texture manager starts loading the textures.
00090          * Sets the current state to "loading textures".
00091          */
00092                 void OnStartLoadingTexturesL();
00093 
00094         /**
00095          * Called when texture manager has completed texture loading.
00096          * Changes the current state to "running".
00097          */
00098                 void OnEndLoadingTexturesL();
00099 
00100                 /** 
00101                  * Draws the ground plane
00102                  */
00103                 void DrawPlane();
00104                 
00105                 /**
00106                  * Draws a tree at the given location.
00107                  * @param aX X-coordinate for the tree.
00108                  * @param aZ Z-coordinate for the tree.
00109                  */
00110                 void DrawTree( GLfloat aX, GLfloat aZ );
00111                 
00112                 /** 
00113                  * Draws the clouds.
00114                  */
00115                 void DrawCloud();
00116 
00117                 /**
00118                  * Returns pointer to the particle-engine effect
00119                  * @return Effect
00120                  */
00121                 CSnowfall* GetSnowfall();
00122     
00123                 /**
00124                  * Returns the current system time.
00125                  * @return System time.
00126                  */
00127                 TTime GetTimeTick(); 
00128 
00129                 /**
00130                  * Returns elapsed time (in sec.) between the current and the previous frame
00131                  * @return Elapsed time (in sec.) between the current and the previous frame
00132                  */
00133                 GLfloat GetElapsedTime();
00134                 
00135                 /**
00136                  * Returns current frames-per-second count.
00137                  * @return Current frames per second.
00138                  */
00139                 GLfloat GetFPSCount();
00140 
00141         /**
00142          * Notifies that the screen size has dynamically changed during execution of
00143          * this program. Resets the viewport to this new size and sets this size
00144          * to the CTextureManager.
00145          * @param aWidth New width of the screen.
00146          * @param aHeight New height of the screen.
00147          */
00148         void SetScreenSize( TUint aWidth, TUint aHeight );
00149 
00150         protected:
00151 
00152         /**
00153          * Standard constructor that must never Leave.
00154          * Stores the given screen width and height.
00155          * @param aWidth Width of the screen.
00156          * @param aHeight Height of the screen.
00157          */
00158                 CSnow( TUint aWidth, TUint aHeight );
00159         
00160         /**
00161          * Second phase contructor. Does nothing.
00162          */
00163                 void ConstructL( void );
00164 
00165         public: // Data
00166         /**
00167          * Application states:
00168          * ELoadingTextures - indicates that the app. is loading textures.
00169          * ERunning - indicates that the app. is running.
00170          */
00171                 enum 
00172                 { 
00173                 ELoadingTextures, 
00174                 ERunning 
00175                 };
00176     
00177         private: // Data
00178 
00179         /** Texture manager that is used to load the used textures. */
00180                 CTextureManager * iTextureManager;
00181         /** Width of the screen */
00182         TUint iScreenWidth;
00183         /** Height of the screen */
00184         TUint iScreenHeight;
00185 
00186                 /** Tree texture. */
00187                 TTexture iTreeTexture;
00188                 /** Ground texture. */
00189                 TTexture iGroundTexture;
00190                 /** Cloud texture. */
00191                 TTexture iCloudTexture;
00192                 /** Cloud texture coordinates. */
00193                 GLfloat iCloudTextureCoords[8];
00194                 /** Cloud texture offset. */
00195                 GLfloat iTextureOffset;
00196 
00197                 /** Snowfall particle effect. */
00198                 CSnowfall* iSnowfall;
00199 
00200                 /** Camera. */
00201                 TCamera iCamera;
00202                 /** Reference time. */
00203                 TTime iT0;
00204                 /** Reference time + time delta. */
00205                 TTime iT1;
00206                 /** Elapsed time = iT1 - iT0 */
00207                 GLfloat iElapsedTime;
00208                 /** Current frames per second. */
00209                 GLfloat iFPSCount;
00210 
00211         /** Vertice coordinates drawing the clouds. */
00212         GLfloat iCloudVertices[12];
00213         };
00214 
00215 /**
00216  * Snowfall particle effect class. Snowflakes are released from within the rectangle
00217  * (iPosition.iX-iWidth/2, iPosition.iY, iPosition.iX-iWidth/2)
00218  * (iPosition.iX+iWidth/2, iPosition.iY, iPosition.iX+iWidth/2).
00219  * Once particle's y coordinate is below (smaller than) iGroundLevel, it is reset.
00220  */
00221 class CSnowfall: public CParticleEngine
00222 {
00223         public: // Constructors and destructor
00224         
00225         /**
00226          * Factory method for creating a new CSnowfall object.
00227          * @param aParticlesCount Number of particles used to create the snow effect.
00228          * @param aPosition Position of the snow particle generator.
00229          * @param aWidth Width of the snow area.
00230          * @param aDepth Height of the snow area.
00231          * @param aGroundLevel Ground level after which particle is reset back to beginning.
00232                  * @param aTextureManager Texture manager that is used to load the snow texture.
00233          */
00234                 static CSnowfall* NewL(GLint aParticlesCount, TVector aPosition,
00235                                                            GLfloat aWidth, GLfloat aDepth, GLfloat aGroundLevel,
00236                                                            CTextureManager* aTextureManager);
00237                 /** Destructor. Does nothing in this implementation. */
00238                 virtual ~CSnowfall();
00239                 
00240         protected: 
00241         
00242         /**
00243          * Standard constructor that must never Leave.
00244          * Does nothing in this implementation.
00245          */
00246                 CSnowfall::CSnowfall();
00247 
00248         /**
00249          * Second phase contructor. Constructs the snow effect using the given parameters.
00250          * @param aParticlesCount Number of particles used to create the snow effect.
00251          * @param aPosition Position of the snow particle generator.
00252          * @param aWidth Width of the snow area.
00253          * @param aDepth Height of the snow area.
00254          * @param aGroundLevel Ground level after which particle is reset back to beginning.
00255                  * @param aTextureManager Texture manager that is used to load the snow texture.
00256          */
00257                 void ConstructL(GLint aParticlesCount, TVector aPosition,
00258                                                 GLfloat aWidth, GLfloat aDepth, GLfloat aGroundLevel,
00259                                                 CTextureManager * aTextureManager);
00260     
00261         public: // Functions from base classes
00262 
00263                 /**
00264                  * Implementation of CParticleEngine::ResetParticle() method
00265                  * Resets the given particle to the rain "start plane" and gives it random location inside
00266                  * the rain area (defined when creating this effect) and random downwards speed.
00267                  * @param aIndex Particle to be reset.
00268                  */
00269                 void ResetParticle(GLint aIndex); 
00270 
00271                 /**
00272                  * Implementation of CParticleEngine::UpdateEngine() method.
00273                  * Moves all the particles according to their current velocity vector and resets all particles
00274                  * that have fallen beneath the ground level back to the beginning plain.
00275                  * @param aElapsedTime Elapsed time since last frame. Used when calculating how much
00276                  *        each particle has moved.
00277                  */
00278                 void UpdateEngine(GLfloat aElapsedTime);
00279 
00280                 /**
00281                  * Implementation of CParticleEngine::RenderEngine().
00282                  * Renders each particle to the screen as texture mapped polygon that uses the iTexture as texture.
00283                  */
00284                 void RenderEngine(TCamera& aCamera);
00285 
00286         public: // Data
00287 
00288                 /** Width of the particle source area. This defines the width of the area where the snow falls. */
00289                 GLfloat iWidth; 
00290                 /** Depth of the particle source area. This defines the depth of the area where the snow falls. */
00291                 GLfloat iDepth;
00292                 /** Ground level that defines the plane after which the particles are reset back to the source plain. */
00293                 GLfloat iGroundLevel; 
00294 
00295 
00296         protected: // Data
00297 
00298                 /** Seed for the random generator (the time when ConstructL was called). */
00299                 TInt64 iSeed; 
00300                 /** Snowflake texture. */
00301                 TTexture iTexture; 
00302 };
00303 
00304 
00305 #endif
00306 
00307 // End of File
00308 

© Nokia 2006

Back to top