Nokia N97 SDK
Example Applications Guide

Snow.h

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

© Nokia 2009

Back to top