S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CHierarchy Class Reference

#include <Hierarchy.h>

List of all members.

Public Member Functions

virtual ~CHierarchy ()
void AppInit (void)
void AppExit (void)
void DrawBox (void)
void DrawCoordSystem (void)
void AppCycle (TInt aFrame)
void RotateObjectUp (TBool aStart)
void RotateObjectDown (TBool aStart)
void SwitchRotateMode (void)
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CHierarchy * NewL (TUint aWidth, TUint aHeight)

Protected Member Functions

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

Private Attributes

TUint iScreenWidth
TUint iScreenHeight
TInt iRotateMode
TBool iRotateUpEnabled
TBool iRotateDownEnabled
TReal32 iElbowRotAngle
TReal32 iFingersRotAngle


Detailed Description

Class that does the actual OpenGL ES rendering.

Definition at line 39 of file Hierarchy.h.


Constructor & Destructor Documentation

CHierarchy::~CHierarchy (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 196 of file Hierarchy.cpp.

00197     {
00198     }

CHierarchy::CHierarchy (  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 161 of file Hierarchy.cpp.

00162 :iScreenWidth(aWidth), iScreenHeight(aHeight)
00163     {
00164     }


Member Function Documentation

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

Factory method for creating a new CSimpleCube object.

Definition at line 182 of file Hierarchy.cpp.

References CHierarchy().

00183     {
00184     /* Symbian 2-phase constructor. Calls both the default 
00185        C++ constructor and Symbian ConstructL methods */
00186     CHierarchy* self = new (ELeave) CHierarchy( aWidth, aHeight );
00187     CleanupStack::PushL( self );
00188     self->ConstructL();
00189     CleanupStack::Pop();
00190 
00191     return self;
00192     }

void CHierarchy::AppInit (  void   ) 
 

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

Definition at line 207 of file Hierarchy.cpp.

References iElbowRotAngle, iFingersRotAngle, iRotateDownEnabled, iRotateMode, iRotateUpEnabled, iScreenHeight, and iScreenWidth.

00208     {
00209     /* Set the screen background color. */
00210     glClearColor( 0.f, 0.f, 0.f, 1.f );
00211 
00212     /* Enable depth test and back face culling. */
00213     glEnable( GL_DEPTH_TEST );
00214     glEnable( GL_CULL_FACE  );
00215 
00216     /* Initialize viewport and projection. */
00217     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00218     glMatrixMode( GL_PROJECTION );
00219 
00220     // Calculate the view frustrum
00221     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00222     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00223                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00224                 FRUSTUM_NEAR, FRUSTUM_FAR );
00225 
00226     glMatrixMode( GL_MODELVIEW );
00227 
00228     /* Enable vertex and color arrays. */
00229     glEnableClientState( GL_VERTEX_ARRAY );
00230     glEnableClientState( GL_COLOR_ARRAY );
00231 
00232     /* Set the initial shading mode */
00233     glShadeModel( GL_FLAT ); 
00234 
00235     /* Do not use perspective correction */
00236     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00237 
00238     /* Rotate mode switch */
00239     iRotateMode      = ROTATE_ELBOW;
00240     iElbowRotAngle   = 0.0;
00241     iFingersRotAngle = 0.0;
00242     iRotateUpEnabled        = EFalse;
00243     iRotateDownEnabled      = EFalse;
00244     }

void CHierarchy::AppExit (  void   ) 
 

Called upon application exit. Does nothing.

Definition at line 252 of file Hierarchy.cpp.

00253     {
00254     }

void CHierarchy::DrawBox (  void   ) 
 

Draws a 3D box.

Definition at line 271 of file Hierarchy.cpp.

00272     {
00273     glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00274     }

void CHierarchy::DrawCoordSystem (  void   ) 
 

Draw a coordinate system

Definition at line 261 of file Hierarchy.cpp.

00262     {
00263     glDrawElements( GL_LINES, 3 * 2, GL_UNSIGNED_BYTE, axisInd );
00264     }

void CHierarchy::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 282 of file Hierarchy.cpp.

References DrawBox(), iElbowRotAngle, iFingersRotAngle, iRotateDownEnabled, iRotateMode, and iRotateUpEnabled.

00283     {
00284     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00285 
00286     glLoadIdentity();
00287 
00288     /* Update rotate angles*/
00289     if ( iRotateUpEnabled )
00290         {
00291         if ( iRotateMode == ROTATE_ELBOW )
00292             {
00293             iElbowRotAngle += 1.0;
00294             if ( iElbowRotAngle > 90.0 ) 
00295                 {
00296                 iElbowRotAngle = 90.0;
00297                 }
00298             }
00299         else if ( iRotateMode == ROTATE_FINGERS ) 
00300             {
00301             iFingersRotAngle += 1.0;
00302             if ( iFingersRotAngle > 80.0 )
00303                 {
00304                 iFingersRotAngle = 80.0;
00305                 }
00306             }
00307         }
00308     
00309     if ( iRotateDownEnabled )
00310         {
00311         if ( iRotateMode == ROTATE_ELBOW )
00312             {
00313             iElbowRotAngle -= 1.0;
00314             if ( iElbowRotAngle < 0.0 )
00315                 {
00316                 iElbowRotAngle = 0.0;
00317                 }
00318             }
00319         else if ( iRotateMode == ROTATE_FINGERS )
00320             {
00321             iFingersRotAngle -= 1.0;
00322             if ( iFingersRotAngle < 0.0 )
00323                 {
00324                 iFingersRotAngle = 0.0;
00325                 }
00326             }
00327         }
00328     
00329     // Move Camera 
00330     glTranslatef( -40.0 , -10.0 , -cameraDistance );
00331     glRotatef(   10., 1., 0., 0. );
00332     glRotatef( -100., 0., 1., 0. );
00333     glRotatef(   60., 0., 0., 1. );
00334 
00335     // Draw Arm
00336     glVertexPointer( 3, GL_BYTE, 0, vertices );
00337     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorArm1 );
00338     glTranslatef( 0.0, 0.0, -30.0 );
00339     glPushMatrix();
00340     glScalef( 6.0, 6.0, 15.0 );
00341     glTranslatef( 0.0, 0.0, 1.0 );
00342     DrawBox();
00343     glPopMatrix();
00344 
00345     // Draw Hand 
00346     glVertexPointer( 3, GL_BYTE, 0, vertices );
00347     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorArm2 );
00348     glTranslatef( 6.0, 0.0, 0.0 );   // move coordisystem back to middle
00349     glRotatef( -iElbowRotAngle, 0.0, 1.0, 0.0 ); // rotate object
00350     glTranslatef( -6.0, 0.0, 0.0 );  // move coordsystem to top of the box
00351     glTranslatef( 0.0, 0.0, -30.0 ); // Move origin to the other end of cube
00352     glPushMatrix();
00353     glScalef( 6.0, 6.0, 15. );       // Scale the vertices
00354     glTranslatef( 0.0, 0.0, 1.0 );   // Translate origin to the side of the cube
00355     DrawBox();
00356     glPopMatrix();
00357 
00358     // Draw upper finger 
00359     glPushMatrix(); // Store the current matrix because we have to draw
00360                        // another finger to the same joint, i.e., arm 2. joint
00361 
00362     // Red part 
00363     glVertexPointer( 3, GL_BYTE, 0, vertices );
00364     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger1 );
00365     glTranslatef( 4.5, 0.0, 0.0 );   // move origin near the top of the arm 2. joint
00366     glTranslatef( 1.5, 0.0, 0.0 );   // move origin to the edge of the finger
00367     glRotatef( float(-10.0 - iFingersRotAngle), 0.0, 1.0, 0.0 ); // rotate 1st joint
00368     glTranslatef( -1.5, 0.0, 0.0 );  // move origin to the middle of finger
00369     glTranslatef( 0.0, 0.0, -10.0 ); // move origin to the other end if box
00370     glPushMatrix();
00371     glScalef( 1.5, 1.5, 5. );
00372     glTranslatef( 0.0, 0.0, 1.0 );
00373     DrawBox();
00374     glPopMatrix();
00375 
00376     // White part 
00377     glVertexPointer( 3, GL_BYTE, 0, vertices );
00378     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger2 );
00379     glTranslatef( -1.5, 0.0, 0.0 );
00380     glRotatef( float(80.0 - iFingersRotAngle), 0.0, 1.0, 0.0 );
00381     glTranslatef( 1.5, 0.0, 0.0 );
00382     glTranslatef( 0.0, 0.0, -6.0 );
00383     glPushMatrix();
00384     glScalef( 1.5, 1.5, 3. );
00385     glTranslatef( 0.0, 0.0, 1.0 );
00386     DrawBox();
00387     glPopMatrix();
00388 
00389     glPopMatrix(); // Pop back to the 'old' matrix in order to draw the second finger
00390 
00391     // Draw lower finger 
00392     glPushMatrix();
00393 
00394     // Red part 
00395     glVertexPointer( 3, GL_BYTE, 0, vertices );
00396     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger1 );
00397     glTranslatef( -4.5, 0.0, 0.0 );  // move origin near the bottom of the arm 2. joint
00398     glTranslatef( -1.5, 0.0, 0.0 );  // move origin to the edge of the finger
00399     glRotatef( float(10.0 + iFingersRotAngle), 0.0, 1.0, 0.0 ); // rotate 1st joint
00400     glTranslatef( 1.5, 0.0, 0.0 );   // move origin to the middle of finger
00401     glTranslatef( 0.0, 0.0, -10.0 ); // move origin to the other end if box
00402     glPushMatrix();
00403     glScalef( 1.5, 1.5, 5. );
00404     glTranslatef( 0.0, 0.0, 1.0 );
00405     DrawBox();
00406     glPopMatrix();
00407 
00408     // White part 
00409     glVertexPointer( 3, GL_BYTE, 0, vertices );
00410     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger2 );
00411     glTranslatef( 1.5, 0.0, 0.0 );
00412     glRotatef( float(-80.0 + iFingersRotAngle), 0.0, 1.0, 0.0 );
00413     glTranslatef( -1.5, 0.0, 0.0 );
00414     glTranslatef( 0.0, 0.0, -6.0 );
00415     glPushMatrix();
00416     glScalef( 1.5, 1.5, 3. );
00417     glTranslatef( 0.0, 0.0, 1.0 );
00418     DrawBox();
00419     glPopMatrix();
00420 
00421     glPopMatrix();
00422 
00423 
00424     }

