Nokia N97 SDK
Example Applications Guide

CMultiTex Class Reference

#include <MultiTex.h>

Inheritance diagram for CMultiTex:

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 38 of file MultiTex.h.


Public Types

enum   { ELoadingTextures, ERunning }

Public Member Functions

virtual  ~CMultiTex ()
void  AppInitL (TAny *aInstance)
void  AppExit (void)
void  AppCyclePbuffer (TInt aFrame)
void  AppCycleWindow (TInt aFrame)
void  OnEnterStateL (TInt aState)
void  OnStartLoadingTexturesL ()
void  OnEndLoadingTexturesL ()
void  CalculateLightsL (void)
void  DrawLights (TInt iFrame)
void  SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CMultiTex *  NewL (TUint aWidth, TUint aHeight)

Protected Member Functions

  CMultiTex (TUint aWidth, TUint aHeight)
void  ConstructL (void)

Private Attributes

CTextureManager *  iTextureManager
TUint  iScreenWidth
TUint  iScreenHeight
GLfloat  iAspectRatio
TTexture  iWorldTexture
TUint8 *  iLightTex
GLuint  iLightTexObjects [2]


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 141 of file MultiTex.h.

00142                 {
00143                 ELoadingTextures,
00144                 ERunning
00145                 };


Constructor & Destructor Documentation

