S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CTexture Class Reference

#include <Texture.h>

List of all members.

Public Types

enum  { ELoadingTextures, ERunning }

Public Member Functions

virtual ~CTexture ()
void AppInitL (void)
void AppExit (void)
void AppCycle (TInt aFrame)
void OnEnterStateL (TInt aState)
void OnStartLoadingTexturesL ()
void OnEndLoadingTexturesL ()
void TogglePerspective (void)
void ToggleWrapping (void)
void ToggleLighting (void)
void ToggleBlending (void)
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CTexture * NewL (TUint aWidth, TUint aHeight)

Public Attributes

TBool iPerspectiveCorrEnabled
TBool iTextureWrapEnabled
TBool iLightingEnabled
TBool iBlendingEnabled

Protected Member Functions

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

Private Attributes

CTextureManager * iTextureManager
TUint iScreenWidth
TUint iScreenHeight
TTexture iNasaHubble
TTexture iOpenGLES


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 42 of file Texture.h.


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 154 of file Texture.h.

00155                 {
00156                 ELoadingTextures, 
00157                 ERunning
00158                 };


Constructor & Destructor Documentation

CTexture::~CTexture (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 254 of file Texture.cpp.

00255 {
00256 }

CTexture::CTexture (  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 218 of file Texture.cpp.

References iScreenHeight, and iScreenWidth.

00218                                                : CFiniteStateMachine()
00219 {
00220     iScreenWidth  = aWidth;
00221     iScreenHeight = aHeight;
00222 }


Member Function Documentation

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

Factory method for creating a new CSimpleCube object.

Definition at line 240 of file Texture.cpp.

References CTexture().

00241 {
00242     /* Symbian 2-phase constructor. Calls both the default 
00243        C++ constructor and Symbian ConstructL methods */
00244     CTexture* self = new (ELeave) CTexture( aWidth, aHeight );
00245     CleanupStack::PushL( self );
00246     self->ConstructL();
00247     CleanupStack::Pop();
00248 
00249     return self;
00250 }

void CTexture::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 264 of file Texture.cpp.

References iBlendingEnabled, iLightingEnabled, iNasaHubble, iOpenGLES, iPerspectiveCorrEnabled, iScreenHeight, iScreenWidth, iTextureManager, and iTextureWrapEnabled.

00265 {
00266     /* Set the screen background color. */
00267     glClearColor( 0.f, 0.f, 0.f, 1.f );
00268 
00269     /* Enable back face culling, texturing, and normalization. */
00270     glEnable( GL_CULL_FACE  );
00271     glEnable( GL_TEXTURE_2D );
00272     glEnable( GL_NORMALIZE  );
00273 
00274     /* Initialize viewport, texture matrix scaling,  and projection. */
00275     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00276     glMatrixMode( GL_PROJECTION );
00277 
00278     // Calculate the view frustrum
00279     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00280     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00281                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00282                 FRUSTUM_NEAR, FRUSTUM_FAR );
00283 
00284     /* Initialize appropriate texture matrix. First we have to translate the
00285        input texture coordinate values to be within a range of [0,255]. Hence
00286        we translate the x- and y-coordinate values by 128. Recall that the 
00287        values in nokTexCoords are between [-128,127], now they are in a range 
00288        of [0,255]. After that we scale the values by 1/255 to make the values 
00289        to be in range [0,1]. */
00290     glMatrixMode( GL_TEXTURE );
00291     glLoadIdentity();
00292     glScalef(     1.0f/255.0f, 1.0f/255.0f, 1.0f );
00293     glTranslatef( 128.0f,      128.0f,      0.0f );
00294 
00295     /* Remember to change the matrix mode to modelview. */
00296     glMatrixMode( GL_MODELVIEW );
00297 
00298     /* Enable vertex and texture arrays. */
00299     glEnableClientState( GL_VERTEX_ARRAY        );
00300     glEnableClientState( GL_TEXTURE_COORD_ARRAY );
00301     glEnableClientState( GL_NORMAL_ARRAY        );
00302 
00303     /* Set array pointers. */
00304     glVertexPointer(   3, GL_BYTE, 0, vertices     );
00305     glTexCoordPointer( 2, GL_BYTE, 0, nokTexCoords );
00306     glNormalPointer(   GL_BYTE, 0, normals         );
00307 
00308     /* Set up global ambient light. */
00309     glLightModelxv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );
00310 
00311     /* Set up lamp. */
00312     glEnable(   GL_LIGHT0 );
00313     glLightxv( GL_LIGHT0, GL_DIFFUSE,  lightDiffuseLamp  );
00314     glLightxv( GL_LIGHT0, GL_AMBIENT,  lightAmbientLamp  );
00315     glLightxv( GL_LIGHT0, GL_SPECULAR, lightSpecularLamp );
00316     glLightxv( GL_LIGHT0, GL_POSITION, lightPositionLamp );
00317 
00318     /* Set shading mode*/
00319     glShadeModel( GL_FLAT ); 
00320     
00321     /* Set the blending function for translucency. */
00322     glBlendFunc( GL_ONE, GL_ONE ); 
00323     
00324     /* Do not use perspective correction, can be enabled from the menu */
00325     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00326 
00327     /* Symbian menu item initialization. See CTextureAppUi.cpp for more. */
00328     iPerspectiveCorrEnabled = EFalse; // no texture perspective correction
00329     iTextureWrapEnabled     = EFalse; // no texture wrapping
00330     iLightingEnabled        = EFalse; // no lighting
00331     iBlendingEnabled        = EFalse; // no blending
00332 
00333         // Construct a texture manager that uses the application's private
00334         // directory as the location for all textures.
00335     iTextureManager = CTextureManager::NewL ( iScreenWidth, iScreenWidth,
00336                                               FRUSTUM_TOP, FRUSTUM_BOTTOM, FRUSTUM_RIGHT, FRUSTUM_LEFT, FRUSTUM_NEAR,                               
00337                                               this );
00338 
00339     // Pushing the textures into the loading queue.
00340     _LIT(KHubbleTexture, "nasa_hubble.jpg");
00341     _LIT(KOGLESTexture,  "ogles.jpg");
00342     iTextureManager->RequestToLoad(KHubbleTexture, &iNasaHubble );
00343     iTextureManager->RequestToLoad(KOGLESTexture,  &iOpenGLES );
00344 
00345     //Start to load the textures.
00346     iTextureManager->DoLoadL();
00347 }

