|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : BumpMap.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 #include <aknnotewrappers.h> 00015 #include <e32math.h> 00016 00017 #include "bumpmap.h" 00018 00019 // MACROS 00020 #define FRUSTUM_LEFT -1.f //left vertical clipping plane 00021 #define FRUSTUM_RIGHT +1.f //right vertical clipping plane 00022 #define FRUSTUM_BOTTOM -1.f //bottom horizontal clipping plane 00023 #define FRUSTUM_TOP +1.f //top horizontal clipping plane 00024 #define FRUSTUM_NEAR +2.f //near depth clipping plane 00025 #define FRUSTUM_FAR +1000.f //far depth clipping plane 00026 00027 // Size of the bumpmapped rectangle 00028 #define BUMPMAP_SIZE 75.f 00029 // Bumpmapped rectangle's z coordinate 00030 #define BUMPMAP_Z_COORD 0.f 00031 // Number of vertices per side in the drawn bumpmapped rectangle has. 00032 #define NUM_VERTICES_PER_SIDE 7 00033 00034 // CONSTANTS 00035 /** 00036 * Fixed tangent matrix that works for this demo. 00037 * For info on how to calculate the tangent matrix in other cases see: 00038 * http://www.blacksmith-studios.dk/projects/downloads/tangent_matrix_derivation.php 00039 * and 00040 * http://www.codesampler.com/oglsrc/oglsrc_4.htm#ogl_dot3_bump_mapping 00041 */ 00042 static const GLfloat tangentMatrix[] = 00043 { 00044 1.f, 0.f, 0.f, 0.f, 00045 0.f,-1.f, 0.f, 0.f, 00046 0.f, 0.f, 1.f, 0.f, 00047 0.f, 0.f, 0.f, 1.f 00048 }; 00049 00050 00051 00052 // ============================= MEMBER FUNCTIONS ============================== 00053 00054 // ----------------------------------------------------------------------------- 00055 // CBumpMap::NewL 00056 // Symbian 2-phase constructor. Can leave. 00057 // ----------------------------------------------------------------------------- 00058 // 00059 CBumpMap* CBumpMap::NewL( GLuint aWidth, GLuint aHeight ) 00060 { 00061 // Symbian 2-phase constructor. Calls both the default 00062 // C++ constructor and Symbian ConstructL methods 00063 CBumpMap* self = new (ELeave) CBumpMap( aWidth, aHeight ); 00064 00065 CleanupStack::PushL( self ); 00066 self->ConstructL(); 00067 CleanupStack::Pop(); 00068 00069 return self; 00070 } 00071 00072 // ---------------------------------------------------------------------------- 00073 // CBumpMap::CBumpMap 00074 // C++ default constructor can NOT contain any code, that 00075 // might leave. 00076 // ---------------------------------------------------------------------------- 00077 // 00078 CBumpMap::CBumpMap( GLuint aWidth, GLuint aHeight ) 00079 : CFiniteStateMachine() 00080 { 00081 iScreenWidth = aWidth; 00082 iScreenHeight = aHeight; 00083 iTextureManager = NULL; 00084 } 00085 00086 // ---------------------------------------------------------------------------- 00087 // CBumpMap::ConstructL 00088 // Symbian 2nd phase constructor can leave. 00089 // ---------------------------------------------------------------------------- 00090 // 00091 void CBumpMap::ConstructL( void ) 00092 { 00093 } 00094 00095 // ---------------------------------------------------------------------------- 00096 // CBumpMap::~CBumpMap() 00097 // Destructor. 00098 // ---------------------------------------------------------------------------- 00099 // 00100 CBumpMap::~CBumpMap() 00101 { 00102 } 00103 00104 // ---------------------------------------------------------------------------- 00105 // CBumpMap::AppInit 00106 // Application initialization code. 00107 // ---------------------------------------------------------------------------- 00108 // 00109 void CBumpMap::AppInitL( void ) 00110 { 00111 // Construct a texture manager that uses the application's private 00112 // directory as the source location for all textures. 00113 iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenWidth, 00114 FRUSTUM_TOP, FRUSTUM_BOTTOM, 00115 FRUSTUM_RIGHT, FRUSTUM_LEFT, 00116 FRUSTUM_NEAR, this ); 00117 00118 // Initialize viewport and projection. 00119 SetScreenSize( iScreenWidth, iScreenHeight ); 00120 00121 // Reset texture matrix 00122 glMatrixMode( GL_TEXTURE ); 00123 glLoadIdentity(); 00124 glMatrixMode( GL_MODELVIEW ); 00125 00126 // Set the screen background color. 00127 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00128 00129 // Enable needed features 00130 glEnable( GL_CULL_FACE ); 00131 glEnable( GL_TEXTURE_2D ); 00132 glDisable( GL_DEPTH_TEST ); 00133 glDisable( GL_LIGHTING ); 00134 00135 // Enable vertex arrays and texture arrays. 00136 glEnableClientState( GL_VERTEX_ARRAY ); 00137 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00138 00139 // Do not use perspective correction 00140 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00141 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); 00142 glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); 00143 00144 GenerateBumpmapVerticesL(); 00145 00146 iLightSource = CFireFly::NewL( -45.f, 45.f, // Min and max X-coordinates 00147 -45.f, 45.f, // Min and max Y-coordinates 00148 7.f, 25.f, // Min and max Z-coordinates 00149 iTextureManager ); // Texture manager used to load any textures 00150 00151 // Push the textures into the loading queue. 00152 _LIT( KBumpMapName, "bumpmap.jpg" ); 00153 iTextureManager->RequestToLoad( KBumpMapName, &iBumpmapTexture, false ); 00154 _LIT( KTextureMapName, "texmap.jpg" ); 00155 iTextureManager->RequestToLoad( KTextureMapName, &iTexture, false ); 00156 00157 // Start to load the textures. 00158 iTextureManager->DoLoadL(); 00159 } 00160 00161 // ---------------------------------------------------------------------------- 00162 // CBumpMap::AppExit 00163 // Application cleanup code. 00164 // ---------------------------------------------------------------------------- 00165 // 00166 void CBumpMap::AppExit( void ) 00167 { 00168 delete[] iBumpmapVertices; 00169 delete[] iBumpmapColors; 00170 delete[] iBumpmapTexCoords; 00171 delete[] iBumpmapIndices; 00172 00173 delete iLightSource; 00174 00175 delete iTextureManager; 00176 } 00177 00178 00179 // ---------------------------------------------------------------------------- 00180 // CBumpMap::AppCycle 00181 // Draws and animates the objects. 00182 // aFrame = Number of the frame to be rendered. 00183 // aTimeSecs = Seconds elapsed since the application started running. 00184 // aDeltaTimeSecs = Seconds elapsed since last call to AppCycle(). 00185 // ---------------------------------------------------------------------------- 00186 // 00187 void CBumpMap::AppCycle( GLuint aFrame, 00188 GLfloat aTimeSecs, 00189 GLfloat aDeltaTimeSecs ) 00190 { 00191 // Clear the color and depth buffers 00192 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 00193 00194 glLoadIdentity(); 00195 gluLookAt( 0.f, 0.f, 150.f, // Position 00196 0.f, 0.f, 0.f, // Look At 00197 0.f, 1.f, 0.f ); // Up Vector 00198 00199 // Position the lightsource 00200 iLightSource->DoMovement( aFrame, aTimeSecs, aDeltaTimeSecs ); 00201 00202 // Draw the bumpmapping 00203 DrawBumpmappedRect(); 00204 00205 // Draw the lightsource as firefly 00206 iLightSource->Draw(); 00207 } 00208 00209 // ---------------------------------------------------------------------------- 00210 // CBumpMap::OnEnterState( TInt aState ) 00211 // Called by TFiniteStateMachine when the f.s.m enters a new state 00212 // ---------------------------------------------------------------------------- 00213 // 00214 void CBumpMap::OnEnterStateL( TInt /*aState*/ ) 00215 { 00216 } 00217 00218 // ---------------------------------------------------------------------------- 00219 // CBumpMap::OnStartLoadingTextures() 00220 // Called for a MTextureLoadingListener by the texture manager when texture 00221 // loading operation starts 00222 // ---------------------------------------------------------------------------- 00223 // 00224 void CBumpMap::OnStartLoadingTexturesL() 00225 { 00226 SetStateL( ELoadingTextures ); 00227 } 00228 00229 // ---------------------------------------------------------------------------- 00230 // CBumpMap::OnEndLoadingTextures() 00231 // Called for a MTextureLoadingListener by the texture manager when texture 00232 // loading operation is completed 00233 // ---------------------------------------------------------------------------- 00234 // 00235 void CBumpMap::OnEndLoadingTexturesL() 00236 { 00237 if ( GetState() == ELoadingTextures ) 00238 { 00239 SetStateL( ERunning ); 00240 } 00241 } 00242 00243 // ---------------------------------------------------------------------------- 00244 // CBumpMap::SetScreenSize 00245 // Reacts to the dynamic screen size change during execution of this program. 00246 // ---------------------------------------------------------------------------- 00247 // 00248 void CBumpMap::SetScreenSize( GLuint aWidth, GLuint aHeight ) 00249 { 00250 iScreenWidth = aWidth; 00251 iScreenHeight = aHeight; 00252 00253 // Notify the texture manager of screen size change 00254 iTextureManager->SetScreenSize( aWidth, aHeight ); 00255 00256 // Reinitialize viewport and projection. 00257 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00258 00259 // Recalculate the view frustrum 00260 glMatrixMode( GL_PROJECTION ); 00261 glLoadIdentity(); 00262 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00263 glFrustumf( FRUSTUM_LEFT * aspectRatio, 00264 FRUSTUM_RIGHT * aspectRatio, 00265 FRUSTUM_BOTTOM, 00266 FRUSTUM_TOP, 00267 FRUSTUM_NEAR, 00268 FRUSTUM_FAR ); 00269 glMatrixMode( GL_MODELVIEW ); 00270 } 00271 00272 // ---------------------------------------------------------------------------- 00273 // CBumpMap::DrawBumpmappedRect 00274 // Draws the bumpmapped rectangle. 00275 // ---------------------------------------------------------------------------- 00276 // 00277 void CBumpMap::DrawBumpmappedRect() 00278 { 00279 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00280 glEnableClientState( GL_COLOR_ARRAY ); 00281 glDisable( GL_BLEND ); 00282 00283 // dot3 bump mapping computes (N.L) per pixel. we do this by storing the N 00284 // vector in a texture (known as the dot3 bump map) and then L is computed 00285 // per vertex and is stored at each vertex as a color. This color is 00286 // interpolated between vertices by OpenGL when the shade model is set to GL_SMOOTH. 00287 // N.L is computed by the texture combiner and then modulated with the base texture. 00288 // 00289 // Note that L vector must be rotated into tangent space (aka texture space) so 00290 // that it is in the same space as N. 00291 00292 // UNIT 0 00293 // Configure the first texture combiner to calculate the dot product of 00294 // bumpmap normal vector N (stored in the bumpmapping texture) and 00295 // light angle vector L (stored as the PRIMARY_COLOR). 00296 glClientActiveTexture(GL_TEXTURE0); 00297 glActiveTexture( GL_TEXTURE0 ); 00298 glEnable( GL_TEXTURE_2D ); 00299 // Use first loaded texture as bumpmap 00300 glBindTexture( GL_TEXTURE_2D, iBumpmapTexture.iID ); 00301 // Configure the texture combiner to do the DOT3 bumpmapping calculation 00302 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); 00303 glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_DOT3_RGB ); 00304 // The first colour for each fragment operation is gotten from the bumpmap texture 00305 glTexEnvf( GL_TEXTURE_ENV, GL_SRC0_RGB, GL_TEXTURE ); 00306 glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); 00307 // The second colour for each fragment operation is gotten from the primary colour 00308 glTexEnvf( GL_TEXTURE_ENV, GL_SRC1_RGB, GL_PRIMARY_COLOR ); 00309 glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); 00310 glTexCoordPointer( 2, GL_FLOAT, 0, iBumpmapTexCoords ); 00311 00312 // UNIT 1 00313 // Configure the second texture combiner to modulate the base texture 00314 // by N.L 00315 glClientActiveTexture(GL_TEXTURE1); 00316 glActiveTexture( GL_TEXTURE1 ); 00317 glEnable( GL_TEXTURE_2D ); 00318 // Use second loaded texture as the "traditional" texture map 00319 glBindTexture( GL_TEXTURE_2D, iTexture.iID ); 00320 // Configure the texture combiner to modulate the colour with the texture 00321 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE ); 00322 glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE ); 00323 // The first colour for each fragment operation is gotten from the previous texture combiners output 00324 glTexEnvf( GL_TEXTURE_ENV, GL_SRC0_RGB, GL_PREVIOUS ); 00325 glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR ); 00326 // The second colour for each fragment operation is gotten from the texture bitmap 00327 glTexEnvf( GL_TEXTURE_ENV, GL_SRC1_RGB, GL_TEXTURE ); 00328 glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR ); 00329 glTexCoordPointer( 2, GL_FLOAT, 0, iBumpmapTexCoords ); 00330 00331 GLfloat lightX = iLightSource->iPosition.iX; 00332 GLfloat lightY = iLightSource->iPosition.iY; 00333 GLfloat lightZ = iLightSource->iPosition.iZ; 00334 TVector vectVert2Light; 00335 for( TInt y = 0; y < NUM_VERTICES_PER_SIDE; y++ ) 00336 { 00337 for( TInt x = 0; x < NUM_VERTICES_PER_SIDE; x++ ) 00338 { 00339 // Compute L and store as vertex color 00340 // Calculate the vector from vertice to light source 00341 TInt idx = x + y * NUM_VERTICES_PER_SIDE; 00342 00343 vectVert2Light.iX = lightX - iBumpmapVertices[idx * 3 ]; 00344 vectVert2Light.iY = lightY - iBumpmapVertices[idx * 3 + 1]; 00345 vectVert2Light.iZ = lightZ - iBumpmapVertices[idx * 3 + 2]; 00346 00347 // Normalize the vector 00348 vectVert2Light.Normalize(); 00349 00350 // Convert vector into tangent space 00351 vectVert2Light.MultMatrix4x4( tangentMatrix ); 00352 00353 // Scale and bias light vector values from range [-0.5, 0.5] to range [0.0, 1.0] 00354 vectVert2Light.iX = vectVert2Light.iX * 0.5 + 0.5; 00355 vectVert2Light.iY = vectVert2Light.iY * 0.5 + 0.5; 00356 vectVert2Light.iZ = vectVert2Light.iZ * 0.5 + 0.5; 00357 00358 // Store the light vector as vertex color 00359 iBumpmapColors[ idx * 4 ] = vectVert2Light.iX; 00360 iBumpmapColors[ idx * 4 + 1 ] = vectVert2Light.iY; 00361 iBumpmapColors[ idx * 4 + 2 ] = vectVert2Light.iZ; 00362 } 00363 } 00364 00365 // Setup the array pointers and draw the bumpmapped rectangle 00366 glVertexPointer( 3, GL_FLOAT, 0, iBumpmapVertices ); 00367 glColorPointer( 4, GL_FLOAT, 0, iBumpmapColors ); 00368 glNormal3f( 0.f, 0.f, 1.f ); 00369 glDrawElements( GL_TRIANGLES, (NUM_VERTICES_PER_SIDE - 1) * (NUM_VERTICES_PER_SIDE - 1) * 6, GL_UNSIGNED_BYTE, iBumpmapIndices ); 00370 00371 // reset texture unit 1 state 00372 glActiveTexture( GL_TEXTURE1 ); 00373 glMatrixMode( GL_TEXTURE ); 00374 glLoadIdentity(); 00375 glMatrixMode( GL_MODELVIEW ); 00376 glDisable( GL_TEXTURE_2D ); 00377 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 00378 00379 // reset texture unit 0 state 00380 glActiveTexture( GL_TEXTURE0 ); 00381 glMatrixMode( GL_TEXTURE ); 00382 glLoadIdentity(); 00383 glMatrixMode( GL_MODELVIEW ); 00384 glDisable( GL_TEXTURE_2D ); 00385 glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 00386 } 00387 00388 // ---------------------------------------------------------------------------- 00389 // CBumpMap::GenerateBumpmapVertices 00390 // Generates the vertices for drawing the bumpmapped rectangle to the screen. 00391 // ---------------------------------------------------------------------------- 00392 // 00393 void CBumpMap::GenerateBumpmapVerticesL() 00394 { 00395 iBumpmapVertices = new GLfloat[ NUM_VERTICES_PER_SIDE * NUM_VERTICES_PER_SIDE * 3]; 00396 iBumpmapTexCoords = new GLfloat[ NUM_VERTICES_PER_SIDE * NUM_VERTICES_PER_SIDE * 2]; 00397 00398 iBumpmapColors = new GLfloat[ NUM_VERTICES_PER_SIDE * NUM_VERTICES_PER_SIDE * 4]; 00399 iBumpmapIndices = new GLubyte[ (NUM_VERTICES_PER_SIDE - 1) * (NUM_VERTICES_PER_SIDE - 1) * 6]; 00400 00401 // Generate the texture coordinates, vertices, colors and normals 00402 GLfloat leftBottom = - BUMPMAP_SIZE / 2.f; 00403 GLfloat divisionSize = BUMPMAP_SIZE / (GLfloat) ( NUM_VERTICES_PER_SIDE - 1 ); 00404 TInt idx = 0; 00405 for( TInt y = 0; y < NUM_VERTICES_PER_SIDE; y++ ) 00406 { 00407 for( TInt x = 0; x < NUM_VERTICES_PER_SIDE; x++ ) 00408 { 00409 iBumpmapVertices[idx * 3 ] = leftBottom + x * divisionSize; 00410 iBumpmapVertices[idx * 3 + 1] = leftBottom + y * divisionSize; 00411 iBumpmapVertices[idx * 3 + 2] = BUMPMAP_Z_COORD; 00412 00413 iBumpmapTexCoords[idx * 2 ] = (GLfloat) x / (GLfloat) ( NUM_VERTICES_PER_SIDE - 1 ); 00414 iBumpmapTexCoords[idx * 2 + 1] = 1.f - (GLfloat) y / (GLfloat) ( NUM_VERTICES_PER_SIDE - 1 ); 00415 00416 iBumpmapColors[idx * 4 ] = 1.f; 00417 iBumpmapColors[idx * 4 + 1] = 1.f; 00418 iBumpmapColors[idx * 4 + 2] = 0.f; 00419 iBumpmapColors[idx * 4 + 3] = 1.f; 00420 00421 idx++; 00422 } 00423 } 00424 00425 // Generate the indices that are used to draw the 00426 // triangles that in turn form the rectangle 00427 idx = 0; 00428 for( TInt y = 0; y < NUM_VERTICES_PER_SIDE - 1; y++ ) 00429 { 00430 for( TInt x = 0; x < NUM_VERTICES_PER_SIDE - 1; x++ ) 00431 { 00432 // First triangle of the quad 00433 iBumpmapIndices[idx * 6 ] = x + y * NUM_VERTICES_PER_SIDE; 00434 iBumpmapIndices[idx * 6 + 2] = x + ( y + 1 ) * NUM_VERTICES_PER_SIDE; 00435 iBumpmapIndices[idx * 6 + 1] = ( x + 1 ) + ( y + 1 ) * NUM_VERTICES_PER_SIDE; 00436 // Second triangle of the quad 00437 iBumpmapIndices[idx * 6 + 3] = x + y * NUM_VERTICES_PER_SIDE; 00438 iBumpmapIndices[idx * 6 + 5] = ( x + 1 ) + ( y + 1 ) * NUM_VERTICES_PER_SIDE; 00439 iBumpmapIndices[idx * 6 + 4] = ( x + 1 ) + y * NUM_VERTICES_PER_SIDE; 00440 00441 idx++; 00442 } 00443 } 00444 } 00445 00446 // End of File
|
© Nokia 2009 |