CMultiTex::~CMultiTex (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 111 of file MultiTex.cpp.

00112 {
00113 }

CMultiTex::CMultiTex (  TUint  aWidth,
TUint  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 89 of file MultiTex.cpp.

References iAspectRatio, iScreenHeight, and iScreenWidth.

Referenced by NewL().

00089                                                   :
00090     CFiniteStateMachine(),
00091     iScreenWidth( aWidth ),
00092     iScreenHeight( aHeight )
00093 {
00094     if ( iScreenHeight != 0 )
00095     {
00096         iAspectRatio = (GLfloat)iScreenWidth / (GLfloat)iScreenHeight;
00097     }
00098     else
00099     {
00100         iAspectRatio = 1.0;
00101     }
00102 }


Member Function Documentation

CMultiTex * CMultiTex::NewL (  TUint  aWidth,
TUint  aHeight
)  [static]
 

Factory method for creating a new CMultiTex object.

Definition at line 70 of file MultiTex.cpp.

References CMultiTex().

Referenced by CMultiTexContainer::ConstructL().

00071 {
00072     /* Symbian 2-phase constructor. Calls both the default
00073        C++ constructor and Symbian ConstructL methods */
00074     CMultiTex* self = new (ELeave) CMultiTex( aWidth, aHeight );
00075     CleanupStack::PushL( self );
00076     self->ConstructL();
00077     CleanupStack::Pop();
00078 
00079     return self;
00080 }

void CMultiTex::AppInitL (  TAny *  aInstance  ) 
 

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 195 of file MultiTex.cpp.

References CalculateLightsL(), iAspectRatio, CMultiTexContainer::iEglContext, CMultiTexContainer::iEglDisplay, CMultiTexContainer::iEglPbufferContext, CMultiTexContainer::iEglPbufferSurface, CMultiTexContainer::iEglSurface, iLightTex, iLightTexObjects, iScreenHeight, iScreenWidth, iTextureManager, and iWorldTexture.

Referenced by CMultiTexContainer::ConstructL().

00196 {
00197 
00198     CMultiTexContainer* instance = (CMultiTexContainer*) aInstance;
00199 
00200     /*=========================================================================*/
00201     /*===================== Initialize Pbuffer context ========================*/
00202     /*=========================================================================*/
00203 
00204     /* Bind the pbuffer context to the current rendering thread and surface.*/
00205     eglMakeCurrent( instance->iEglDisplay, instance->iEglPbufferSurface,
00206                     instance->iEglPbufferSurface, instance->iEglPbufferContext );
00207 
00208     /* Create light texture map */
00209     CalculateLightsL();
00210     const GLvoid *texPtr = iLightTex;
00211 
00212     // Set the screen background color (black).
00213     glClearColor( 0.f, 0.f, 0.f, 1.f );
00214 
00215     // Initialize viewport and projection.
00216     glViewport( 0, 0, 128, 128 );
00217     glMatrixMode( GL_PROJECTION );
00218     glFrustumf( -1.f, 1.f, -1.f, 1.f, 3.f, 1000.f );
00219 
00220     /* Initialize appropriate texture matrix. First we have to translate the
00221        input texture coordinate values to be within a range of [0,255]. Hence
00222        we translate the x- and y-coordinate values by 128. Recall that the
00223        values in nokTexCoords are between [-128,127], now they are in a range
00224        of [0,255]. After that we scale the values by 1/255 to make the values
00225        to be in range [0,1]. */
00226     glMatrixMode( GL_TEXTURE );
00227     glLoadIdentity();
00228     glScalef(     1.0f/255.0f, 1.0f/255.0f, 1.0f );
00229     glTranslatef( 128.0f,      128.0f,      0.0f );
00230 
00231     // Remember to change the matrix mode to modelview.
00232     glMatrixMode( GL_MODELVIEW );
00233 
00234     // Enable back face culling, texturing and blending.
00235     glEnable( GL_CULL_FACE  );
00236     glEnable( GL_TEXTURE_2D );
00237     glEnable( GL_BLEND );
00238     glBlendFunc( GL_SRC_ALPHA, GL_ONE );
00239 
00240     // Enable vertex arrays.
00241     glEnableClientState( GL_VERTEX_ARRAY );
00242     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00243 
00244     // Set array pointers.
00245     glVertexPointer( 3, GL_BYTE, 0, LightVertices );
00246     glTexCoordPointer( 2, GL_BYTE, 0, LightTexCoords );
00247 
00248     // Set the initial shading mode
00249     glShadeModel( GL_FLAT );
00250 
00251     /* Create texture names */
00252     glGenTextures( 2, iLightTexObjects );
00253 
00254     // Bind and set the light texture.
00255     glBindTexture( GL_TEXTURE_2D, iLightTexObjects[0] );
00256     glTexImage2D(  GL_TEXTURE_2D, 0, GL_RGBA, 64, 32,
00257                    0, GL_RGBA, GL_UNSIGNED_BYTE, texPtr );
00258     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00259     glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
00260 
00261     /* We can now free the memory reserved for light texture,
00262        since it's loaded inside OpenGL ES */
00263     delete[] iLightTex;
00264     iLightTex = NULL;
00265 
00266     /* Bind and set the environment map texture. We pass NULL as pixels,
00267        since we use glCopyTexImage2D later to initialize the texture memory. */
00268     glBindTexture( GL_TEXTURE_2D, iLightTexObjects[1] );
00269     glTexImage2D(  GL_TEXTURE_2D, 0, GL_RGB, 128, 128,
00270                    0, GL_RGB, GL_UNSIGNED_BYTE, NULL );
00271     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00272     glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00273 
00274     // Do not use perspective correction
00275     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00276 
00277 
00278     /*=========================================================================*/
00279     /*====================== Initialize Window context ========================*/
00280     /*=========================================================================*/
00281 
00282 
00283     // Bind the Window context to the current rendering thread and surface.
00284     eglMakeCurrent( instance->iEglDisplay, instance->iEglSurface,
00285                     instance->iEglSurface, instance->iEglContext );
00286 
00287     // Set the screen background color.
00288     glClearColor( 0.f, 0.f, 0.4f, 1.f );
00289 
00290     /* Enable depth testing and back face culling. */
00291     glEnable( GL_DEPTH_TEST );
00292     glEnable( GL_CULL_FACE  );
00293     glEnable( GL_TEXTURE_2D );
00294 
00295     // Initialize viewport and projection.
00296     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00297     glMatrixMode( GL_PROJECTION );
00298 
00299     // Calculate the view frustrum
00300     glFrustumf( FRUSTUM_LEFT * iAspectRatio, FRUSTUM_RIGHT * iAspectRatio,
00301                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00302                 FRUSTUM_NEAR, FRUSTUM_FAR );
00303 
00304 
00305     // Initialize appropriate texture matrix
00306     glMatrixMode( GL_TEXTURE );
00307     glLoadIdentity();
00308     glScalef(     1.0f/32767.0f, 1.0f/32767.0f, 1.0f );
00309 
00310     // Remember to change the matrix mode to modelview.
00311     glMatrixMode( GL_MODELVIEW );
00312 
00313     // Set shading mode
00314     glShadeModel( GL_FLAT );
00315 
00316     // Do not use perspective correction
00317     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00318 
00319 
00320         // Construct a texture manager that uses the application's private
00321         // directory as the location for all textures.
00322     iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenWidth,
00323                                               iAspectRatio*1.f, iAspectRatio*-1.f, 1.f, -1.f, 3.f,
00324                                               this );
00325 
00326     // Pushing the textures into the loading queue.
00327     _LIT(KWorldTexture, "world.jpg");
00328     iTextureManager->RequestToLoad(KWorldTexture, &iWorldTexture );
00329 
00330     //Start to load the textures.
00331     iTextureManager->DoLoadL();
00332 
00333 }

void CMultiTex::AppExit (  void   ) 
 

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

Definition at line 419 of file MultiTex.cpp.

References iTextureManager.

Referenced by CMultiTexContainer::~CMultiTexContainer().

00420 {
00421     delete iTextureManager;
00422 }

void CMultiTex::AppCyclePbuffer (  TInt  aFrame  ) 
 

Renders one frame with Pbuffer context to Ppbuffer surface.

Parameters:
aFrame  Number of the frame to be rendered.

Definition at line 454 of file MultiTex.cpp.

References DrawLights(), and iLightTexObjects.

Referenced by CMultiTexContainer::DrawCallBack().

00455 {
00456     glClear( GL_COLOR_BUFFER_BIT );
00457 
00458     // Bind the light texture as current
00459     glBindTexture( GL_TEXTURE_2D, iLightTexObjects[0] );
00460 
00461     /* Animate and draw lights */
00462     glLoadIdentity();
00463     gluLookAt( 0.f, 0.f, 200.f,
00464                0.f, 0.f, 0.f,
00465                0.f, 1.f, 0.f );
00466 
00467     glScalex( 2 << 16, 2 << 16, 2 << 16 );
00468     DrawLights( iFrame );
00469 
00470     // Bind the environment map texture as current
00471     glBindTexture( GL_TEXTURE_2D, iLightTexObjects[1] );
00472 
00473     // Copy the content of the pbuffer to the currently bound texture
00474     glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 128, 128, 0 );
00475 }

