Nokia N97 SDK
Example Applications Guide

EnvMapping.h

00001 /*
00002  * ==============================================================================
00003  *  Name        : EnvMapping.h
00004  *  Part of     : OpenGLEx / EnvMapping
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 ENVMAPPING_H
00014 #define ENVMAPPING_H
00015 
00016 //  INCLUDES
00017 #include <e32base.h>      // for CBase definition
00018 #include <GLES/gl.h>      // OpenGL ES header file
00019 #include "duck.h"         // Data for Duck model
00020 #include "head.h"         // Data for Head model
00021 #include "Utils3d.h"         // Utilities (state machine, texture handler etc.)
00022 
00023 // MACROS
00024 #define DUCK_MODEL 11
00025 #define HEAD_MODEL 22
00026 
00027 #define NUM_OF_MODELS 2    // i.e., duck and head models
00028 
00029 #define COORD_SCALE 16384   // a value used to scale the texture coordinates
00030 #define CAMERA_DISTANCE 270 // object distance from the camera
00031 
00032 #define TEX_WIDTH     256 // Texture size must be power of two and max size
00033 #define TEX_HEIGHT    256 // for textures is 256 x 256
00034 
00035 #define FRUSTUM_LEFT   -1.f     //left vertical clipping plane
00036 #define FRUSTUM_RIGHT   1.f     //right vertical clipping plane
00037 #define FRUSTUM_BOTTOM -1.f     //bottom horizontal clipping plane
00038 #define FRUSTUM_TOP     1.f     //top horizontal clipping plane
00039 #define FRUSTUM_NEAR    3.f     //near depth clipping plane
00040 #define FRUSTUM_FAR  1000.f     //far depth clipping plane
00041 
00042 // CLASS DECLARATION
00043 
00044 /**
00045  * Class that does the actual OpenGL ES rendering.
00046  * Subclass of CFiniteStateMachine because the application
00047  * needs to handle loading textures and running states.
00048  */
00049 class CEnvMapping : public CFiniteStateMachine, public MTextureLoadingListener
00050 {
00051   public:  // Constructors and destructor
00052 
00053         /**
00054          * Factory method for creating a new CSimpleCube object.
00055          */
00056         static CEnvMapping* NewL( TUint aWidth, TUint aHeight );
00057 
00058         /**
00059          * Destructor. Does nothing.
00060          */
00061         virtual ~CEnvMapping();
00062 
00063   public: // New functions
00064 
00065         /**
00066          * Initializes OpenGL ES, sets the vertex and color
00067          * arrays and pointers. Also selects the shading mode and
00068          * creates the CTextureManager instance and starts loading
00069          * of required textures.
00070          */
00071         void AppInitL( void );
00072 
00073         /**
00074          * Called upon application exit. Destroys the CTextureManager
00075          * instance that was created in AppInitL() method.
00076          */
00077         void AppExit( void );
00078 
00079         /**
00080          * Renders one frame.
00081          * @param aFrame Number of the frame to be rendered.
00082          */
00083         void AppCycle( TInt aFrame );
00084 
00085         /**
00086          * Called when the finite state machine enters a new state.
00087          * Does nothing in this implementation.
00088          * @param aState State that is about to be entered.
00089          */
00090         void OnEnterStateL( TInt aState );
00091 
00092         /**
00093          * Called when texture manager starts loading the textures.
00094          * Sets the current state to "loading textures".
00095          */
00096         void OnStartLoadingTexturesL();
00097 
00098         /**
00099          * Called when texture manager has completed texture loading.
00100          * Changes the current state to "running".
00101          */
00102         void OnEndLoadingTexturesL();
00103 
00104         /**
00105          * Initializes arrays used to calculate objects texture coordinates.
00106          * Reserves dynamically memories for the arrays depending on the size on the models.
00107          */
00108         void InitializeArraysL( void );
00109 
00110         /**
00111          * Calculates texture coordinates for the models.
00112          */
00113         void CalculateTextureCoordinatesL( void );
00114 
00115         /**
00116          * Rotates object left.
00117          */
00118         void RotateObjectLeft( void );
00119 
00120         /**
00121          * Rotates object right.
00122          */
00123         void RotateObjectRight( void );
00124 
00125         /**
00126          * Draw the environment mapped duck model.
00127          * @param aFrame Current frame in animation
00128          */
00129         void DrawDuck( TInt aFrame );
00130 
00131         /**
00132          * Draw the environment mapped head model.
00133          * @param aFrame Current frame in animation
00134          */
00135         void DrawHead( TInt aFrame );
00136 
00137         /**
00138          * Change to duck model
00139          */
00140         void DuckModel( void );
00141 
00142         /**
00143          * Change to head model
00144          */
00145         void HeadModel( void );
00146 
00147         /**
00148          * Notifies that the screen size has dynamically changed during execution of
00149          * this program. Resets the viewport to this new size and sets this size
00150          * to the CTextureManager.
00151          * @param aWidth New width of the screen.
00152          * @param aHeight New height of the screen.
00153          */
00154         void SetScreenSize( TUint aWidth, TUint aHeight );
00155 
00156   protected:
00157 
00158         /**
00159          * Standard constructor that must never Leave.
00160          * Stores the given screen width and height.
00161          * @param aWidth Width of the screen.
00162          * @param aHeight Height of the screen.
00163          */
00164         CEnvMapping( TUint aWidth, TUint aHeight );
00165 
00166         /**
00167          * Second phase contructor. Does nothing.
00168          */
00169         void ConstructL( void );
00170 
00171   public: // Data
00172 
00173         /**
00174          * Application states:
00175          * ELoadingTextures - indicates that the app. is loading textures.
00176          * ERunning - indicates that the app. is running.
00177          */
00178                 enum
00179                         {
00180                         ELoadingTextures,
00181                         ERunning
00182                         };
00183 
00184   private:  // Data
00185 
00186         /** Texture manager that is used to load the used textures. */
00187         CTextureManager* iTextureManager;
00188         /** Environment map texture. */
00189         TTexture iEnvMapTexture;
00190 
00191         /** Width of the screen */
00192         TUint iScreenWidth;
00193         /** Height of the screen */
00194         TUint iScreenHeight;
00195 
00196         /** Num of object faces. Used when calculating texture coords. */
00197         GLshort iNumFaces;
00198         /** Num of object vertices. Used when calculating texture coords. */
00199         GLshort iNumVertices;
00200 
00201         /** Whether object is rotated left or not. */
00202         TBool iRotateObjectLeftEnabled;
00203         /** Whether object is rotated right or not. */
00204         TBool iRotateObjectRightEnabled;
00205         /** Current rotation angle of the object. */
00206         GLint iObjectRotAngle;
00207         /** Currently shown object model (DUCK_MODEL or HEAD_MODEL). */
00208         GLint iObjectModel;
00209 
00210         /** Vertex normals stored here are used as straight texture coordinates for the duck's environmental mapping. */
00211         GLshort iDuckTexCoords[duckVertices * 3];
00212         /** Vertex normals stored here are used as straight texture coordinates for the head's environmental mapping. */
00213         GLshort iHeadTexCoords[headVertices * 3]; // headVertices == MAX_VERTEX_NUM
00214 
00215         /** Temporary storage of face normals. */
00216         GLfloat* iFaceNormals;
00217 
00218         /**
00219          * Tells how many faces are connected to each vertex. This value is used
00220          * when calculating an average normal for each vertices. That normal is
00221          * then used are direct texture coordinate value for that particular vertex.
00222          */
00223         GLubyte* iNormalCounter;
00224 };
00225 
00226 #endif

© Nokia 2009

Back to top