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