void CMultiTex::AppCycleWindow (  TInt  aFrame  ) 
 

Renders one frame with Window context to Window surface.

Parameters:
aFrame  Number of the frame to be rendered.

Definition at line 484 of file MultiTex.cpp.

Referenced by CMultiTexContainer::DrawCallBack().

00485 {
00486     // Set the TEXTURE1 unit as active (env map texture)
00487     glActiveTexture( GL_TEXTURE1 );
00488     glClientActiveTexture( GL_TEXTURE1 );
00489 
00490     // Set the correct texture matrix
00491     glMatrixMode( GL_TEXTURE );
00492     glLoadIdentity();
00493     glTranslatef( 0.5f, 0.5f, 0.5f );
00494     glRotatex( 180 << 16, 0, 0, 1 << 16 );
00495     glRotatex( iFrame << 16, 0, 1 << 16, 0 );
00496     glScalef( 1.0f/65534.0f, 1.0f/65534.0f, 1.0f/65534.0f );
00497 
00498     glMatrixMode( GL_MODELVIEW );
00499 
00500     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00501 
00502 
00503     glLoadIdentity();
00504     glTranslatex( 0 , 0 , -200 << 16 );
00505     glRotatex( 180 << 16, 0, 0, 1 << 16 );
00506 
00507 
00508     glRotatex( iFrame << 16, 0, 1 << 16, 0 );
00509 
00510 
00511     // Scale the world model to fit the screen.
00512     glScalex( 1 << 9, 1 << 9, 1 << 9 );
00513     glDrawElements( GL_TRIANGLES, objFaces * 3,
00514                     GL_UNSIGNED_SHORT, objIndices );
00515 }

void CMultiTex::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 407 of file MultiTex.cpp.

00408         {
00409             // Nothing to do here...
00410         }

void CMultiTex::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 340 of file MultiTex.cpp.

