S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CPickSelect Class Reference

#include <PickSelect.h>

List of all members.

Public Member Functions

virtual ~CPickSelect ()
void AppInit (void)
void AppExit (void)
void AppCycle (TInt aFrame)
void UpdateCursorPosition (void)
void DrawCursor (void)
void DrawSingleBox (TInt aFrame)
void DrawMultipleBoxes (TInt aFrame)
void MoveUp (TBool move)
void MoveDown (TBool move)
void MoveLeft (TBool move)
void MoveRight (TBool move)
void SelectObject (void)
void SetPolygonSelection (void)
void SetObjectSelection (void)
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CPickSelect * NewL (TUint aWidth, TUint aHeight)

Protected Member Functions

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

Private Attributes

TUint iScreenWidth
TUint iScreenHeight
TInt iSelectionMode
TBool iMoveUp
TBool iMoveDown
TBool iMoveLeft
TBool iMoveRight
TInt iSelectionIdx
GLfloat iCubeRotX
GLfloat iCubeRotY
GLint iCursorPosX
GLint iCursorPosY
GLbyte iSelectedPoly [3]
GLfloat iAspectRatio


Detailed Description

Class that does the actual OpenGL ES rendering.

Definition at line 36 of file PickSelect.h.


Constructor & Destructor Documentation

