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