Nokia N97 SDK
Example Applications Guide

MultiTexContainer.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : MultiTexContainer.cpp
00004  *  Part of     : OpenGLEx / MultiTex
00005  *
00006  *  Copyright (c) 2004-2006 Nokia Corporation.
00007  *  This material, including documentation and any related
00008  *  computer programs, is protected by copyright controlled by
00009  *  Nokia Corporation.
00010  * ==============================================================================
00011  */
00012 
00013 
00014 // INCLUDE FILES
00015 #include <Eikenv.h>
00016 #include <w32std.h>
00017 
00018 #include "MultiTexContainer.h"
00019 #include <AknUtils.h> // Pen support. 
00020 
00021 
00022 // ================= MEMBER FUNCTIONS =======================
00023 
00024 // ---------------------------------------------------------
00025 // CMultiTexContainer::ConstructL(const TRect& aRect)
00026 // EPOC two phased constructor
00027 // ---------------------------------------------------------
00028 //
00029 void CMultiTexContainer::ConstructL(const TRect& aRect, CAknAppUi* aAppUi)
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 }
00245 
00246 // Destructor
00247 CMultiTexContainer::~CMultiTexContainer()
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 }
00266 
00267 // ---------------------------------------------------------
00268 // CMultiTexContainer::SizeChanged()
00269 // Called by framework when the view size is changed
00270 // ---------------------------------------------------------
00271 //
00272 void CMultiTexContainer::SizeChanged()
00273     {
00274     if( iOpenGlInitialized && iMultiTex )
00275         {
00276         TSize size;
00277         size = this->Size();
00278 
00279         iMultiTex->SetScreenSize( size.iWidth, size.iHeight );
00280         }
00281     }
00282 
00283 
00284 // ---------------------------------------------------------
00285 // CMultiTexContainer::HandleResourceChange(
00286 //     TInt aType)
00287 // Dynamic screen resize changes by calling the
00288 // SetExtentToWholeScreen() method again.
00289 // ---------------------------------------------------------
00290 //
00291  void CMultiTexContainer::HandleResourceChange(TInt aType)
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     }
00308 
00309 
00310 // ---------------------------------------------------------
00311 // CMultiTexContainer::CountComponentControls() const
00312 // ---------------------------------------------------------
00313 //
00314 TInt CMultiTexContainer::CountComponentControls() const
00315 {
00316     return 0;
00317 }
00318 
00319 // ---------------------------------------------------------
00320 // CMultiTexContainer::ComponentControl(TInt aIndex) const
00321 // ---------------------------------------------------------
00322 //
00323 CCoeControl* CMultiTexContainer::ComponentControl(TInt /*aIndex*/ ) const
00324 {
00325     return NULL;
00326 }
00327 
00328 // ---------------------------------------------------------
00329 // CMultiTexContainer::Draw(const TRect& aRect) const
00330 // ---------------------------------------------------------
00331 //
00332 void CMultiTexContainer::Draw(const TRect& /*aRect*/ ) const
00333 {
00334 }
00335 
00336 // ---------------------------------------------------------
00337 // CMultiTexContainer::DrawCallBack( TAny* aInstance )
00338 // Called by the CPeriodic in order to draw the graphics
00339 // ---------------------------------------------------------
00340 //
00341 TInt CMultiTexContainer::DrawCallBack( TAny* aInstance )
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 }
00389 
00390 // ---------------------------------------------------------
00391 // CMultiTexContainer::HandleControlEventL(
00392 //     CCoeControl* aControl,TCoeEvent aEventType)
00393 // ---------------------------------------------------------
00394 //
00395 void CMultiTexContainer::HandleControlEventL(
00396     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00397 {
00398 }
00399 
00400 // End of File

© Nokia 2009

Back to top