void CTexture::AppExit (  void   ) 
 

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

Definition at line 355 of file Texture.cpp.

References iTextureManager.

00356 {
00357     delete iTextureManager;
00358 }

void CTexture::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 366 of file Texture.cpp.

References iNasaHubble, and iOpenGLES.

00367 {
00368     #define cameraDistance 120
00369 
00370     glClear( GL_COLOR_BUFFER_BIT );
00371 
00372     /* Animate and draw the cube. */
00373     glLoadIdentity();
00374     glTranslatex( 0 , 0 , -cameraDistance << 16 );
00375     glRotatex( aFrame << 16, 1 << 16, 0      , 0       );
00376     glRotatex( aFrame << 15, 0      , 1 << 16, 0       );
00377     glRotatex( aFrame << 14, 0      , 0      , 1 << 16 );
00378 
00379     /* Scale the cube coordinates to fit the screen. */
00380     glScalex( 20 << 16, 20 << 16, 20 << 16 );
00381 
00382     /* Draw the first 5 sides of the cube with the texture Nasa Hubble. */
00383     glBindTexture(  GL_TEXTURE_2D, iNasaHubble.iID );
00384     glDrawElements( GL_TRIANGLES, 10 * 3, GL_UNSIGNED_BYTE, triangles );
00385 
00386     /* Draw the remaining side of the cube with the ogles.jpg texture. */
00387     glBindTexture(  GL_TEXTURE_2D, iOpenGLES.iID );
00388     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles2 );
00389 }

void CTexture::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 418 of file Texture.cpp.

00419         {
00420             // Nothing to do here...
00421         }

void CTexture::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 396 of file Texture.cpp.

00397         {
00398     SetStateL( ELoadingTextures );
00399         }

void CTexture::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 405 of file Texture.cpp.

00406         {
00407         if ( GetState() == ELoadingTextures )
00408                 {
00409                 SetStateL( ERunning );
00410                 }
00411         }

void CTexture::TogglePerspective (  void   ) 
 

Toggles perspective correction on and off.

Definition at line 434 of file Texture.cpp.

References iPerspectiveCorrEnabled.

00435 {
00436     if( iPerspectiveCorrEnabled)
00437     {
00438         iPerspectiveCorrEnabled = EFalse;
00439         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00440     }
00441     else
00442     {
00443         iPerspectiveCorrEnabled = ETrue;
00444         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
00445     }
00446 }

void CTexture::ToggleWrapping (  void   ) 
 

Toggles texture wrapping on and off.

Definition at line 456 of file Texture.cpp.

References iTextureWrapEnabled.

