Nokia N97 SDK
Example Applications Guide

CShadows Class Reference

#include <Shadows.h>

Inheritance diagram for CShadows:

List of all members.

Detailed Description

Class that does the actual OpenGL ES rendering.

Definition at line 35 of file Shadows.h.


Public Member Functions

virtual  ~CShadows ()
void  AppInit (void)
void  AppExit (void)
void  DrawPlane (void)
void  DrawLamp (void)
void  DrawShadowObject (void)
void  DrawObject (void)
void  CalculateProjectionMatrix (void)
void  AppCycle (TInt aFrame)
void  RotateLightLeft (void)
void  RotateLightRight (void)
void  SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CShadows *  NewL (TUint aWidth, TUint aHeight)

Protected Member Functions

  CShadows (TUint aWidth, TUint aHeight)
void  ConstructL (void)

Private Attributes

TUint  iScreenWidth
TUint  iScreenHeight
GLfloat  iLightRadius
GLfloat  iLightAngle
GLfloat  iLightPosX
GLfloat  iLightPosY
GLfloat  iLightPosZ
GLfloat  iLightPos [4]
GLfloat  iShadowMat [16]
TBool  iIsLightRotatedLeft
TBool  iIsLightRotatedRight


Constructor & Destructor Documentation

CShadows::~CShadows (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 307 of file Shadows.cpp.

00308     {
00309     }

CShadows::CShadows (  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 272 of file Shadows.cpp.

Referenced by NewL().

00273 :iScreenWidth(aWidth), iScreenHeight(aHeight)
00274     {
00275     }


Member Function Documentation

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

Factory method for creating a new CShadows object.

Definition at line 293 of file Shadows.cpp.

References CShadows().

Referenced by CShadowsContainer::ConstructL().

00294     {
00295     /* Symbian 2-phase constructor. Calls both the default
00296        C++ constructor and Symbian ConstructL methods */
00297     CShadows* self = new (ELeave) CShadows( aWidth, aHeight );
00298     CleanupStack::PushL( self );
00299     self->ConstructL();
00300     CleanupStack::Pop();
00301 
00302     return self;
00303     }

void CShadows::AppInit (  void   ) 
 

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

Definition at line 317 of file Shadows.cpp.

References iIsLightRotatedLeft, iIsLightRotatedRight, iLightAngle, iLightPos, iLightPosX, iLightPosY, iLightPosZ, iLightRadius, iScreenHeight, iScreenWidth, and SetScreenSize().

Referenced by CShadowsContainer::ConstructL().

00318     {
00319     // Initialize viewport and projection.
00320                 SetScreenSize( iScreenWidth, iScreenHeight );
00321 
00322     // Set the screen background color.
00323     glClearColor( 0.f, 0.4f, 0.7f, 1.f );
00324 
00325     // Enable auto normalization for object's normals.
00326     glEnable( GL_NORMALIZE  );
00327 
00328     // Enable vertex and  normal arrays.
00329     glEnableClientState( GL_VERTEX_ARRAY );
00330     glEnableClientState( GL_NORMAL_ARRAY );
00331 
00332     // Set shadow object's front and back materials.
00333     glMaterialxv( GL_FRONT_AND_BACK, GL_AMBIENT,  objAmbientFront  );
00334     glMaterialxv( GL_FRONT_AND_BACK, GL_DIFFUSE,  objDiffuseFront  );
00335 
00336     // Set the normal pointer.
00337     glNormalPointer( GL_BYTE, 0, objNormals );
00338 
00339     // Set up global ambient light.
00340     glLightModelxv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );
00341 
00342     // Do not use perspective correction
00343     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00344 
00345     // Set up lamp.
00346     glEnable(   GL_LIGHT0 );
00347     glLightxv( GL_LIGHT0, GL_DIFFUSE,  lightDiffuseLamp  );
00348     glLightxv( GL_LIGHT0, GL_AMBIENT,  lightAmbientLamp  );
00349     glLightxv( GL_LIGHT0, GL_SPECULAR, lightDiffuseLamp  );
00350 
00351     glPointSize( 5.0 ); // For drawing the lamp object
00352 
00353     // Initial position of the lamp
00354     iLightRadius = 10.0;
00355     iLightAngle  = 0.0;
00356     iLightPosX   = iLightRadius * cos(iLightAngle); // 10.0
00357     iLightPosY   = iLightRadius * sin(iLightAngle); // 0.0
00358     iLightPosZ   = 50.0;
00359 
00360     iLightPos[0] = iLightPosX;
00361     iLightPos[1] = iLightPosY;
00362     iLightPos[2] = iLightPosZ;
00363     iLightPos[3] = 0.0;
00364 
00365     iIsLightRotatedLeft  = EFalse;
00366     iIsLightRotatedRight = EFalse;
00367     }

void CShadows::AppExit (  void   ) 
 

Called upon application exit. Does nothing.

Definition at line 375 of file Shadows.cpp.

Referenced by CShadowsContainer::~CShadowsContainer().

00376     {
00377     }

void CShadows::DrawPlane (  void   ) 
 

Draws the ground plane.

Definition at line 385 of file Shadows.cpp.

Referenced by AppCycle().

00386     {
00387     glColor4f( 255.f, 255.f, 255.f, 255.f ); // White
00388     glVertexPointer( 3, GL_BYTE, 0, planeVertices );
00389     glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles );
00390     }