void CHierarchy::RotateObjectUp (  TBool  aStart  ) 
 

Toggle object "up" rotation on and off based on the given value.

Parameters:
aStart True if object should be rotated up every frame. False if object shouldn't be rotated up every frame.

Definition at line 454 of file Hierarchy.cpp.

References iRotateDownEnabled, and iRotateUpEnabled.

00455     {
00456     if( aStart )
00457         {
00458         iRotateUpEnabled = ETrue;
00459         iRotateDownEnabled = EFalse;
00460         } else
00461         {
00462         iRotateUpEnabled = EFalse;
00463         }
00464     }

void CHierarchy::RotateObjectDown (  TBool  aStart  ) 
 

Toggle object "down" rotation on and off based on the given value.

Parameters:
aStart True if object should be rotated down every frame. False if object shouldn't be rotated down every frame.

Definition at line 471 of file Hierarchy.cpp.

References iRotateDownEnabled, and iRotateUpEnabled.

00472     {
00473     if( aStart )
00474         {
00475         iRotateDownEnabled = ETrue;
00476         iRotateUpEnabled = EFalse;
00477         } else
00478         {
00479         iRotateDownEnabled = EFalse;
00480         }
00481     }

void CHierarchy::SwitchRotateMode (  void   ) 
 

Toggles the rotation mode between rotating the arm or the fingers

