Nokia N97 SDK
Example Applications Guide

SimpleLight.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : SimpleLight.cpp
00004  *  Part of     : OpenGLEx / SimpleLight
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 // INCLUDES
00014 #include <e32std.h>
00015 #include <e32math.h>
00016 
00017 #include "SimpleLight.h"
00018 
00019 #include "duck.h"        // Geometry, normals, and indices for the duck model
00020 #include "head.h"        // Geometry, normals, and indices for the head model
00021 
00022 // MACROS
00023 #define MATERIAL_MAX 1
00024 #define LIGHT_MAX    1
00025 
00026 #define cameraDistance 270
00027 
00028 
00029 #define MATERIALCOLOR(r, g, b, a)     \
00030        (GLfloat)(r * MATERIAL_MAX),   \
00031        (GLfloat)(g * MATERIAL_MAX),   \
00032        (GLfloat)(b * MATERIAL_MAX),   \
00033        (GLfloat)(a * MATERIAL_MAX)
00034 
00035 #define LIGHTCOLOR(r, g, b, a)        \
00036        (GLfloat)(r * LIGHT_MAX),      \
00037        (GLfloat)(g * LIGHT_MAX),      \
00038        (GLfloat)(b * LIGHT_MAX),      \
00039        (GLfloat)(a * LIGHT_MAX)
00040 
00041 // CONSTANTS
00042 /** Materials for the duck object. */
00043 static const GLfloat objDiffuseDuck[4]  = { MATERIALCOLOR(0.8, 0.8, 0.2, 1.0) };
00044 static const GLfloat objAmbientDuck[4]  = { MATERIALCOLOR(0.8, 0.8, 0.2, 1.0) };
00045 static const GLfloat objSpecularDuck[4] = { MATERIALCOLOR(1.0, 1.0, 1.0, 1.0) };
00046 static const GLfloat objEmissionDuck[4] = { MATERIALCOLOR(0.0, 0.0, 0.0, 1.0) };
00047 
00048 /* Materials for the head object. */
00049 static const GLfloat objDiffuseHead[4]  = { MATERIALCOLOR(0.8, 0.4, 0.0, 1.0) };
00050 static const GLfloat objAmbientHead[4]  = { MATERIALCOLOR(0.8, 0.4, 0.0, 1.0) };
00051 static const GLfloat objSpecularHead[4] = { MATERIALCOLOR(1.0, 1.0, 1.0, 1.0) };
00052 
00053 /* Global ambient light. */
00054 static const GLfloat globalAmbient[4]   = { LIGHTCOLOR(0.0, 0.0, 0.0, 1.0) };
00055 
00056 /* Lamp parameters. */
00057 static const GLfloat lightDiffuseLamp[4]   = { LIGHTCOLOR(0.7, 0.7, 0.7, 1.0) };
00058 static const GLfloat lightAmbientLamp[4]   = { LIGHTCOLOR(0.3, 0.3, 0.3, 1.0) };
00059 static const GLfloat lightPositionLamp[4]  = { 0, 10, 10, 0 };
00060 
00061 /* Spotlight parameters. */
00062 static const GLfloat lightDiffuseSpot[4]   = { LIGHTCOLOR(0.0, 0.0, 0.0, 1.0) };
00063 static const GLfloat lightAmbientSpot[4]   = { LIGHTCOLOR(0.0, 0.0, 0.0, 1.0) };
00064 static const GLfloat lightSpecularSpot[4]  = { LIGHTCOLOR(0.0, 0.0, 3.0, 1.0) };
00065 static const GLfloat lightPositionSpot[4]  = {  0, -10, -1, 0 };
00066 static const GLfloat lightDirectionSpot[4] = {  0,  10,  1, 1 };
00067 
00068 
00069 // ============================= LOCAL FUNCTIONS ===============================
00070 
00071 // -----------------------------------------------------------------------------
00072 // sin
00073 // Help function to make the duck 'quak'.
00074 // -----------------------------------------------------------------------------
00075 //
00076 GLfloat CSimpleLight::sin(GLfloat aRad)
00077     {
00078     TReal trg, src = (TReal)aRad;
00079     if ( Math::Sin(trg, src) == KErrNone )
00080         {
00081         return (GLfloat)trg;
00082         }
00083     return 0;
00084 
00085     }
00086 
00087 
00088 // ============================ MEMBER FUNCTIONS ===============================
00089 
00090 // -----------------------------------------------------------------------------
00091 // CSimpleLight::CSimpleLight
00092 // C++ default constructor can NOT contain any code, that
00093 // might leave.
00094 // -----------------------------------------------------------------------------
00095 //
00096 CSimpleLight::CSimpleLight( TUint aWidth, TUint aHeight )
00097 :iScreenWidth(aWidth), iScreenHeight(aHeight)
00098     {
00099     }
00100 
00101 // -----------------------------------------------------------------------------
00102 // CSimpleLight::ConstructL
00103 // Symbian 2nd phase constructor can leave.
00104 // -----------------------------------------------------------------------------
00105 //
00106 void CSimpleLight::ConstructL( void )
00107     {
00108     }
00109 
00110 
00111 // -----------------------------------------------------------------------------
00112 // CSimpleLight::NewL
00113 // Two-phased constructor.
00114 // -----------------------------------------------------------------------------
00115 //
00116 CSimpleLight* CSimpleLight::NewL( TUint aWidth, TUint aHeight )
00117     {
00118     /* Symbian 2-phase constructor. Calls both the default
00119        C++ constructor and Symbian ConstructL methods */
00120     CSimpleLight* self = new (ELeave) CSimpleLight( aWidth, aHeight );
00121     CleanupStack::PushL( self );
00122     self->ConstructL();
00123     CleanupStack::Pop();
00124 
00125     return self;
00126     }
00127 
00128 
00129 // Destructor.
00130 CSimpleLight::~CSimpleLight()
00131     {
00132     }
00133 
00134 
00135 
00136 // -----------------------------------------------------------------------------
00137 // CSimpleLight::AppInit
00138 // Initialize OpenGL ES, set the vertex and color arrays and pointers,
00139 // and select the shading mode.
00140 // -----------------------------------------------------------------------------
00141 //
00142 void CSimpleLight::AppInit( void )
00143     {
00144     // Initialize viewport and projection.
00145                 SetScreenSize( iScreenWidth, iScreenHeight );
00146 
00147       // Set the screen background color.
00148     glClearColor( 0.f, 0.f, 0.f, 1.f );
00149 
00150     // Enable depth testing, back face culling, and lighting.
00151     glEnable( GL_DEPTH_TEST );
00152     glEnable( GL_CULL_FACE  );
00153     glEnable( GL_LIGHTING   );
00154     /* Make OpenGL ES automatically normalize all normals after tranformations.
00155        This is important when making irregular xforms like scaling, or if we
00156        have specified nonunit-length normals. */
00157     glEnable( GL_NORMALIZE  );
00158 
00159     // Enable vertex and normal arrays.
00160     glEnableClientState( GL_VERTEX_ARRAY );
00161     glEnableClientState( GL_NORMAL_ARRAY );
00162 
00163     // Set array pointers. Initially for the duck object.
00164     glVertexPointer( 3, GL_SHORT, 0, objVertexdataDuck );
00165     glNormalPointer( GL_BYTE, 0, objNormaldataDuck     );
00166 
00167     // Set up global ambient light.
00168     glLightModelfv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );
00169 
00170     // Set up lamp.
00171     glEnable( GL_LIGHT0 );
00172     glLightfv(  GL_LIGHT0, GL_DIFFUSE,  lightDiffuseLamp  );
00173     glLightfv(  GL_LIGHT0, GL_AMBIENT,  lightAmbientLamp  );
00174     glLightfv(  GL_LIGHT0, GL_SPECULAR, lightDiffuseLamp  );
00175     glLightfv(  GL_LIGHT0, GL_POSITION, lightPositionLamp );
00176 
00177     // Set up spot.  Initially spot is disabled.
00178     glLightfv(  GL_LIGHT1, GL_DIFFUSE,  lightDiffuseSpot  );
00179     glLightfv(  GL_LIGHT1, GL_AMBIENT,  lightAmbientSpot  );
00180     glLightfv(  GL_LIGHT1, GL_SPECULAR, lightSpecularSpot );
00181     glLightfv(  GL_LIGHT1, GL_POSITION, lightPositionSpot );
00182 
00183     glLightf(   GL_LIGHT1, GL_CONSTANT_ATTENUATION,  1.5  );
00184     glLightf(   GL_LIGHT1, GL_LINEAR_ATTENUATION,    0.5  );
00185     glLightf(   GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.5  );
00186 
00187     glLightf(   GL_LIGHT1, GL_SPOT_CUTOFF,   17.0                );
00188     glLightf(   GL_LIGHT1, GL_SPOT_EXPONENT,  2.0                );
00189     glLightfv(  GL_LIGHT1, GL_SPOT_DIRECTION, lightDirectionSpot );
00190 
00191     // Set duck material
00192     glMaterialfv(   GL_FRONT_AND_BACK, GL_DIFFUSE,  objDiffuseDuck      );
00193     glMaterialfv(   GL_FRONT_AND_BACK, GL_AMBIENT,  objAmbientDuck      );
00194     glMaterialfv(   GL_FRONT_AND_BACK, GL_SPECULAR, objSpecularDuck     );
00195     glMaterialfv(   GL_FRONT_AND_BACK, GL_EMISSION, objEmissionDuck     );
00196     glMaterialx( GL_FRONT_AND_BACK, GL_SHININESS,   10 << 16         );
00197 
00198     // Set shading mode
00199     glShadeModel( GL_SMOOTH );
00200 
00201     // Do not use perspective correction
00202     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00203 
00204     // Initialize menu state variables, Symbian menu stuff.
00205     iLightingEnabled = ETrue;          // Lighting is enabled
00206     iLampEnabled     = ETrue;          // Lamp is enabled
00207     iSpotEnabled     = EFalse;         // Spot is disabled
00208     iObjectModel     = EDuckModel;     // Default model is duck
00209     }
00210 
00211 
00212 // -----------------------------------------------------------------------------
00213 // CSimpleLight::AppExit
00214 // Release any allocations made in AppInit.
00215 // -----------------------------------------------------------------------------
00216 //
00217 void CSimpleLight::AppExit( void )
00218     {
00219     }
00220 
00221 
00222 // -----------------------------------------------------------------------------
00223 // CSimpleLight::AppCycle
00224 // Draws and animates the objects
00225 // -----------------------------------------------------------------------------
00226 //
00227 void CSimpleLight::AppCycle( TInt iFrame )
00228     {
00229     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00230 
00231     glLoadIdentity();
00232 
00233     switch( iObjectModel )
00234         {
00235         case EDuckModel:
00236             {
00237             glTranslatex( 0 , -20 << 16 , -cameraDistance << 16 );
00238 
00239             glRotatex((-90 << 16) + (GLint) (sin(iFrame * 0.17f) * 15 * 65536),
00240                       1 << 16, 0, 0);
00241             glRotatex((GLint) (sin(iFrame * 0.13f) * 10 * 65536), 0, 1 << 16, 0);
00242             glRotatex(iFrame << 16, 0, 0, 1 << 16);
00243 
00244             // Use some magic numbers to scale the duck model to fit the screen.
00245             glScalex( 3 << 6, 3 << 6, 3 << 6 );
00246             glDrawElements( GL_TRIANGLES, objFaces * 3,
00247                             GL_UNSIGNED_BYTE, objFacedataDuck );
00248             }
00249             break;
00250         case EHeadModel:
00251             {
00252             glTranslatex( 0, 0, -cameraDistance << 16 );
00253 
00254             glRotatex(-90 << 16, 1 << 16, 0, 0);
00255             glRotatex( iFrame << 16, 5 << 16, 1 << 12, 0);
00256             glRotatex( iFrame << 16, 0, 0, 1 << 16);
00257 
00258             // Use some magic numbers to scale the head model to fit the screen.
00259             glScalex( 1 << 10, 1 << 10, 1 << 10 );
00260             glDrawElements( GL_TRIANGLES, objFacesHead * 3,
00261                             GL_UNSIGNED_SHORT, objFacedataHead );
00262             }
00263             break;
00264         default:
00265             // Do nothing.
00266             break;
00267         }
00268     }
00269 
00270 
00271 //----------------------------------------------------------
00272 // The following methods are called by the CSimpleLightAppUi
00273 // class when handling the incoming menu events.
00274 //----------------------------------------------------------
00275 
00276 
00277 // -----------------------------------------------------------------------------
00278 // CSimpleLight::ToggleLighting
00279 // Enable/Disable lighting from the application menu.
00280 // -----------------------------------------------------------------------------
00281 //
00282 void CSimpleLight::ToggleLighting( void )
00283     {
00284     if ( iLightingEnabled)
00285         {
00286         iLightingEnabled = EFalse;
00287         glDisable( GL_LIGHTING );
00288         }
00289     else
00290         {
00291         iLightingEnabled = ETrue;
00292         glEnable( GL_LIGHTING );
00293         }
00294     }
00295 
00296 
00297 // -----------------------------------------------------------------------------
00298 // CSimpleLight::ToggleLamp
00299 // Enable/Disable lamp from the application menu.
00300 // -----------------------------------------------------------------------------
00301 //
00302 void CSimpleLight::ToggleLamp( void )
00303     {
00304     if ( iLampEnabled )
00305         {
00306         iLampEnabled = EFalse;
00307         glDisable( GL_LIGHT0 );
00308         }
00309     else
00310         {
00311         iLampEnabled = ETrue;
00312         glEnable( GL_LIGHT0 );
00313         }
00314     }
00315 
00316 
00317 // -----------------------------------------------------------------------------
00318 // CSimpleLight::ToggleSpot
00319 // Enable/Disable spot from the application menu.
00320 // -----------------------------------------------------------------------------
00321 //
00322 void CSimpleLight::ToggleSpot( void )
00323     {
00324     if ( iSpotEnabled )
00325         {
00326         iSpotEnabled = EFalse;
00327         glDisable( GL_LIGHT1 );
00328         }
00329     else
00330         {
00331         iSpotEnabled = ETrue;
00332         glEnable( GL_LIGHT1 );
00333         }
00334     }
00335 
00336 
00337 // -----------------------------------------------------------------------------
00338 // CSimpleLight::DuckModel
00339 // Change the current model to duck object.
00340 // -----------------------------------------------------------------------------
00341 //
00342 void CSimpleLight::DuckModel( void )
00343     {
00344     iObjectModel = EDuckModel;
00345 
00346     // Set array pointers for duck model.
00347     glVertexPointer( 3, GL_SHORT, 0, objVertexdataDuck );
00348     glNormalPointer( GL_BYTE, 0, objNormaldataDuck );
00349 
00350     // Set duck material
00351     glMaterialfv(   GL_FRONT_AND_BACK, GL_AMBIENT,  objAmbientDuck  );
00352     glMaterialfv(   GL_FRONT_AND_BACK, GL_DIFFUSE,  objDiffuseDuck  );
00353     glMaterialfv(   GL_FRONT_AND_BACK, GL_SPECULAR, objSpecularDuck );
00354     glMaterialx( GL_FRONT_AND_BACK, GL_SHININESS,   10 << 16     );
00355     }
00356 
00357 
00358 // -----------------------------------------------------------------------------
00359 // CSimpleLight::HeadModel
00360 // Change the current model to head object.
00361 // -----------------------------------------------------------------------------
00362 //
00363 void CSimpleLight::HeadModel( void )
00364     {
00365     iObjectModel = EHeadModel;
00366 
00367     // Set array pointers for head model.
00368     glVertexPointer( 3, GL_SHORT, 0, objVertexdataHead );
00369     glNormalPointer( GL_BYTE, 0, objNormaldataHead );
00370 
00371     // Set head front material
00372     glMaterialfv(   GL_FRONT_AND_BACK, GL_AMBIENT,  objAmbientHead  );
00373     glMaterialfv(   GL_FRONT_AND_BACK, GL_DIFFUSE,  objDiffuseHead  );
00374     glMaterialfv(   GL_FRONT_AND_BACK, GL_SPECULAR, objSpecularHead );
00375     glMaterialx( GL_FRONT_AND_BACK, GL_SHININESS,   12 << 16     );
00376 
00377     }
00378 
00379 
00380 // -----------------------------------------------------------------------------
00381 // CSimpleLight::SetScreenSize
00382 // Reacts to the dynamic screen size change during execution of this program.
00383 // -----------------------------------------------------------------------------
00384 //
00385 void CSimpleLight::SetScreenSize( TUint aWidth, TUint aHeight )
00386     {
00387     iScreenWidth  = aWidth;
00388     iScreenHeight = aHeight;
00389 
00390     // Reinitialize viewport and projection.
00391     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00392 
00393     // Recalculate the view frustrum
00394     glMatrixMode( GL_PROJECTION );
00395     glLoadIdentity();
00396     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00397     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00398                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00399                 FRUSTUM_NEAR, FRUSTUM_FAR );
00400     glMatrixMode( GL_MODELVIEW );
00401     }
00402 
00403 //  End of File

© Nokia 2009

Back to top