S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CSimpleLight Class Reference

#include <SimpleLight.h>

List of all members.

Public Member Functions

virtual ~CSimpleLight ()
void AppInit (void)
void AppExit (void)
void AppCycle (TInt aFrame)
void ToggleLighting (void)
void ToggleLamp (void)
void ToggleSpot (void)
void DuckModel (void)
void HeadModel (void)
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CSimpleLight * NewL (TUint aWidth, TUint aHeight)

Public Attributes

TBool iLightingEnabled
TBool iLampEnabled
TBool iSpotEnabled

Protected Member Functions

 CSimpleLight (TUint aWidth, TUint aHeight)
void ConstructL (void)
GLfloat sin (GLfloat aRad)

Private Attributes

TUint iScreenWidth
TUint iScreenHeight
TUsedModel iObjectModel


Detailed Description

Class that does the actual OpenGL ES rendering.

Definition at line 39 of file SimpleLight.h.


Constructor & Destructor Documentation

CSimpleLight::~CSimpleLight (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 133 of file SimpleLight.cpp.

00134     {
00135     }

CSimpleLight::CSimpleLight (  TUint  aWidth,
TUint  aHeight
)  [protected]
 

Standard constructor that must never Leave. Stores the given screen width and height.

Parameters:
aWidth Width of the screen.
aHeight Height of the screen.

Definition at line 99 of file SimpleLight.cpp.

00100 :iScreenWidth(aWidth), iScreenHeight(aHeight)
00101     {
00102     }


Member Function Documentation

CSimpleLight * CSimpleLight::NewL (  TUint  aWidth,
TUint  aHeight
)  [static]
 

Factory method for creating a new CSimpleCube object.

Definition at line 119 of file SimpleLight.cpp.

References CSimpleLight().

00120     {
00121     /* Symbian 2-phase constructor. Calls both the default 
00122        C++ constructor and Symbian ConstructL methods */
00123     CSimpleLight* self = new (ELeave) CSimpleLight( aWidth, aHeight );
00124     CleanupStack::PushL( self );
00125     self->ConstructL();
00126     CleanupStack::Pop();
00127 
00128     return self;
00129     }

void CSimpleLight::AppInit (  void   ) 
 

Initializes OpenGL ES, sets the vertex and color arrays and pointers. Also selects the shading mode.

Definition at line 145 of file SimpleLight.cpp.

References iLampEnabled, iLightingEnabled, iObjectModel, iScreenHeight, iScreenWidth, and iSpotEnabled.

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     // 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 and normal arrays. 
00172     glEnableClientState( GL_VERTEX_ARRAY );
00173     glEnableClientState( GL_NORMAL_ARRAY );
00174     
00175     // Set array pointers. Initially for the duck object. 
00176     glVertexPointer( 3, GL_SHORT, 0, objVertexdataDuck );
00177     glNormalPointer( GL_BYTE, 0, objNormaldataDuck     );
00178 
00179     // Set up global ambient light. 
00180     glLightModelfv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );
00181 
00182     // Set up lamp. 
00183     glEnable( GL_LIGHT0 );
00184     glLightfv(  GL_LIGHT0, GL_DIFFUSE,  lightDiffuseLamp  );
00185     glLightfv(  GL_LIGHT0, GL_AMBIENT,  lightAmbientLamp  );
00186     glLightfv(  GL_LIGHT0, GL_SPECULAR, lightDiffuseLamp  );
00187     glLightfv(  GL_LIGHT0, GL_POSITION, lightPositionLamp );
00188 
00189     // Set up spot.  Initially spot is disabled. 
00190     glLightfv(  GL_LIGHT1, GL_DIFFUSE,  lightDiffuseSpot  );
00191     glLightfv(  GL_LIGHT1, GL_AMBIENT,  lightAmbientSpot  );
00192     glLightfv(  GL_LIGHT1, GL_SPECULAR, lightSpecularSpot );
00193     glLightfv(  GL_LIGHT1, GL_POSITION, lightPositionSpot );
00194 
00195     glLightf(   GL_LIGHT1, GL_CONSTANT_ATTENUATION,  1.5  );
00196     glLightf(   GL_LIGHT1, GL_LINEAR_ATTENUATION,    0.5  );
00197     glLightf(   GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.5  );
00198 
00199     glLightf(   GL_LIGHT1, GL_SPOT_CUTOFF,   17.0                );
00200     glLightf(   GL_LIGHT1, GL_SPOT_EXPONENT,  2.0                );
00201     glLightfv(  GL_LIGHT1, GL_SPOT_DIRECTION, lightDirectionSpot );
00202 
00203     // Set duck material 
00204     glMaterialfv(   GL_FRONT_AND_BACK, GL_DIFFUSE,  objDiffuseDuck      );
00205     glMaterialfv(   GL_FRONT_AND_BACK, GL_AMBIENT,  objAmbientDuck      );
00206     glMaterialfv(   GL_FRONT_AND_BACK, GL_SPECULAR, objSpecularDuck     );
00207     glMaterialfv(   GL_FRONT_AND_BACK, GL_EMISSION, objEmissionDuck     );
00208     glMaterialx( GL_FRONT_AND_BACK, GL_SHININESS,   10 << 16         );
00209 
00210     // Set shading mode
00211     glShadeModel( GL_SMOOTH ); 
00212 
00213     // Do not use perspective correction 
00214     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00215 
00216     // Initialize menu state variables, Symbian menu stuff. 
00217     iLightingEnabled = ETrue;          // Lighting is enabled
00218     iLampEnabled     = ETrue;          // Lamp is enabled
00219     iSpotEnabled     = EFalse;         // Spot is disabled
00220     iObjectModel     = EDuckModel;     // Default model is duck
00221     }

