Nokia N97 SDK
Example Applications Guide

CBumpMap Class Reference

#include <BumpMap.h>

Inheritance diagram for CBumpMap:

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 BumpMap.h.


Public Types

enum   { ELoadingTextures, ERunning }

Public Member Functions

virtual  ~CBumpMap ()
void  AppInitL (void)
void  AppExit (void)
void  AppCycle (GLuint aFrame, GLfloat aTimeSecs, GLfloat aDeltaTimeSecs)
void  OnEnterStateL (TInt aState)
void  OnStartLoadingTexturesL ()
void  OnEndLoadingTexturesL ()
void  SetScreenSize (GLuint aWidth, GLuint aHeight)

Static Public Member Functions

static CBumpMap *  NewL (GLuint aWidth, GLuint aHeight)

Protected Member Functions

  CBumpMap (GLuint aWidth, GLuint aHeight)
void  ConstructL (void)

Private Member Functions

void  DrawBumpmappedRect ()
void  GenerateBumpmapVerticesL ()

Private Attributes

CTextureManager *  iTextureManager
CFireFly *  iLightSource
GLuint  iScreenWidth
GLuint  iScreenHeight
TTexture  iBumpmapTexture
TTexture  iTexture
GLfloat *  iBumpmapVertices
GLfloat *  iBumpmapColors
GLfloat *  iBumpmapTexCoords
GLubyte *  iBumpmapIndices
TCamera  iCamera


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 135 of file BumpMap.h.

00136                 {
00137                 ELoadingTextures,
00138                 ERunning
00139                 };


Constructor & Destructor Documentation

