Nokia N97 SDK
Example Applications Guide

CTexture Class Reference

#include <Texture.h>

Inheritance diagram for CTexture:

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


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


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

00153                 {
00154                 ELoadingTextures,
00155                 ERunning
00156                 };


Constructor & Destructor Documentation

CTexture::~CTexture (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 251 of file Texture.cpp.

00252 {
00253 }

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 215 of file Texture.cpp.

References iScreenHeight, and iScreenWidth.

Referenced by NewL().

00215                                                : CFiniteStateMachine()
00216 {
00217     iScreenWidth  = aWidth;
00218     iScreenHeight = aHeight;
00219 }


Member Function Documentation

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

Factory method for creating a new CSimpleCube object.

Definition at line 237 of file Texture.cpp.

References CTexture().

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

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 261 of file Texture.cpp.

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

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

void CTexture::AppExit (  void   ) 
 

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

Definition at line 345 of file Texture.cpp.

References iTextureManager.

Referenced by CTextureContainer::~CTextureContainer().

00346 {
00347     delete iTextureManager;
00348 }

void CTexture::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame  Number of the frame to be rendered.

Definition at line 356 of file Texture.cpp.

References iNasaHubble, and iOpenGLES.

Referenced by CTextureContainer::DrawCallBack().

00357 {
00358     glClear( GL_COLOR_BUFFER_BIT );
00359 
00360     /* Animate and draw the cube. */
00361     glLoadIdentity();
00362     gluLookAt( 0.f, 0.f, 120.f,     // Camera position
00363                0.f, 0.f,   0.f,     // Look at point
00364                0.f, 1.f,   0.f );   // Up direction
00365 
00366 
00367     glRotatex( aFrame << 16, 1 << 16, 0      , 0       );
00368     glRotatex( aFrame << 15, 0      , 1 << 16, 0       );
00369     glRotatex( aFrame << 14, 0      , 0      , 1 << 16 );
00370 
00371     /* Scale the cube coordinates to fit the screen. */
00372     glScalex( 20 << 16, 20 << 16, 20 << 16 );
00373 
00374     /* Draw the first 5 sides of the cube with the texture Nasa Hubble. */
00375     glBindTexture(  GL_TEXTURE_2D, iNasaHubble.iID );
00376     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00377     glDrawElements( GL_TRIANGLES, 10 * 3, GL_UNSIGNED_BYTE, triangles );
00378 
00379     /* Draw the remaining side of the cube with the ogles.jpg texture. */
00380     glBindTexture(  GL_TEXTURE_2D, iOpenGLES.iID );
00381     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
00382     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles2 );
00383 }

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 412 of file Texture.cpp.

00413         {
00414             // Nothing to do here...
00415         }

void CTexture::OnStartLoadingTexturesL (   ) 
 

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

Definition at line 390 of file Texture.cpp.

00391         {
00392     SetStateL( ELoadingTextures );
00393         }

void CTexture::OnEndLoadingTexturesL (   ) 
 

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

Definition at line 399 of file Texture.cpp.

00400         {
00401         if ( GetState() == ELoadingTextures )
00402                 {
00403                 SetStateL( ERunning );
00404                 }
00405         }

void CTexture::TogglePerspective (  void   ) 
 

Toggles perspective correction on and off.

Definition at line 428 of file Texture.cpp.

References iPerspectiveCorrEnabled.

Referenced by CTextureAppUi::HandleCommandL().

00429 {
00430     if( iPerspectiveCorrEnabled)
00431     {
00432         iPerspectiveCorrEnabled = EFalse;
00433         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00434     }
00435     else
00436     {
00437         iPerspectiveCorrEnabled = ETrue;
00438         glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
00439     }
00440 }

void CTexture::ToggleWrapping (  void   ) 
 

Toggles texture wrapping on and off.

Definition at line 450 of file Texture.cpp.

References iTextureWrapEnabled.

Referenced by CTextureAppUi::HandleCommandL().

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

void CTexture::ToggleLighting (  void   ) 
 

Toggles lighting on and off.

Definition at line 484 of file Texture.cpp.

References iLightingEnabled, iNasaHubble, and iOpenGLES.

Referenced by CTextureAppUi::HandleCommandL().

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

void CTexture::ToggleBlending (  void   ) 
 

Toggles blending enabled and disabled.

Definition at line 530 of file Texture.cpp.

References iBlendingEnabled.

Referenced by CTextureAppUi::HandleKeyEventL().

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

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 553 of file Texture.cpp.

References iScreenHeight, iScreenWidth, and iTextureManager.

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

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

void CTexture::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 227 of file Texture.cpp.

00228 {
00229 }


Member Data Documentation

TBool CTexture::iPerspectiveCorrEnabled
 

Whether perspective correction is used or not.

Definition at line 159 of file Texture.h.

Referenced by AppInitL(), CTextureAppUi::DynInitMenuPaneL(), and TogglePerspective().

TBool CTexture::iTextureWrapEnabled
 

Whether texture wrapping is on or off.

Definition at line 162 of file Texture.h.

Referenced by AppInitL(), CTextureAppUi::DynInitMenuPaneL(), and ToggleWrapping().

TBool CTexture::iLightingEnabled
 

Whether lighting is on or off.

Definition at line 165 of file Texture.h.

Referenced by AppInitL(), and ToggleLighting().

TBool CTexture::iBlendingEnabled
 

Whether blending is enabled or disabled.

Definition at line 168 of file Texture.h.

Referenced by AppInitL(), and ToggleBlending().

CTextureManager* CTexture::iTextureManager [private]
 

Texture manager that is used to load the used textures.

Definition at line 173 of file Texture.h.

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

TUint CTexture::iScreenWidth [private]
 

Width of the screen

Definition at line 175 of file Texture.h.

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

TUint CTexture::iScreenHeight [private]
 

Height of the screen

Definition at line 177 of file Texture.h.

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

TTexture CTexture::iNasaHubble [private]
 

Nasa Hubble space telescope texture.

Definition at line 179 of file Texture.h.

Referenced by AppCycle(), AppInitL(), and ToggleLighting().

TTexture CTexture::iOpenGLES [private]
 

"OpenGL ES" texture.

Definition at line 181 of file Texture.h.

Referenced by AppCycle(), AppInitL(), and ToggleLighting().


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

© Nokia 2009

Back to top