|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : Texture.cpp 00004 * Part of : OpenGLEx / Texture 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 // INCLUDE FILES 00014 #include <e32std.h> 00015 #include "Texture.h" 00016 00017 // MACROS 00018 #define LIGHT_MAX (1 << 16) 00019 #define LIGHTCOLOR(r, g, b, a) \ 00020 (GLfixed)(r * LIGHT_MAX), \ 00021 (GLfixed)(g * LIGHT_MAX), \ 00022 (GLfixed)(b * LIGHT_MAX), \ 00023 (GLfixed)(a * LIGHT_MAX) 00024 00025 /* Define global ambient light. */ 00026 static const GLfixed globalAmbient[4] = { LIGHTCOLOR(0.0, 0.0, 0.0, 1.0) }; 00027 00028 /* Define lamp parameters. */ 00029 static const GLfixed lightDiffuseLamp[4] = { LIGHTCOLOR(1.0, 1.0, 1.0, 1.0) }; 00030 static const GLfixed lightAmbientLamp[4] = { LIGHTCOLOR(0.3, 0.3, 0.3, 1.0) }; 00031 static const GLfixed lightSpecularLamp[4] = { LIGHTCOLOR(0.5, 0.5, 0.5, 1.0) }; 00032 static const GLfixed lightPositionLamp[4] = { 0, 0, 10, 0 }; 00033 00034 /* Define vertice coordinates for the cube 00035 Duplication of vertices needed for texturing every surface of the cube */ 00036 static const GLbyte vertices[24 * 3] = 00037 { 00038 /* top */ 00039 -1, 1, 1, 00040 1, 1, 1, 00041 1, -1, 1, 00042 -1, -1, 1, 00043 00044 /* front */ 00045 1, 1, 1, 00046 1, 1, -1, 00047 1, -1, -1, 00048 1, -1, 1, 00049 00050 /* right */ 00051 -1, 1, 1, 00052 -1, 1, -1, 00053 1, 1, -1, 00054 1, 1, 1, 00055 00056 /* left */ 00057 1, -1, 1, 00058 1, -1, -1, 00059 -1, -1, -1, 00060 -1, -1, 1, 00061 00062 /* back */ 00063 -1, -1, 1, 00064 -1, -1, -1, 00065 -1, 1, -1, 00066 -1, 1, 1, 00067 00068 /* bottom */ 00069 -1, 1, -1, 00070 1, 1, -1, 00071 1, -1, -1, 00072 -1, -1, -1 00073 }; 00074 00075 /* Define normals for the cube */ 00076 static const GLbyte normals[24 * 3] = 00077 { 00078 /* top */ 00079 0,0,1, 00080 0,0,1, 00081 0,0,1, 00082 0,0,1, 00083 00084 /* front */ 00085 1,0,0, 00086 1,0,0, 00087 1,0,0, 00088 1,0,0, 00089 00090 /* right */ 00091 0,1,0, 00092 0,1,0, 00093 0,1,0, 00094 0,1,0, 00095 00096 /* left */ 00097 0,-1,0, 00098 0,-1,0, 00099 0,-1,0, 00100 0,-1,0, 00101 00102 /* back */ 00103 -1,0,0, 00104 -1,0,0, 00105 -1,0,0, 00106 -1,0,0, 00107 00108 /* bottom */ 00109 0,0,-1, 00110 0,0,-1, 00111 0,0,-1, 00112 0,0,-1 00113 }; 00114 00115 /* Define indices for drawing the triangles ( 5 sides of the cube ) 00116 Nasa Hubble texture is drawn on these sides. */ 00117 static const GLubyte triangles[10 * 3] = 00118 { 00119 /* top */ 00120 1,0,3, 00121 1,3,2, 00122 00123 /* front */ 00124 5,4,7, 00125 5,7,6, 00126 00127 /* right */ 00128 9,8,11, 00129 9,11,10, 00130 00131 /* left */ 00132 13,12,15, 00133 13,15,14, 00134 00135 /* back */ 00136 17,16,19, 00137 17,19,18 00138 }; 00139 00140 00141 /* The 6th side of the cube. 00142 ogles.jpg texture is drawn on this side. */ 00143 static const GLubyte triangles2[2 * 3] = 00144 { 00145 /* bottom */ 00146 21,22,23, 00147 21,23,20 00148 }; 00149 00150 00151 /* Macro for changing the input texture coordinate values from 00152 GLubyte [0,255] to GLbyte [-128,127]. See more info below. */ 00153 #define tex(u,v) (GLbyte)( (u) - 128 ) , (GLbyte)( (v) - 128 ) 00154 00155 00156 /* Input texture coordinates for each of the object vertices. 00157 The coordinates are given in GLbyte [-128,127] format. 00158 Since the texture picture coordinates are between values 00159 [0,255] for both width and height, we have defined a macro 00160 to change the input coordinates into a appropriate form. 00161 It is easier to think the texture coordinates as corresponding 00162 image pixel coordinates. This alone is not enough because 00163 if texture coordinates are not given between values [0,1], 00164 texture wrapping will occur. Therefore we need to 00165 scale the texture coordinates with appropriate texture matrix, 00166 which is defined in AppInit(). */ 00167 static const GLbyte nokTexCoords[24 * 2] = 00168 { 00169 /* top, whole texture nasa_hubble.h */ 00170 tex(0,0), 00171 tex(255,0), 00172 tex(255,255), 00173 tex(0,255), 00174 00175 /* front, spiral with tail */ 00176 tex(0,255), 00177 tex(127,255), 00178 tex(127,127), 00179 tex(0,127), 00180 00181 /* right, red nebulae */ 00182 tex(127,255), 00183 tex(255,255), 00184 tex(255,127), 00185 tex(127,127), 00186 00187 /* left, plasma cloud */ 00188 tex(0,127), 00189 tex(127,127), 00190 tex(127,0), 00191 tex(0,0), 00192 00193 /* back, 2 spirals */ 00194 tex(127,127), 00195 tex(255,127), 00196 tex(255,0), 00197 tex(127,0), 00198 00199 /* bottom, whole texture ogles.jpg */ 00200 tex(255,255), 00201 tex(255,0), 00202 tex(0,0), 00203 tex(0,255) 00204 }; 00205 00206 00207 // ============================ MEMBER FUNCTIONS =============================== 00208 00209 // ----------------------------------------------------------------------------- 00210 // CTexture::CTexture 00211 // C++ default constructor can NOT contain any code, that 00212 // might leave. 00213 // ----------------------------------------------------------------------------- 00214 // 00215 CTexture::CTexture( TUint aWidth, TUint aHeight ): CFiniteStateMachine() 00216 { 00217 iScreenWidth = aWidth; 00218 iScreenHeight = aHeight; 00219 } 00220 00221 00222 // ----------------------------------------------------------------------------- 00223 // CTexture::ConstructL 00224 // Symbian 2nd phase constructor can leave. 00225 // ----------------------------------------------------------------------------- 00226 // 00227 void CTexture::ConstructL( void ) 00228 { 00229 } 00230 00231 00232 // ----------------------------------------------------------------------------- 00233 // CTexture::NewL 00234 // Two-phased constructor. 00235 // ----------------------------------------------------------------------------- 00236 // 00237 CTexture* CTexture::NewL( TUint aWidth, TUint aHeight ) 00238 { 00239 /* Symbian 2-phase constructor. Calls both the default 00240 C++ constructor and Symbian ConstructL methods */ 00241 CTexture* self = new (ELeave) CTexture( aWidth, aHeight ); 00242 CleanupStack::PushL( self ); 00243 self->ConstructL(); 00244 CleanupStack::Pop(); 00245 00246 return self; 00247 } 00248 00249 00250 // Destructor 00251 CTexture::~CTexture() 00252 { 00253 } 00254 00255 00256 // ----------------------------------------------------------------------------- 00257 // CTexture::AppInit 00258 // OpenGL ES initialization code. Set the required OpenGL ES state 00259 // ----------------------------------------------------------------------------- 00260 // 00261 void CTexture::AppInitL() 00262 { 00263 // Construct a texture manager that uses the application's private 00264 // directory as the location for all textures. 00265 iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenHeight, 00266 FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR, 00267 this ); 00268 00269 // Initialize viewport and projection. 00270 SetScreenSize( iScreenWidth, iScreenHeight ); 00271 00272 /* Set the screen background color. */ 00273 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00274 00275 /* Enable back face culling, texturing, and normalization. */ 00276 glEnable( GL_CULL_FACE ); 00277 glEnable( GL_TEXTURE_2D ); 00278 glEnable( GL_NORMALIZE ); 00279 00280 /* Initialize appropriate texture matrix. First we have to translate the 00281 input texture coordinate values to be within a range of [0,255]. Hence 00282 we translate the x- and y-coordinate values by 128. Recall that the 00283 values in nokTexCoords are between [-128,127], now they are in a range 00284 of [0,255]. After that we scale the values by 1/255 to make the values 00285 to be in range [0,1]. */ 00286 glMatrixMode( GL_TEXTURE ); 00287 glLoadIdentity(); 00288 glScalef( 1.0f/255.0f, 1.0f/255.0f, 1.0f ); 00289 glTranslatef( 128.0f, 128.0f, 0.0f ); 00290 00291 /* Remember to change the matrix mode to modelview. */ 00292 glMatrixMode( GL_MODELVIEW ); 00293 00294 /* Enable vertex and texture arrays. */ 00295 glEnableClientState( GL_VERTEX_ARRAY ); 00296 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00297 glEnableClientState( GL_NORMAL_ARRAY ); 00298 00299 /* Set array pointers. */ 00300 glVertexPointer( 3, GL_BYTE, 0, vertices ); 00301 glTexCoordPointer( 2, GL_BYTE, 0, nokTexCoords ); 00302 glNormalPointer( GL_BYTE, 0, normals ); 00303 00304 /* Set up global ambient light. */ 00305 glLightModelxv( GL_LIGHT_MODEL_AMBIENT, globalAmbient ); 00306 00307 /* Set up lamp. */ 00308 glEnable( GL_LIGHT0 ); 00309 glLightxv( GL_LIGHT0, GL_DIFFUSE, lightDiffuseLamp ); 00310 glLightxv( GL_LIGHT0, GL_AMBIENT, lightAmbientLamp ); 00311 glLightxv( GL_LIGHT0, GL_SPECULAR, lightSpecularLamp ); 00312 glLightxv( GL_LIGHT0, GL_POSITION, lightPositionLamp ); 00313 00314 /* Set shading mode*/ 00315 glShadeModel( GL_FLAT ); 00316 00317 /* Set the blending function for translucency. */ 00318 glBlendFunc( GL_ONE, GL_ONE ); 00319 00320 /* Do not use perspective correction, can be enabled from the menu */ 00321 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00322 00323 /* Symbian menu item initialization. See CTextureAppUi.cpp for more. */ 00324 iPerspectiveCorrEnabled = EFalse; // no texture perspective correction 00325 iTextureWrapEnabled = EFalse; // no texture wrapping 00326 iLightingEnabled = EFalse; // no lighting 00327 iBlendingEnabled = EFalse; // no blending 00328 00329 // Pushing the textures into the loading queue. 00330 _LIT(KHubbleTexture, "nasa_hubble.jpg"); 00331 _LIT(KOGLESTexture, "ogles.jpg"); 00332 iTextureManager->RequestToLoad(KHubbleTexture, &iNasaHubble ); 00333 iTextureManager->RequestToLoad(KOGLESTexture, &iOpenGLES ); 00334 00335 //Start to load the textures. 00336 iTextureManager->DoLoadL(); 00337 } 00338 00339 00340 // ----------------------------------------------------------------------------- 00341 // CTexture::AppExit 00342 // Relase any allocations made in AppInit 00343 // ----------------------------------------------------------------------------- 00344 // 00345 void CTexture::AppExit( void ) 00346 { 00347 delete iTextureManager; 00348 } 00349 00350 00351 // ----------------------------------------------------------------------------- 00352 // CTexture::AppCycle 00353 // OpenGL ES main rendering loop. 00354 // ----------------------------------------------------------------------------- 00355 // 00356 void CTexture::AppCycle( TInt aFrame ) 00357 { 00358 glClear( GL_COLOR_BUFFER_BIT ); 00359 00360 /* Animate and draw the cube. */ 00361 glLoadIdentity(); 00362 gluLookAt( 0.f, 0.f, 120.f, // Camera position 00363 0.f, 0.f, 0.f, // Look at point 00364 0.f, 1.f, 0.f ); // Up direction 00365 00366 00367 glRotatex( aFrame << 16, 1 << 16, 0 , 0 ); 00368 glRotatex( aFrame << 15, 0 , 1 << 16, 0 ); 00369 glRotatex( aFrame << 14, 0 , 0 , 1 << 16 ); 00370 00371 /* Scale the cube coordinates to fit the screen. */ 00372 glScalex( 20 << 16, 20 << 16, 20 << 16 ); 00373 00374 /* Draw the first 5 sides of the cube with the texture Nasa Hubble. */ 00375 glBindTexture( GL_TEXTURE_2D, iNasaHubble.iID ); 00376 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00377 glDrawElements( GL_TRIANGLES, 10 * 3, GL_UNSIGNED_BYTE, triangles ); 00378 00379 /* Draw the remaining side of the cube with the ogles.jpg texture. */ 00380 glBindTexture( GL_TEXTURE_2D, iOpenGLES.iID ); 00381 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00382 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles2 ); 00383 } 00384 00385 // ------------------------------------------------------------------------------------------------------- 00386 // CBillboard::OnStartLoadingTextures() 00387 // Called for a MTextureLoadingListener by the texture manager when texture loading operation starts 00388 // ------------------------------------------------------------------------------------------------------- 00389 00390 void CTexture::OnStartLoadingTexturesL() 00391 { 00392 SetStateL( ELoadingTextures ); 00393 } 00394 00395 // ------------------------------------------------------------------------------------------------------------ 00396 // CBillboard::OnEndLoadingTextures() 00397 // Called for a MTextureLoadingListener by the texture manager when texture loading operation is completed 00398 // ------------------------------------------------------------------------------------------------------------ 00399 void CTexture::OnEndLoadingTexturesL() 00400 { 00401 if ( GetState() == ELoadingTextures ) 00402 { 00403 SetStateL( ERunning ); 00404 } 00405 } 00406 00407 // --------------------------------------------------------- 00408 // CBillboard::OnEnterState( TInt aState ) 00409 // Called by TFiniteStateMachine when the f.s.m enters a new state 00410 // --------------------------------------------------------- 00411 00412 void CTexture::OnEnterStateL( TInt /*aState*/ ) 00413 { 00414 // Nothing to do here... 00415 } 00416 00417 //---------------------------------------------------------- 00418 // The following methods are called by the CTextureAppUi 00419 // class when handling the incoming menu events. 00420 //---------------------------------------------------------- 00421 00422 00423 // ----------------------------------------------------------------------------- 00424 // CTexture::TogglePerspective 00425 // User defines whether or not to use perspective correction when rendering 00426 // ----------------------------------------------------------------------------- 00427 // 00428 void CTexture::TogglePerspective( void ) 00429 { 00430 if( iPerspectiveCorrEnabled) 00431 { 00432 iPerspectiveCorrEnabled = EFalse; 00433 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00434 } 00435 else 00436 { 00437 iPerspectiveCorrEnabled = ETrue; 00438 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); 00439 } 00440 } 00441 00442 /** 00443 * Enable/Disable texture wrapping. 00444 */ 00445 // ----------------------------------------------------------------------------- 00446 // CTexture::ToggleWrapping 00447 // Implement texture wrapping by changing the texture matrix scale 00448 // ----------------------------------------------------------------------------- 00449 // 00450 void CTexture::ToggleWrapping( void ) 00451 { 00452 if( iTextureWrapEnabled ) 00453 { 00454 iTextureWrapEnabled = EFalse; 00455 glMatrixMode( GL_TEXTURE ); 00456 glLoadIdentity(); 00457 glScalef( 1.0f/255.0f, 1.0f/255.0f, 1.0f ); 00458 glTranslatef( 128.0f, 128.0f, 1.0f ); 00459 glMatrixMode( GL_MODELVIEW ); 00460 } 00461 /* Texture wrapping is implemented here simply by changing the scale of 00462 texture matrix. Now the coordinate values are between [0,2], which 00463 causes the texture to wrap one time. */ 00464 else 00465 { 00466 iTextureWrapEnabled = ETrue; 00467 glMatrixMode( GL_TEXTURE ); 00468 glLoadIdentity(); 00469 glScalef( 1.0f/128.0f, 1.0f/128.0f, 1.0f ); 00470 glTranslatef( 128.0f, 128.0f, 1.0f ); 00471 glMatrixMode( GL_MODELVIEW ); 00472 } 00473 } 00474 00475 /** 00476 * Enable/Disable lighting. 00477 */ 00478 // ----------------------------------------------------------------------------- 00479 // CTexture::ToggleLighting 00480 // When using textures with lights the texenv must be set to GL_MODULATE, 00481 // and shade model must be GL_SMOOTH. 00482 // ----------------------------------------------------------------------------- 00483 // 00484 void CTexture::ToggleLighting( void ) 00485 { 00486 // If texture loading is still going on, return from this method without doing anything. 00487 if ( GetState() == ELoadingTextures ) 00488 { 00489 return; 00490 } 00491 00492 if( iLightingEnabled ) 00493 { 00494 iLightingEnabled = EFalse; 00495 glShadeModel( GL_FLAT ); // Don't need smooth shading when no lights 00496 glDisable( GL_LIGHTING ); // Disable lighting 00497 00498 /* Set both textures environments to GL_REPLACE. */ 00499 glBindTexture( GL_TEXTURE_2D, iNasaHubble.iID ); 00500 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); 00501 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00502 00503 glBindTexture( GL_TEXTURE_2D, iOpenGLES.iID ); 00504 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE ); 00505 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00506 } 00507 else 00508 { 00509 iLightingEnabled = ETrue; 00510 glShadeModel( GL_SMOOTH ); // For the lighting to work properly 00511 glEnable( GL_LIGHTING ); // Enable lighting 00512 00513 /* Set both textures environments to GL_MODULATE. */ 00514 glBindTexture( GL_TEXTURE_2D, iNasaHubble.iID ); 00515 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 00516 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00517 00518 glBindTexture( GL_TEXTURE_2D, iOpenGLES.iID ); 00519 glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 00520 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00521 } 00522 } 00523 00524 00525 // ----------------------------------------------------------------------------- 00526 // CTexture::ToggleBlending 00527 // User defines whether or not blending is enabled from the menu. 00528 // ----------------------------------------------------------------------------- 00529 // 00530 void CTexture::ToggleBlending( void ) 00531 { 00532 if( iBlendingEnabled ) 00533 { 00534 // Blending was enabled, disable blending 00535 iBlendingEnabled = EFalse; 00536 glDisable( GL_BLEND ); 00537 glEnable( GL_CULL_FACE ); 00538 } 00539 else 00540 { 00541 // Blending was disabled, enable blending 00542 iBlendingEnabled = ETrue; 00543 glEnable( GL_BLEND ); 00544 glDisable( GL_CULL_FACE ); 00545 } 00546 } 00547 00548 // ----------------------------------------------------------------------------- 00549 // CTexture::SetScreenSize 00550 // Reacts to the dynamic screen size change during execution of this program. 00551 // ----------------------------------------------------------------------------- 00552 // 00553 void CTexture::SetScreenSize( TUint aWidth, TUint aHeight ) 00554 { 00555 iScreenWidth = aWidth; 00556 iScreenHeight = aHeight; 00557 00558 // Notify the texture manager of screen size change 00559 iTextureManager->SetScreenSize( aWidth, aHeight ); 00560 00561 // Reinitialize viewport and projection. 00562 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00563 00564 // Recalculate the view frustrum 00565 glMatrixMode( GL_PROJECTION ); 00566 glLoadIdentity(); 00567 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00568 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00569 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00570 FRUSTUM_NEAR, FRUSTUM_FAR ); 00571 glMatrixMode( GL_MODELVIEW ); 00572 }
|
© Nokia 2009 |