00341         {
00342     SetStateL( ELoadingTextures );
00343         }

void CMultiTex::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 349 of file MultiTex.cpp.

References iLightTexObjects, and iWorldTexture.

00350         {
00351     /* TEXTURE0 is used for Marble texture */
00352     glActiveTexture( GL_TEXTURE0 );
00353     glClientActiveTexture( GL_TEXTURE0 );
00354 
00355     // Enable texturing
00356     glEnable( GL_TEXTURE_2D );
00357 
00358     // Enable vertex and texture coordinates arrays.
00359     glEnableClientState( GL_VERTEX_ARRAY );
00360     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00361 
00362     // Set array pointers.
00363     glVertexPointer( 3, GL_SHORT, 0, objVertex );
00364     glTexCoordPointer( 2, GL_SHORT, 0, objTexCoords );
00365 
00366     /* Bind the marble.jpg texture to iWorldTexture, set the texture
00367        to the texPtr defined above, and set the texture environment. */
00368     glBindTexture( GL_TEXTURE_2D, iWorldTexture.iID );
00369     glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00370     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00371 
00372     /* Texture to TEXTURE1 is taken from Pbuffer
00373        context, i.e, iLightTexObjects[1] */
00374     glActiveTexture( GL_TEXTURE1 );
00375     glClientActiveTexture( GL_TEXTURE1 );
00376 
00377     // Enable texturing
00378     glEnable( GL_TEXTURE_2D );
00379 
00380     // Enable vertex and texture coordinates arrays.
00381     glEnableClientState( GL_VERTEX_ARRAY );
00382     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00383 
00384     // Set array pointers.
00385     glVertexPointer( 3, GL_SHORT, 0, objVertex );
00386     glTexCoordPointer( 3, GL_SHORT, 0, envNormals );
00387 
00388     /* Bind the environment map texture created with Pbuffer context.
00389        Actual creation and updating is done in AppCycle. */
00390     glBindTexture( GL_TEXTURE_2D, iLightTexObjects[1] );
00391     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00392 
00393     // Set the texture env mode for environment map texture
00394     glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ADD );
00395 
00396         if ( GetState() == ELoadingTextures )
00397                 {
00398                 SetStateL( ERunning );
00399                 }
00400         }

void CMultiTex::CalculateLightsL (  void   ) 
 

Calculate light texture for environment map lights.

Definition at line 134 of file MultiTex.cpp.

References iLightTex.

Referenced by AppInitL().

00135 {
00136     // Reserve memory for light texture
00137     iLightTex = new (ELeave) TUint8[64*32*4];
00138     if ( !iLightTex )
00139     {
00140         User::LeaveNoMemory();
00141     }
00142 
00143     TInt d         = 0;   // distance from the center of the spot
00144     GLfloat alpha = 0.f;  // alpha value
00145 
00146     for ( TInt i = 0; i < 32; i++ )
00147     {
00148         for ( TInt j = 0; j < 32; j++ )
00149         {
00150             // distance from the center of spotlight
00151             d = (i-16)*(i-16) + (j-16)*(j-16);
00152 
00153             // simple linear attenuation used for alpha
00154             alpha = 255.f*(1.f-(1.f/250.f)*(GLfloat)(d));
00155 
00156             // alpha < 0 corresponds to spotlight cut-off value
00157             if ( alpha < 0.f )
00158             {
00159                 iLightTex[64*4*i+4*j]   = 255;      //r
00160                 iLightTex[64*4*i+4*j+1] = 0;        //g
00161                 iLightTex[64*4*i+4*j+2] = 0;        //b
00162                 iLightTex[64*4*i+4*j+3] = 0;        //a
00163 
00164                 iLightTex[64*4*i+4*j+32*4]   = 0;   //r
00165                 iLightTex[64*4*i+4*j+1+32*4] = 255; //g
00166                 iLightTex[64*4*i+4*j+2+32*4] = 0;   //b
00167                 iLightTex[64*4*i+4*j+3+32*4] = 0;   //a
00168             }
00169             // inside the cut-off, set alpha as calculated above/
00170             else
00171             {
00172                 iLightTex[64*4*i+4*j]   = 255;                   //r
00173                 iLightTex[64*4*i+4*j+1] = 0;                     //g
00174                 iLightTex[64*4*i+4*j+2] = 0;                     //b
00175                 iLightTex[64*4*i+4*j+3] = (GLubyte)(alpha);      //a
00176 
00177                 iLightTex[64*4*i+4*j+32*4]   = 0;                //r
00178                 iLightTex[64*4*i+4*j+1+32*4] = 255;              //g
00179                 iLightTex[64*4*i+4*j+2+32*4] = 0;                //b
00180                 iLightTex[64*4*i+4*j+3+32*4] = (GLubyte)(alpha); //a
00181             }
00182         }
00183     }
00184 }

