Nokia N97 SDK
Example Applications Guide

SimpleCubePbuffer.cpp

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

© Nokia 2009

Back to top