void CSimpleLight::AppExit (  void   ) 
 

Called upon application exit. Does nothing.

Definition at line 229 of file SimpleLight.cpp.

00230     {
00231     }

void CSimpleLight::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 239 of file SimpleLight.cpp.

References iObjectModel, and sin().

00240     {
00241     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00242 
00243     glLoadIdentity();
00244 
00245     switch( iObjectModel )
00246         {
00247         case EDuckModel:
00248             {
00249             glTranslatex( 0 , -20 << 16 , -cameraDistance << 16 );
00250 
00251             glRotatex((-90 << 16) + (GLint) (sin(iFrame * 0.17f) * 15 * 65536), 
00252                       1 << 16, 0, 0);
00253             glRotatex((GLint) (sin(iFrame * 0.13f) * 10 * 65536), 0, 1 << 16, 0);
00254             glRotatex(iFrame << 16, 0, 0, 1 << 16);
00255             
00256             // Use some magic numbers to scale the duck model to fit the screen.
00257             glScalex( 3 << 6, 3 << 6, 3 << 6 );
00258             glDrawElements( GL_TRIANGLES, objFaces * 3, 
00259                             GL_UNSIGNED_BYTE, objFacedataDuck );
00260             }
00261             break;
00262         case EHeadModel:
00263             {
00264             glTranslatex( 0, 0, -cameraDistance << 16 );
00265             
00266             glRotatex(-90 << 16, 1 << 16, 0, 0);
00267             glRotatex( iFrame << 16, 5 << 16, 1 << 12, 0);
00268             glRotatex( iFrame << 16, 0, 0, 1 << 16);
00269             
00270             // Use some magic numbers to scale the head model to fit the screen. 
00271             glScalex( 1 << 10, 1 << 10, 1 << 10 );
00272             glDrawElements( GL_TRIANGLES, objFacesHead * 3, 
00273                             GL_UNSIGNED_SHORT, objFacedataHead );
00274             }
00275             break;
00276         default:
00277             // Do nothing.
00278             break;
00279         }
00280     }

void CSimpleLight::ToggleLighting (  void   ) 
 

Toggles lighting enabled/disabled.

Definition at line 294 of file SimpleLight.cpp.

References iLightingEnabled.

00295     {
00296     if ( iLightingEnabled)
00297         {
00298         iLightingEnabled = EFalse;
00299         glDisable( GL_LIGHTING );
00300         }
00301     else
00302         {
00303         iLightingEnabled = ETrue;
00304         glEnable( GL_LIGHTING );
00305         }
00306     }

void CSimpleLight::ToggleLamp (  void   ) 
 

Toggles lamp enabled/disabled.

Definition at line 314 of file SimpleLight.cpp.

References iLampEnabled.

00315     {
00316     if ( iLampEnabled )
00317         {
00318         iLampEnabled = EFalse;
00319         glDisable( GL_LIGHT0 );
00320         }
00321     else
00322         {
00323         iLampEnabled = ETrue;
00324         glEnable( GL_LIGHT0 );
00325         }
00326     }

void CSimpleLight::ToggleSpot (  void   ) 
 

Toggles spot light enabled/disabled.

Definition at line 334 of file SimpleLight.cpp.

References iSpotEnabled.

00335     {
00336     if ( iSpotEnabled )
00337         {
00338         iSpotEnabled = EFalse;
00339         glDisable( GL_LIGHT1 );
00340         }
00341     else
00342         {
00343         iSpotEnabled = ETrue;
00344         glEnable( GL_LIGHT1 );
00345         }
00346     }

