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