void CShadows::DrawLamp (  void   ) 
 

Draws the lamp.

Definition at line 398 of file Shadows.cpp.

Referenced by AppCycle().

00399     {
00400     /* The lamp is drawn using one vertex from the shadow object
00401        vertex list. The lamp is drawn as a single point. */
00402 
00403     glColor4f( 255.f, 255.f, 0.f, 255.f ); // Yellow
00404     glVertexPointer( 3, GL_BYTE, 0, objVertices );
00405     glDrawElements( GL_POINTS, 1, GL_UNSIGNED_BYTE, objTriangles );
00406     }

void CShadows::DrawShadowObject (  void   ) 
 

Draws the shadows.

Definition at line 414 of file Shadows.cpp.

Referenced by AppCycle().

00415     {
00416     glColor4f( 0.f, 0.f, 0.f, 255.f ); // Black
00417     glVertexPointer( 3, GL_BYTE, 0, objVertices );
00418     glDrawElements( GL_TRIANGLES, 48 * 3, GL_UNSIGNED_BYTE, objTriangles );
00419     }

void CShadows::DrawObject (  void   ) 
 

Draw the object.

Definition at line 427 of file Shadows.cpp.

Referenced by AppCycle().

00428     {
00429     glDrawElements( GL_TRIANGLES, 48 * 3, GL_UNSIGNED_BYTE, objTriangles );
00430     }

void CShadows::CalculateProjectionMatrix (  void   ) 
 

Calculate proper shadow matrix.

Definition at line 438 of file Shadows.cpp.

References iLightPosX, iLightPosY, iLightPosZ, and iShadowMat.

Referenced by AppCycle().

00439     {
00440     /* Shadows are calculated using appropriate projection matrix, which is
00441        defined, e.g., in OpenGL Programming Guide on page 584, or in Jim Blinn's
00442        Corner: A Trip Down the Graphics Pipeline on page 60. The latter also
00443        includes localized light sources as in our case. The general modelview
00444        matrix for creating a planar shadow can be expressed as:
00445 
00446        shadowMat =
00447 
00448        [ dtop-l[0]*g[0],     -l[1]*g[0],     -l[2]*g[0],     -l[3]*g[0],
00449              -l[0]*g[1], dotp-l[1]*g[1],     -l[2]*g[1],     -l[3]*g[1],
00450              -l[0]*g[2],     -l[1]*g[2], dotp-l[2]*g[2],     -l[3]*g[2],
00451          -l[0]*g[3],     -l[1]*g[3],     -l[2]*g[3], dotp-l[3]*g[3] ],
00452 
00453        where l - list(4) - light position
00454              g - list(4) - ground plane equation in form Ax + By + Cz + d = 0
00455          dotp = l[0]*g[0]+l[1]*g[1]+l[2]*g[2]+l[3]*g[3]
00456 
00457        Note that in this example the ground plane is a x-y-plane in origin, i.e.,
00458        the plane equation is g = [0,0,1,0]. Therefore the matrix can be written
00459        using only the light position parameters.
00460 
00461        Note that if you have a local light source, then g[2] = 1. In the case of
00462        directional light g[2] = 0.
00463     */
00464 
00465     GLfloat shadowMat_local[16] = { iLightPosZ,     0.0,        0.0,    0.0, \
00466                                        0.0,      iLightPosZ,    0.0,    0.0, \
00467                                       -iLightPosX, -iLightPosY,    0.0,   -1.0, \
00468                                       0.0,        0.0,         0.0, iLightPosZ };
00469 
00470     for ( int i=0;i<16;i++ )
00471         {
00472         iShadowMat[i] = shadowMat_local[i];
00473         }
00474 
00475     }

