Nokia N97 SDK
Example Applications Guide

CParticles Class Reference

#include <Particles.h>

Inheritance diagram for CParticles:

List of all members.

Detailed Description

Class that does the actual OpenGL ES rendering. Subclass of CFiniteStateMachine because the application needs to handle loading textures and running states.

Definition at line 31 of file Particles.h.


Public Types

enum   { ELoadingTextures, ERunning }

Public Member Functions

virtual  ~CParticles ()
void  AppInitL (void)
void  AppExit (void)
void  AppCycle (TInt aFrame, TReal aTimeSecs, TReal aDeltaTimeSecs)
void  OnEnterStateL (TInt aState)
void  OnStartLoadingTexturesL ()
void  OnEndLoadingTexturesL ()
void  SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CParticles *  NewL (TUint aWidth, TUint aHeight, TUint aParticleCount=64)

Protected Member Functions

  CParticles (TUint aWidth, TUint aHeight, TUint aParticleCount)
void  ConstructL (void)

Private Member Functions

void  UpdateParticles (TReal aTime)
void  DrawParticles (TReal aTime)
void  DrawFeedbackEffect (TReal aTime)
void  UpdateFeedbackEffect ()

Private Attributes

CTextureManager *  iTextureManager
TUint  iScreenWidth
TUint  iScreenHeight
TCamera  iCamera
TTexture  iParticleTexture
TUint  iParticleCount
GLfixed *  iParticleCoords
GLfixed *  iParticleColors
GLuint  iFeedbackTextureID
TInt  iFeedbackTextureWidth
TInt  iFeedbackTextureHeight


Member Enumeration Documentation

anonymous enum
 

Application states: ELoadingTextures - indicates that the app. is loading textures. ERunning - indicates that the app. is running.

Definition at line 122 of file Particles.h.

00123         {
00124         ELoadingTextures,
00125         ERunning
00126         };


Constructor & Destructor Documentation

CParticles::~CParticles (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 78 of file Particles.cpp.

00079     {
00080     }

CParticles::CParticles (  TUint  aWidth,
TUint  aHeight,
TUint  aParticleCount
)  [protected]
 

Standard constructor that must never Leave. Stores the given screen width and height.

Parameters:
aWidth  Width of the screen.
aHeight  Height of the screen.
aParticleCount  Particle count.

Definition at line 56 of file Particles.cpp.

References iParticleCount, iScreenHeight, and iScreenWidth.

Referenced by NewL().

00057     : CFiniteStateMachine()
00058     {
00059     iScreenWidth  = aWidth;
00060     iScreenHeight = aHeight;
00061     iParticleCount = aParticleCount;
00062     }


Member Function Documentation

CParticles * CParticles::NewL (  TUint  aWidth,
TUint  aHeight,
TUint  aParticleCount = 64
)  [static]
 

Factory method for creating a new CParticles object. Calls the hidden constructors.

Definition at line 38 of file Particles.cpp.

References CParticles().

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     }

void CParticles::ConstructL (  void   )  [protected]
 

Second phase constructor that can call methods that may leave. Does nothing.

Definition at line 69 of file Particles.cpp.

00070     {
00071     }

void CParticles::AppInitL (  void   ) 
 

Initializes OpenGL ES, sets the vertex and color arrays and pointers. Also selects the shading mode and creates the CTextureManager instance and starts loading of required textures.

Definition at line 87 of file Particles.cpp.

References iCamera, iFeedbackTextureID, iParticleColors, iParticleCoords, iParticleCount, iParticleTexture, iScreenHeight, iScreenWidth, iTextureManager, and SetScreenSize().

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     }

void CParticles::AppExit (  void   ) 
 

Called upon application exit. Destroys the CTextureManager instance that was created in AppInitL() method.

Definition at line 148 of file Particles.cpp.

References iParticleColors, iParticleCoords, and iTextureManager.

Referenced by CParticlesContainer::~CParticlesContainer().

00149     {
00150     delete iTextureManager;
00151     delete[] iParticleCoords;
00152     delete[] iParticleColors;
00153     }

void CParticles::AppCycle (  TInt  aFrame,
TReal  aTimeSecs,
TReal  aDeltaTimeSecs
) 
 

Renders one frame.

Parameters:
aFrame  Number of the frame to be rendered.
aTimeSecs  Seconds elapsed since the application started running.
aDeltaTimeSecs  Seconds elapsed since last call to AppCycle().

Definition at line 164 of file Particles.cpp.

References DrawFeedbackEffect(), DrawParticles(), UpdateFeedbackEffect(), and UpdateParticles().

Referenced by CParticlesContainer::DrawCallBack().

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    }

void CParticles::OnEnterStateL (  TInt  aState  ) 
 