CBumpMap::~CBumpMap (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 100 of file BumpMap.cpp.

00101         {
00102         }

CBumpMap::CBumpMap (  GLuint  aWidth,
GLuint  aHeight
)  [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.

Definition at line 78 of file BumpMap.cpp.

References iScreenHeight, iScreenWidth, and iTextureManager.

Referenced by NewL().

00079         : CFiniteStateMachine()
00080         {
00081     iScreenWidth    = aWidth;
00082     iScreenHeight   = aHeight;
00083     iTextureManager = NULL;
00084         }


Member Function Documentation

CBumpMap * CBumpMap::NewL (  GLuint  aWidth,
GLuint  aHeight
)  [static]
 

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

Definition at line 59 of file BumpMap.cpp.

References CBumpMap().

00060         {
00061     // Symbian 2-phase constructor. Calls both the default
00062     // C++ constructor and Symbian ConstructL methods
00063     CBumpMap* self = new (ELeave) CBumpMap( aWidth, aHeight );
00064 
00065     CleanupStack::PushL( self );
00066     self->ConstructL();
00067     CleanupStack::Pop();
00068 
00069     return self;
00070         }

void CBumpMap::ConstructL (  void   )  [protected]
 

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

Definition at line 91 of file BumpMap.cpp.

00092         {
00093         }

void CBumpMap::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 109 of file BumpMap.cpp.

References GenerateBumpmapVerticesL(), iBumpmapTexture, iLightSource, iScreenHeight, iScreenWidth, iTexture, iTextureManager, CFireFly::NewL(), and SetScreenSize().

00110         {
00111     // Construct a texture manager that uses the application's private
00112         // directory as the source location for all textures.
00113     iTextureManager = CTextureManager::NewL ( iScreenWidth,  iScreenWidth,
00114                                               FRUSTUM_TOP,   FRUSTUM_BOTTOM,
00115                                               FRUSTUM_RIGHT, FRUSTUM_LEFT,
00116                                               FRUSTUM_NEAR,  this );
00117 
00118     // Initialize viewport and projection.
00119     SetScreenSize( iScreenWidth, iScreenHeight );
00120 
00121     // Reset texture matrix
00122     glMatrixMode( GL_TEXTURE );
00123     glLoadIdentity();
00124     glMatrixMode( GL_MODELVIEW );
00125 
00126     // Set the screen background color.
00127     glClearColor( 0.f, 0.f, 0.f, 1.f );
00128 
00129     // Enable needed features
00130     glEnable( GL_CULL_FACE  );
00131     glEnable( GL_TEXTURE_2D );
00132     glDisable( GL_DEPTH_TEST );
00133     glDisable( GL_LIGHTING );
00134 
00135     // Enable vertex arrays and texture arrays.
00136     glEnableClientState( GL_VERTEX_ARRAY );
00137     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00138 
00139     // Do not use perspective correction
00140     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00141     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR);
00142     glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00143 
00144     GenerateBumpmapVerticesL();
00145 
00146     iLightSource = CFireFly::NewL( -45.f, 45.f,         // Min and max X-coordinates
00147                                    -45.f, 45.f,         // Min and max Y-coordinates
00148                                      7.f, 25.f,         // Min and max Z-coordinates
00149                                     iTextureManager );  // Texture manager used to load any textures
00150 
00151     // Push the textures into the loading queue.
00152         _LIT( KBumpMapName,    "bumpmap.jpg"   );
00153     iTextureManager->RequestToLoad( KBumpMapName,     &iBumpmapTexture, false );
00154         _LIT( KTextureMapName, "texmap.jpg" );
00155     iTextureManager->RequestToLoad( KTextureMapName,  &iTexture, false );
00156 
00157     // Start to load the textures.
00158     iTextureManager->DoLoadL();
00159         }

void CBumpMap::AppExit (  void   ) 
 

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

Definition at line 166 of file BumpMap.cpp.

References iBumpmapColors, iBumpmapIndices, iBumpmapTexCoords, iBumpmapVertices, iLightSource, and iTextureManager.

Referenced by CBumpMapContainer::~CBumpMapContainer().

00167         {
00168     delete[] iBumpmapVertices;
00169     delete[] iBumpmapColors;
00170     delete[] iBumpmapTexCoords;
00171     delete[] iBumpmapIndices;
00172 
00173     delete iLightSource;
00174 
00175     delete iTextureManager;
00176         }

void CBumpMap::AppCycle (  GLuint  aFrame,
GLfloat  aTimeSecs,
GLfloat  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 187 of file BumpMap.cpp.

References CFireFly::DoMovement(), CFireFly::Draw(), DrawBumpmappedRect(), and iLightSource.

Referenced by CBumpMapContainer::DrawCallBack().

00190         {
00191         // Clear the color and depth buffers
00192     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00193 
00194     glLoadIdentity();
00195     gluLookAt(  0.f,  0.f,     150.f,   // Position
00196                 0.f,  0.f,       0.f,   // Look At
00197                 0.f,  1.f,       0.f ); // Up Vector
00198 
00199     // Position the lightsource
00200     iLightSource->DoMovement( aFrame, aTimeSecs, aDeltaTimeSecs );
00201 
00202     // Draw the bumpmapping
00203     DrawBumpmappedRect();
00204 
00205     // Draw the lightsource as firefly
00206     iLightSource->Draw();
00207         }

void CBumpMap::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 214 of file BumpMap.cpp.

00215         {
00216         }

void CBumpMap::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 224 of file BumpMap.cpp.

00225         {
00226     SetStateL( ELoadingTextures );
00227         }

void CBumpMap::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 235 of file BumpMap.cpp.

00236         {
00237         if ( GetState() == ELoadingTextures )
00238                 {
00239         SetStateL( ERunning );
00240                 }
00241         }

void CBumpMap::SetScreenSize (  GLuint  aWidth,
GLuint  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 248 of file BumpMap.cpp.

References iScreenHeight, iScreenWidth, and iTextureManager.

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

00249     {
00250     iScreenWidth  = aWidth;
00251     iScreenHeight = aHeight;
00252 
00253     // Notify the texture manager of screen size change
00254     iTextureManager->SetScreenSize( aWidth, aHeight );
00255 
00256     // Reinitialize viewport and projection.
00257     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00258 
00259     // Recalculate the view frustrum
00260     glMatrixMode( GL_PROJECTION );
00261     glLoadIdentity();
00262     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00263     glFrustumf( FRUSTUM_LEFT * aspectRatio,
00264                 FRUSTUM_RIGHT * aspectRatio,
00265                 FRUSTUM_BOTTOM,
00266                 FRUSTUM_TOP,
00267                 FRUSTUM_NEAR,
00268                 FRUSTUM_FAR );
00269     glMatrixMode( GL_MODELVIEW );
00270     }

void CBumpMap::DrawBumpmappedRect (   )  [private]
 

Draws the bumpmapped rectangle.

Definition at line 277 of file BumpMap.cpp.

References iBumpmapColors, iBumpmapTexCoords, iBumpmapTexture, iBumpmapVertices, iLightSource, CFireFly::iPosition, and iTexture.

Referenced by AppCycle().

00278     {
00279     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00280     glEnableClientState( GL_COLOR_ARRAY );
00281     glDisable( GL_BLEND );
00282 
00283     // dot3 bump mapping computes (N.L) per pixel.  we do this by storing the N
00284     // vector in a texture (known as the dot3 bump map) and then L is computed
00285     // per vertex and is stored at each vertex as a color.  This color is
00286     // interpolated between vertices by OpenGL when the shade model is set to GL_SMOOTH.
00287     // N.L is computed by the texture combiner and then modulated with the base texture.
00288     //
00289     // Note that L vector must be rotated into tangent space (aka texture space) so
00290     // that it is in the same space as N.
00291 
00292     // UNIT 0
00293     // Configure the first texture combiner to calculate the dot product of
00294     // bumpmap normal vector N (stored in the bumpmapping texture) and
00295     // light angle vector L (stored as the PRIMARY_COLOR).
00296     glClientActiveTexture(GL_TEXTURE0);
00297     glActiveTexture( GL_TEXTURE0 );
00298     glEnable( GL_TEXTURE_2D );
00299     // Use first loaded texture as bumpmap
00300     glBindTexture( GL_TEXTURE_2D, iBumpmapTexture.iID );
00301     // Configure the texture combiner to do the DOT3 bumpmapping calculation
00302     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
00303     glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB,  GL_DOT3_RGB );
00304     // The first colour for each fragment operation is gotten from the bumpmap texture
00305     glTexEnvf( GL_TEXTURE_ENV, GL_SRC0_RGB,  GL_TEXTURE );
00306     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR );
00307     // The second colour for each fragment operation is gotten from the primary colour
00308     glTexEnvf( GL_TEXTURE_ENV, GL_SRC1_RGB,  GL_PRIMARY_COLOR );
00309     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR );
00310     glTexCoordPointer( 2, GL_FLOAT, 0, iBumpmapTexCoords );
00311 
00312     // UNIT 1
00313     // Configure the second texture combiner to modulate the base texture
00314     // by N.L
00315     glClientActiveTexture(GL_TEXTURE1);
00316     glActiveTexture( GL_TEXTURE1 );
00317     glEnable( GL_TEXTURE_2D );
00318     // Use second loaded texture as the "traditional" texture map
00319     glBindTexture( GL_TEXTURE_2D, iTexture.iID );
00320     // Configure the texture combiner to modulate the colour with the texture
00321     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE );
00322     glTexEnvf( GL_TEXTURE_ENV, GL_COMBINE_RGB,  GL_MODULATE );
00323     // The first colour for each fragment operation is gotten from the previous texture combiners output
00324     glTexEnvf( GL_TEXTURE_ENV, GL_SRC0_RGB,  GL_PREVIOUS );
00325     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR );
00326     // The second colour for each fragment operation is gotten from the texture bitmap
00327     glTexEnvf( GL_TEXTURE_ENV, GL_SRC1_RGB,  GL_TEXTURE );
00328     glTexEnvf( GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR );
00329     glTexCoordPointer( 2, GL_FLOAT, 0, iBumpmapTexCoords );
00330 
00331     GLfloat lightX = iLightSource->iPosition.iX;
00332     GLfloat lightY = iLightSource->iPosition.iY;
00333     GLfloat lightZ = iLightSource->iPosition.iZ;
00334     TVector vectVert2Light;
00335     for( TInt y = 0; y < NUM_VERTICES_PER_SIDE; y++ )
00336         {
00337         for( TInt x = 0; x < NUM_VERTICES_PER_SIDE; x++ )
00338             {
00339             // Compute L and store as vertex color
00340             // Calculate the vector from vertice to light source
00341             TInt idx = x + y * NUM_VERTICES_PER_SIDE;
00342 
00343             vectVert2Light.iX = lightX - iBumpmapVertices[idx * 3    ];
00344             vectVert2Light.iY = lightY - iBumpmapVertices[idx * 3 + 1];
00345             vectVert2Light.iZ = lightZ - iBumpmapVertices[idx * 3 + 2];
00346 
00347             // Normalize the vector
00348             vectVert2Light.Normalize();
00349 
00350             // Convert vector into tangent space
00351             vectVert2Light.MultMatrix4x4( tangentMatrix );
00352 
00353             // Scale and bias light vector values from range [-0.5, 0.5] to range [0.0, 1.0]
00354             vectVert2Light.iX = vectVert2Light.iX * 0.5 + 0.5;
00355             vectVert2Light.iY = vectVert2Light.iY * 0.5 + 0.5;
00356             vectVert2Light.iZ = vectVert2Light.iZ * 0.5 + 0.5;
00357 
00358             // Store the light vector as vertex color
00359             iBumpmapColors[ idx * 4     ] = vectVert2Light.iX;
00360             iBumpmapColors[ idx * 4 + 1 ] = vectVert2Light.iY;
00361             iBumpmapColors[ idx * 4 + 2 ] = vectVert2Light.iZ;
00362             }
00363         }
00364 
00365     // Setup the array pointers and draw the bumpmapped rectangle
00366     glVertexPointer(   3, GL_FLOAT, 0, iBumpmapVertices );
00367     glColorPointer(    4, GL_FLOAT, 0, iBumpmapColors );
00368     glNormal3f( 0.f, 0.f, 1.f );
00369     glDrawElements( GL_TRIANGLES, (NUM_VERTICES_PER_SIDE - 1) * (NUM_VERTICES_PER_SIDE - 1) * 6, GL_UNSIGNED_BYTE, iBumpmapIndices );
00370 
00371     // reset texture unit 1 state
00372     glActiveTexture( GL_TEXTURE1 );
00373     glMatrixMode( GL_TEXTURE );
00374     glLoadIdentity();
00375     glMatrixMode( GL_MODELVIEW );
00376     glDisable( GL_TEXTURE_2D );
00377     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00378 
00379     // reset texture unit 0 state
00380     glActiveTexture( GL_TEXTURE0 );
00381     glMatrixMode( GL_TEXTURE );
00382     glLoadIdentity();
00383     glMatrixMode( GL_MODELVIEW );
00384     glDisable( GL_TEXTURE_2D );
00385     glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00386     }

