|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : Firefly.cpp 00004 * Part of : OpenGLEx / Bumpmap 00005 * 00006 * Copyright (c) 2005-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 // INCLUDE FILES 00014 00015 #include <e32cmn.h> // Abs, Min and Max 00016 #include "firefly.h" 00017 00018 // CONSTANTS 00019 00020 /** Vertices for drawing the glow of the firefly. */ 00021 static const GLfloat KGlowVerticeData[]= 00022 { 00023 -10.f, 10.f, 0.f, 00024 10.f, 10.f, 0.f, 00025 10.f, -10.f, 0.f, 00026 -10.f, -10.f, 0.f 00027 }; 00028 00029 /** Texture coordinates for drawing the glow of the firefly. */ 00030 static const GLfloat KGlowTexCoordsData[] = 00031 { 00032 0.f, 1.f, 00033 1.f, 1.f, 00034 1.f, 0.f, 00035 0.f, 0.f 00036 }; 00037 00038 /** Indices for drawing the glow of the firefly. */ 00039 static const GLubyte KGlowIndices[] = 00040 { 00041 0, 3, 1, 1, 3, 2 00042 }; 00043 00044 /** Vertices for drawing the firefly itself. */ 00045 static const GLfloat KFlyVerticeData[] = 00046 { 00047 -.3f, 0.f, 0.f, 00048 .3f, 0.f, 0.f, 00049 0.f, -.3f, 0.f, 00050 0.f, .3f, 0.f 00051 }; 00052 00053 // ============================= MEMBER FUNCTIONS =============================== 00054 00055 00056 // ----------------------------------------------------------------------------- 00057 // CFireFly::NewL 00058 // Symbian 2-phase constructor. Can leave. 00059 // ----------------------------------------------------------------------------- 00060 // 00061 CFireFly* CFireFly::NewL( GLfloat aLeft, GLfloat aRight, GLfloat aBottom, GLfloat aTop, 00062 GLfloat aMinZCoord, GLfloat aMaxZCoord, CTextureManager* aTexManager ) 00063 { 00064 // Symbian 2-phase constructor. Calls both the default 00065 // C++ constructor and Symbian ConstructL methods 00066 CFireFly* self = new (ELeave) CFireFly( aLeft, aRight, aBottom, aTop, aMinZCoord, aMaxZCoord ); 00067 00068 CleanupStack::PushL( self ); 00069 self->ConstructL( aTexManager ); 00070 CleanupStack::Pop(); 00071 00072 return self; 00073 } 00074 00075 // ----------------------------------------------------------------------------- 00076 // CFireFly::CFireFly 00077 // C++ constructor 00078 // ----------------------------------------------------------------------------- 00079 // 00080 CFireFly::CFireFly( GLfloat aLeft, GLfloat aRight, GLfloat aBottom, GLfloat aTop, GLfloat aMinZCoord, GLfloat aMaxZCoord ) 00081 { 00082 iLeft = aLeft; 00083 iRight = aRight; 00084 iBottom = aBottom; 00085 iTop = aTop; 00086 iMinZ = aMinZCoord; 00087 iMaxZ = aMaxZCoord; 00088 iPosition.iZ = aMinZCoord; 00089 iTarget.iZ = aMinZCoord; 00090 } 00091 00092 // ---------------------------------------------------------------------- 00093 // CFireFly::ConstructL 00094 // Symbian 2nd phase constructor can leave. 00095 // ---------------------------------------------------------------------- 00096 // 00097 void CFireFly::ConstructL( CTextureManager* aTexManager ) 00098 { 00099 TTime now; 00100 iSeed = now.Int64(); 00101 00102 // Add required textures to the texture manager's loading queue 00103 _LIT( KFireFlyGlowName, "firefly.jpg" ); 00104 aTexManager->RequestToLoad( KFireFlyGlowName, &iGlowTexture, false ); 00105 } 00106 00107 // --------------------------------------------------------- 00108 // CFireFly::~() 00109 // Destructor. 00110 // --------------------------------------------------------- 00111 CFireFly::~CFireFly() 00112 { 00113 } 00114 00115 // --------------------------------------------------------- 00116 // CFireFly::DoMovement() 00117 // Calculates the movement of the firefly. 00118 // --------------------------------------------------------- 00119 void CFireFly::DoMovement( GLuint /*aFrame*/, GLfloat /*aTimeSecs*/, GLfloat aDeltaTimeSecs ) 00120 { 00121 // Speed in x and y directions is based on the distance to the target 00122 iSpeed.iX = ( iTarget.iX - iPosition.iX ) / 40.f; 00123 iSpeed.iY = ( iTarget.iY - iPosition.iY ) / 40.f; 00124 iSpeed.iZ = ( iTarget.iZ - iPosition.iZ ) / 40.f; 00125 00126 // Make sure the minimum speed is 0.4 in every direction 00127 if( iSpeed.iX != 0.f ) 00128 { 00129 iSpeed.iX = iSpeed.iX > 0.f ? Max( iSpeed.iX, 0.4f ) : Min( iSpeed.iX, -0.4f ); 00130 00131 } 00132 if( iSpeed.iY != 0.f ) 00133 { 00134 iSpeed.iY = iSpeed.iY > 0.f ? Max( iSpeed.iY, 0.4f ) : Min( iSpeed.iY, -0.4f ); 00135 } 00136 if( iSpeed.iZ != 0.f ) 00137 { 00138 iSpeed.iZ = iSpeed.iZ > 0.f ? Max( iSpeed.iZ, 0.4f ) : Min( iSpeed.iZ, -0.4f ); 00139 } 00140 00141 00142 iPosition += ( iSpeed * ( aDeltaTimeSecs * 40.f ) ); 00143 00144 // Check that we don't overshoot the target 00145 if ( ( iSpeed.iX < 0 && iPosition.iX < iTarget.iX ) || ( iSpeed.iX > 0 && iPosition.iX > iTarget.iX ) ) 00146 { 00147 iPosition.iX = iTarget.iX; 00148 iSpeed.iX = 0.0f; 00149 } 00150 00151 if ( ( iSpeed.iY < 0 && iPosition.iY < iTarget.iY ) || ( iSpeed.iY > 0 && iPosition.iY > iTarget.iY ) ) 00152 { 00153 iPosition.iY = iTarget.iY; 00154 iSpeed.iY = 0.0f; 00155 } 00156 00157 if ( ( iSpeed.iZ < 0 && iPosition.iZ < iTarget.iZ ) || ( iSpeed.iZ > 0 && iPosition.iZ > iTarget.iZ ) ) 00158 { 00159 iPosition.iZ = iTarget.iZ; 00160 iSpeed.iZ = 0.0f; 00161 } 00162 00163 // Check if the fly has reached it's current target, 00164 // if it has generate a new target 00165 if( iPosition.iX == iTarget.iX && iPosition.iY == iTarget.iY && iPosition.iZ == iTarget.iZ ) 00166 { 00167 iTarget.iX = randrange( iLeft, iRight, iSeed ); 00168 iTarget.iY = randrange( iBottom, iTop, iSeed ); 00169 iTarget.iZ = randrange( iMinZ, iMaxZ, iSeed ); 00170 } 00171 } 00172 00173 00174 // --------------------------------------------------------- 00175 // CFireFly::Draw() 00176 // Draws the firefly at it's current position. 00177 // --------------------------------------------------------- 00178 void CFireFly::Draw() 00179 { 00180 // Colour and normal arrays are not needed to draw the firefly 00181 glDisableClientState( GL_COLOR_ARRAY ); 00182 glDisableClientState( GL_NORMAL_ARRAY ); 00183 00184 // Vertex and texture coordinate arrays are needed 00185 glEnableClientState( GL_VERTEX_ARRAY ); 00186 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00187 00188 glPushMatrix(); 00189 00190 // Translate all drawing to the current location of the firefly 00191 glTranslatef( iPosition.iX, iPosition.iY, iPosition.iZ ); 00192 00193 // To enhance the illusion of the fly flying towards and away from the viewer 00194 // we scale the size based on the distance of the fly from the viewer. 00195 GLfloat scale = ( iPosition.iZ - iMinZ ) / ( iMaxZ - iMinZ ); 00196 scale += 0.2f; 00197 glScalef( scale, scale, scale ); 00198 00199 // Setup colour and normal to be used when drawing the firefly 00200 glColor4f( 0.f, 0.93f, 0.87f, 1.f ); 00201 glNormal3f( 0.f, 0.f, 1.f ); 00202 00203 // Use texture unit 0 to render the glow 00204 glClientActiveTexture(GL_TEXTURE0); 00205 glActiveTexture( GL_TEXTURE0 ); 00206 glEnable( GL_TEXTURE_2D ); 00207 00208 // Set blending function translucency and enable blending 00209 glBindTexture( GL_TEXTURE_2D, iGlowTexture.iID ); 00210 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00211 glEnable( GL_BLEND ); 00212 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR ); 00213 00214 // First draw the "halo" for the firefly 00215 glVertexPointer( 3, GL_FLOAT, 0, KGlowVerticeData ); 00216 glTexCoordPointer( 2, GL_FLOAT, 0, KGlowTexCoordsData ); 00217 glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, KGlowIndices ); 00218 00219 // Disable texturing and blending before drawing the firefly itself 00220 glBlendFunc( GL_SRC_ALPHA,GL_ONE ); 00221 glDisable( GL_TEXTURE_2D ); 00222 glDisableClientState( GL_TEXTURE_COORD_ARRAY ); 00223 00224 // Draw the firefly as a dot 00225 glColor4f( 1.f, 1.f, 1.f, 1.f ); 00226 glVertexPointer( 3, GL_FLOAT, 0, KFlyVerticeData ); 00227 glDrawArrays( GL_LINES, 0, 4 ); 00228 00229 glPopMatrix(); 00230 00231 // Restore states and translation 00232 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00233 glEnableClientState( GL_COLOR_ARRAY ); 00234 }
|
© Nokia 2009 |