Nokia N97 SDK
Example Applications Guide

Rain.h

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

© Nokia 2009

Back to top