Nokia N97 SDK
Example Applications Guide

CMipmapping Class Reference

#include <Mipmapping.h>

Inheritance diagram for CMipmapping:

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 36 of file Mipmapping.h.


Public Types

enum   EFilteringMode {
  EMipmappedNearestNeighbourFiltering, EMipmappedBilinearFiltering, EMipmappedTrilinearFiltering, ENearestNeighbourFiltering,
  EBilinearFiltering
}
enum   { ELoadingTextures, ERunning }

Public Member Functions

virtual  ~CMipmapping ()
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)
void  SetFilteringMode (EFilteringMode aMode)

Static Public Member Functions

static CMipmapping *  NewL (TUint aWidth, TUint aHeight, TUint aSegments=16, TUint aSlices=7)

Protected Member Functions

  CMipmapping (TUint aWidth, TUint aHeight, TUint aSegments, TUint aSlices)
void  ConstructL (void)

Private Member Functions

void  UpdateTunnel (TReal aTime)
void  TunnelFunction (TReal aTime, TReal aPhase, TReal aPos[3], TReal &aRadius) const

Static Private Member Functions

static GLfixed  RealToFixed (TReal aReal)

Private Attributes

CTextureManager *  iTextureManager
TUint  iScreenWidth
TUint  iScreenHeight
TUint  iSegments
TUint  iSlices
TTexture  iTexture
GLfixed *  iVertices
GLfixed *  iNormals
GLfixed *  iTexCoords
GLshort *  iIndices
EFilteringMode  iFilteringMode


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 142 of file Mipmapping.h.

00143                 {
00144                 ELoadingTextures,
00145                 ERunning
00146                 };


Constructor & Destructor Documentation

CMipmapping::~CMipmapping (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 111 of file Mipmapping.cpp.

References iIndices, iNormals, iTexCoords, and iVertices.

00112         {
00113     delete[] iVertices;  iVertices = 0;
00114     delete[] iNormals;   iNormals = 0;
00115     delete[] iTexCoords; iTexCoords = 0;
00116     delete[] iIndices;   iIndices = 0;
00117         }

CMipmapping::CMipmapping (  TUint  aWidth,
TUint  aHeight,
TUint  aSegments,
TUint  aSlices
)  [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.
aSegments  Number of segments in the tunnel.
aSlices  Number of slices in one segment of the tunnel.

Definition at line 54 of file Mipmapping.cpp.

References iFilteringMode, iScreenHeight, iScreenWidth, iSegments, and iSlices.

Referenced by NewL().

00055         : CFiniteStateMachine()
00056         {
00057     iScreenWidth  = aWidth;
00058     iScreenHeight = aHeight;
00059     iSegments = aSegments;
00060     iSlices = aSlices;
00061     iFilteringMode = EMipmappedBilinearFiltering;
00062         }


Member Function Documentation

CMipmapping * CMipmapping::NewL (  TUint  aWidth,
TUint  aHeight,
TUint  aSegments = 16,
TUint  aSlices = 7
)  [static]
 

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

Definition at line 36 of file Mipmapping.cpp.

References CMipmapping().

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         }

void CMipmapping::ConstructL (  void   )  [protected]
 

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

Definition at line 69 of file Mipmapping.cpp.

References iIndices, iNormals, iSegments, iSlices, iTexCoords, and iVertices.

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         }

void CMipmapping::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 124 of file Mipmapping.cpp.

References iNormals, iScreenHeight, iScreenWidth, iTexCoords, iTexture, iTextureManager, iVertices, and SetScreenSize().

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         }

void CMipmapping::AppExit (  void   ) 
 

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

Definition at line 182 of file Mipmapping.cpp.

References iTextureManager.

Referenced by CMipmappingContainer::~CMipmappingContainer().

00183         {
00184     delete iTextureManager;
00185         }

void CMipmapping::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 196 of file Mipmapping.cpp.

References iFilteringMode, iIndices, iScreenHeight, iScreenWidth, iSegments, iSlices, iTexture, and UpdateTunnel().

Referenced by CMipmappingContainer::DrawCallBack().

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         }

void CMipmapping::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 284 of file Mipmapping.cpp.

00285         {
00286         }

void CMipmapping::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 294 of file Mipmapping.cpp.

00295         {
00296     SetStateL( ELoadingTextures );
00297         }

