Nokia N97 SDK
Example Applications Guide

CMultiTexContainer Class Reference

#include <MultiTexContainer.h>

Inheritance diagram for CMultiTexContainer:

List of all members.

Detailed Description

Container control class that handles the OpenGL ES initialization and deinitializations. Also uses the CMultiTex class to do the actual OpenGL ES rendering.

Definition at line 30 of file MultiTexContainer.h.


Public Member Functions

void  ConstructL (const TRect &aRect, CAknAppUi *aAppUi)
virtual  ~CMultiTexContainer ()

Static Public Member Functions

static TInt  DrawCallBack (TAny *aInstance)

Public Attributes

EGLDisplay  iEglDisplay
EGLSurface  iEglContext
EGLContext  iEglPbufferContext
EGLSurface  iEglSurface
EGLSurface  iEglPbufferSurface
TInt  iFrame
CPeriodic *  iPeriodic
CMultiTex *  iMultiTex
CAknAppUi *  iAppUi

Private Member Functions

void  SizeChanged ()
void  HandleResourceChange (TInt aType)
TInt  CountComponentControls () const
CCoeControl *  ComponentControl (TInt aIndex) const
void  Draw (const TRect &aRect) const
void  HandleControlEventL (CCoeControl *aControl, TCoeEvent aEventType)

Private Attributes

TBool  iOpenGlInitialized


Constructor & Destructor Documentation

CMultiTexContainer::~CMultiTexContainer (   )  [virtual]
 

Destructor. Destroys the CPeriodic, CMultiTex and uninitializes OpenGL ES.

Definition at line 247 of file MultiTexContainer.cpp.

References CMultiTex::AppExit(), iEglContext, iEglDisplay, iEglPbufferSurface, iEglSurface, iMultiTex, and iPeriodic.

00248 {
00249     delete iPeriodic;
00250 
00251     /* AppExit call is made to release
00252        any allocations made in AppInit. */
00253     if ( iMultiTex )
00254     {
00255         iMultiTex->AppExit();
00256         delete iMultiTex;
00257     }
00258 
00259     eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT );
00260     eglDestroySurface( iEglDisplay, iEglSurface );
00261     eglDestroySurface( iEglDisplay, iEglPbufferSurface );
00262     eglDestroyContext( iEglDisplay, iEglContext );
00263     eglTerminate( iEglDisplay );                   // Release resources associated
00264     // with EGL and OpenGL ES
00265 }


Member Function Documentation

void CMultiTexContainer::ConstructL (  const TRect &  aRect,
CAknAppUi *  aAppUi
) 
 

EPOC default constructor.

Parameters:
aRect  Frame rectangle for container.

Definition at line 29 of file MultiTexContainer.cpp.

References CMultiTex::AppInitL(), DrawCallBack(), iAppUi, iEglContext, iEglDisplay, iEglPbufferContext, iEglPbufferSurface, iEglSurface, iFrame, iMultiTex, iOpenGlInitialized, iPeriodic, and CMultiTex::NewL().

Referenced by CMultiTexAppUi::ConstructL().

