|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : MultiTex.cpp 00004 * Part of : OpenGLEx / MultiTex 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 00014 // INCLUDE FILES 00015 #include "MultiTex.h" 00016 #include <e32std.h> 00017 #include <e32math.h> 00018 #include "MultiTexContainer.h" // For accessing the contexts etc. 00019 #include "model.h" 00020 00021 // CONSTANTS 00022 00023 /* Define vertice coordinates for the triangles */ 00024 static const GLbyte LightVertices[8 * 3] = 00025 { 00026 -10, 10, 0, 00027 -10, -10, 0, 00028 10, 10, 0, 00029 10, -10, 0, 00030 00031 -10, 10, 0, 00032 -10, -10, 0, 00033 10, 10, 0, 00034 10, -10, 0 00035 }; 00036 00037 00038 /* Macro for changing the input texture coordinate values from 00039 GLubyte [0,255] to GLbyte [-128,127]. See more info below. */ 00040 #define tex(u,v) (GLbyte)( (u) - 128 ) , (GLbyte)( (v) - 128 ) 00041 00042 static const GLbyte LightTexCoords[8 * 2] = 00043 { 00044 // Red light 00045 tex(0,0), 00046 tex(0,255), 00047 tex(127,0), 00048 tex(127,255), 00049 00050 // Green light 00051 tex(127,0), 00052 tex(127,255), 00053 tex(255,0), 00054 tex(255,255) 00055 }; 00056 00057 00058 00059 // ============================ MEMBER FUNCTIONS =============================== 00060 00061 // ----------------------------------------------------------------------------- 00062 // CMultiTex::NewL 00063 // 00064 // The usual Symbian NewL implementation. 00065 // Creates the object, pushes it to cleanup stack and calls ContructL. 00066 // Returns the contructed object or leaves. 00067 // ----------------------------------------------------------------------------- 00068 // 00069 00070 CMultiTex* CMultiTex::NewL( TUint aWidth, TUint aHeight ) 00071 { 00072 /* Symbian 2-phase constructor. Calls both the default 00073 C++ constructor and Symbian ConstructL methods */ 00074 CMultiTex* self = new (ELeave) CMultiTex( aWidth, aHeight ); 00075 CleanupStack::PushL( self ); 00076 self->ConstructL(); 00077 CleanupStack::Pop(); 00078 00079 return self; 00080 } 00081 00082 // ----------------------------------------------------------------------------- 00083 // CMultiTex::CMultiTex 00084 // 00085 // C++ constructor. Initializes the size instance variables from arguments. 00086 // ----------------------------------------------------------------------------- 00087 // 00088 00089 CMultiTex::CMultiTex( TUint aWidth, TUint aHeight ) : 00090 CFiniteStateMachine(), 00091 iScreenWidth( aWidth ), 00092 iScreenHeight( aHeight ) 00093 { 00094 if ( iScreenHeight != 0 ) 00095 { 00096 iAspectRatio = (GLfloat)iScreenWidth / (GLfloat)iScreenHeight; 00097 } 00098 else 00099 { 00100 iAspectRatio = 1.0; 00101 } 00102 } 00103 00104 // ----------------------------------------------------------------------------- 00105 // CMultiTex::~CMultiTex 00106 // 00107 // C++ destructor. 00108 // ----------------------------------------------------------------------------- 00109 // 00110 00111 CMultiTex::~CMultiTex() 00112 { 00113 } 00114 00115 // ----------------------------------------------------------------------------- 00116 // CMultiTex::ConstructL 00117 // 00118 // 2nd phase constructor. 00119 // ----------------------------------------------------------------------------- 00120 // 00121 00122 void CMultiTex::ConstructL( void ) 00123 { 00124 } 00125 00126 // ----------------------------------------------------------------------------- 00127 // CMultiTex::CalculateEnvLightsL 00128 // 00129 // Texture map consist of two lights: red and green. 00130 // Alpha is calculated so that is gradually goes from 255 to zero starting 00131 // from the center of the spotlight. 00132 // ----------------------------------------------------------------------------- 00133 // 00134 void CMultiTex::CalculateLightsL( void ) 00135 { 00136 // Reserve memory for light texture 00137 iLightTex = new (ELeave) TUint8[64*32*4]; 00138 if ( !iLightTex ) 00139 { 00140 User::LeaveNoMemory(); 00141 } 00142 00143 TInt d = 0; // distance from the center of the spot 00144 GLfloat alpha = 0.f; // alpha value 00145 00146 for ( TInt i = 0; i < 32; i++ ) 00147 { 00148 for ( TInt j = 0; j < 32; j++ ) 00149 { 00150 // distance from the center of spotlight 00151 d = (i-16)*(i-16) + (j-16)*(j-16); 00152 00153 // simple linear attenuation used for alpha 00154 alpha = 255.f*(1.f-(1.f/250.f)*(GLfloat)(d)); 00155 00156 // alpha < 0 corresponds to spotlight cut-off value 00157 if ( alpha < 0.f ) 00158 { 00159 iLightTex[64*4*i+4*j] = 255; //r 00160 iLightTex[64*4*i+4*j+1] = 0; //g 00161 iLightTex[64*4*i+4*j+2] = 0; //b 00162 iLightTex[64*4*i+4*j+3] = 0; //a 00163 00164 iLightTex[64*4*i+4*j+32*4] = 0; //r 00165 iLightTex[64*4*i+4*j+1+32*4] = 255; //g 00166 iLightTex[64*4*i+4*j+2+32*4] = 0; //b 00167 iLightTex[64*4*i+4*j+3+32*4] = 0; //a 00168 } 00169 // inside the cut-off, set alpha as calculated above/ 00170 else 00171 { 00172 iLightTex[64*4*i+4*j] = 255; //r 00173 iLightTex[64*4*i+4*j+1] = 0; //g 00174 iLightTex[64*4*i+4*j+2] = 0; //b 00175 iLightTex[64*4*i+4*j+3] = (GLubyte)(alpha); //a 00176 00177 iLightTex[64*4*i+4*j+32*4] = 0; //r 00178 iLightTex[64*4*i+4*j+1+32*4] = 255; //g 00179 iLightTex[64*4*i+4*j+2+32*4] = 0; //b 00180 iLightTex[64*4*i+4*j+3+32*4] = (GLubyte)(alpha); //a 00181 } 00182 } 00183 } 00184 } 00185 00186 00187 // ----------------------------------------------------------------------------- 00188 // CMultiTex::AppInit 00189 // 00190 // Initializes OpenGL ES, sets the vertex and color arrays and pointers, 00191 // and selects the shading mode. 00192 // ----------------------------------------------------------------------------- 00193 // 00194 00195 void CMultiTex::AppInitL( TAny* aInstance ) 00196 { 00197 00198 CMultiTexContainer* instance = (CMultiTexContainer*) aInstance; 00199 00200 /*=========================================================================*/ 00201 /*===================== Initialize Pbuffer context ========================*/ 00202 /*=========================================================================*/ 00203 00204 /* Bind the pbuffer context to the current rendering thread and surface.*/ 00205 eglMakeCurrent( instance->iEglDisplay, instance->iEglPbufferSurface, 00206 instance->iEglPbufferSurface, instance->iEglPbufferContext ); 00207 00208 /* Create light texture map */ 00209 CalculateLightsL(); 00210 const GLvoid *texPtr = iLightTex; 00211 00212 // Set the screen background color (black). 00213 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00214 00215 // Initialize viewport and projection. 00216 glViewport( 0, 0, 128, 128 ); 00217 glMatrixMode( GL_PROJECTION ); 00218 glFrustumf( -1.f, 1.f, -1.f, 1.f, 3.f, 1000.f ); 00219 00220 /* Initialize appropriate texture matrix. First we have to translate the 00221 input texture coordinate values to be within a range of [0,255]. Hence 00222 we translate the x- and y-coordinate values by 128. Recall that the 00223 values in nokTexCoords are between [-128,127], now they are in a range 00224 of [0,255]. After that we scale the values by 1/255 to make the values 00225 to be in range [0,1]. */ 00226 glMatrixMode( GL_TEXTURE ); 00227 glLoadIdentity(); 00228 glScalef( 1.0f/255.0f, 1.0f/255.0f, 1.0f ); 00229 glTranslatef( 128.0f, 128.0f, 0.0f ); 00230 00231 // Remember to change the matrix mode to modelview. 00232 glMatrixMode( GL_MODELVIEW ); 00233 00234 // Enable back face culling, texturing and blending. 00235 glEnable( GL_CULL_FACE ); 00236 glEnable( GL_TEXTURE_2D ); 00237 glEnable( GL_BLEND ); 00238 glBlendFunc( GL_SRC_ALPHA, GL_ONE ); 00239 00240 // Enable vertex arrays. 00241 glEnableClientState( GL_VERTEX_ARRAY ); 00242 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00243 00244 // Set array pointers. 00245 glVertexPointer( 3, GL_BYTE, 0, LightVertices ); 00246 glTexCoordPointer( 2, GL_BYTE, 0, LightTexCoords ); 00247 00248 // Set the initial shading mode 00249 glShadeModel( GL_FLAT ); 00250 00251 /* Create texture names */ 00252 glGenTextures( 2, iLightTexObjects ); 00253 00254 // Bind and set the light texture. 00255 glBindTexture( GL_TEXTURE_2D, iLightTexObjects[0] ); 00256 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 64, 32, 00257 0, GL_RGBA, GL_UNSIGNED_BYTE, texPtr ); 00258 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00259 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD ); 00260 00261 /* We can now free the memory reserved for light texture, 00262 since it's loaded inside OpenGL ES */ 00263 delete[] iLightTex; 00264 iLightTex = NULL; 00265 00266 /* Bind and set the environment map texture. We pass NULL as pixels, 00267 since we use glCopyTexImage2D later to initialize the texture memory. */ 00268 glBindTexture( GL_TEXTURE_2D, iLightTexObjects[1] ); 00269 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 128, 128, 00270 0, GL_RGB, GL_UNSIGNED_BYTE, NULL ); 00271 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00272 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); 00273 00274 // Do not use perspective correction 00275 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00276 00277 00278 /*=========================================================================*/ 00279 /*====================== Initialize Window context ========================*/ 00280 /*=========================================================================*/ 00281 00282 00283 // Bind the Window context to the current rendering thread and surface. 00284 eglMakeCurrent( instance->iEglDisplay, instance->iEglSurface, 00285 instance->iEglSurface, instance->iEglContext ); 00286 00287 // Set the screen background color. 00288 glClearColor( 0.f, 0.f, 0.4f, 1.f ); 00289 00290 /* Enable depth testing and back face culling. */ 00291 glEnable( GL_DEPTH_TEST ); 00292 glEnable( GL_CULL_FACE ); 00293 glEnable( GL_TEXTURE_2D ); 00294 00295 // Initialize viewport and projection. 00296 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00297 glMatrixMode( GL_PROJECTION ); 00298 00299 // Calculate the view frustrum 00300 glFrustumf( FRUSTUM_LEFT * iAspectRatio, FRUSTUM_RIGHT * iAspectRatio, 00301 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00302 FRUSTUM_NEAR, FRUSTUM_FAR ); 00303 00304 00305 // Initialize appropriate texture matrix 00306 glMatrixMode( GL_TEXTURE ); 00307 glLoadIdentity(); 00308 glScalef( 1.0f/32767.0f, 1.0f/32767.0f, 1.0f ); 00309 00310 // Remember to change the matrix mode to modelview. 00311 glMatrixMode( GL_MODELVIEW ); 00312 00313 // Set shading mode 00314 glShadeModel( GL_FLAT ); 00315 00316 // Do not use perspective correction 00317 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00318 00319 00320 // Construct a texture manager that uses the application's private 00321 // directory as the location for all textures. 00322 iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenWidth, 00323 iAspectRatio*1.f, iAspectRatio*-1.f, 1.f, -1.f, 3.f, 00324 this ); 00325 00326 // Pushing the textures into the loading queue. 00327 _LIT(KWorldTexture, "world.jpg"); 00328 iTextureManager->RequestToLoad(KWorldTexture, &iWorldTexture ); 00329 00330 //Start to load the textures. 00331 iTextureManager->DoLoadL(); 00332 00333 } 00334 00335 // ------------------------------------------------------------------------------------------------------- 00336 // CBillboard::OnStartLoadingTextures() 00337 // Called for a MTextureLoadingListener by the texture manager when texture loading operation starts 00338 // ------------------------------------------------------------------------------------------------------- 00339 00340 void CMultiTex::OnStartLoadingTexturesL() 00341 { 00342 SetStateL( ELoadingTextures ); 00343 } 00344 00345 // ------------------------------------------------------------------------------------------------------------ 00346 // CBillboard::OnEndLoadingTextures() 00347 // Called for a MTextureLoadingListener by the texture manager when texture loading operation is completed 00348 // ------------------------------------------------------------------------------------------------------------ 00349 void CMultiTex::OnEndLoadingTexturesL() 00350 { 00351 /* TEXTURE0 is used for Marble texture */ 00352 glActiveTexture( GL_TEXTURE0 ); 00353 glClientActiveTexture( GL_TEXTURE0 ); 00354 00355 // Enable texturing 00356 glEnable( GL_TEXTURE_2D ); 00357 00358 // Enable vertex and texture coordinates arrays. 00359 glEnableClientState( GL_VERTEX_ARRAY ); 00360 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00361 00362 // Set array pointers. 00363 glVertexPointer( 3, GL_SHORT, 0, objVertex ); 00364 glTexCoordPointer( 2, GL_SHORT, 0, objTexCoords ); 00365 00366 /* Bind the marble.jpg texture to iWorldTexture, set the texture 00367 to the texPtr defined above, and set the texture environment. */ 00368 glBindTexture( GL_TEXTURE_2D, iWorldTexture.iID ); 00369 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); 00370 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00371 00372 /* Texture to TEXTURE1 is taken from Pbuffer 00373 context, i.e, iLightTexObjects[1] */ 00374 glActiveTexture( GL_TEXTURE1 ); 00375 glClientActiveTexture( GL_TEXTURE1 ); 00376 00377 // Enable texturing 00378 glEnable( GL_TEXTURE_2D ); 00379 00380 // Enable vertex and texture coordinates arrays. 00381 glEnableClientState( GL_VERTEX_ARRAY ); 00382 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00383 00384 // Set array pointers. 00385 glVertexPointer( 3, GL_SHORT, 0, objVertex ); 00386 glTexCoordPointer( 3, GL_SHORT, 0, envNormals ); 00387 00388 /* Bind the environment map texture created with Pbuffer context. 00389 Actual creation and updating is done in AppCycle. */ 00390 glBindTexture( GL_TEXTURE_2D, iLightTexObjects[1] ); 00391 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00392 00393 // Set the texture env mode for environment map texture 00394 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD ); 00395 00396 if ( GetState() == ELoadingTextures ) 00397 { 00398 SetStateL( ERunning ); 00399 } 00400 } 00401 00402 // --------------------------------------------------------- 00403 // CBillboard::OnEnterState( TInt aState ) 00404 // Called by TFiniteStateMachine when the f.s.m enters a new state 00405 // --------------------------------------------------------- 00406 00407 void CMultiTex::OnEnterStateL( TInt /*aState*/ ) 00408 { 00409 // Nothing to do here... 00410 } 00411 00412 // ----------------------------------------------------------------------------- 00413 // CMultiTex::AppExit 00414 // 00415 // Release any allocations made in AppInit. 00416 // ----------------------------------------------------------------------------- 00417 // 00418 00419 void CMultiTex::AppExit( void ) 00420 { 00421 delete iTextureManager; 00422 } 00423 00424 // ----------------------------------------------------------------------------- 00425 // CMultiTex::DrawLight 00426 // 00427 // Draws lights. 00428 // ----------------------------------------------------------------------------- 00429 // 00430 00431 void CMultiTex::DrawLights( TInt iFrame ) 00432 { 00433 // Draw red light 00434 glPushMatrix(); 00435 glTranslatex(( 2 << 16) + (GLint) (sin(iFrame * 0.10f) * 5 * 65536) , (2 << 16) + (GLint) (cos(iFrame * 0.13f) * 5 * 65536) , 0 ); 00436 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); 00437 glPopMatrix(); 00438 00439 // Draw green light 00440 glPushMatrix(); 00441 glTranslatex( (-2 << 16) + (GLint) (sin(iFrame * 0.10f) * 5 * 65536) , (-2 << 16) + (GLint) (cos(iFrame * 0.10f) * 8 * 65536) , 0 ); 00442 glDrawArrays( GL_TRIANGLE_STRIP, 4, 4 ); 00443 glPopMatrix(); 00444 00445 } 00446 00447 // ----------------------------------------------------------------------------- 00448 // CMultiTex::AppCyclePbuffer 00449 // 00450 // Create a dynamic environment map, where two spot lights (red, green) moves 00451 // ----------------------------------------------------------------------------- 00452 // 00453 00454 void CMultiTex::AppCyclePbuffer( TInt iFrame ) 00455 { 00456 glClear( GL_COLOR_BUFFER_BIT ); 00457 00458 // Bind the light texture as current 00459 glBindTexture( GL_TEXTURE_2D, iLightTexObjects[0] ); 00460 00461 /* Animate and draw lights */ 00462 glLoadIdentity(); 00463 gluLookAt( 0.f, 0.f, 200.f, 00464 0.f, 0.f, 0.f, 00465 0.f, 1.f, 0.f ); 00466 00467 glScalex( 2 << 16, 2 << 16, 2 << 16 ); 00468 DrawLights( iFrame ); 00469 00470 // Bind the environment map texture as current 00471 glBindTexture( GL_TEXTURE_2D, iLightTexObjects[1] ); 00472 00473 // Copy the content of the pbuffer to the currently bound texture 00474 glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 128, 128, 0 ); 00475 } 00476 00477 // ----------------------------------------------------------------------------- 00478 // CMultiTex::AppCycleWindow 00479 // 00480 // Draw the sphere with Marble and dynamic environment textures 00481 // ----------------------------------------------------------------------------- 00482 // 00483 00484 void CMultiTex::AppCycleWindow( TInt iFrame ) 00485 { 00486 // Set the TEXTURE1 unit as active (env map texture) 00487 glActiveTexture( GL_TEXTURE1 ); 00488 glClientActiveTexture( GL_TEXTURE1 ); 00489 00490 // Set the correct texture matrix 00491 glMatrixMode( GL_TEXTURE ); 00492 glLoadIdentity(); 00493 glTranslatef( 0.5f, 0.5f, 0.5f ); 00494 glRotatex( 180 << 16, 0, 0, 1 << 16 ); 00495 glRotatex( iFrame << 16, 0, 1 << 16, 0 ); 00496 glScalef( 1.0f/65534.0f, 1.0f/65534.0f, 1.0f/65534.0f ); 00497 00498 glMatrixMode( GL_MODELVIEW ); 00499 00500 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 00501 00502 00503 glLoadIdentity(); 00504 glTranslatex( 0 , 0 , -200 << 16 ); 00505 glRotatex( 180 << 16, 0, 0, 1 << 16 ); 00506 00507 00508 glRotatex( iFrame << 16, 0, 1 << 16, 0 ); 00509 00510 00511 // Scale the world model to fit the screen. 00512 glScalex( 1 << 9, 1 << 9, 1 << 9 ); 00513 glDrawElements( GL_TRIANGLES, objFaces * 3, 00514 GL_UNSIGNED_SHORT, objIndices ); 00515 } 00516 00517 // ----------------------------------------------------------------------------- 00518 // CMultiTex::SetScreenSize 00519 // Reacts to the dynamic screen size change during execution of this program. 00520 // ----------------------------------------------------------------------------- 00521 // 00522 void CMultiTex::SetScreenSize( TUint aWidth, TUint aHeight ) 00523 { 00524 iScreenWidth = aWidth; 00525 iScreenHeight = aHeight; 00526 00527 // Notify the texture manager of screen size change 00528 iTextureManager->SetScreenSize( aWidth, aHeight ); 00529 00530 // Reinitialize viewport and projection. 00531 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00532 00533 // Recalculate the view frustrum 00534 if ( iScreenHeight != 0 ) 00535 { 00536 iAspectRatio = (GLfloat)iScreenWidth / (GLfloat)iScreenHeight; 00537 } 00538 else 00539 { 00540 iAspectRatio = 1.0; 00541 } 00542 00543 glMatrixMode( GL_PROJECTION ); 00544 glLoadIdentity(); 00545 glFrustumf( FRUSTUM_LEFT * iAspectRatio, FRUSTUM_RIGHT * iAspectRatio, 00546 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00547 FRUSTUM_NEAR, FRUSTUM_FAR ); 00548 glMatrixMode( GL_PROJECTION ); 00549 00550 } 00551 00552 00553 // End of File
|
© Nokia 2009 |