|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : Particles.cpp 00004 * Part of : OpenGLEx / Particles 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 "particles.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 +3.f //near depth clipping plane 00025 #define FRUSTUM_FAR +1000.f //far depth clipping plane 00026 00027 // CONSTANTS 00028 00029 // ============================= MEMBER FUNCTIONS ============================== 00030 // ============================= LOCAL FUNCTIONS =============================== 00031 // ============================= MEMBER FUNCTIONS =============================== 00032 00033 // ----------------------------------------------------------------------------- 00034 // CParticles::NewL 00035 // Symbian 2-phase constructor. Can leave. 00036 // ----------------------------------------------------------------------------- 00037 // 00038 CParticles* CParticles::NewL( TUint aWidth, TUint aHeight, TUint aParticleCount ) 00039 { 00040 // Symbian 2-phase constructor. Calls both the default 00041 // C++ constructor and Symbian ConstructL methods 00042 CParticles* self = new (ELeave) CParticles( aWidth, aHeight, aParticleCount ); 00043 CleanupStack::PushL( self ); 00044 self->ConstructL(); 00045 CleanupStack::Pop(); 00046 00047 return self; 00048 } 00049 00050 // ---------------------------------------------------------------------------- 00051 // CParticles::CParticles 00052 // C++ default constructor can NOT contain any code, that 00053 // might leave. 00054 // ---------------------------------------------------------------------------- 00055 // 00056 CParticles::CParticles( TUint aWidth, TUint aHeight, TUint aParticleCount ) 00057 : CFiniteStateMachine() 00058 { 00059 iScreenWidth = aWidth; 00060 iScreenHeight = aHeight; 00061 iParticleCount = aParticleCount; 00062 } 00063 00064 // ---------------------------------------------------------------------------- 00065 // CParticles::ConstructL 00066 // Symbian 2nd phase constructor can leave. 00067 // ---------------------------------------------------------------------------- 00068 // 00069 void CParticles::ConstructL( void ) 00070 { 00071 } 00072 00073 // ---------------------------------------------------------------------------- 00074 // CParticles::~CParticles() 00075 // Destructor. 00076 // ---------------------------------------------------------------------------- 00077 // 00078 CParticles::~CParticles() 00079 { 00080 } 00081 00082 // ---------------------------------------------------------------------------- 00083 // CParticles::AppInit 00084 // Application initialization code. 00085 // ---------------------------------------------------------------------------- 00086 // 00087 void CParticles::AppInitL( void ) 00088 { 00089 // Construct a texture manager that uses the application's private 00090 // directory as the location for all textures. 00091 iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenHeight, 00092 FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR, 00093 this ); 00094 // Create the feedback effect texture 00095 glGenTextures(1, &iFeedbackTextureID); 00096 00097 // Initialize viewport and projection. 00098 SetScreenSize( iScreenWidth, iScreenHeight ); 00099 00100 // Set the screen background color. 00101 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00102 00103 // Enable back face culling. 00104 glEnable( GL_CULL_FACE ); 00105 00106 // Enable point sprites. 00107 glEnable( GL_TEXTURE_2D ); 00108 glEnable( GL_POINT_SPRITE_OES ); 00109 00110 // We don't need a depth buffer in this example 00111 glDisable( GL_DEPTH_TEST ); 00112 glDepthMask( 0 ); 00113 00114 // Enable blending for transparency. 00115 glEnable( GL_BLEND ); 00116 glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); 00117 00118 // Make the points bigger. 00119 glPointSize( 32 ); 00120 00121 // Disable lighting 00122 glDisable( GL_LIGHTING ); 00123 00124 00125 // Have the material track the current color 00126 glEnable( GL_COLOR_MATERIAL ); 00127 00128 // Setup the camera 00129 iCamera.LookAt( TVector( 0.f, 30.0f, 0.f ), 00130 TVector( 0.f, 0.f, -( FRUSTUM_FAR + FRUSTUM_NEAR ) / 2.f ), 00131 TVector( 0.f, 1.f, 0.f ) ); 00132 00133 // Create particle array 00134 iParticleCoords = new GLfixed[iParticleCount * 3]; 00135 iParticleColors = new GLfixed[iParticleCount * 4]; 00136 00137 iTextureManager->RequestToLoad(_L("particle.png"), &iParticleTexture); 00138 00139 // Start to load the queued textures 00140 iTextureManager->DoLoadL(); 00141 } 00142 00143 // ---------------------------------------------------------------------------- 00144 // CParticles::AppExit 00145 // Application cleanup code. 00146 // ---------------------------------------------------------------------------- 00147 // 00148 void CParticles::AppExit( void ) 00149 { 00150 delete iTextureManager; 00151 delete[] iParticleCoords; 00152 delete[] iParticleColors; 00153 } 00154 00155 00156 // ---------------------------------------------------------------------------- 00157 // CParticles::AppCycle 00158 // Draws and animates the objects. 00159 // aFrame = Number of the frame to be rendered. 00160 // aTimeSecs = Seconds elapsed since the application started running. 00161 // aDeltaTimeSecs = Seconds elapsed since last call to AppCycle(). 00162 // ---------------------------------------------------------------------------- 00163 // 00164 void CParticles::AppCycle( TInt /*aFrame*/, 00165 TReal aTimeSecs, 00166 TReal /*aDeltaTimeSecs*/ ) 00167 { 00168 // Move the particles 00169 UpdateParticles( aTimeSecs ); 00170 00171 // Draw the feedback effect in the background 00172 DrawFeedbackEffect( aTimeSecs ); 00173 00174 // Draw the particles on top 00175 DrawParticles( aTimeSecs ); 00176 00177 // Update the feedback texture from the framebuffer 00178 UpdateFeedbackEffect(); 00179 } 00180 00181 // ---------------------------------------------------------------------------- 00182 // CParticles::OnEnterState( TInt aState ) 00183 // Called by TFiniteStateMachine when the f.s.m enters a new state 00184 // ---------------------------------------------------------------------------- 00185 // 00186 void CParticles::OnEnterStateL( TInt /*aState*/ ) 00187 { 00188 } 00189 00190 // ---------------------------------------------------------------------------- 00191 // CParticles::OnStartLoadingTextures() 00192 // Called for a MTextureLoadingListener by the texture manager when texture 00193 // loading operation starts 00194 // ---------------------------------------------------------------------------- 00195 // 00196 void CParticles::OnStartLoadingTexturesL() 00197 { 00198 SetStateL( ELoadingTextures ); 00199 } 00200 00201 // ---------------------------------------------------------------------------- 00202 // CParticles::OnEndLoadingTextures() 00203 // Called for a MTextureLoadingListener by the texture manager when texture 00204 // loading operation is completed 00205 // ---------------------------------------------------------------------------- 00206 void CParticles::OnEndLoadingTexturesL() 00207 { 00208 if ( GetState() == ELoadingTextures ) 00209 { 00210 SetStateL( ERunning ); 00211 } 00212 } 00213 00214 // ---------------------------------------------------------------------------- 00215 // CParticles::SetScreenSize 00216 // Reacts to the dynamic screen size change during execution of this program. 00217 // ---------------------------------------------------------------------------- 00218 // 00219 void CParticles::SetScreenSize( TUint aWidth, TUint aHeight ) 00220 { 00221 iScreenWidth = aWidth; 00222 iScreenHeight = aHeight; 00223 00224 // Notify the texture manager of screen size change 00225 if( iTextureManager ) 00226 { 00227 iTextureManager->SetScreenSize( aWidth, aHeight ); 00228 } 00229 00230 // Reinitialize viewport and projection. 00231 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00232 00233 // Recalculate the view frustrum 00234 glMatrixMode( GL_PROJECTION ); 00235 glLoadIdentity(); 00236 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00237 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00238 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00239 FRUSTUM_NEAR, FRUSTUM_FAR ); 00240 glMatrixMode( GL_MODELVIEW ); 00241 00242 // Clear the color buffer 00243 glClear(GL_COLOR_BUFFER_BIT); 00244 00245 // Initialize the feedback texture 00246 iFeedbackTextureWidth = nextPowerOfTwo(iScreenWidth); 00247 iFeedbackTextureHeight = nextPowerOfTwo(iScreenHeight); 00248 00249 // Choose the smaller one to get a reasonably small square texture 00250 if (iFeedbackTextureWidth < iFeedbackTextureHeight) 00251 iFeedbackTextureHeight = iFeedbackTextureWidth; 00252 else 00253 iFeedbackTextureWidth = iFeedbackTextureHeight; 00254 00255 glBindTexture(GL_TEXTURE_2D, iFeedbackTextureID); 00256 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, iFeedbackTextureWidth, iFeedbackTextureHeight, 00257 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); 00258 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 00259 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 00260 00261 // Read the initial black image 00262 UpdateFeedbackEffect(); 00263 } 00264 00265 void CParticles::UpdateParticles(TReal aTime) 00266 { 00267 TInt i; 00268 TReal theta1 = 3 * aTime; 00269 TReal theta2 = 5 * aTime; 00270 TReal theta3 = 1 * aTime; 00271 00272 for (i = 0; i < iParticleCount; i++) 00273 { 00274 // Update position and color 00275 iParticleColors[i * 4 + 0] = iParticleCoords[i * 3 + 0] = (GLfixed)(sin(theta1) * (1 << 16)); 00276 iParticleColors[i * 4 + 1] = iParticleCoords[i * 3 + 1] = (GLfixed)(cos(theta2) * (1 << 16)); 00277 iParticleColors[i * 4 + 2] = iParticleCoords[i * 3 + 2] = (GLfixed)(sin(theta3) * (1 << 16)); 00278 iParticleColors[i * 4 + 0] = (1 << 16) - iParticleColors[i * 4 + 0]; 00279 iParticleColors[i * 4 + 3] = 1 << 16; 00280 theta1 += 0.1f; 00281 theta2 += 0.2f; 00282 theta3 += 0.3f; 00283 } 00284 } 00285 00286 void CParticles::DrawParticles(TReal aTime) 00287 { 00288 // Move the viewer away from the particles 00289 glLoadIdentity(); 00290 glTranslatef( 0, 0, -5 ); 00291 glRotatef( aTime, 0, 1, 0 ); 00292 00293 // Enable vertex and color arrays. 00294 glEnableClientState( GL_VERTEX_ARRAY ); 00295 glEnableClientState( GL_COLOR_ARRAY ); 00296 glVertexPointer( 3, GL_FIXED, 0, iParticleCoords ); 00297 glColorPointer( 4, GL_FIXED, 0, iParticleColors ); 00298 00299 // Use the particle texture 00300 glBindTexture( GL_TEXTURE_2D, iParticleTexture.iID ); 00301 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 00302 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 00303 00304 // Draw the array of particles 00305 glTexEnvi( GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE ); 00306 glDrawArrays( GL_POINTS, 0, iParticleCount ); 00307 glTexEnvi( GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_FALSE ); 00308 00309 glDisableClientState( GL_VERTEX_ARRAY ); 00310 glDisableClientState( GL_COLOR_ARRAY ); 00311 } 00312 00313 void CParticles::DrawFeedbackEffect(TReal aTime) 00314 { 00315 // Use the feedback texture 00316 glBindTexture( GL_TEXTURE_2D, iFeedbackTextureID ); 00317 00318 // Set orthographic projection 00319 glMatrixMode( GL_PROJECTION ); 00320 glPushMatrix(); 00321 glLoadIdentity(); 00322 glOrthof( 0, 1, 0, 1, -10, 10 ); 00323 00324 // Transform the texture matrix to animate the feedback effect 00325 glMatrixMode( GL_TEXTURE ); 00326 glLoadIdentity(); 00327 00328 TReal zoom = 0.75f; 00329 TReal tw = iScreenWidth / (TReal)(iFeedbackTextureWidth); 00330 TReal th = iScreenHeight / (TReal)(iFeedbackTextureHeight); 00331 00332 glTranslatef( 0.5 * tw, 0.5 * th, 0 ); 00333 glScalef( zoom, zoom, 1 ); 00334 glRotatef( aTime * 10, 0, 0, 1 ); 00335 glTranslatef( -0.5 * tw, -0.5 * th, 0 ); 00336 00337 glMatrixMode( GL_MODELVIEW ); 00338 glLoadIdentity(); 00339 00340 // Draw a full screen textured quad 00341 glColor4f( 0.95f, 0.95f, 0.95f, 1 ); 00342 00343 GLfloat texCoords[] = 00344 { 00345 0, 0, 00346 tw, 0, 00347 0, th, 00348 tw, th, 00349 }; 00350 GLfixed vertices[] = 00351 { 00352 0, 0, 00353 1 << 16, 0, 00354 0, 1 << 16, 00355 1 << 16, 1 << 16, 00356 }; 00357 00358 glDisable( GL_BLEND ); 00359 glEnableClientState( GL_VERTEX_ARRAY ); 00360 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 00361 glTexCoordPointer( 2, GL_FLOAT, 0, texCoords ); 00362 glVertexPointer( 2, GL_FIXED, 0, vertices ); 00363 glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 ); 00364 glDisableClientState( GL_VERTEX_ARRAY ); 00365 glDisableClientState( GL_TEXTURE_COORD_ARRAY ); 00366 glEnable( GL_BLEND ); 00367 00368 glMatrixMode( GL_PROJECTION ); 00369 glPopMatrix(); 00370 glMatrixMode( GL_TEXTURE ); 00371 glLoadIdentity(); 00372 glMatrixMode( GL_MODELVIEW ); 00373 glLoadIdentity(); 00374 } 00375 00376 void CParticles::UpdateFeedbackEffect() 00377 { 00378 // Take a snapshot of the framebuffer 00379 glBindTexture( GL_TEXTURE_2D, iFeedbackTextureID ); 00380 00381 // Two alternative methods 00382 //glCopyTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, 0, 0, iScreenWidth, iScreenHeight ); 00383 glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, iFeedbackTextureWidth, iFeedbackTextureHeight, 0); 00384 } 00385 00386 // End of File
|
© Nokia 2009 |