S60 3rd Edition SDK for Symbian OS
Example Applications Guide

CSimpleCube Class Reference

#include <SimpleCube.h>

List of all members.

Public Member Functions

virtual ~CSimpleCube ()
void AppInit (void)
void AppExit (void)
void DrawBox (GLfloat aSizeX, GLfloat aSizeY, GLfloat aSizeZ)
void AppCycle (TInt aFrame)
void FlatShading (void)
void SmoothShading (void)
void TriangleMode (void)
void TriangleFanMode (void)
void SetScreenSize (TUint aWidth, TUint aHeight)

Static Public Member Functions

static CSimpleCube * NewL (TUint aWidth, TUint aHeight)

Protected Member Functions

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

Private Attributes

TUint iScreenWidth
TUint iScreenHeight
TRenderingMode iDrawingMode


Detailed Description

Class that does the actual OpenGL ES rendering.

Definition at line 40 of file SimpleCube.h.


Constructor & Destructor Documentation

CSimpleCube::~CSimpleCube (   )  [virtual]
 

Destructor. Does nothing.

Definition at line 152 of file SimpleCube.cpp.

00153     {
00154     }

CSimpleCube::CSimpleCube (  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 139 of file SimpleCube.cpp.

00139                                                       : 
00140     iScreenWidth( aWidth ),
00141     iScreenHeight( aHeight )
00142     {
00143     }


Member Function Documentation

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

Factory method for creating a new CSimpleCube object.

Definition at line 120 of file SimpleCube.cpp.

References CSimpleCube().

00121     {
00122     /* Symbian 2-phase constructor. Calls both the default 
00123        C++ constructor and Symbian ConstructL methods */
00124     CSimpleCube* self = new (ELeave) CSimpleCube( aWidth, aHeight );
00125     CleanupStack::PushL( self );
00126     self->ConstructL();
00127     CleanupStack::Pop();
00128 
00129     return self;
00130     }

void CSimpleCube::AppInit (  void   ) 
 

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

Definition at line 175 of file SimpleCube.cpp.

References iDrawingMode, iScreenHeight, and iScreenWidth.

00176     {
00177     // Set the screen background color. 
00178     glClearColor( 0.f, 0.f, 0.f, 1.f );
00179 
00180     // Enable back face culling. 
00181     glEnable( GL_CULL_FACE  );
00182 
00183     // Initialize viewport and projection. 
00184     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00185     glMatrixMode( GL_PROJECTION );
00186 
00187     // Calculate the view frustrum
00188     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00189     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00190                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00191                 FRUSTUM_NEAR, FRUSTUM_FAR );
00192 
00193     glMatrixMode( GL_MODELVIEW );
00194 
00195     // Enable vertex arrays. 
00196     glEnableClientState( GL_VERTEX_ARRAY );
00197 
00198     // Set array pointers. 
00199     glVertexPointer( 3, GL_BYTE, 0, vertices );
00200 
00201     // Enable color arrays. 
00202     glEnableClientState( GL_COLOR_ARRAY );
00203 
00204     // Set color pointers. 
00205     glColorPointer( 4, GL_UNSIGNED_BYTE, 0, colors );
00206 
00207     // Set the initial shading mode 
00208     glShadeModel( GL_FLAT ); 
00209 
00210     // Do not use perspective correction 
00211     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
00212 
00213     // Set the initial drawing mode 
00214     iDrawingMode = ETriangles;
00215     }

void CSimpleCube::AppExit (  void   ) 
 

Called upon application exit. Does nothing.

Definition at line 224 of file SimpleCube.cpp.

00225     {
00226     }

void CSimpleCube::DrawBox (  GLfloat  aSizeX,
GLfloat  aSizeY,
GLfloat  aSizeZ
) 
 

Draws a 3D box with triangles or triangle fans depending on the current rendering mode.Scales the box to the given size using glScalef.

Parameters:
aSizeX X-size of the box.
aSizeY Y-size of the box.
aSizeZ Z-size of the box.

