Nokia N97 SDK
Example Applications Guide

Hierarchy.cpp

00001 /*
00002  * ==============================================================================
00003  *  Name        : Hierarchy.cpp
00004  *  Part of     : OpenGLEx / Hierarchy
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 // INCLUDE FILES
00014 #include <e32std.h>
00015 #include "Hierarchy.h"
00016 
00017 // EXTERNAL DATA STRUCTURES
00018 
00019 // EXTERNAL FUNCTION PROTOTYPES
00020 
00021 // CONSTANTS
00022 
00023 // MACROS
00024 
00025 // LOCAL CONSTANTS AND MACROS
00026 
00027 /**
00028  * Vertice coordinates for the cube.
00029  * This is used to draw every part of the robot hand.
00030  */
00031 static const GLbyte vertices[8 * 3] =
00032     {
00033      -1,  1,  1,
00034       1,  1,  1,
00035       1, -1,  1,
00036      -1, -1,  1,
00037 
00038      -1,  1, -1,
00039       1,  1, -1,
00040       1, -1, -1,
00041      -1, -1, -1
00042     };
00043 
00044 /**
00045  * Indices for drawing the triangles.
00046  */
00047 static const GLubyte triangles[12 * 3] =
00048     {
00049     /* front */
00050      1,0,3,
00051      1,3,2,
00052 
00053      /* right */
00054      2,6,5,
00055      2,5,1,
00056 
00057      /* back */
00058      7,4,5,
00059      7,5,6,
00060 
00061      /* left */
00062      0,4,7,
00063      0,7,3,
00064 
00065      /* top */
00066      5,4,0,
00067      5,0,1,
00068 
00069      /* bottom */
00070      3,7,6,
00071      3,6,2
00072     };
00073 
00074 
00075 /** Colors for the first arm part of the robot's hand. */
00076 static const GLubyte colorArm1[8 * 4] =
00077     {
00078     /* Dark gray */
00079     150, 150, 150,255,
00080     150, 150, 150,255,
00081     150, 150, 150,255,
00082     150, 150, 150,255,
00083 
00084     150, 150, 150,255,
00085     150, 150, 150,255,
00086     150, 150, 150,255,
00087     150, 150, 150,255
00088     };
00089 
00090 /** Colors for the second arm part of the robot's hand. */
00091 static const GLubyte colorArm2[8 * 4] =
00092     {
00093     /* Light gray */
00094     200, 200, 200,255,
00095     200, 200, 200,255,
00096     200, 200, 200,255,
00097     200, 200, 200,255,
00098 
00099     200, 200, 200,255,
00100     200, 200, 200,255,
00101     200, 200, 200,255,
00102     200, 200, 200,255
00103     };
00104 
00105 /** Colors for the first finger on the robot's hand. */
00106 static const GLubyte colorFinger1[8 * 4] =
00107     {
00108     /* Red */
00109     255,   0,   0,255,
00110     255,   0,   0,255,
00111     255,   0,   0,255,
00112     255,   0,   0,255,
00113 
00114     255,   0,   0,255,
00115     255,   0,   0,255,
00116     255,   0,   0,255,
00117     255,   0,   0,255
00118     };
00119 
00120 /** Colors for the second finger on the robot's hand. */
00121 static const GLubyte colorFinger2[8 * 4] =
00122     {
00123     /* White */
00124     255, 255, 255,255,
00125     255, 255, 255,255,
00126     255, 255, 255,255,
00127     255, 255, 255,255,
00128 
00129     255, 255, 255,255,
00130     255, 255, 255,255,
00131     255, 255, 255,255,
00132     255, 255, 255,255
00133     };
00134 
00135 
00136 /**
00137  * Used to create a helper, which simply draws the coordinate system.
00138  * This is very useful if your transformations are not correct.
00139  * Kind of graphical debugging.
00140  */
00141 static const GLubyte axisInd[3 * 2] =
00142     {
00143     0,1,
00144     0,2,
00145     0,3
00146     };
00147 
00148 // ============================ MEMBER FUNCTIONS ===============================
00149 
00150 // -----------------------------------------------------------------------------
00151 // CHierarchy::CHierarchy
00152 // C++ default constructor can NOT contain any code, that
00153 // might leave.
00154 // -----------------------------------------------------------------------------
00155 //
00156 CHierarchy::CHierarchy( TUint aWidth, TUint aHeight )
00157 :iScreenWidth(aWidth), iScreenHeight(aHeight)
00158     {
00159     }
00160 
00161 
00162 // -----------------------------------------------------------------------------
00163 // CHierarchy::ConstructL
00164 // Symbian 2nd phase constructor can leave.
00165 // -----------------------------------------------------------------------------
00166 //
00167 void CHierarchy::ConstructL( void )
00168     {
00169     }
00170 
00171 
00172 // -----------------------------------------------------------------------------
00173 // CHierarchy::NewL
00174 // Two-phased constructor.
00175 // -----------------------------------------------------------------------------
00176 //
00177 CHierarchy* CHierarchy::NewL( TUint aWidth, TUint aHeight )
00178     {
00179     /* Symbian 2-phase constructor. Calls both the default
00180        C++ constructor and Symbian ConstructL methods */
00181     CHierarchy* self = new (ELeave) CHierarchy( aWidth, aHeight );
00182     CleanupStack::PushL( self );
00183     self->ConstructL();
00184     CleanupStack::Pop();
00185 
00186     return self;
00187     }
00188 
00189 
00190 // Destructor.
00191 CHierarchy::~CHierarchy()
00192     {
00193     }
00194 
00195 
00196 // -----------------------------------------------------------------------------
00197 // CHierarchy::AppInit
00198 // Initialize OpenGL ES, set the vertex and color arrays and pointers,
00199 // and select the shading mode.
00200 // -----------------------------------------------------------------------------
00201 //
00202 void CHierarchy::AppInit( void )
00203     {
00204     // Initialize viewport and projection.
00205     SetScreenSize( iScreenWidth, iScreenHeight );
00206 
00207     /* Set the screen background color. */
00208     glClearColor( 0.f, 0.f, 0.f, 1.f );
00209 
00210     /* Enable depth test and back face culling. */
00211     glEnable( GL_DEPTH_TEST );
00212     glEnable( GL_CULL_FACE  );
00213 
00214     /* Enable vertex and color arrays. */
00215     glEnableClientState( GL_VERTEX_ARRAY );
00216     glEnableClientState( GL_COLOR_ARRAY );
00217 
00218     /* Set the initial shading mode */
00219     glShadeModel( GL_FLAT );
00220 
00221     /* Do not use perspective correction */
00222     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00223 
00224     /* Rotate mode switch */
00225     iRotateMode      = ROTATE_ELBOW;
00226     iElbowRotAngle   = 0.0;
00227     iFingersRotAngle = 0.0;
00228     iRotateUpEnabled        = EFalse;
00229     iRotateDownEnabled      = EFalse;
00230     }
00231 
00232 
00233 // -----------------------------------------------------------------------------
00234 // CHierarchy::AppExit
00235 // Release any allocations made in AppInit.
00236 // -----------------------------------------------------------------------------
00237 //
00238 void CHierarchy::AppExit( void )
00239     {
00240     }
00241 
00242 
00243 // -----------------------------------------------------------------------------
00244 // CHierarchy::DrawCoordSystem
00245 // -----------------------------------------------------------------------------
00246 //
00247 void CHierarchy::DrawCoordSystem( void )
00248     {
00249     glDrawElements( GL_LINES, 3 * 2, GL_UNSIGNED_BYTE, axisInd );
00250     }
00251 
00252 // -----------------------------------------------------------------------------
00253 // CHierarchy::DrawBox
00254 // Draw a box. Used to draw every part of the robot hand.
00255 // -----------------------------------------------------------------------------
00256 //
00257 void CHierarchy::DrawBox( void )
00258     {
00259     glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00260     }
00261 
00262 
00263 // -----------------------------------------------------------------------------
00264 // CHierarchy::AppCycle
00265 // Draws and animates the objects
00266 // -----------------------------------------------------------------------------
00267 //
00268 void CHierarchy::AppCycle( TInt /*aFrame*/ )
00269     {
00270     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00271 
00272     glLoadIdentity();
00273 
00274     /* Update rotate angles*/
00275     if ( iRotateUpEnabled )
00276         {
00277         if ( iRotateMode == ROTATE_ELBOW )
00278             {
00279             iElbowRotAngle += 1.0;
00280             if ( iElbowRotAngle > 90.0 )
00281                 {
00282                 iElbowRotAngle = 90.0;
00283                 }
00284             }
00285         else if ( iRotateMode == ROTATE_FINGERS )
00286             {
00287             iFingersRotAngle += 1.0;
00288             if ( iFingersRotAngle > 80.0 )
00289                 {
00290                 iFingersRotAngle = 80.0;
00291                 }
00292             }
00293         }
00294 
00295     if ( iRotateDownEnabled )
00296         {
00297         if ( iRotateMode == ROTATE_ELBOW )
00298             {
00299             iElbowRotAngle -= 1.0;
00300             if ( iElbowRotAngle < 0.0 )
00301                 {
00302                 iElbowRotAngle = 0.0;
00303                 }
00304             }
00305         else if ( iRotateMode == ROTATE_FINGERS )
00306             {
00307             iFingersRotAngle -= 1.0;
00308             if ( iFingersRotAngle < 0.0 )
00309                 {
00310                 iFingersRotAngle = 0.0;
00311                 }
00312             }
00313         }
00314 
00315     // Move Camera
00316     gluLookAt(   40.f, 10.f, 150.f,  // Camera location
00317                   50.f,    10.f, 0.f,  // Look at point
00318                    0.f,    1.f, 0.f ); // Up vector
00319 
00320     // Rotate the arm
00321     glRotatef( -100.f, 0., 1., 0. );
00322     glRotatef(   60.f, 0., 0., 1. );
00323 
00324     // Draw Arm
00325     glVertexPointer( 3, GL_BYTE, 0, vertices );
00326     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorArm1 );
00327     glTranslatef( 0.0, 0.0, -30.0 );
00328     glPushMatrix();
00329     glScalef( 6.0, 6.0, 15.0 );
00330     glTranslatef( 0.0, 0.0, 1.0 );
00331     DrawBox();
00332     glPopMatrix();
00333 
00334     // Draw Hand
00335     glVertexPointer( 3, GL_BYTE, 0, vertices );
00336     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorArm2 );
00337     glTranslatef( 6.0, 0.0, 0.0 );   // move coordisystem back to middle
00338     glRotatef( -iElbowRotAngle, 0.0, 1.0, 0.0 ); // rotate object
00339     glTranslatef( -6.0, 0.0, 0.0 );  // move coordsystem to top of the box
00340     glTranslatef( 0.0, 0.0, -30.0 ); // Move origin to the other end of cube
00341     glPushMatrix();
00342     glScalef( 6.0, 6.0, 15. );       // Scale the vertices
00343     glTranslatef( 0.0, 0.0, 1.0 );   // Translate origin to the side of the cube
00344     DrawBox();
00345     glPopMatrix();
00346 
00347     // Draw upper finger
00348     glPushMatrix(); // Store the current matrix because we have to draw
00349                        // another finger to the same joint, i.e., arm 2. joint
00350 
00351     // Red part
00352     glVertexPointer( 3, GL_BYTE, 0, vertices );
00353     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger1 );
00354     glTranslatef( 4.5, 0.0, 0.0 );   // move origin near the top of the arm 2. joint
00355     glTranslatef( 1.5, 0.0, 0.0 );   // move origin to the edge of the finger
00356     glRotatef( float(-10.0 - iFingersRotAngle), 0.0, 1.0, 0.0 ); // rotate 1st joint
00357     glTranslatef( -1.5, 0.0, 0.0 );  // move origin to the middle of finger
00358     glTranslatef( 0.0, 0.0, -10.0 ); // move origin to the other end if box
00359     glPushMatrix();
00360     glScalef( 1.5, 1.5, 5. );
00361     glTranslatef( 0.0, 0.0, 1.0 );
00362     DrawBox();
00363     glPopMatrix();
00364 
00365     // White part
00366     glVertexPointer( 3, GL_BYTE, 0, vertices );
00367     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger2 );
00368     glTranslatef( -1.5, 0.0, 0.0 );
00369     glRotatef( float(80.0 - iFingersRotAngle), 0.0, 1.0, 0.0 );
00370     glTranslatef( 1.5, 0.0, 0.0 );
00371     glTranslatef( 0.0, 0.0, -6.0 );
00372     glPushMatrix();
00373     glScalef( 1.5, 1.5, 3. );
00374     glTranslatef( 0.0, 0.0, 1.0 );
00375     DrawBox();
00376     glPopMatrix();
00377 
00378     glPopMatrix(); // Pop back to the 'old' matrix in order to draw the second finger
00379 
00380     // Draw lower finger
00381     glPushMatrix();
00382 
00383     // Red part
00384     glVertexPointer( 3, GL_BYTE, 0, vertices );
00385     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger1 );
00386     glTranslatef( -4.5, 0.0, 0.0 );  // move origin near the bottom of the arm 2. joint
00387     glTranslatef( -1.5, 0.0, 0.0 );  // move origin to the edge of the finger
00388     glRotatef( float(10.0 + iFingersRotAngle), 0.0, 1.0, 0.0 ); // rotate 1st joint
00389     glTranslatef( 1.5, 0.0, 0.0 );   // move origin to the middle of finger
00390     glTranslatef( 0.0, 0.0, -10.0 ); // move origin to the other end if box
00391     glPushMatrix();
00392     glScalef( 1.5, 1.5, 5. );
00393     glTranslatef( 0.0, 0.0, 1.0 );
00394     DrawBox();
00395     glPopMatrix();
00396 
00397     // White part
00398     glVertexPointer( 3, GL_BYTE, 0, vertices );
00399     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorFinger2 );
00400     glTranslatef( 1.5, 0.0, 0.0 );
00401     glRotatef( float(-80.0 + iFingersRotAngle), 0.0, 1.0, 0.0 );
00402     glTranslatef( -1.5, 0.0, 0.0 );
00403     glTranslatef( 0.0, 0.0, -6.0 );
00404     glPushMatrix();
00405     glScalef( 1.5, 1.5, 3. );
00406     glTranslatef( 0.0, 0.0, 1.0 );
00407     DrawBox();
00408     glPopMatrix();
00409 
00410     glPopMatrix();
00411 
00412 
00413     }
00414 
00415 
00416 //----------------------------------------------------------
00417 // The following methods are called by the CHierarchyAppUi
00418 // class when handling the incoming key events.
00419 //----------------------------------------------------------
00420 
00421 // -----------------------------------------------------------------------------
00422 // CHierarchy::SwitchRotateMode
00423 // Switch the rotation mode between rotating the arm or fingers
00424 // -----------------------------------------------------------------------------
00425 //
00426 void CHierarchy::SwitchRotateMode( void )
00427     {
00428     if ( iRotateMode == ROTATE_ELBOW )
00429         {
00430         iRotateMode = ROTATE_FINGERS;
00431         }
00432     else
00433         {
00434         iRotateMode = ROTATE_ELBOW;
00435         }
00436     }
00437 
00438 // -----------------------------------------------------------------------------
00439 // CHierarchy::RotateObjectUp
00440 // Rotates object up
00441 // -----------------------------------------------------------------------------
00442 //
00443 void CHierarchy::RotateObjectUp( TBool aStart )
00444     {
00445     if( aStart )
00446         {
00447         iRotateUpEnabled = ETrue;
00448         iRotateDownEnabled = EFalse;
00449         } else
00450         {
00451         iRotateUpEnabled = EFalse;
00452         }
00453     }
00454 
00455 // -----------------------------------------------------------------------------
00456 // CHierarchy::RotateObjectDown
00457 // Rotates object down
00458 // -----------------------------------------------------------------------------
00459 //
00460 void CHierarchy::RotateObjectDown( TBool aStart )
00461     {
00462     if( aStart )
00463         {
00464         iRotateDownEnabled = ETrue;
00465         iRotateUpEnabled = EFalse;
00466         } else
00467         {
00468         iRotateDownEnabled = EFalse;
00469         }
00470     }
00471 
00472 // -----------------------------------------------------------------------------
00473 // CHierarchy::SetScreenSize
00474 // Reacts to the dynamic screen size change during execution of this program.
00475 // -----------------------------------------------------------------------------
00476 //
00477 void CHierarchy::SetScreenSize( TUint aWidth, TUint aHeight )
00478     {
00479     iScreenWidth  = aWidth;
00480     iScreenHeight = aHeight;
00481 
00482     // Reinitialize viewport and projection.
00483     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00484 
00485     // Recalculate the view frustrum
00486     glMatrixMode( GL_PROJECTION );
00487     glLoadIdentity();
00488     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00489     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00490                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00491                 FRUSTUM_NEAR, FRUSTUM_FAR );
00492     glMatrixMode( GL_MODELVIEW );
00493     }
00494 
00495 // End of File
00496 

© Nokia 2009

Back to top