Nokia N97 SDK
Example Applications Guide

CSimpleLight Class Reference

#include <SimpleLight.h>

Inheritance diagram for CSimpleLight:

List of all members.

Detailed Description

Class that does the actual OpenGL ES rendering.

Definition at line 36 of file SimpleLight.h.


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


Constructor & Destructor Documentation

CSimpleLight::~CSimpleLight (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 130 of file SimpleLight.cpp.

00131     {
00132     }

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 96 of file SimpleLight.cpp.

Referenced by NewL().

00097 :iScreenWidth(aWidth), iScreenHeight(aHeight)
00098     {
00099     }


Member Function Documentation

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

Factory method for creating a new CSimpleCube object.

Definition at line 116 of file SimpleLight.cpp.

References CSimpleLight().

Referenced by CSimpleLightContainer::ConstructL().

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     }

void CSimpleLight::AppInit (  void   ) 
 

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

Definition at line 142 of file SimpleLight.cpp.

References iLampEnabled, iLightingEnabled, iObjectModel, iScreenHeight, iScreenWidth, iSpotEnabled, and SetScreenSize().

Referenced by CSimpleLightContainer::ConstructL().

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     }

void CSimpleLight::AppExit (  void   ) 
 

Called upon application exit. Does nothing.

Definition at line 217 of file SimpleLight.cpp.

Referenced by CSimpleLightContainer::~CSimpleLightContainer().

00218     {
00219     }

void CSimpleLight::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame  Number of the frame to be rendered.

Definition at line 227 of file SimpleLight.cpp.

References iObjectModel, and sin().

Referenced by CSimpleLightContainer::DrawCallBack().

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     }

void CSimpleLight::ToggleLighting (  void   ) 
 

Toggles lighting enabled/disabled.

Definition at line 282 of file SimpleLight.cpp.

References iLightingEnabled.

Referenced by CSimpleLightAppUi::HandleCommandL().

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     }

void CSimpleLight::ToggleLamp (  void   ) 
 

Toggles lamp enabled/disabled.

Definition at line 302 of file SimpleLight.cpp.

References iLampEnabled.

Referenced by CSimpleLightAppUi::HandleCommandL().

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     }

void CSimpleLight::ToggleSpot (  void   ) 
 

Toggles spot light enabled/disabled.

Definition at line 322 of file SimpleLight.cpp.

References iSpotEnabled.

Referenced by CSimpleLightAppUi::HandleCommandL().

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     }

void CSimpleLight::DuckModel (  void   ) 
 

Changes the current model to duck object.

Definition at line 342 of file SimpleLight.cpp.

References iObjectModel.

Referenced by CSimpleLightAppUi::HandleCommandL().

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     }

void CSimpleLight::HeadModel (  void   ) 
 

Changes the current model to head object.

Definition at line 363 of file SimpleLight.cpp.

References iObjectModel.

Referenced by CSimpleLightAppUi::HandleCommandL().

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     }

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 385 of file SimpleLight.cpp.

References iScreenHeight, and iScreenWidth.

Referenced by AppInit(), and CSimpleLightContainer::SizeChanged().

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     }

void CSimpleLight::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 106 of file SimpleLight.cpp.

00107     {
00108     }

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 76 of file SimpleLight.cpp.

Referenced by AppCycle().

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     }


Member Data Documentation

TBool CSimpleLight::iLightingEnabled
 

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

Definition at line 129 of file SimpleLight.h.

Referenced by AppInit(), CSimpleLightAppUi::DynInitMenuPaneL(), and ToggleLighting().

TBool CSimpleLight::iLampEnabled
 

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

Definition at line 132 of file SimpleLight.h.

Referenced by AppInit(), CSimpleLightAppUi::DynInitMenuPaneL(), and ToggleLamp().

TBool CSimpleLight::iSpotEnabled
 

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

Definition at line 135 of file SimpleLight.h.

Referenced by AppInit(), CSimpleLightAppUi::DynInitMenuPaneL(), and ToggleSpot().

TUint CSimpleLight::iScreenWidth [private]
 

Width of the screen

Definition at line 140 of file SimpleLight.h.

Referenced by AppInit(), and SetScreenSize().

TUint CSimpleLight::iScreenHeight [private]
 

Height of the screen

Definition at line 143 of file SimpleLight.h.

Referenced by AppInit(), and SetScreenSize().

TUsedModel CSimpleLight::iObjectModel [private]
 

Enumerated value for switching between the HEAD and DUCK model.

Definition at line 146 of file SimpleLight.h.

Referenced by AppCycle(), AppInit(), DuckModel(), and HeadModel().


The documentation for this class was generated from the following files:

© Nokia 2009

Back to top