void CSimpleLight::DuckModel (  void   ) 
 

Changes the current model to duck object.

Definition at line 354 of file SimpleLight.cpp.

References iObjectModel.

00355     {
00356     iObjectModel = EDuckModel;
00357 
00358     // Set array pointers for duck model. 
00359     glVertexPointer( 3, GL_SHORT, 0, objVertexdataDuck );
00360     glNormalPointer( GL_BYTE, 0, objNormaldataDuck );
00361     
00362     // Set duck material 
00363     glMaterialfv(   GL_FRONT_AND_BACK, GL_AMBIENT,  objAmbientDuck  );
00364     glMaterialfv(   GL_FRONT_AND_BACK, GL_DIFFUSE,  objDiffuseDuck  );
00365     glMaterialfv(   GL_FRONT_AND_BACK, GL_SPECULAR, objSpecularDuck );
00366     glMaterialx( GL_FRONT_AND_BACK, GL_SHININESS,   10 << 16     );
00367     }

void CSimpleLight::HeadModel (  void   ) 
 

Changes the current model to head object.

Definition at line 375 of file SimpleLight.cpp.

References iObjectModel.

00376     {
00377     iObjectModel = EHeadModel;
00378 
00379     // Set array pointers for head model. 
00380     glVertexPointer( 3, GL_SHORT, 0, objVertexdataHead );
00381     glNormalPointer( GL_BYTE, 0, objNormaldataHead );
00382 
00383     // Set head front material 
00384     glMaterialfv(   GL_FRONT_AND_BACK, GL_AMBIENT,  objAmbientHead  );
00385     glMaterialfv(   GL_FRONT_AND_BACK, GL_DIFFUSE,  objDiffuseHead  );
00386     glMaterialfv(   GL_FRONT_AND_BACK, GL_SPECULAR, objSpecularHead );
00387     glMaterialx( GL_FRONT_AND_BACK, GL_SHININESS,   12 << 16     );
00388 
00389     }

void CSimpleLight::SetScreenSize (  TUint  aWidth,
TUint  aHeight
) 
 

Notifies that the screen size has dynamically changed during execution of this program. Resets the viewport to this new size.

Parameters:
aWidth New width of the screen.
aHeight New height of the screen.

Definition at line 397 of file SimpleLight.cpp.

References iScreenHeight, and iScreenWidth.

00398     {
00399     iScreenWidth  = aWidth;
00400     iScreenHeight = aHeight;
00401 
00402     // Reinitialize viewport and projection. 
00403     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00404 
00405     // Recalculate the view frustrum
00406     glMatrixMode( GL_PROJECTION );
00407     glLoadIdentity();
00408     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00409     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00410                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00411                 FRUSTUM_NEAR, FRUSTUM_FAR );
00412     glMatrixMode( GL_MODELVIEW );
00413     }

void CSimpleLight::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 109 of file SimpleLight.cpp.

00110     {
00111     }

GLfloat CSimpleLight::sin (  GLfloat  aRad  )  [protected]
 

Helper function that is used to make the duck 'quak'. Calculates the sine for the given angle. Returns 0 if any errors occur while calling the Math::Sin() method.

Parameters:
aRad Radian angle whose sine is to be calculated.
Returns:
The sine of the given angle or 0 if error occured while calculating the sine.

Definition at line 79 of file SimpleLight.cpp.

00080     {
00081     TReal trg, src = (TReal)aRad;
00082     if ( Math::Sin(trg, src) == KErrNone )
00083         {
00084         return (GLfloat)trg;
00085         }
00086     return 0;
00087 
00088     }


Member Data Documentation

TBool CSimpleLight::iLightingEnabled
 

Whether or not the lighting is enabled. Used when building the menu for the application.

Definition at line 132 of file SimpleLight.h.

TBool CSimpleLight::iLampEnabled
 

Whether or not the lamp is enabled. Used when building the menu for the application.

Definition at line 135 of file SimpleLight.h.

TBool CSimpleLight::iSpotEnabled
 

Whether or not the spotlight is enabled. Used when building the menu for the application.

Definition at line 138 of file SimpleLight.h.

TUint CSimpleLight::iScreenWidth [private]
 

Width of the screen

Definition at line 143 of file SimpleLight.h.

TUint CSimpleLight::iScreenHeight [private]
 

Height of the screen

Definition at line 146 of file SimpleLight.h.

TUsedModel CSimpleLight::iObjectModel [private]
 

Enumerated value for switching between the HEAD and DUCK model.

Definition at line 149 of file SimpleLight.h.


© Nokia 2006

Back to top