void CShadows::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame  Number of the frame to be rendered.

Definition at line 483 of file Shadows.cpp.

References CalculateProjectionMatrix(), DrawLamp(), DrawObject(), DrawPlane(), DrawShadowObject(), iIsLightRotatedLeft, iIsLightRotatedRight, iLightAngle, iLightPos, iLightPosX, iLightPosY, iLightPosZ, iLightRadius, and iShadowMat.

Referenced by CShadowsContainer::DrawCallBack().

00484     {
00485     /* The order of rendering in our case is following:
00486        1. Draw all the ground object, i.e., the plane
00487        2. Draw shadows. If we have multiple ground object, each would have
00488           to be rendered separately with each different light sources.
00489        3. Draw the shadow objects. */
00490 
00491     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00492 
00493     // Update light position
00494     if ( iIsLightRotatedLeft )
00495         {
00496         iLightAngle += (GLfloat)0.1;
00497         }
00498 
00499     if ( iIsLightRotatedRight )
00500         {
00501         iLightAngle -= (GLfloat)0.1;
00502         }
00503 
00504     iLightPosX   = iLightRadius * cos( iLightAngle );
00505     iLightPosY   = iLightRadius * sin( iLightAngle );
00506     iLightPos[0] = iLightPosX;
00507     iLightPos[1] = iLightPosY;
00508 
00509 
00510     // Move camera
00511     glLoadIdentity();
00512     gluLookAt( 0.f, 20.f,  150.0,   // Camera position
00513                0.f, 20.f,    0.f,   // Look at point
00514                0.f,  1.f,    0.f ); // Up direction
00515     glRotatef( -75.f, 1.f, 0.f, 0.f );
00516 
00517     glDisable( GL_LIGHTING ); // Plane, lamp, and shadows don't need lighting
00518     glShadeModel( GL_FLAT );  // Plane, lamp, and shadows are single colored
00519 
00520     // Draw plane
00521     glPushMatrix();
00522     glScalef( 80.0, 60.0, 1.0 );
00523     DrawPlane();
00524     glPopMatrix();
00525 
00526     // Draw the lamp
00527     glLightfv( GL_LIGHT0, GL_POSITION, iLightPos ); // Update lamp position
00528     glPushMatrix();
00529     glTranslatef( iLightPosX, iLightPosY, iLightPosZ );
00530     DrawLamp();
00531     glPopMatrix();
00532 
00533     glDisable( GL_DEPTH_TEST );  // Disable depth test while drawing shadows
00534                                  // To ensure that shadows are drawn on top
00535                                  // of the ground plane
00536     CalculateProjectionMatrix(); // Calculate/update shadowMat
00537     glPushMatrix();              // Store the current modelview for drawing
00538                                  // the actual objects after shadows are drawn
00539                                  // modelview = M_camera
00540     glMultMatrixf( iShadowMat ); // Multiply shadow matrix with current
00541                                  // modelview after camera xforms and before
00542                                  // model xforms
00543 
00544     // Draw shadows
00545     glTranslatef( 0.0, 0.0, 30.0 );  // Translate object above the ground plane
00546     glRotatef( float(aFrame), 1.0, 0.0, 0.0 ); // Rotate object
00547     glRotatef( float(aFrame), 0.0, 1.0, 0.0 );
00548     glRotatef( float(aFrame), 0.0, 0.0, 1.0 );
00549     glTranslatef( 5.0, -5.0, -5.0 ); // Move origin to the middle of the
00550                                      // shadow object
00551 
00552     DrawShadowObject(); // Draw shadows, i.e., draw the shadow object with the
00553                         // correct modelview matrix:
00554                         // modelview = M_camera * M_shadow * M_object
00555     glPopMatrix();      // modelview = M_camera
00556 
00557     glEnable( GL_DEPTH_TEST ); // Shadows are drawn -> enable depth test
00558     glEnable( GL_LIGHTING );   // Enable lighting for drawing the objects
00559     glShadeModel( GL_SMOOTH ); // To make the lighting work properly
00560 
00561     // Draw shadow object
00562     glTranslatef( 0.0, 0.0, 30.0 );
00563     glRotatef( float(aFrame), 1.0, 0.0, 0.0 );
00564     glRotatef( float(aFrame), 0.0, 1.0, 0.0 );
00565     glRotatef( float(aFrame), 0.0, 0.0, 1.0 );
00566     glTranslatef( 5.0, -5.0, -5.0 );
00567     DrawObject();
00568 
00569     }