CPickSelect::~CPickSelect (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 317 of file PickSelect.cpp.

00318     {
00319     }

CPickSelect::CPickSelect (  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 273 of file PickSelect.cpp.

References iAspectRatio, iScreenHeight, and iScreenWidth.

00274 :iScreenWidth(aWidth), iScreenHeight(aHeight)
00275     {
00276     // Recalculate aspect ratio
00277     if ( iScreenHeight != 0 )
00278         {
00279         iAspectRatio = (GLfloat)iScreenWidth / (GLfloat)iScreenHeight;
00280         }
00281     else
00282         {
00283         iAspectRatio = 1.0;
00284         }
00285     }


Member Function Documentation

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

Factory method for creating a new CPickSelect object.

Definition at line 303 of file PickSelect.cpp.

References CPickSelect().

00304     {
00305     /* Symbian 2-phase constructor. Calls both the default 
00306        C++ constructor and Symbian ConstructL methods */
00307     CPickSelect* self = new (ELeave) CPickSelect( aWidth, aHeight );
00308     CleanupStack::PushL( self );
00309     self->ConstructL();
00310     CleanupStack::Pop();
00311 
00312     return self;
00313     }

void CPickSelect::AppInit (  void   ) 
 

Initializes OpenGL ES, sets the vertex and color arrays and pointers etc.

Definition at line 328 of file PickSelect.cpp.

References iCubeRotX, iCubeRotY, iCursorPosX, iCursorPosY, iMoveDown, iMoveLeft, iMoveRight, iMoveUp, iScreenHeight, iScreenWidth, iSelectionIdx, and iSelectionMode.

00329     {
00330     // Set the screen background color. 
00331     glClearColor( 0.f, 0.f, 0.f, 0.f );
00332 
00333     glEnable( GL_DEPTH_TEST );   // z-compare enabled
00334     glEnable( GL_CULL_FACE  );   // backface culling enabled
00335 
00336     // Initialize viewport. 
00337     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00338 
00339     // Enable vertex and color arrays. 
00340     glEnableClientState( GL_VERTEX_ARRAY );
00341     glEnableClientState( GL_COLOR_ARRAY );
00342 
00343     glLineWidth( 1.0 );
00344 
00345     // Set the initial shading mode 
00346     glShadeModel( GL_FLAT ); 
00347 
00348     iSelectionMode = POLYGON_SELECTION;
00349     iCubeRotX      = GLfloat( 0.0 );
00350     iCubeRotY      = GLfloat( 0.0 );
00351     iCursorPosX    = GLint( 30 );
00352     iCursorPosY    = GLint( 30 );
00353 
00354     iMoveUp       = EFalse;
00355     iMoveDown     = EFalse;
00356     iMoveLeft     = EFalse;
00357     iMoveRight    = EFalse;
00358     iSelectionIdx = 0;
00359 
00360     }

void CPickSelect::AppExit (  void   ) 
 

Called upon application exit. Does nothing.

Definition at line 370 of file PickSelect.cpp.

00371 {
00372 }

void CPickSelect::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 380 of file PickSelect.cpp.

References DrawCursor(), DrawMultipleBoxes(), DrawSingleBox(), iSelectionMode, and UpdateCursorPosition().

00381     {
00382 
00383     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
00384 
00385     if ( iSelectionMode == POLYGON_SELECTION )
00386         {
00387         DrawSingleBox( aFrame ); 
00388         }
00389     else if ( iSelectionMode == OBJECT_SELECTION ) 
00390         {
00391         DrawMultipleBoxes( aFrame ); 
00392         } 
00393 
00394     // Update the cursor position and redraw it 
00395     UpdateCursorPosition();
00396     DrawCursor();
00397     }

void CPickSelect::UpdateCursorPosition (  void   ) 
 

Updates cursor position.

Definition at line 407 of file PickSelect.cpp.

References iCursorPosX, iCursorPosY, iMoveDown, iMoveLeft, iMoveRight, iMoveUp, iScreenHeight, and iScreenWidth.

00408     {
00409     // Check if cursor should move in up/down direction
00410     if ( iMoveUp )
00411         { 
00412         iCursorPosY += 2;
00413         if ( iCursorPosY > iScreenWidth )
00414             {
00415             iCursorPosY = iScreenWidth;
00416             }
00417         }
00418     else if ( iMoveDown )
00419         { 
00420         iCursorPosY -= 2;
00421         if ( iCursorPosY < 0 )
00422             {
00423             iCursorPosY = 0;
00424             }
00425         }
00426     
00427     // Check if cursor should move in left/right direction
00428     if ( iMoveLeft )
00429         { 
00430         iCursorPosX -= 2;
00431         if ( iCursorPosX < 0 )
00432             {
00433             iCursorPosX = 0;
00434             }
00435         }
00436     else if ( iMoveRight ) 
00437         { 
00438         iCursorPosX += 2;
00439         if ( iCursorPosX > iScreenHeight )
00440             {
00441             iCursorPosX = iScreenHeight;
00442             }
00443         }
00444     }

void CPickSelect::DrawCursor (  void   ) 
 

Draws the cursor.

Definition at line 453 of file PickSelect.cpp.

References iCursorPosX, iCursorPosY, iScreenHeight, and iScreenWidth.

00454     {
00455     // Setup orthogonal projection for drawing the cursor to the screen
00456     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00457     glMatrixMode( GL_PROJECTION );
00458     glLoadIdentity();
00459     glOrthof( 0.f, (float) iScreenWidth, 0.f, (float) iScreenHeight, -1, 1 ); // set the same size as viewport
00460     
00461     glMatrixMode( GL_MODELVIEW );
00462     glLoadIdentity();
00463     glTranslatex( iCursorPosX << 16 , iCursorPosY << 16, 0 );
00464     glScalex( 65536 / 100, 65536 / 100, 0); // Scale down the geometry
00465 
00466     glVertexPointer( 2, GL_SHORT, 0, cross );
00467     glDisableClientState( GL_COLOR_ARRAY ); // disable color array
00468     glColor4f( 0.f, 1.f, 1.f, 1.f );        // use current color for drawing
00469     glDrawElements( GL_LINES, 8, GL_UNSIGNED_BYTE, crossIndices );
00470     glEnableClientState( GL_COLOR_ARRAY );  // re-enable color array
00471     // Switch back to original Viewport definitions 
00472     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00473 
00474     }

void CPickSelect::DrawSingleBox (  TInt  aFrame  ) 
 

Draw a single cube and white lines around the selected polygon if one is selected.

Parameters:
aFrame The number of the frame to be rendered.

Definition at line 483 of file PickSelect.cpp.

References iAspectRatio, iSelectedPoly, and iSelectionIdx.

00484     {
00485       // Set the projection and modelview matrices 
00486     glMatrixMode( GL_PROJECTION );
00487     glLoadIdentity();
00488     glFrustumf( FRUSTUM_LEFT * iAspectRatio, FRUSTUM_RIGHT * iAspectRatio,
00489                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00490                 FRUSTUM_NEAR, FRUSTUM_FAR );
00491     
00492     glMatrixMode( GL_MODELVIEW );
00493     glLoadIdentity();
00494     glTranslatex( 0 , 0 , -cameraDistance << 16 );
00495     glRotatex( aFrame << 16, 1 << 16,    0   ,    0    );
00496     glRotatex( aFrame << 15,    0   , 1 << 16,    0    );
00497     glRotatex( aFrame << 14,    0   ,    0   , 1 << 16 );
00498     
00499     // draw box 
00500     glVertexPointer( 3, GL_BYTE, 0, vertices );
00501     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
00502     glPushMatrix();
00503     glScalex( 10 << 16, 10 << 16, 10 << 16 );
00504     glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00505     
00506     
00507     /* draw white lines around the polygon selected. Zero is for background
00508        and 255 is for debugging purposes only. */
00509     if ( iSelectionIdx != 0 && iSelectionIdx != 255 )
00510         {
00511         // gather indices of the selected polygon 
00512         for ( int i = 0; i < 3; i++ )
00513             {
00514             iSelectedPoly[i] = triangles[ 3*(iSelectionIdx - 1) + i ];
00515             }
00516         
00517         glDisable( GL_DEPTH_TEST ); // draw on top of the existing graphics
00518         glDisableClientState( GL_COLOR_ARRAY ); // disable color array
00519         glColor4f( 1.f, 1.f, 1.f, 1.f ); // use white for the selected polygon
00520         glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_BYTE, iSelectedPoly );
00521         glEnableClientState( GL_COLOR_ARRAY ); // re-enable color array
00522         glEnable( GL_DEPTH_TEST );
00523         }
00524     
00525     glPopMatrix();    
00526     }

void CPickSelect::DrawMultipleBoxes (  TInt  aFrame  ) 
 

Draw two rotating boxes. The selected cube, if there is one, is draw in wireframe mode.

Parameters:
aFrame The number of the frame to be rendered.

Definition at line 535 of file PickSelect.cpp.

References iSelectionIdx.

00536     {
00537     // Set the projection and modelview matrices 
00538     glMatrixMode( GL_PROJECTION );
00539     glLoadIdentity();
00540     glFrustumf( FRUSTUM_LEFT * iAspectRatio, FRUSTUM_RIGHT * iAspectRatio,
00541                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00542                 FRUSTUM_NEAR, FRUSTUM_FAR );
00543     
00544     glMatrixMode( GL_MODELVIEW );
00545     glLoadIdentity();
00546     glTranslatex( 0 , 0 , -cameraDistance << 16 );
00547     
00548     // Set the correct vertex pointer
00549     glVertexPointer( 3, GL_BYTE, 0, vertices );
00550 
00551     /* Check whether or not the first box was selected.
00552        If it was, draw it completely white, otherwise with filled polygons */
00553     glEnableClientState( GL_COLOR_ARRAY ); // enable color array
00554     glColor4f( 1.f, 1.f, 1.f, 1.f );       // set color to white
00555     if ( iSelectionIdx == BOX_ALPHA_ONE )
00556         {
00557         glDisableClientState( GL_COLOR_ARRAY ); // disable color array
00558         }
00559 
00560     // draw box1
00561     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorsBox1 );
00562     glPushMatrix();
00563     glTranslatex( 12 << 16, 0, 0 );
00564     glRotatex( 40 << 16,       1 << 16,    0   , 1 << 16 );
00565     glRotatex( 2*aFrame << 16, 1 << 16,    0   ,    0    );
00566     glRotatex( 2*aFrame << 15,    0   , 1 << 16,    0    );
00567     glRotatex( 2*aFrame << 14,    0   ,    0   , 1 << 16 );
00568     glScalex( 8 << 16, 8 << 16, 8 << 16 );
00569     glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00570     glPopMatrix();    
00571 
00572     /* Check whether or not the second box was selected.
00573        If it was, draw it completely white, otherwise with filled polygons */
00574     glEnableClientState( GL_COLOR_ARRAY ); // enable color array
00575     if ( iSelectionIdx == BOX_ALPHA_TWO )
00576         {
00577         glDisableClientState( GL_COLOR_ARRAY ); // disable color array
00578         }
00579 
00580     // draw box2
00581     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colorsBox2 );
00582     glPushMatrix();
00583     glTranslatex( -12 << 16, 0, 0 );
00584     glRotatex( 2*aFrame << 16, 1 << 16,    0   ,    0    );
00585     glRotatex( 2*aFrame << 15,    0   , 1 << 16,    0    );
00586     glRotatex( 2*aFrame << 14,    0   ,    0   , 1 << 16 );
00587     glScalex( 8 << 16, 8 << 16, 8 << 16 );
00588     glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00589     glPopMatrix();    
00590 
00591     // By default we want to use color arrays. 
00592     glEnableClientState( GL_COLOR_ARRAY ); // enable color array
00593     }