void CBumpMap::GenerateBumpmapVerticesL (   )  [private]
 

Generates the vertices for drawing the bumpmapped rectangle to the screen.

Definition at line 393 of file BumpMap.cpp.

References iBumpmapColors, iBumpmapIndices, iBumpmapTexCoords, and iBumpmapVertices.

Referenced by AppInitL().

00394     {
00395     iBumpmapVertices  = new GLfloat[ NUM_VERTICES_PER_SIDE * NUM_VERTICES_PER_SIDE * 3];
00396     iBumpmapTexCoords = new GLfloat[ NUM_VERTICES_PER_SIDE * NUM_VERTICES_PER_SIDE * 2];
00397 
00398     iBumpmapColors    = new GLfloat[ NUM_VERTICES_PER_SIDE * NUM_VERTICES_PER_SIDE * 4];
00399     iBumpmapIndices   = new GLubyte[ (NUM_VERTICES_PER_SIDE - 1) * (NUM_VERTICES_PER_SIDE - 1) * 6];
00400 
00401     // Generate the texture coordinates, vertices, colors and normals
00402     GLfloat leftBottom   = - BUMPMAP_SIZE / 2.f;
00403     GLfloat divisionSize = BUMPMAP_SIZE / (GLfloat) ( NUM_VERTICES_PER_SIDE - 1 );
00404     TInt idx = 0;
00405     for( TInt y = 0; y < NUM_VERTICES_PER_SIDE; y++ )
00406         {
00407         for( TInt x = 0; x < NUM_VERTICES_PER_SIDE; x++ )
00408             {
00409             iBumpmapVertices[idx * 3    ] = leftBottom + x * divisionSize;
00410             iBumpmapVertices[idx * 3 + 1] = leftBottom + y * divisionSize;
00411             iBumpmapVertices[idx * 3 + 2] = BUMPMAP_Z_COORD;
00412 
00413             iBumpmapTexCoords[idx * 2    ] = (GLfloat) x / (GLfloat) ( NUM_VERTICES_PER_SIDE - 1 );
00414             iBumpmapTexCoords[idx * 2 + 1] = 1.f - (GLfloat) y / (GLfloat) ( NUM_VERTICES_PER_SIDE - 1 );
00415 
00416             iBumpmapColors[idx * 4    ] = 1.f;
00417             iBumpmapColors[idx * 4 + 1] = 1.f;
00418             iBumpmapColors[idx * 4 + 2] = 0.f;
00419             iBumpmapColors[idx * 4 + 3] = 1.f;
00420 
00421             idx++;
00422             }
00423         }
00424 
00425     // Generate the indices that are used to draw the
00426     // triangles that in turn form the rectangle
00427     idx = 0;
00428     for( TInt y = 0; y < NUM_VERTICES_PER_SIDE - 1; y++ )
00429         {
00430         for( TInt x = 0; x < NUM_VERTICES_PER_SIDE - 1; x++ )
00431             {
00432             // First triangle of the quad
00433             iBumpmapIndices[idx * 6     ] =    x       +   y       * NUM_VERTICES_PER_SIDE;
00434             iBumpmapIndices[idx * 6  + 2] =    x       + ( y + 1 ) * NUM_VERTICES_PER_SIDE;
00435             iBumpmapIndices[idx * 6  + 1] =  ( x + 1 ) + ( y + 1 ) * NUM_VERTICES_PER_SIDE;
00436             // Second triangle of the quad
00437             iBumpmapIndices[idx * 6  + 3] =    x       +   y       * NUM_VERTICES_PER_SIDE;
00438             iBumpmapIndices[idx * 6  + 5] =  ( x + 1 ) + ( y + 1 ) * NUM_VERTICES_PER_SIDE;
00439             iBumpmapIndices[idx * 6  + 4] =  ( x + 1 ) +   y       * NUM_VERTICES_PER_SIDE;
00440 
00441             idx++;
00442             }
00443         }
00444     }