00030     {
00031     iAppUi = aAppUi;
00032     iOpenGlInitialized = EFalse;
00033     CreateWindowL();
00034 
00035     // Do not go to full-screen in touch devices.    
00036     if (AknLayoutUtils::PenEnabled())
00037     {
00038         SetRect(aRect);
00039     }
00040     else
00041     {
00042         SetExtentToWholeScreen();                // Take the whole screen into use
00043     }
00044     ActivateL();
00045 
00046     iFrame = 0;                              // Initialize frame counter
00047 
00048     EGLConfig config;                       // Describes the format, type and
00049                                              // size of the color buffers and
00050                                              // ancillary buffers for EGLSurface
00051 
00052     // Get the display for drawing graphics
00053     iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
00054     if ( iEglDisplay == NULL )
00055     {
00056         _LIT(KGetDisplayFailed, "eglGetDisplay failed");
00057         User::Panic( KGetDisplayFailed, 0 );
00058     }
00059 
00060     // Initialize display
00061     if ( eglInitialize( iEglDisplay, NULL, NULL ) == EGL_FALSE )
00062     {
00063         _LIT(KInitializeFailed, "eglInitialize failed");
00064         User::Panic( KInitializeFailed, 0 );
00065     }
00066 
00067     EGLConfig *configList = NULL;            // Pointer for EGLConfigs
00068     TInt numOfConfigs = 0;
00069     TInt configSize   = 0;
00070 
00071     // Get the number of possible EGLConfigs
00072     if ( eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs )
00073          == EGL_FALSE )
00074     {
00075         _LIT(KGetConfigsFailed, "eglGetConfigs failed");
00076         User::Panic( KGetConfigsFailed, 0 );
00077     }
00078 
00079     configSize = numOfConfigs;
00080 
00081     // Allocate memory for the configList
00082     configList = (EGLConfig*) User::Alloc( sizeof(EGLConfig)*configSize );
00083     if ( configList == NULL )
00084     {
00085         _LIT(KConfigAllocFailed, "config alloc failed");
00086         User::Panic( KConfigAllocFailed, 0 );
00087     }
00088 
00089     /* Define properties for the wanted EGLSurface
00090        To get the best possible performance, choose
00091        an EGLConfig with a buffersize matching
00092        the current window's display mode*/
00093     TDisplayMode DMode = Window().DisplayMode();
00094     TInt BufferSize = 0;
00095 
00096     switch ( DMode )
00097     {
00098     case(EColor4K):
00099         BufferSize = 12;
00100         break;
00101     case(EColor64K):
00102         BufferSize = 16;
00103         break;
00104     case(EColor16M):
00105         BufferSize = 24;
00106         break;
00107     case(EColor16MU):
00108     case(EColor16MA):
00109         BufferSize = 32;
00110         break;
00111     default:
00112         _LIT(KDModeError,       "unsupported displaymode");
00113         User::Panic( KDModeError, 0 );
00114         break;
00115     }
00116 
00117     // Define properties for requesting a full-screen antialiased window surface
00118     const EGLint attrib_list_fsaa[] =
00119         {
00120                                 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
00121         EGL_BUFFER_SIZE,  BufferSize,
00122         EGL_DEPTH_SIZE,   16,
00123                                 EGL_SAMPLE_BUFFERS, 1,
00124                                 EGL_SAMPLES,        4,
00125         EGL_NONE
00126         };
00127 
00128     // Define properties for requesting a non-antialiased window surface
00129     const EGLint attrib_list[] =
00130         {
00131                                 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
00132         EGL_BUFFER_SIZE,  BufferSize,
00133         EGL_DEPTH_SIZE,   16,
00134         EGL_NONE
00135         };
00136 
00137     // Choose an EGLConfig that best matches to the properties in attrib_list_fsaa
00138     if ( eglChooseConfig( iEglDisplay, attrib_list_fsaa, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00139         {
00140         _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00141         User::Panic( KChooseConfigFailed, 0 );
00142         }
00143 
00144     // Check if no configurations were found
00145     if ( numOfConfigs == 0 )
00146         {
00147         // No configs with antialising were found. Try to get the non-antialiased config
00148                     if ( eglChooseConfig( iEglDisplay, attrib_list, configList, configSize, &numOfConfigs ) == EGL_FALSE )
00149                               {
00150                   _LIT( KChooseConfigFailed, "eglChooseConfig failed" );
00151                   User::Panic( KChooseConfigFailed, 0 );
00152                               }
00153 
00154                     if ( numOfConfigs == 0 )
00155                               {
00156                               // No configs found without antialiasing
00157                   _LIT( KNoConfig, "Can't find the requested config." );
00158                   User::Panic( KNoConfig, 0 );
00159                   }
00160               }
00161 
00162     config = configList[0];     // Choose the best EGLConfig. EGLConfigs
00163                                  // returned by eglChooseConfig are sorted so
00164                                  // that the best matching EGLConfig is first in
00165                                  // the list.
00166 
00167     // Create Pbuffer surface
00168     const EGLint attrib_list2[] = { EGL_WIDTH,
00169                                     128,
00170                                     EGL_HEIGHT,
00171                                     128,
00172                                     EGL_NONE };
00173 
00174     iEglPbufferSurface = eglCreatePbufferSurface( iEglDisplay, config, attrib_list2 );
00175 
00176     if ( iEglPbufferSurface == NULL )
00177     {
00178         _LIT(KCreatePbufferSurfaceFailed, "eglCreatePbufferSurface failed");
00179         User::Panic( KCreatePbufferSurfaceFailed, 0 );
00180     }
00181 
00182     // Create a Pbuffer context
00183     iEglPbufferContext = eglCreateContext( iEglDisplay, config, EGL_NO_CONTEXT, NULL );
00184     if ( iEglPbufferContext == NULL )
00185     {
00186         _LIT(KCreateContextFailed, "eglCreateContext failed");
00187         User::Panic( KCreateContextFailed, 0 );
00188     }
00189 
00190     // Define properties for the another EGLSurface
00191     const EGLint attrib_list_window[] = { EGL_SURFACE_TYPE,
00192                                           EGL_WINDOW_BIT,
00193                                           EGL_BUFFER_SIZE,
00194                                           BufferSize,
00195                                           EGL_DEPTH_SIZE,
00196                                           16,              //16-bit depth-buffer
00197                                           EGL_NONE };
00198 
00199     // Choose an EGLConfig that best matches to the properties in attrib_list
00200     if ( eglChooseConfig( iEglDisplay, attrib_list_window, configList, configSize,
00201                           &numOfConfigs ) == EGL_FALSE )
00202     {
00203         _LIT(KChooseConfigFailed, "eglChooseConfig failed");
00204         User::Panic( KChooseConfigFailed, 0 );
00205     }
00206 
00207     config = configList[0];    // Choose the best EGLConfig. EGLConfigs
00208                                 // returned by eglChooseConfig are sorted so
00209                                 // that the best matching EGLConfig is first in
00210                                 // the list.
00211 
00212     User::Free( configList ); // Free configList, not used anymore.
00213 
00214     // Create window surface
00215     iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(), NULL );
00216 
00217     if ( iEglSurface == NULL )
00218     {
00219         _LIT(KCreateWindowSurfaceFailed, "eglCreateWindowSurface failed");
00220         User::Panic( KCreateWindowSurfaceFailed, 0 );
00221     }
00222 
00223 
00224     // Create a window context
00225     iEglContext = eglCreateContext( iEglDisplay, config, iEglPbufferContext, NULL );
00226     if ( iEglContext == NULL )
00227     {
00228         _LIT(KCreateContextFailed, "eglCreateContext failed");
00229         User::Panic( KCreateContextFailed, 0 );
00230     }
00231 
00232     TSize size;
00233     size = this->Size();
00234 
00235     iMultiTex = CMultiTex::NewL( size.iWidth, size.iHeight ); // Create an instance of MultiTex
00236     iMultiTex->AppInitL( this );
00237 
00238     iOpenGlInitialized = ETrue;
00239 
00240     iPeriodic = CPeriodic::NewL( CActive::EPriorityIdle );    // Create an active object for
00241                                                               // animating the scene
00242 
00243     iPeriodic->Start( 100, 100, TCallBack( CMultiTexContainer::DrawCallBack, this ) );
00244 }