void CPickSelect::MoveUp (  TBool  move  ) 
 

Sets the move up flag to the given value.

Parameters:
move True if cursor should move upwards each frame. False if cursor shouldn't move up.

Definition at line 606 of file PickSelect.cpp.

References iMoveUp.

00607     {
00608     iMoveUp = move;
00609     }

void CPickSelect::MoveDown (  TBool  move  ) 
 

Sets the move down flag to the given value.

Parameters:
move True if cursor should move downwards each frame. False if cursor shouldn't move down.

Definition at line 615 of file PickSelect.cpp.

References iMoveDown.

00616     {
00617     iMoveDown = move;
00618     }

void CPickSelect::MoveLeft (  TBool  move  ) 
 

Sets the move left flag to the given value.

Parameters:
move True if cursor should move left each frame. False if cursor shouldn't move left.

Definition at line 624 of file PickSelect.cpp.

References iMoveLeft.

00625     {
00626     iMoveLeft = move;
00627     }

void CPickSelect::MoveRight (  TBool  move  ) 
 

Sets the move right flag to the given value.

Parameters:
move True if cursor should move right each frame. False if cursor shouldn't move right.

Definition at line 633 of file PickSelect.cpp.

References iMoveRight.

00634     {
00635     iMoveRight = move;
00636     }