00457 {
00458     if( iTextureWrapEnabled )
00459     {
00460         iTextureWrapEnabled = EFalse;
00461         glMatrixMode( GL_TEXTURE );
00462         glLoadIdentity();
00463         glScalef( 1.0f/255.0f, 1.0f/255.0f, 1.0f );
00464         glTranslatef( 128.0f, 128.0f, 1.0f );
00465         glMatrixMode( GL_MODELVIEW );
00466     }
00467     /* Texture wrapping is implemented here simply by changing the scale of
00468        texture matrix. Now the coordinate values are between [0,2], which
00469        causes the texture to wrap one time. */
00470     else
00471     {
00472         iTextureWrapEnabled = ETrue;
00473         glMatrixMode( GL_TEXTURE );
00474         glLoadIdentity();
00475         glScalef( 1.0f/128.0f, 1.0f/128.0f, 1.0f );
00476         glTranslatef( 128.0f, 128.0f, 1.0f );
00477         glMatrixMode( GL_MODELVIEW );
00478     }
00479 }

void CTexture::ToggleLighting (  void   ) 
 

Toggles lighting on and off.

Definition at line 490 of file Texture.cpp.

References iLightingEnabled, iNasaHubble, and iOpenGLES.

00491 {
00492     // If texture loading is still going on, return from this method without doing anything.
00493         if ( GetState() == ELoadingTextures )
00494     {
00495         return;
00496     }
00497 
00498     if( iLightingEnabled )
00499     {
00500         iLightingEnabled = EFalse;
00501         glShadeModel( GL_FLAT );  // Don't need smooth shading when no lights
00502         glDisable( GL_LIGHTING ); // Disable lighting
00503 
00504         /* Set both textures environments to GL_REPLACE. */
00505         glBindTexture( GL_TEXTURE_2D, iNasaHubble.iID );
00506         glTexEnvx(  GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00507         
00508         glBindTexture( GL_TEXTURE_2D, iOpenGLES.iID );
00509         glTexEnvx(  GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
00510     }
00511     else
00512     {
00513         iLightingEnabled = ETrue;
00514         glShadeModel( GL_SMOOTH ); // For the lighting to work properly 
00515         glEnable( GL_LIGHTING );   // Enable lighting
00516 
00517         /* Set both textures environments to GL_MODULATE. */
00518         glBindTexture( GL_TEXTURE_2D, iNasaHubble.iID );
00519         glTexEnvx(  GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00520         
00521         glBindTexture( GL_TEXTURE_2D, iOpenGLES.iID );
00522         glTexEnvx(  GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
00523     }
00524 }

void CTexture::ToggleBlending (  void   ) 
 

Toggles blending enabled and disabled.

Definition at line 532 of file Texture.cpp.

References iBlendingEnabled.

00533 {
00534     if( iBlendingEnabled )
00535     {
00536         // Blending was enabled, disable blending
00537         iBlendingEnabled = EFalse;
00538         glDisable( GL_BLEND );
00539         glEnable( GL_CULL_FACE  );
00540     }
00541     else
00542     {
00543         // Blending was disabled, enable blending
00544         iBlendingEnabled = ETrue;
00545         glEnable( GL_BLEND );
00546         glDisable( GL_CULL_FACE  );
00547     }
00548 }

void CTexture::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 555 of file Texture.cpp.

References iScreenHeight, iScreenWidth, and iTextureManager.

00556     {
00557     iScreenWidth  = aWidth;
00558     iScreenHeight = aHeight;
00559     
00560     // Notify the texture manager of screen size change
00561     iTextureManager->SetScreenSize( aWidth, aHeight );
00562 
00563     // Reinitialize viewport and projection.
00564     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00565 
00566     // Recalculate the view frustrum
00567     glMatrixMode( GL_PROJECTION );
00568     glLoadIdentity();
00569     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00570     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00571                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00572                 FRUSTUM_NEAR, FRUSTUM_FAR );
00573     glMatrixMode( GL_MODELVIEW );
00574     }

void CTexture::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 230 of file Texture.cpp.

00231 {
00232 }


Member Data Documentation

TBool CTexture::iPerspectiveCorrEnabled
 

Whether perspective correction is used or not.

Definition at line 161 of file Texture.h.

TBool CTexture::iTextureWrapEnabled
 

Whether texture wrapping is on or off.

Definition at line 164 of file Texture.h.

TBool CTexture::iLightingEnabled
 

Whether lighting is on or off.

Definition at line 167 of file Texture.h.

TBool CTexture::iBlendingEnabled
 

Whether blending is enabled or disabled.

Definition at line 170 of file Texture.h.

CTextureManager* CTexture::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 175 of file Texture.h.

TUint CTexture::iScreenWidth [private]
 

Width of the screen

Definition at line 177 of file Texture.h.

TUint CTexture::iScreenHeight [private]
 

Height of the screen

Definition at line 179 of file Texture.h.

TTexture CTexture::iNasaHubble [private]
 

Nasa Hubble space telescope texture.

Definition at line 181 of file Texture.h.

TTexture CTexture::iOpenGLES [private]
 

"OpenGL ES" texture.

Definition at line 183 of file Texture.h.


© Nokia 2006

Back to top