TInt CMultiTexContainer::DrawCallBack (  TAny *  aInstance  )  [static]
 

Callback function for the CPeriodic. Calculates the current frame, keeps the background light from turning off and orders the CMultiTex to do the rendering for each frame.

Parameters:
aInstance  Pointer to this instance of CMultiTexContainer.

Definition at line 341 of file MultiTexContainer.cpp.

References CMultiTex::AppCyclePbuffer(), CMultiTex::AppCycleWindow(), iEglContext, iEglDisplay, iEglPbufferContext, iEglPbufferSurface, iEglSurface, iFrame, and iMultiTex.

Referenced by ConstructL().

00342 {
00343     CMultiTexContainer* instance = (CMultiTexContainer*) aInstance;
00344 
00345     if ( instance->iMultiTex->GetState() == CMultiTex::ERunning )
00346     {
00347         instance->iFrame++;
00348 
00349         /*=========================================================================*/
00350         /* Start by rendering with Pbuffer context to pbuffer and create the dynamic
00351            environment map. First, render to pbuffer and then copy the content
00352            to the second texture (iLightTexObjects[1]).                             */
00353         /*=========================================================================*/
00354         eglMakeCurrent( instance->iEglDisplay, instance->iEglPbufferSurface,
00355                         instance->iEglPbufferSurface, instance->iEglPbufferContext );
00356 
00357         instance->iMultiTex->AppCyclePbuffer( instance->iFrame );
00358 
00359         /*=========================================================================*/
00360         /* Now that we have the environment map ready, we can render the sphere with
00361            marble and environment map (dynamic lights) textures                    */
00362         /*=========================================================================*/
00363         eglMakeCurrent( instance->iEglDisplay, instance->iEglSurface,
00364                         instance->iEglSurface, instance->iEglContext );
00365 
00366         instance->iMultiTex->AppCycleWindow( instance->iFrame );
00367 
00368         // Call eglSwapBuffers, which blit the graphics to the window
00369         eglSwapBuffers( instance->iEglDisplay, instance->iEglSurface );
00370 
00371         // To keep the background light on
00372         if ( !(instance->iFrame%100) )
00373         {
00374             User::ResetInactivityTime();
00375         }
00376 
00377         /* Suspend the current thread for a short while. Give some time
00378            to other threads and AOs, avoids the ViewSrv error in ARMI and
00379            THUMB release builds. One may try to decrease the callback
00380            function instead of this. */
00381         if ( !(instance->iFrame%50) )
00382         {
00383             User::After(0);
00384         }
00385 
00386     }
00387     return 0;
00388 }