void CMipmapping::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 304 of file Mipmapping.cpp.

00305         {
00306         if ( GetState() == ELoadingTextures )
00307                 {
00308         SetStateL( ERunning );
00309                 }
00310         }

void CMipmapping::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 317 of file Mipmapping.cpp.

References iScreenHeight, iScreenWidth, and iTextureManager.

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

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     }

void CMipmapping::SetFilteringMode (  EFilteringMode  aMode  ) 
 

Set texture filtering mode.

Definition at line 400 of file Mipmapping.cpp.

References iFilteringMode.

Referenced by CMipmappingAppUi::HandleCommandL().

00401     {
00402     iFilteringMode = aMode;
00403     }

void CMipmapping::UpdateTunnel (  TReal  aTime  )  [private]
 

Update the tunnel vertices based on the current time.

Parameters:
aTime  Time parameter.

Definition at line 339 of file Mipmapping.cpp.

References iNormals, iSegments, iSlices, iVertices, RealToFixed(), and TunnelFunction().

Referenced by AppCycle().

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     }

void CMipmapping::TunnelFunction (  TReal  aTime,
TReal  aPhase,
TReal  aPos[3],
TReal &  aRadius
)  const [private]
 

The tunnel function calculates the coordinates and radius of the tunnel at a given point aTime with the animation phase aPhase.

Parameters:
aTime  Time parameter.
aPhase  Phase parameter. This controls the animation of the tunnel.
aPos  Coordinates of the tunnel (output).
aRadius  Radius of the tunnel (output).

Definition at line 385 of file Mipmapping.cpp.

Referenced by UpdateTunnel().

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     }

GLfixed CMipmapping::RealToFixed (  TReal  aReal  )  [inline, static, private]
 

Converts a real number to fixed point format.

Parameters:
aReal  Number to convert.
Returns:
the given number in fixed point format.

Definition at line 395 of file Mipmapping.cpp.

Referenced by UpdateTunnel().

00396     {
00397     return (GLfixed)(aReal * (1 << 16));
00398     }


Member Data Documentation

CTextureManager* CMipmapping::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 175 of file Mipmapping.h.

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

TUint CMipmapping::iScreenWidth [private]
 

Width of the screen

Definition at line 177 of file Mipmapping.h.

Referenced by AppCycle(), AppInitL(), CMipmapping(), and SetScreenSize().

TUint CMipmapping::iScreenHeight [private]
 

Height of the screen

Definition at line 179 of file Mipmapping.h.

Referenced by AppCycle(), AppInitL(), CMipmapping(), and SetScreenSize().

TUint CMipmapping::iSegments [private]
 

Number of segments in the tunnel

Definition at line 181 of file Mipmapping.h.

Referenced by AppCycle(), CMipmapping(), ConstructL(), and UpdateTunnel().

TUint CMipmapping::iSlices [private]
 

Number of slices in one segment of the tunnel

Definition at line 183 of file Mipmapping.h.

Referenced by AppCycle(), CMipmapping(), ConstructL(), and UpdateTunnel().

TTexture CMipmapping::iTexture [private]
 

Tunnel texture

Definition at line 185 of file Mipmapping.h.

Referenced by AppCycle(), and AppInitL().

GLfixed* CMipmapping::iVertices [private]
 

Vertices of the tunnel

Definition at line 187 of file Mipmapping.h.

Referenced by AppInitL(), ConstructL(), UpdateTunnel(), and ~CMipmapping().

GLfixed* CMipmapping::iNormals [private]
 

Normals of the tunnel

Definition at line 189 of file Mipmapping.h.

Referenced by AppInitL(), ConstructL(), UpdateTunnel(), and ~CMipmapping().

GLfixed* CMipmapping::iTexCoords [private]
 

Texture coordinates of the tunnel

Definition at line 191 of file Mipmapping.h.

Referenced by AppInitL(), ConstructL(), and ~CMipmapping().

GLshort* CMipmapping::iIndices [private]
 

Indices for the tunnel triangle strips

Definition at line 193 of file Mipmapping.h.

Referenced by AppCycle(), ConstructL(), and ~CMipmapping().

EFilteringMode CMipmapping::iFilteringMode [private]
 

Texture filtering mode

Definition at line 195 of file Mipmapping.h.

Referenced by AppCycle(), CMipmapping(), and SetFilteringMode().


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

© Nokia 2009

Back to top