void CShadows::RotateLightLeft (  void   ) 
 

Rotates light to left

Definition at line 583 of file Shadows.cpp.

References iIsLightRotatedLeft.

Referenced by CShadowsAppUi::HandleKeyEventL().

00584     {
00585     iIsLightRotatedLeft = !iIsLightRotatedLeft;
00586     }

void CShadows::RotateLightRight (  void   ) 
 

Rotates light to right

Definition at line 594 of file Shadows.cpp.

Referenced by CShadowsAppUi::HandleKeyEventL().

00595     {
00596     iIsLightRotatedRight = !iIsLightRotatedRight;
00597     }

void CShadows::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 604 of file Shadows.cpp.

References iScreenHeight, and iScreenWidth.

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

00605     {
00606     iScreenWidth  = aWidth;
00607     iScreenHeight = aHeight;
00608 
00609     // Reinitialize viewport and projection.
00610     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00611 
00612     // Recalculate the view frustrum
00613     glMatrixMode( GL_PROJECTION );
00614     glLoadIdentity();
00615     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00616     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00617                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00618                 FRUSTUM_NEAR, FRUSTUM_FAR );
00619     glMatrixMode( GL_MODELVIEW );
00620     }

void CShadows::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 283 of file Shadows.cpp.

00284     {
00285     }


Member Data Documentation

TUint CShadows::iScreenWidth [private]
 

Width of the screen

Definition at line 129 of file Shadows.h.

Referenced by AppInit(), and SetScreenSize().

TUint CShadows::iScreenHeight [private]
 

Height of the screen

Definition at line 132 of file Shadows.h.

Referenced by AppInit(), and SetScreenSize().

GLfloat CShadows::iLightRadius [private]
 

Radius of the light.

Definition at line 136 of file Shadows.h.

Referenced by AppCycle(), and AppInit().

GLfloat CShadows::iLightAngle [private]
 

Angle of the light.

Definition at line 139 of file Shadows.h.

Referenced by AppCycle(), and AppInit().

GLfloat CShadows::iLightPosX [private]
 

X coordinate of the light

Definition at line 143 of file Shadows.h.

Referenced by AppCycle(), AppInit(), and CalculateProjectionMatrix().

GLfloat CShadows::iLightPosY [private]
 

Y coordinate of the light

Definition at line 146 of file Shadows.h.

Referenced by AppCycle(), AppInit(), and CalculateProjectionMatrix().

GLfloat CShadows::iLightPosZ [private]
 

Z coordinate of the light

Definition at line 149 of file Shadows.h.

Referenced by AppCycle(), AppInit(), and CalculateProjectionMatrix().

GLfloat CShadows::iLightPos[4] [private]
 

Light position.

Definition at line 153 of file Shadows.h.

Referenced by AppCycle(), and AppInit().

GLfloat CShadows::iShadowMat[16] [private]
 

Shadow projection matrix.

Definition at line 156 of file Shadows.h.

Referenced by AppCycle(), and CalculateProjectionMatrix().

TBool CShadows::iIsLightRotatedLeft [private]
 

Whether the light is rotated left each frame or not.

Definition at line 160 of file Shadows.h.

Referenced by AppCycle(), AppInit(), and RotateLightLeft().

TBool CShadows::iIsLightRotatedRight [private]
 

Whether the light is rotated right each frame or not.

Definition at line 163 of file Shadows.h.

Referenced by AppCycle(), and AppInit().


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

© Nokia 2009

Back to top