void CMultiTex::DrawLights (  TInt  iFrame  ) 
 

Draws lights with given dimensions.

Parameters:
iFrame  The current frame number

Definition at line 431 of file MultiTex.cpp.

Referenced by AppCyclePbuffer().

00432 {
00433     // Draw red light
00434     glPushMatrix();
00435     glTranslatex(( 2 << 16) + (GLint) (sin(iFrame * 0.10f) * 5 * 65536) , (2 << 16) + (GLint) (cos(iFrame * 0.13f) * 5 * 65536) , 0 );
00436     glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );
00437     glPopMatrix();
00438 
00439     // Draw green light
00440     glPushMatrix();
00441     glTranslatex( (-2 << 16) + (GLint) (sin(iFrame * 0.10f) * 5 * 65536) , (-2 << 16) + (GLint) (cos(iFrame * 0.10f) * 8 * 65536) , 0 );
00442     glDrawArrays( GL_TRIANGLE_STRIP, 4, 4 );
00443     glPopMatrix();
00444 
00445 }

void CMultiTex::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 522 of file MultiTex.cpp.

References iAspectRatio, iScreenHeight, iScreenWidth, and iTextureManager.

Referenced by CMultiTexContainer::SizeChanged().

00523     {
00524     iScreenWidth  = aWidth;
00525     iScreenHeight = aHeight;
00526 
00527     // Notify the texture manager of screen size change
00528     iTextureManager->SetScreenSize( aWidth, aHeight );
00529 
00530     // Reinitialize viewport and projection.
00531     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00532 
00533     // Recalculate the view frustrum
00534     if ( iScreenHeight != 0 )
00535         {
00536         iAspectRatio = (GLfloat)iScreenWidth / (GLfloat)iScreenHeight;
00537         }
00538     else
00539         {
00540         iAspectRatio = 1.0;
00541         }
00542 
00543     glMatrixMode( GL_PROJECTION );
00544     glLoadIdentity();
00545     glFrustumf( FRUSTUM_LEFT * iAspectRatio, FRUSTUM_RIGHT * iAspectRatio,
00546                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00547                 FRUSTUM_NEAR, FRUSTUM_FAR );
00548     glMatrixMode( GL_PROJECTION );
00549 
00550     }

void CMultiTex::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 122 of file MultiTex.cpp.

00123 {
00124 }


Member Data Documentation

CTextureManager* CMultiTex::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 151 of file MultiTex.h.

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

TUint CMultiTex::iScreenWidth [private]
 

Width of the screen

Definition at line 153 of file MultiTex.h.

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

TUint CMultiTex::iScreenHeight [private]
 

Height of the screen

Definition at line 155 of file MultiTex.h.

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

GLfloat CMultiTex::iAspectRatio [private]
 

Aspect ratio of the screen (iScreenHeight / iScreenWidth).

Definition at line 157 of file MultiTex.h.

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

TTexture CMultiTex::iWorldTexture [private]
 

World texture.

Definition at line 160 of file MultiTex.h.

Referenced by AppInitL(), and OnEndLoadingTexturesL().

TUint8* CMultiTex::iLightTex [private]
 

Pointer to the light texture data.

Definition at line 162 of file MultiTex.h.

Referenced by AppInitL(), and CalculateLightsL().

GLuint CMultiTex::iLightTexObjects[2] [private]
 

List for light and env map texture objects.

Definition at line 164 of file MultiTex.h.

Referenced by AppCyclePbuffer(), AppInitL(), and OnEndLoadingTexturesL().


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

© Nokia 2009

Back to top