Definition at line 236 of file SimpleCube.cpp.

References iDrawingMode.

00237     {
00238     glScalef( aSizeX, aSizeY, aSizeZ );
00239 
00240     if ( iDrawingMode == ETriangles )
00241         {
00242         glDrawElements( GL_TRIANGLES, 12 * 3, GL_UNSIGNED_BYTE, triangles );
00243         }
00244     else if ( iDrawingMode == ETriangleFans )
00245         {
00246         glDrawElements( GL_TRIANGLE_FAN, 6 * 3, GL_UNSIGNED_BYTE, fanOne );
00247         glDrawElements( GL_TRIANGLE_FAN, 6 * 3, GL_UNSIGNED_BYTE, fanTwo );
00248         }
00249     }

void CSimpleCube::AppCycle (  TInt  aFrame  ) 
 

Renders one frame.

Parameters:
aFrame Number of the frame to be rendered.

Definition at line 259 of file SimpleCube.cpp.

References DrawBox().

00260     {
00261     const GLint cameraDistance = 100;
00262 
00263     glClear( GL_COLOR_BUFFER_BIT );
00264 
00265     /* Animate and draw box */
00266     glLoadIdentity();
00267     glTranslatex( 0 , 0 , -cameraDistance << 16 );
00268     glRotatex( aFrame << 16, 1 << 16,    0   ,    0    );
00269     glRotatex( aFrame << 15,    0   , 1 << 16,    0    );
00270     glRotatex( aFrame << 14,    0   ,    0   , 1 << 16 );
00271     DrawBox( 15.f, 15.f, 15.f );
00272     }

void CSimpleCube::FlatShading (  void   ) 
 

Sets the shading mode to flat.

Definition at line 286 of file SimpleCube.cpp.

00287     {
00288     glShadeModel( GL_FLAT );
00289     }

void CSimpleCube::SmoothShading (  void   ) 
 

Sets the shading to smooth (gradient).

Definition at line 298 of file SimpleCube.cpp.

00299     {
00300     glShadeModel( GL_SMOOTH );
00301     }

void CSimpleCube::TriangleMode (  void   ) 
 

Sets the rendering to use triangles.

Definition at line 310 of file SimpleCube.cpp.

References iDrawingMode.

00311     {
00312     iDrawingMode = ETriangles;
00313     }

void CSimpleCube::TriangleFanMode (  void   ) 
 

Sets the rendering to use triangle fans.

Definition at line 322 of file SimpleCube.cpp.

References iDrawingMode.

00323     {
00324     iDrawingMode = ETriangleFans;
00325     }

void CSimpleCube::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 332 of file SimpleCube.cpp.

References iScreenHeight, and iScreenWidth.

00333     {
00334     iScreenWidth  = aWidth;
00335     iScreenHeight = aHeight;
00336 
00337     // Reinitialize viewport and projection.
00338     glViewport( 0, 0, iScreenWidth, iScreenHeight );
00339 
00340     // Recalculate the view frustrum
00341     glMatrixMode( GL_PROJECTION );
00342     glLoadIdentity();
00343     GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
00344     glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
00345                 FRUSTUM_BOTTOM, FRUSTUM_TOP,
00346                 FRUSTUM_NEAR, FRUSTUM_FAR );
00347     glMatrixMode( GL_MODELVIEW );
00348     }

void CSimpleCube::ConstructL (  void   )  [protected]
 

Second phase contructor. Does nothing.

Definition at line 163 of file SimpleCube.cpp.

00164     {
00165     }


Member Data Documentation

TUint CSimpleCube::iScreenWidth [private]
 

Width of the screen

Definition at line 128 of file SimpleCube.h.

TUint CSimpleCube::iScreenHeight [private]
 

Height of the screen

Definition at line 131 of file SimpleCube.h.

TRenderingMode CSimpleCube::iDrawingMode [private]
 

The current rendering mode

Definition at line 134 of file SimpleCube.h.


© Nokia 2006

Back to top