Called when the finite state machine enters a new state. Does nothing in this implementation.

Parameters:
aState  State that is about to be entered.

Definition at line 186 of file Particles.cpp.

00187     {
00188     }

void CParticles::OnStartLoadingTexturesL (   ) 
 

Called when texture manager starts loading the textures. Sets the current state to "loading textures".

Definition at line 196 of file Particles.cpp.

00197     {
00198     SetStateL( ELoadingTextures );
00199     }

void CParticles::OnEndLoadingTexturesL (   ) 
 

Called when texture manager has completed texture loading. Changes the current state to "running".

Definition at line 206 of file Particles.cpp.

00207     {
00208     if ( GetState() == ELoadingTextures )
00209         {
00210         SetStateL( ERunning );
00211         }
00212     }

void CParticles::SetScreenSize (  TUint  aWidth,
TUint  aHeight
) 
 

Notifies that the screen size has dynamically changed during execution of this program. Resets the viewport to this new size and sets this size to the CTextureManager.

Parameters:
aWidth  New width of the screen.
aHeight  New height of the screen.

Definition at line 219 of file Particles.cpp.

References iScreenHeight, iScreenWidth, and iTextureManager.

Referenced by AppInitL(), and CParticlesContainer::SizeChanged().

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     }

void CParticles::UpdateParticles (  TReal  aTime  )  [private]
 

Animate the particles.

Parameters:
aTime  Animation time parameter (seconds).

Definition at line 265 of file Particles.cpp.

References iParticleColors, iParticleCoords, and iParticleCount.

Referenced by AppCycle().

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     }

void CParticles::DrawParticles (  TReal  aTime  )  [private]
 

Draw the particles.

Definition at line 286 of file Particles.cpp.

References iParticleColors, iParticleCoords, iParticleCount, and iParticleTexture.

Referenced by AppCycle().

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     }

void CParticles::DrawFeedbackEffect (  TReal  aTime  )  [private]
 

Draw a feedback effect.

Parameters:
aTime  Animation time parameter (seconds).

Definition at line 313 of file Particles.cpp.

References iFeedbackTextureHeight, iFeedbackTextureID, iFeedbackTextureWidth, and iScreenWidth.

Referenced by AppCycle().

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     }

void CParticles::UpdateFeedbackEffect (   )  [private]
 

Update the feedback effect based on the contents of the framebuffer.

Definition at line 376 of file Particles.cpp.

References iFeedbackTextureHeight, iFeedbackTextureID, and iFeedbackTextureWidth.

Referenced by AppCycle().

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     }


Member Data Documentation

CTextureManager* CParticles::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 154 of file Particles.h.

Referenced by AppExit(), AppInitL(), and SetScreenSize().

TUint CParticles::iScreenWidth [private]
 

Width of the screen

Definition at line 156 of file Particles.h.

Referenced by AppInitL(), CParticles(), DrawFeedbackEffect(), and SetScreenSize().

TUint CParticles::iScreenHeight [private]
 

Height of the screen

Definition at line 158 of file Particles.h.

Referenced by AppInitL(), CParticles(), and SetScreenSize().

TCamera CParticles::iCamera [private]
 

Camera.

Definition at line 160 of file Particles.h.

Referenced by AppInitL().

TTexture CParticles::iParticleTexture [private]
 

Particle texture

Definition at line 162 of file Particles.h.

Referenced by AppInitL(), and DrawParticles().

TUint CParticles::iParticleCount [private]
 

Particle count

Definition at line 164 of file Particles.h.

Referenced by AppInitL(), CParticles(), DrawParticles(), and UpdateParticles().

GLfixed* CParticles::iParticleCoords [private]
 

Particle coordinates

Definition at line 166 of file Particles.h.

Referenced by AppExit(), AppInitL(), DrawParticles(), and UpdateParticles().

GLfixed* CParticles::iParticleColors [private]
 

Particle colors

Definition at line 168 of file Particles.h.

Referenced by AppExit(), AppInitL(), DrawParticles(), and UpdateParticles().

GLuint CParticles::iFeedbackTextureID [private]
 

Feedback texture handle

Definition at line 170 of file Particles.h.

Referenced by AppInitL(), DrawFeedbackEffect(), and UpdateFeedbackEffect().

TInt CParticles::iFeedbackTextureWidth [private]
 

Feedback texture width

Definition at line 172 of file Particles.h.

Referenced by DrawFeedbackEffect(), and UpdateFeedbackEffect().

TInt CParticles::iFeedbackTextureHeight [private]
 

Feedback texture height

Definition at line 174 of file Particles.h.

Referenced by DrawFeedbackEffect(), and UpdateFeedbackEffect().


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top