|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : Mipmapping.cpp 00004 * Part of : OpenGLEx / Mipmapping 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 <aknnotewrappers.h> 00016 #include <e32math.h> 00017 #include "Mipmapping.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 +1.f //near depth clipping plane 00025 #define FRUSTUM_FAR +1000.f //far depth clipping plane 00026 00027 // CONSTANTS 00028 00029 // ============================= MEMBER FUNCTIONS ============================== 00030 00031 // ----------------------------------------------------------------------------- 00032 // CMipmapping::NewL 00033 // Symbian 2-phase constructor. Can leave. 00034 // ----------------------------------------------------------------------------- 00035 // 00036 CMipmapping* CMipmapping::NewL( TUint aWidth, TUint aHeight, TUint aSegments, TUint aSlices ) 00037 { 00038 // Symbian 2-phase constructor. Calls both the default 00039 // C++ constructor and Symbian ConstructL methods 00040 CMipmapping* self = new (ELeave) CMipmapping( aWidth, aHeight, aSegments, aSlices ); 00041 CleanupStack::PushL( self ); 00042 self->ConstructL(); 00043 CleanupStack::Pop(); 00044 00045 return self; 00046 } 00047 00048 // ---------------------------------------------------------------------------- 00049 // CMipmapping::CMipmapping 00050 // C++ default constructor can NOT contain any code, that 00051 // might leave. 00052 // ---------------------------------------------------------------------------- 00053 // 00054 CMipmapping::CMipmapping( TUint aWidth, TUint aHeight, TUint aSegments, TUint aSlices ) 00055 : CFiniteStateMachine() 00056 { 00057 iScreenWidth = aWidth; 00058 iScreenHeight = aHeight; 00059 iSegments = aSegments; 00060 iSlices = aSlices; 00061 iFilteringMode = EMipmappedBilinearFiltering; 00062 } 00063 00064 // ---------------------------------------------------------------------------- 00065 // CMipmapping::ConstructL 00066 // Symbian 2nd phase constructor can leave. 00067 // ---------------------------------------------------------------------------- 00068 // 00069 void CMipmapping::ConstructL( void ) 00070 { 00071 // Create vertex, normal and index arrays 00072 iVertices = new GLfixed[ 3 * iSegments * iSlices ]; 00073 iNormals = new GLfixed[ 3 * iSegments * iSlices ]; 00074 iTexCoords = new GLfixed[ 2 * iSegments * iSlices ]; 00075 iIndices = new GLshort[ 2 + (iSegments - 1) * iSlices * 2 ]; 00076 00077 // Fill index arrays for rendering the tunnel as a triangle strip 00078 GLshort * i = iIndices; 00079 TInt segment, edge; 00080 00081 *i++ = 0; 00082 for (segment = 0; segment < iSegments - 1; segment++) 00083 { 00084 for (edge = 0; edge < iSlices; edge++) 00085 { 00086 *i++ = segment * iSlices + (edge + 1) % iSlices; 00087 *i++ = segment * iSlices + (edge + 1) % iSlices + iSlices; 00088 } 00089 } 00090 *i++ = (iSegments - 1) * iSlices + 1; 00091 00092 // Calculate the texture coordinates 00093 GLfixed * t = iTexCoords; 00094 00095 for (segment = 0; segment < iSegments; segment++) 00096 { 00097 for (edge = 0; edge < iSlices; edge++) 00098 { 00099 *t++ = segment << 15; 00100 *t++ = (4 * (edge << 16) / iSlices) & ((1 << 16) - 1); 00101 } 00102 } 00103 00104 } 00105 00106 // ---------------------------------------------------------------------------- 00107 // CMipmapping::~CMipmapping() 00108 // Destructor. 00109 // ---------------------------------------------------------------------------- 00110 // 00111 CMipmapping::~CMipmapping() 00112 { 00113 delete[] iVertices; iVertices = 0; 00114 delete[] iNormals; iNormals = 0; 00115 delete[] iTexCoords; iTexCoords = 0; 00116 delete[] iIndices; iIndices = 0; 00117 } 00118 00119 // ---------------------------------------------------------------------------- 00120 // CMipmapping::AppInit 00121 // Application initialization code. 00122 // ---------------------------------------------------------------------------- 00123 // 00124 void CMipmapping::AppInitL( void ) 00125 { 00126 // Construct a texture manager that uses the application's private 00127 // directory as the location for all textures. 00128 iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenHeight, 00129 FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR, 00130 this ); 00131 00132 // Initialize viewport and projection. 00133 SetScreenSize( iScreenWidth, iScreenHeight ); 00134 00135 // Set the screen background color. 00136 glClearColor( 0.0f, 0.3f, 0.0f, 1.0f ); 00137 00138 // Set required OpenGL state 00139 glEnable( GL_CULL_FACE ); 00140 glEnable( GL_TEXTURE_2D ); 00141 glEnable( GL_DEPTH_TEST ); 00142 00143 // Disable lighting 00144 glEnable( GL_LIGHTING ); 00145 glEnable( GL_LIGHT0 ); 00146 00147 static const GLfloat lightDiffuse[4] = { 1.0, 1.0, 1.0, 1.0 }; 00148 static const GLfloat lightAmbient[4] = { 0.4, 0.4, 0.4, 1.0 }; 00149 static const GLfloat lightPosition[4] = { 0, 10, 10, 0 }; 00150 glLightfv( GL_LIGHT0, GL_DIFFUSE, lightDiffuse ); 00151 glLightfv( GL_LIGHT0, GL_AMBIENT, lightAmbient ); 00152 glLightfv( GL_LIGHT0, GL_SPECULAR, lightDiffuse ); 00153 glLightfv( GL_LIGHT0, GL_POSITION, lightPosition ); 00154 00155 // Request perspective correct rendering 00156 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); 00157 00158 // Enable vertex, normal and texture arrays 00159 glEnableClientState( GL_VERTEX_ARRAY ); 00160 glEnableClientState( GL_NORMAL_ARRAY ); 00161 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00162 glVertexPointer( 3, GL_FIXED, 0, iVertices ); 00163 glNormalPointer( GL_FIXED, 0, iNormals ); 00164 glTexCoordPointer( 2, GL_FIXED, 0, iTexCoords ); 00165 00166 #if defined(MIPMAPPING_SPLIT_SCREEN) 00167 glEnable(GL_SCISSOR_TEST); 00168 #endif 00169 00170 // Push the textures into the loading queue 00171 iTextureManager->RequestToLoad(_L("texture.jpg"), &iTexture, NULL, NULL, ETrue ); 00172 00173 // Start to load the queued textures 00174 iTextureManager->DoLoadL(); 00175 } 00176 00177 // ---------------------------------------------------------------------------- 00178 // CMipmapping::AppExit 00179 // Application cleanup code. 00180 // ---------------------------------------------------------------------------- 00181 // 00182 void CMipmapping::AppExit( void ) 00183 { 00184 delete iTextureManager; 00185 } 00186 00187 00188 // ---------------------------------------------------------------------------- 00189 // CMipmapping::AppCycle 00190 // Draws and animates the objects. 00191 // aFrame = Number of the frame to be rendered. 00192 // aTimeSecs = Seconds elapsed since the application started running. 00193 // aDeltaTimeSecs = Seconds elapsed since last call to AppCycle(). 00194 // ---------------------------------------------------------------------------- 00195 // 00196 void CMipmapping::AppCycle( TInt /*aFrame*/, 00197 TReal aTimeSecs, 00198 TReal /*aDeltaTimeSecs*/ ) 00199 { 00200 #if defined(MIPMAPPING_SPLIT_SCREEN) 00201 // Clear the entire screen 00202 glScissor(0, 0, iScreenWidth, iScreenHeight); 00203 #endif 00204 00205 // Clear the color and depth buffers 00206 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 00207 00208 // Pan the texture toward the viewer 00209 glMatrixMode( GL_TEXTURE ); 00210 glLoadIdentity(); 00211 glTranslatex( (TInt)((1 << 14) * aTimeSecs) & ((1 << 16) - 1), 0, 0); 00212 glMatrixMode( GL_MODELVIEW ); 00213 00214 // Roll the camera around the z-axis 00215 glLoadIdentity(); 00216 glRotatef( 16 * aTimeSecs, 0, 0, 1 ); 00217 00218 // Move the tunnel vertices 00219 UpdateTunnel ( aTimeSecs ); 00220 00221 // Set up texturing 00222 glBindTexture( GL_TEXTURE_2D, iTexture.iID ); 00223 00224 switch (iFilteringMode) 00225 { 00226 case EMipmappedNearestNeighbourFiltering: 00227 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST ); 00228 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00229 break; 00230 case EMipmappedBilinearFiltering: 00231 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST ); 00232 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00233 break; 00234 case EMipmappedTrilinearFiltering: 00235 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); 00236 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00237 break; 00238 case ENearestNeighbourFiltering: 00239 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00240 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00241 break; 00242 case EBilinearFiltering: 00243 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00244 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00245 break; 00246 } 00247 00248 #if defined(MIPMAPPING_SPLIT_SCREEN) 00249 // Draw the left side of the screen with mipmapping. 00250 glScissor(0, 0, iScreenWidth / 2 - 1, iScreenHeight); 00251 #endif 00252 00253 glDrawElements( GL_TRIANGLE_STRIP, 2 + (iSegments - 1) * iSlices * 2, GL_UNSIGNED_SHORT, iIndices); 00254 00255 #if defined(MIPMAPPING_SPLIT_SCREEN) 00256 // Draw the right side of the screen without mipmapping. 00257 glScissor(iScreenWidth / 2, 0, iScreenWidth / 2, iScreenHeight); 00258 00259 switch (iFilteringMode) 00260 { 00261 case EMipmappedNearestNeighbourFiltering: 00262 case ENearestNeighbourFiltering: 00263 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 00264 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); 00265 break; 00266 case EMipmappedTrilinearFiltering: 00267 case EMipmappedBilinearFiltering: 00268 case EBilinearFiltering: 00269 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); 00270 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); 00271 break; 00272 } 00273 00274 glDrawElements( GL_TRIANGLE_STRIP, 2 + (iSegments - 1) * iSlices * 2, GL_UNSIGNED_SHORT, iIndices); 00275 #endif 00276 00277 } 00278 00279 // ---------------------------------------------------------------------------- 00280 // CMipmapping::OnEnterState( TInt aState ) 00281 // Called by TFiniteStateMachine when the f.s.m enters a new state 00282 // ---------------------------------------------------------------------------- 00283 // 00284 void CMipmapping::OnEnterStateL( TInt /*aState*/ ) 00285 { 00286 } 00287 00288 // ---------------------------------------------------------------------------- 00289 // CMipmapping::OnStartLoadingTextures() 00290 // Called for a MTextureLoadingListener by the texture manager when texture 00291 // loading operation starts 00292 // ---------------------------------------------------------------------------- 00293 // 00294 void CMipmapping::OnStartLoadingTexturesL() 00295 { 00296 SetStateL( ELoadingTextures ); 00297 } 00298 00299 // ---------------------------------------------------------------------------- 00300 // CMipmapping::OnEndLoadingTextures() 00301 // Called for a MTextureLoadingListener by the texture manager when texture 00302 // loading operation is completed 00303 // ---------------------------------------------------------------------------- 00304 void CMipmapping::OnEndLoadingTexturesL() 00305 { 00306 if ( GetState() == ELoadingTextures ) 00307 { 00308 SetStateL( ERunning ); 00309 } 00310 } 00311 00312 // ---------------------------------------------------------------------------- 00313 // CMipmapping::SetScreenSize 00314 // Reacts to the dynamic screen size change during execution of this program. 00315 // ---------------------------------------------------------------------------- 00316 // 00317 void CMipmapping::SetScreenSize( TUint aWidth, TUint aHeight ) 00318 { 00319 iScreenWidth = aWidth; 00320 iScreenHeight = aHeight; 00321 00322 // Notify the texture manager of screen size change 00323 iTextureManager->SetScreenSize( aWidth, aHeight ); 00324 00325 // Reinitialize viewport and projection. 00326 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00327 00328 // Recalculate the view frustrum 00329 glMatrixMode( GL_PROJECTION ); 00330 glLoadIdentity(); 00331 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00332 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00333 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00334 FRUSTUM_NEAR, FRUSTUM_FAR ); 00335 glMatrixMode( GL_MODELVIEW ); 00336 } 00337 00338 00339 void CMipmapping::UpdateTunnel( TReal aTime ) 00340 { 00341 GLfixed * vertex = iVertices; 00342 GLfixed * normal = iNormals; 00343 TInt segment, edge; 00344 TReal time = 0, phase = aTime; 00345 TReal timeStep = 0.1; // Time units per segment 00346 const TReal timeAcceleration = 0.03; // Acceleration of the time step 00347 const TReal phaseStep = 0.01; // Phase units per segment 00348 TReal pos[3], radius; 00349 GLfixed origin[3] = {0, 0, 0}; 00350 00351 for (segment = 0; segment < iSegments; segment++) 00352 { 00353 // Evaluate the tunnel at this position 00354 TunnelFunction(time, phase, pos, radius); 00355 if (segment == 0) 00356 { 00357 origin[0] = RealToFixed(pos[0]); 00358 origin[1] = RealToFixed(pos[1]); 00359 origin[2] = RealToFixed(pos[2]); 00360 } 00361 00362 // Build a ring of vertices at this position. Since the tunnel's 00363 // deformations are quite negligible, the ring is aligned to the 00364 // XY-plane. 00365 TReal angle; 00366 for (edge = 0, angle = 0; edge < iSlices; edge++, angle += 2 * KPi / iSlices) 00367 { 00368 TReal c, s; 00369 Math::Sin(s, angle); 00370 Math::Cos(c, angle); 00371 *vertex++ = RealToFixed(pos[0] + c * radius) - origin[0]; 00372 *vertex++ = RealToFixed(pos[1] + s * radius) - origin[1]; 00373 *vertex++ = RealToFixed(pos[2]) - origin[2]; 00374 *normal++ = RealToFixed(c); 00375 *normal++ = RealToFixed(s); 00376 *normal++ = 0; 00377 } 00378 // Update our position along the tunnel 00379 time += timeStep; 00380 timeStep += timeAcceleration; 00381 phase += phaseStep; 00382 } 00383 } 00384 00385 void CMipmapping::TunnelFunction( TReal aTime, TReal aPhase, TReal aPos[3], TReal& aRadius ) const 00386 { 00387 // Calculate a wavy parametric curve 00388 Math::Sin(aPos[0], aTime + aPhase); 00389 Math::Cos(aPos[1], aTime + aPhase); 00390 aPos[1] *= 2; 00391 aPos[2] = -aTime * 16; 00392 aRadius = 4; 00393 } 00394 00395 GLfixed CMipmapping::RealToFixed( TReal aReal ) 00396 { 00397 return (GLfixed)(aReal * (1 << 16)); 00398 } 00399 00400 void CMipmapping::SetFilteringMode( CMipmapping::EFilteringMode aMode ) 00401 { 00402 iFilteringMode = aMode; 00403 } 00404 00405 // End of File
|
© Nokia 2009 |