Member Data Documentation

CTextureManager* CBumpMap::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 144 of file BumpMap.h.

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

CFireFly* CBumpMap::iLightSource [private]
 

Firefly that also acts as the light source.

Definition at line 147 of file BumpMap.h.

Referenced by AppCycle(), AppExit(), AppInitL(), and DrawBumpmappedRect().

GLuint CBumpMap::iScreenWidth [private]
 

Width of the screen

Definition at line 149 of file BumpMap.h.

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

GLuint CBumpMap::iScreenHeight [private]
 

Height of the screen

Definition at line 151 of file BumpMap.h.

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

TTexture CBumpMap::iBumpmapTexture [private]
 

Bumpmapping texture.

Definition at line 153 of file BumpMap.h.

Referenced by AppInitL(), and DrawBumpmappedRect().

TTexture CBumpMap::iTexture [private]
 

Normal color texture.

Definition at line 155 of file BumpMap.h.

Referenced by AppInitL(), and DrawBumpmappedRect().

GLfloat* CBumpMap::iBumpmapVertices [private]
 

Vertices for the bumpmap rectangle.

Definition at line 158 of file BumpMap.h.

Referenced by AppExit(), DrawBumpmappedRect(), and GenerateBumpmapVerticesL().

GLfloat* CBumpMap::iBumpmapColors [private]
 

Color values for the vertices in the bumpmap rectangle.

Definition at line 160 of file BumpMap.h.

Referenced by AppExit(), DrawBumpmappedRect(), and GenerateBumpmapVerticesL().

GLfloat* CBumpMap::iBumpmapTexCoords [private]
 

Texture coordinates for the bumpmap rectangle.

Definition at line 162 of file BumpMap.h.

Referenced by AppExit(), DrawBumpmappedRect(), and GenerateBumpmapVerticesL().

GLubyte* CBumpMap::iBumpmapIndices [private]
 

Indices for drawing the bumpmap rectangle.

Definition at line 164 of file BumpMap.h.

Referenced by AppExit(), and GenerateBumpmapVerticesL().

TCamera CBumpMap::iCamera [private]
 

Camera.

Definition at line 167 of file BumpMap.h.


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

© Nokia 2009

Back to top