void CPickSelect::SelectObject (  void   ) 
 

Tries to select the object currently under the cursor. Uses the cursor position to get the current pixel's alpha value and stores it to iSelectionIdx. The iSelectionIdx is then used to check if a polygon or an object is chosen.

Definition at line 643 of file PickSelect.cpp.

References iCursorPosX, iCursorPosY, and iSelectionIdx.

00644     {
00645     /* Read the cursor's position pixel value using glReadPixels. Then get the 
00646        3rd byte value, which is the alpha value of the pixel, and store it to 
00647        iSelectionIdx that is used to check if a polygon or an object is chosen.*/
00648     GLuint aPixel;
00649     // At the moment GL_RGBA/GL_UNSIGNED_BYTE is the only supported combo. 
00650     glReadPixels( iCursorPosX, iCursorPosY, 1, 1, GL_RGBA, 
00651                   GL_UNSIGNED_BYTE, &aPixel );
00652 
00653     iSelectionIdx = aPixel >> 24; // get the alpha value of the pixel
00654     }

void CPickSelect::SetPolygonSelection (  void   ) 
 

Changes selection mode to polygon selection.

Definition at line 660 of file PickSelect.cpp.

References iSelectionIdx, and iSelectionMode.

00661     {
00662     iSelectionMode = POLYGON_SELECTION;
00663     iSelectionIdx = 0;
00664     }