Definition at line 437 of file Hierarchy.cpp.

References iRotateMode.

00438     {
00439     if ( iRotateMode == ROTATE_ELBOW )
00440         {
00441         iRotateMode = ROTATE_FINGERS;
00442         }
00443     else
00444         {
00445         iRotateMode = ROTATE_ELBOW;
00446         }
00447     }

void CHierarchy::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 488 of file Hierarchy.cpp.

References iScreenHeight, and iScreenWidth.

00489     {
00490     iScreenWidth  = aWidth;
00491     iScreenHeight = aHeight;
00492     
00493     // Reinitialize viewport and projection.
00494     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00495 
00496     // Recalculate the view frustrum
00497     glMatrixMode( GL_PROJECTION );
00498     glLoadIdentity();
00499     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00500     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00501                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00502                 FRUSTUM_NEAR, FRUSTUM_FAR );
00503     glMatrixMode( GL_MODELVIEW );
00504     }

void CHierarchy::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 172 of file Hierarchy.cpp.

00173     {
00174     }


Member Data Documentation

TUint CHierarchy::iScreenWidth [private]
 

Width of the screen

Definition at line 127 of file Hierarchy.h.

TUint CHierarchy::iScreenHeight [private]
 

Height of the screen

Definition at line 130 of file Hierarchy.h.

TInt CHierarchy::iRotateMode [private]
 

The current rotation mode (ROTATE_ELBOW or ROTATE_FINGERS).

Definition at line 133 of file Hierarchy.h.

TBool CHierarchy::iRotateUpEnabled [private]
 

Whether object should be rotated up during each rendered frame or not.

Definition at line 136 of file Hierarchy.h.

TBool CHierarchy::iRotateDownEnabled [private]
 

Whether object should be rotated down during each rendered frame or not.

Definition at line 139 of file Hierarchy.h.

TReal32 CHierarchy::iElbowRotAngle [private]
 

Current rotation angle of the elbow.

Definition at line 142 of file Hierarchy.h.

TReal32 CHierarchy::iFingersRotAngle [private]
 

Current rotation angle of the fingers.

Definition at line 145 of file Hierarchy.h.


© Nokia 2006

Back to top