S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SimpleCubePixmap.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SimpleCubePixmap.cpp
00004 *  Part of     : OpenGLEx / SimpleCubePixmap
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 
00017 // INCLUDE FILES
00018 #include <e32std.h>
00019 #include "SimpleCubePixmap.h"
00020 
00021 // MACROS
00022 #define cameraDistance 100
00023 
00024 // CONSTANTS
00025 /** Vertice coordinates for the cube. */
00026 static const GLbyte vertices[8 * 3] =
00027     {
00028     -1,  1,  1,
00029      1,  1,  1,
00030      1, -1,  1,
00031     -1, -1,  1,
00032 
00033     -1,  1, -1,
00034      1,  1, -1,
00035      1, -1, -1,
00036     -1, -1, -1
00037     };
00038 
00039 /** Colors for vertices (Red, Green, Blue, Alpha). */
00040 static const GLubyte colors[8 * 4] =
00041     {
00042     0  ,255,  0,255,
00043     0  ,  0,255,255,
00044     0  ,255,  0,255,
00045     255,  0,  0,255,
00046 
00047     0  ,  0,255,255,
00048     255,  0,  0,255,
00049     0  ,  0,255,255,
00050     0  ,255,  0,255
00051     };
00052 
00053 
00054 /**
00055  * Indices for drawing the triangles.
00056  * The color of the triangle is determined by 
00057  * the color of the last vertex of the triangle.
00058  */
00059 static const GLubyte triangles[12 * 3] =
00060     {
00061     /* front */
00062     1,0,3,
00063     1,3,2,
00064 
00065     /* right */
00066     2,6,5,
00067     2,5,1,
00068     
00069     /* back */
00070     7,4,5,
00071     7,5,6,
00072     
00073     /* left */
00074     0,4,7,
00075     0,7,3,
00076     
00077     /* top */
00078     5,4,0,
00079     5,0,1,
00080     
00081     /* bottom */
00082     3,7,6,
00083     3,6,2
00084     };
00085 
00086 
00087 
00088 
00089 // ============================ MEMBER FUNCTIONS ===============================
00090 
00091 // -----------------------------------------------------------------------------
00092 // CSimpleCubePixmap::NewL
00093 // 
00094 // The usual Symbian NewL implementation. 
00095 // Creates the object, pushes it to cleanup stack and calls ContructL.
00096 // Returns the contructed object or leaves.
00097 // -----------------------------------------------------------------------------
00098 //
00099 
00100 CSimpleCubePixmap* CSimpleCubePixmap::NewL( TUint aWidth, TUint aHeight )
00101     {
00102     /* Symbian 2-phase constructor. Calls both the default 
00103        C++ constructor and Symbian ConstructL methods */
00104     CSimpleCubePixmap* self = new (ELeave) CSimpleCubePixmap( aWidth, aHeight );
00105     CleanupStack::PushL( self );
00106     self->ConstructL();
00107     CleanupStack::Pop();
00108 
00109     return self;
00110     }
00111 
00112 
00113 // -----------------------------------------------------------------------------
00114 // CSimpleCubePixmap::CSimpleCubePixmap
00115 // 
00116 // C++ constructor. Initializes the size instance variables from arguments.
00117 // -----------------------------------------------------------------------------
00118 //
00119 
00120 CSimpleCubePixmap::CSimpleCubePixmap( TUint aWidth, TUint aHeight ) : 
00121     iScreenWidth( aWidth ),
00122     iScreenHeight( aHeight )
00123     {
00124     }
00125 
00126 // Destructor. 
00127 CSimpleCubePixmap::~CSimpleCubePixmap()
00128     {
00129     }
00130 
00131 
00132 // -----------------------------------------------------------------------------
00133 // CSimpleCubePixmap::ConstructL
00134 // 
00135 // 2nd phase constructor.
00136 // -----------------------------------------------------------------------------
00137 //
00138 
00139 void CSimpleCubePixmap::ConstructL( void )
00140     {
00141     }
00142 
00143 
00144 // -----------------------------------------------------------------------------
00145 // CSimpleCubePixmap::AppInit
00146 // 
00147 // Initializes OpenGL ES, sets the vertex and color arrays and pointers, 
00148 // and selects the shading mode.
00149 // -----------------------------------------------------------------------------
00150 //
00151 
00152 void CSimpleCubePixmap::AppInit( void )
00153     {
00154     // Set the screen background color. 
00155     glClearColor( 0.f, 0.f, 0.f, 1.f );
00156 
00157     // Enable back face culling. 
00158     glEnable( GL_CULL_FACE  );
00159 
00160     // Initialize viewport and projection. 
00161     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00162     glMatrixMode( GL_PROJECTION );
00163 
00164     // Calculate the view frustrum
00165     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00166     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00167                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00168                 FRUSTUM_NEAR, FRUSTUM_FAR );
00169 
00170     glMatrixMode( GL_MODELVIEW );
00171 
00172     // Enable vertex arrays. 
00173     glEnableClientState( GL_VERTEX_ARRAY );
00174 
00175     // Set array pointers. 
00176     glVertexPointer( 3, GL_BYTE, 0, vertices );
00177 
00178     // Enable color arrays.
00179     glEnableClientState( GL_COLOR_ARRAY );
00180 
00181     // Set color pointers. 
00182     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
00183 
00184     // Set the initial shading mode 
00185     glShadeModel( GL_FLAT ); 
00186 
00187     // Do not use perspective correction 
00188     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00189 
00190     
00191     }
00192 
00193 
00194 // -----------------------------------------------------------------------------
00195 // CSimpleCubePixmap::AppExit
00196 // 
00197 // Release any allocations made in AppInit.
00198 // -----------------------------------------------------------------------------
00199 //
00200 void CSimpleCubePixmap::AppExit( void )
00201     {
00202     }
00203 
00204 
00205 // -----------------------------------------------------------------------------
00206 // CSimpleCubePixmap::DrawBox
00207 // 
00208 // Draws a box with triangles.
00209 // Scales the box to the given size using glScalef.
00210 // -----------------------------------------------------------------------------
00211 //
00212 
00213 void CSimpleCubePixmap::DrawBox( GLfloat aSizeX, GLfloat aSizeY, GLfloat aSizeZ )
00214     {
00215     glScalef( aSizeX, aSizeY, aSizeZ );
00216 
00217     glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00218     }
00219 
00220 
00221 // -----------------------------------------------------------------------------
00222 // CSimpleCubePixmap::AppCycle
00223 // 
00224 // Draws and animates the objects.
00225 // The frame number determines the amount of rotation.
00226 // -----------------------------------------------------------------------------
00227 //
00228 
00229 void CSimpleCubePixmap::AppCycle( TInt aFrame )
00230     {
00231 
00232     glClear( GL_COLOR_BUFFER_BIT );
00233 
00234     // Animate and draw box 
00235     glLoadIdentity();
00236     glTranslatex( 0 , 0 , -cameraDistance << 16 );
00237     glRotatex( aFrame << 16, 1 << 16,    0   ,    0    );
00238     glRotatex( aFrame << 15,    0   , 1 << 16,    0    );
00239     glRotatex( aFrame << 14,    0   ,    0   , 1 << 16 );
00240     DrawBox( 15.f, 15.f, 15.f );
00241     }
00242 
00243 
00244 //----------------------------------------------------------
00245 // The following methods are called by the CSimpleCubePixmapAppUi 
00246 // class when handling the incoming menu events.
00247 //----------------------------------------------------------
00248 
00249 // -----------------------------------------------------------------------------
00250 // CSimpleCube::FlatShading
00251 // 
00252 // Sets the GL shading model to flat.
00253 // -----------------------------------------------------------------------------
00254 //
00255 
00256 void CSimpleCubePixmap::FlatShading( void )
00257     {
00258     glShadeModel( GL_FLAT );
00259     }
00260 
00261 
00262 // -----------------------------------------------------------------------------
00263 // CSimpleCube::SmoothShading
00264 // 
00265 // Sets the GL shading model to smooth.
00266 // -----------------------------------------------------------------------------
00267 //
00268 
00269 void CSimpleCubePixmap::SmoothShading( void )
00270     {
00271     glShadeModel( GL_SMOOTH );
00272     }
00273 
00274 
00275 // -----------------------------------------------------------------------------
00276 // CSimpleCubePixmap::SetScreenSize
00277 // Reacts to the dynamic screen size change during execution of this program.
00278 // -----------------------------------------------------------------------------
00279 //
00280 void CSimpleCubePixmap::SetScreenSize( TUint aWidth, TUint aHeight )
00281     {
00282     iScreenWidth  = aWidth;
00283     iScreenHeight = aHeight;
00284 
00285     // Set the screen background color. 
00286     glClearColor( 0.f, 0.f, 0.f, 1.f );
00287 
00288     // Reinitialize OpenGL ES rendering. Needed because context has been changed.
00289 
00290     // Enable back face culling. 
00291     glEnable( GL_CULL_FACE  );
00292 
00293     // Initialize viewport and projection. 
00294     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00295     glMatrixMode( GL_PROJECTION );
00296 
00297     GLfloat ar = (GLfloat)(iScreenHeight) / (GLfloat)(iScreenWidth);
00298     glFrustumf( -1.f, 1.f, -1.f*ar, 1.f*ar, 3.f, 1000.f );
00299 
00300 
00301     glFrustumf( -1.f, 1.f, -1.f, 1.f, 3.f, 1000.f );
00302     glMatrixMode( GL_MODELVIEW );
00303 
00304     // Enable vertex arrays. 
00305     glEnableClientState( GL_VERTEX_ARRAY );
00306 
00307     // Set array pointers. 
00308     glVertexPointer( 3, GL_BYTE, 0, vertices );
00309 
00310     // Enable color arrays.
00311     glEnableClientState( GL_COLOR_ARRAY );
00312 
00313     // Set color pointers. 
00314     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
00315 
00316     // Set the initial shading mode 
00317     glShadeModel( GL_FLAT ); 
00318 
00319     // Do not use perspective correction 
00320     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00321 
00322     // Recalculate the view frustrum
00323     glMatrixMode( GL_PROJECTION );
00324     glLoadIdentity();
00325     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00326     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00327                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00328                 FRUSTUM_NEAR, FRUSTUM_FAR );
00329     glMatrixMode( GL_MODELVIEW );
00330     }
00331     
00332 // End of File

© Nokia 2006

Back to top