void CPickSelect::SetObjectSelection (  void   ) 
 

Changes selection mode to object selection.

Definition at line 670 of file PickSelect.cpp.

References iSelectionIdx, and iSelectionMode.

00671     {
00672     iSelectionMode = OBJECT_SELECTION;
00673     iSelectionIdx = 0;
00674     }

void CPickSelect::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 681 of file PickSelect.cpp.

References iCursorPosX, iCursorPosY, iScreenHeight, and iScreenWidth.

00682     {
00683     iScreenWidth  = aWidth;
00684     iScreenHeight = aHeight;
00685 
00686     // Reinitialize viewport and projection.
00687     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00688 
00689     // Recalculate aspect ratio
00690     if ( iScreenHeight != 0 )
00691         {
00692         iAspectRatio = (GLfloat)iScreenWidth / (GLfloat)iScreenHeight;
00693         }
00694     else
00695         {
00696         iAspectRatio = 1.0;
00697         }
00698 
00699     // Make sure cursor stays on the screen after resolution changes
00700     if ( iCursorPosX > iScreenHeight )
00701         {
00702         iCursorPosX = iScreenHeight;
00703         }
00704     if ( iCursorPosY > iScreenWidth )
00705         {
00706         iCursorPosY = iScreenWidth;
00707         }
00708     }

void CPickSelect::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 293 of file PickSelect.cpp.

00294     {
00295     }


Member Data Documentation

TUint CPickSelect::iScreenWidth [private]
 

Width of the screen

Definition at line 166 of file PickSelect.h.

TUint CPickSelect::iScreenHeight [private]
 

Height of the screen

Definition at line 169 of file PickSelect.h.

TInt CPickSelect::iSelectionMode [private]
 

Current selection mode (either POLYGON_SELECTION or OBJECT_SELECTION).

Definition at line 172 of file PickSelect.h.

TBool CPickSelect::iMoveUp [private]
 

Whether cursor should move up every frame or not.

Definition at line 175 of file PickSelect.h.

TBool CPickSelect::iMoveDown [private]
 

Whether cursor should move down every frame or not.

Definition at line 178 of file PickSelect.h.

TBool CPickSelect::iMoveLeft [private]
 

Whether cursor should move left every frame or not.

Definition at line 181 of file PickSelect.h.

TBool CPickSelect::iMoveRight [private]
 

Whether cursor should move right every frame or not.

Definition at line 184 of file PickSelect.h.

TInt CPickSelect::iSelectionIdx [private]
 

Selection ID.

Definition at line 187 of file PickSelect.h.

GLfloat CPickSelect::iCubeRotX [private]
 

Current X axis rotation of the cube(s).

Definition at line 190 of file PickSelect.h.

GLfloat CPickSelect::iCubeRotY [private]
 

Current Y axis rotation of the cube(s).

Definition at line 193 of file PickSelect.h.

GLint CPickSelect::iCursorPosX [private]
 

Current X coordinate of the cursor.

Definition at line 196 of file PickSelect.h.

GLint CPickSelect::iCursorPosY [private]
 

Current Y coordinate of the cursor.

Definition at line 199 of file PickSelect.h.

GLbyte CPickSelect::iSelectedPoly[3] [private]
 

Currently selected pelected polygon.

Definition at line 203 of file PickSelect.h.

GLfloat CPickSelect::iAspectRatio [private]
 

Current screen aspect ratio (width/height).

Definition at line 206 of file PickSelect.h.


© Nokia 2006

Back to top