void CMultiTexContainer::SizeChanged (   )  [private]
 

Method from CoeControl that gets called when the display size changes. If OpenGL has been initialized, notifies the renderer class that the screen size has changed.

Definition at line 272 of file MultiTexContainer.cpp.

References iMultiTex, iOpenGlInitialized, and CMultiTex::SetScreenSize().

00273     {
00274     if( iOpenGlInitialized && iMultiTex )
00275         {
00276         TSize size;
00277         size = this->Size();
00278 
00279         iMultiTex->SetScreenSize( size.iWidth, size.iHeight );
00280         }
00281     }

void CMultiTexContainer::HandleResourceChange (  TInt  aType  )  [private]
 

Handles a change to the control's resources. This method reacts to the KEikDynamicLayoutVariantSwitch event (that notifies of screen size change) by calling the SetExtentToWholeScreen() again so that this control fills the new screen size. This will then trigger a call to the SizeChanged() method.

Parameters:
aType  Message UID value, only KEikDynamicLayoutVariantSwitch is handled by this method.

Definition at line 291 of file MultiTexContainer.cpp.

References iAppUi.

00292     {
00293         switch( aType )
00294         {
00295             case KEikDynamicLayoutVariantSwitch:
00296                 // Do not go full-screen in touch devices.    
00297             if (AknLayoutUtils::PenEnabled())
00298             {
00299                 SetRect(iAppUi->ClientRect());
00300             }
00301             else
00302             {
00303                 SetExtentToWholeScreen();                // Take the whole screen into use
00304             }
00305                 break;
00306             }
00307     }

