|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : SimpleCubePbuffer.cpp 00004 * Part of : OpenGLEx / SimpleCubePbuffer 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 "SimpleCubePbuffer.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 // CSimpleCubePbuffer::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 CSimpleCubePbuffer* CSimpleCubePbuffer::NewL( TUint aWidth, TUint aHeight ) 00101 { 00102 /* Symbian 2-phase constructor. Calls both the default 00103 C++ constructor and Symbian ConstructL methods */ 00104 CSimpleCubePbuffer* self = new (ELeave) CSimpleCubePbuffer( aWidth, aHeight ); 00105 CleanupStack::PushL( self ); 00106 self->ConstructL(); 00107 CleanupStack::Pop(); 00108 00109 return self; 00110 } 00111 00112 00113 // ----------------------------------------------------------------------------- 00114 // CSimpleCubePbuffer::CSimpleCubePbuffer 00115 // 00116 // C++ constructor. Initializes the size instance variables from arguments. 00117 // ----------------------------------------------------------------------------- 00118 // 00119 00120 CSimpleCubePbuffer::CSimpleCubePbuffer( TUint aWidth, TUint aHeight ) : 00121 iScreenWidth( aWidth ), 00122 iScreenHeight( aHeight ) 00123 { 00124 } 00125 00126 // Destructor. 00127 CSimpleCubePbuffer::~CSimpleCubePbuffer() 00128 { 00129 } 00130 00131 00132 // ----------------------------------------------------------------------------- 00133 // CSimpleCubePbuffer::ConstructL 00134 // 00135 // 2nd phase constructor. 00136 // ----------------------------------------------------------------------------- 00137 // 00138 00139 void CSimpleCubePbuffer::ConstructL( void ) 00140 { 00141 } 00142 00143 00144 // ----------------------------------------------------------------------------- 00145 // CSimpleCubePbuffer::AppInit 00146 // 00147 // Initializes OpenGL ES, sets the vertex and color arrays and pointers, 00148 // and selects the shading mode. 00149 // ----------------------------------------------------------------------------- 00150 // 00151 void CSimpleCubePbuffer::AppInit( void ) 00152 { 00153 // Set the screen background color. 00154 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00155 00156 // Enable back face culling. 00157 glEnable( GL_CULL_FACE ); 00158 00159 // Initialize viewport and projection. 00160 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00161 glMatrixMode( GL_PROJECTION ); 00162 00163 // Calculate the view frustrum 00164 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00165 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00166 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00167 FRUSTUM_NEAR, FRUSTUM_FAR ); 00168 00169 glMatrixMode( GL_MODELVIEW ); 00170 00171 // Enable vertex arrays. 00172 glEnableClientState( GL_VERTEX_ARRAY ); 00173 00174 // Set array pointers. 00175 glVertexPointer( 3, GL_BYTE, 0, vertices ); 00176 00177 // Enable color arrays. 00178 glEnableClientState( GL_COLOR_ARRAY ); 00179 00180 // Set color pointers. 00181 glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors ); 00182 00183 // Set the initial shading mode 00184 glShadeModel( GL_FLAT ); 00185 00186 // Do not use perspective correction 00187 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00188 00189 00190 } 00191 00192 00193 // ----------------------------------------------------------------------------- 00194 // CSimpleCubePbuffer::AppExit 00195 // 00196 // Release any allocations made in AppInit. 00197 // ----------------------------------------------------------------------------- 00198 // 00199 void CSimpleCubePbuffer::AppExit( void ) 00200 { 00201 } 00202 00203 00204 // ----------------------------------------------------------------------------- 00205 // CSimpleCubePbuffer::DrawBox 00206 // 00207 // Draws a box with triangles. 00208 // Scales the box to the given size using glScalef. 00209 // ----------------------------------------------------------------------------- 00210 // 00211 00212 void CSimpleCubePbuffer::DrawBox( GLfloat aSizeX, GLfloat aSizeY, GLfloat aSizeZ ) 00213 { 00214 glScalef( aSizeX, aSizeY, aSizeZ ); 00215 00216 glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles ); 00217 } 00218 00219 00220 // ----------------------------------------------------------------------------- 00221 // CSimpleCubePbuffer::AppCycle 00222 // 00223 // Draws and animates the objects. 00224 // The frame number determines the amount of rotation. 00225 // ----------------------------------------------------------------------------- 00226 // 00227 00228 void CSimpleCubePbuffer::AppCycle( TInt aFrame ) 00229 { 00230 00231 glClear( GL_COLOR_BUFFER_BIT ); 00232 00233 // Animate and draw box 00234 glLoadIdentity(); 00235 glTranslatex( 0 , 0 , -cameraDistance << 16 ); 00236 glRotatex( aFrame << 16, 1 << 16, 0 , 0 ); 00237 glRotatex( aFrame << 15, 0 , 1 << 16, 0 ); 00238 glRotatex( aFrame << 14, 0 , 0 , 1 << 16 ); 00239 DrawBox( 15.f, 15.f, 15.f ); 00240 } 00241 00242 00243 //---------------------------------------------------------- 00244 // The following methods are called by the CSimpleCubePbufferAppUi 00245 // class when handling the incoming menu events. 00246 //---------------------------------------------------------- 00247 00248 // ----------------------------------------------------------------------------- 00249 // CSimpleCube::FlatShading 00250 // 00251 // Sets the GL shading model to flat. 00252 // ----------------------------------------------------------------------------- 00253 // 00254 00255 void CSimpleCubePbuffer::FlatShading( void ) 00256 { 00257 glShadeModel( GL_FLAT ); 00258 } 00259 00260 00261 // ----------------------------------------------------------------------------- 00262 // CSimpleCube::SmoothShading 00263 // 00264 // Sets the GL shading model to smooth. 00265 // ----------------------------------------------------------------------------- 00266 // 00267 00268 void CSimpleCubePbuffer::SmoothShading( void ) 00269 { 00270 glShadeModel( GL_SMOOTH ); 00271 } 00272 00273 00274 // ----------------------------------------------------------------------------- 00275 // CSimpleCubePbuffer::SetScreenSize 00276 // Reacts to the dynamic screen size change during execution of this program. 00277 // ----------------------------------------------------------------------------- 00278 // 00279 void CSimpleCubePbuffer::SetScreenSize( TUint aWidth, TUint aHeight ) 00280 { 00281 iScreenWidth = aWidth; 00282 iScreenHeight = aHeight; 00283 00284 // Reinitialize OpenGL ES 00285 // Set the screen background color. 00286 glClearColor( 0.f, 0.f, 0.f, 1.f ); 00287 00288 // Enable back face culling. 00289 glEnable( GL_CULL_FACE ); 00290 00291 // Initialize viewport and projection. 00292 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00293 glMatrixMode( GL_PROJECTION ); 00294 glFrustumf( -1.f, 1.f, -1.f, 1.f, 3.f, 1000.f ); 00295 glMatrixMode( GL_MODELVIEW ); 00296 00297 // Enable vertex arrays. 00298 glEnableClientState( GL_VERTEX_ARRAY ); 00299 00300 // Set array pointers. 00301 glVertexPointer( 3, GL_BYTE, 0, vertices ); 00302 00303 // Enable color arrays. 00304 glEnableClientState( GL_COLOR_ARRAY ); 00305 00306 // Set color pointers. 00307 glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors ); 00308 00309 // Set the initial shading mode 00310 glShadeModel( GL_FLAT ); 00311 00312 // Do not use perspective correction 00313 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00314 00315 // Recalculate the view frustrum 00316 glMatrixMode( GL_PROJECTION ); 00317 glLoadIdentity(); 00318 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00319 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00320 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00321 FRUSTUM_NEAR, FRUSTUM_FAR ); 00322 glMatrixMode( GL_MODELVIEW ); 00323 } 00324 00325 // End of File
|
© Nokia 2006 |