TInt CMultiTexContainer::CountComponentControls (   )  const [private]
 

Method from CoeControl. Does nothing in this implementation.

Definition at line 314 of file MultiTexContainer.cpp.

00315 {
00316     return 0;
00317 }

CCoeControl * CMultiTexContainer::ComponentControl (  TInt  aIndex  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation.

Definition at line 323 of file MultiTexContainer.cpp.

00324 {
00325     return NULL;
00326 }

void CMultiTexContainer::Draw (  const TRect &  aRect  )  const [private]
 

Method from CCoeControl. Does nothing in this implementation. All rendering is done in the DrawCallBack() method.

Definition at line 332 of file MultiTexContainer.cpp.

00333 {
00334 }

void CMultiTexContainer::HandleControlEventL (  CCoeControl *  aControl,
TCoeEvent  aEventType
)  [private]
 

Method from MCoeControlObserver that handles an event from the observed control. Does nothing in this implementation.

Parameters:
aControl  Control changing its state.
aEventType  Type of the control event.

Definition at line 395 of file MultiTexContainer.cpp.

00397 {
00398 }


Member Data Documentation

EGLDisplay CMultiTexContainer::iEglDisplay
 

Display where the OpenGL ES window surface resides.

Definition at line 100 of file MultiTexContainer.h.

Referenced by CMultiTex::AppInitL(), ConstructL(), DrawCallBack(), and ~CMultiTexContainer().

EGLSurface CMultiTexContainer::iEglContext
 

Window context.

Definition at line 103 of file MultiTexContainer.h.

Referenced by CMultiTex::AppInitL(), ConstructL(), DrawCallBack(), and ~CMultiTexContainer().

EGLContext CMultiTexContainer::iEglPbufferContext
 

Pbuffer context.

Definition at line 106 of file MultiTexContainer.h.

Referenced by CMultiTex::AppInitL(), ConstructL(), and DrawCallBack().

EGLSurface CMultiTexContainer::iEglSurface
 

Window surface.

Definition at line 109 of file MultiTexContainer.h.

Referenced by CMultiTex::AppInitL(), ConstructL(), DrawCallBack(), and ~CMultiTexContainer().

EGLSurface CMultiTexContainer::iEglPbufferSurface
 

The Pbuffer where the graphics are drawn.

Definition at line 112 of file MultiTexContainer.h.

Referenced by CMultiTex::AppInitL(), ConstructL(), DrawCallBack(), and ~CMultiTexContainer().

TInt CMultiTexContainer::iFrame
 

Frame counter variable, used in the animation.

Definition at line 115 of file MultiTexContainer.h.

Referenced by ConstructL(), and DrawCallBack().

CPeriodic* CMultiTexContainer::iPeriodic
 

Active object that is the timing source for the animation.

Definition at line 118 of file MultiTexContainer.h.

Referenced by ConstructL(), and ~CMultiTexContainer().

CMultiTex* CMultiTexContainer::iMultiTex
 

Used in DrawCallBack() method to do the actual OpenGL ES rendering.

Definition at line 121 of file MultiTexContainer.h.

Referenced by ConstructL(), DrawCallBack(), SizeChanged(), and ~CMultiTexContainer().

CAknAppUi* CMultiTexContainer::iAppUi
 

Application UI class for querying the client rectangle size.

Definition at line 124 of file MultiTexContainer.h.

Referenced by ConstructL(), and HandleResourceChange().

TBool CMultiTexContainer::iOpenGlInitialized [private]
 

Flag that indicates if OpenGL ES has been initialized or not. Used to check if SizeChanged() can react to incoming notifications.

Definition at line 132 of file MultiTexContainer.h.

Referenced by ConstructL(), and SizeChanged().


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

© Nokia 2009

Back to top