📄 snow.cpp
字号:
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
}
// -----------------------------------------------------------------------------
// CSnow::DrawCloud
// Draws the clouds.
// -----------------------------------------------------------------------------
//
void CSnow::DrawCloud()
{
iTextureOffset += (GLfloat)(0.01*iElapsedTime);
iCloudTextureCoords[0] = iCloudTextureCoords[2] = iTextureOffset;
iCloudTextureCoords[4] = iCloudTextureCoords[6] = iTextureOffset + 0.4f;
glDisable( GL_DEPTH_TEST );
glLoadIdentity();
glBindTexture( GL_TEXTURE_2D, iCloudTexture.iID );
glVertexPointer( 3, GL_FLOAT, 0, cloudVertices );
glTexCoordPointer( 2, GL_FLOAT, 0, iCloudTextureCoords );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, cloudTriangles );
glEnable( GL_DEPTH_TEST );
}
// -----------------------------------------------------------------------------
// CSnow::DrawTree
// Draws a tree.
// -----------------------------------------------------------------------------
//
void CSnow::DrawTree( GLfloat aX, GLfloat aZ)
{
glLoadIdentity();
//Turns the tree so that it always faces the camera.
T3DModel::MakeBillboardWorldViewMatrix(iCamera, TVector(aX, 0.0f, aZ));
glScalef( 10, 30, 10 );
glBindTexture( GL_TEXTURE_2D, iTreeTexture.iID );
glVertexPointer( 3, GL_BYTE, 0, treeVertices );
glTexCoordPointer( 2, GL_BYTE, 0, treeTextureCoords );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, treeTriangles );
}
// -----------------------------------------------------------------------------
// CSnow::AppCycle
// Draws and animates the objects.
// -----------------------------------------------------------------------------
//
void CSnow::AppCycle( TInt /*aFrame*/ )
{
glClear( GL_DEPTH_BUFFER_BIT );
iT0 = iT1;
do
{
iT1 = GetTimeTick();
//Compute the elapsed time (in sec.) between the current and the previous frame.
iElapsedTime = (iT1.MicroSecondsFrom(iT0)).Int64().Low() * (1/1000000.0f);
} while ( iElapsedTime == 0 );
iFPSCount = 1 / (iElapsedTime); //Compute the frame rate.
/* Draws the clouds, and the ground plane */
glDisable( GL_DEPTH_TEST );
DrawCloud();
DrawPlane();
const TReal32 treeStepX = 30.0f;
const TReal32 treeStepZ = 100.0f;
//Draw the trees.
DrawTree( -treeStepX, -1*treeStepZ);
DrawTree( +treeStepX, -1*treeStepZ);
DrawTree( -treeStepX, -2*treeStepZ);
DrawTree( +treeStepX, -2*treeStepZ);
glEnable( GL_DEPTH_TEST );
/*Update and render the particle engine */
iSnowfall->UpdateEngine(iElapsedTime);
iSnowfall->RenderEngine(iCamera);
}
// ----------------------------------------------------------------------
// CSnow::CSnow
// C++ default constructor can NOT contain any code, that
// might leave.
// ----------------------------------------------------------------------
//
CSnow::CSnow( TUint aWidth, TUint aHeight )
: CFiniteStateMachine()
{
iScreenWidth = aWidth;
iScreenHeight = aHeight;
}
// ----------------------------------------------------------------------
// CSnow::ConstructL
// Symbian 2nd phase constructor can leave.
// ----------------------------------------------------------------------
//
void CSnow::ConstructL( void )
{
}
// ---------------------------------------------------------
// CSnow::OnEnterState( TInt aState )
// Called by TFiniteStateMachine when the f.s.m enters a new state
// ---------------------------------------------------------
void CSnow::OnEnterStateL( TInt aState )
{
if ( aState == ERunning )
{
iT1 = GetTimeTick();
}
}
// -------------------------------------------------------------------------------------------------------
// CSnow::OnStartLoadingTextures()
// Called for a MTextureLoadingListener by the texture manager when texture loading operation starts
// -------------------------------------------------------------------------------------------------------
void CSnow::OnStartLoadingTexturesL()
{
SetStateL( ELoadingTextures );
}
// ------------------------------------------------------------------------------------------------------------
// CSnow::OnEndLoadingTextures()
// Called for a MTextureLoadingListener by the texture manager when texture loading operation is completed
// ------------------------------------------------------------------------------------------------------------
void CSnow::OnEndLoadingTexturesL()
{
if ( GetState() == ELoadingTextures )
{
SetStateL( ERunning );
}
}
// ----------------------------------------------------------------------
// CSnow::GetTimeTick
// Get the current time
// Returns: The current time.
// ----------------------------------------------------------------------
//
TTime CSnow::GetTimeTick()
{
TTime time;
time.UniversalTime();
return time;
}
// ----------------------------------------------------------------------
// CSnowfall::CSnowfall
// C++ default constructor can NOT contain any code, that
// ----------------------------------------------------------------------
//
CSnowfall::CSnowfall()
{
};
// ----------------------------------------------------------------------
// CSnowfall::NewL
// 2nd Two-phased constructor.
// Creates and initializes a CSnowfall object:
// snowflakes are released from within
// the rectangle (aPosition.iX-aWidth/2, aPosition.iY, aPosition.iX-aWidth/2)
// (aPosition.iX+aWidth/2, aPosition.iY, aPosition.iX+aWidth/2).
// Once their height is < iGroundLevel, they are reset.// Returns: The current time.
// ----------------------------------------------------------------------
//
CSnowfall* CSnowfall::NewL(GLint aParticlesCount, TVector aPosition,
GLfloat aWidth, GLfloat aDepth, GLfloat aGroundLevel,
CTextureManager * aTextureManager)
{
/* Symbian 2-phase constructor. Calls both the default
C++ constructor and Symbian ConstructL methods */
CSnowfall* self = new (ELeave) CSnowfall;
CleanupStack::PushL( self );
self->ConstructL( aParticlesCount, aPosition, aWidth, aDepth, aGroundLevel, aTextureManager);
CleanupStack::Pop();
return self;
}
// ----------------------------------------------------------------------
// CSnowfall::ConstructL
// 2nd phase constructor
// Initializes a CSnowfall object:
// snowflakes are released from within
// the rectangle (aPosition.iX-aWidth/2, aPosition.iY, aPosition.iX-aWidth/2)
// (aPosition.iX+aWidth/2, aPosition.iY, aPosition.iX+aWidth/2).
// Once their height is < iGroundLevel, they are reset.
// ----------------------------------------------------------------------
//
void CSnowfall::ConstructL(GLint aParticlesCount, TVector aPosition,
GLfloat aWidth, GLfloat aDepth, GLfloat aGroundLevel,
CTextureManager * aTextureManager)
{
CParticleEngine::ConstructL(aParticlesCount, aPosition);
iWidth = aWidth;
iDepth = aDepth;
iGroundLevel = aGroundLevel;
TTime now;
now.HomeTime();
iSeed = now.Int64();
ResetEngine();
_LIT(KSnowName, "snow.jpg");
aTextureManager->RequestToLoad(KSnowName, &iTexture );
}
// Destructor.
CSnowfall::~CSnowfall()
{
}
// ----------------------------------------------------------------------
// CSnowfall::ResetParticle
// Implementation of CParticleEngine::ResetParticle(GLint)
// ----------------------------------------------------------------------
//
void CSnowfall::ResetParticle(GLint aIndex)
{
TParticle& Particle = iParticles[aIndex];
Particle.iPosition = TVector( iWidth * ((float)(Math::FRand(iSeed)) - 0.5f),
0,
iDepth * ((float)(Math::FRand(iSeed)) - 0.5f)) + iPosition;
Particle.iVelocity = TVector( (float)(Math::FRand(iSeed)) * 4.0f - 2.0f,
-((float)(Math::FRand(iSeed)) * 20.0f + 20.0f),
(float)(Math::FRand(iSeed)) * 4.0f - 2.0f);
}
// ----------------------------------------------------------------------
// CSnowfall::UpdateEngine
// Implementation of CParticleEngine::UpdateEngine(GLfloat aElapsedTime)
// ----------------------------------------------------------------------
//
void CSnowfall::UpdateEngine(GLfloat aElapsedTime)
{
for ( GLint i = 0; i < iParticlesCount; i++ )
{
iParticles[i].iPosition += (iParticles[i].iVelocity * aElapsedTime);
if ( iParticles[i].iPosition.iY < iGroundLevel )
{
ResetParticle(i);
}
}
}
// ----------------------------------------------------------------------
// CSnowfall::RenderEngine
// Implementation of CParticleEngine::RenderEngine(TCamera &)
// ----------------------------------------------------------------------
//
void CSnowfall::RenderEngine(TCamera& aCamera)
{
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
TVector CameraPos = aCamera.GetPosition();
glBindTexture( GL_TEXTURE_2D, iTexture.iID );
glVertexPointer( 3, GL_BYTE, 0, snowVertices );
glTexCoordPointer( 2, GL_BYTE, 0, snowTextureCoords );
for ( GLint i = 0; i < iParticlesCount; i++ )
{
glLoadIdentity();
glTranslatef(iParticles[i].iPosition.iX-CameraPos.iX,
iParticles[i].iPosition.iY-CameraPos.iY,
iParticles[i].iPosition.iZ-CameraPos.iZ);
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, snowTriangles );
}
glDisable(GL_BLEND);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -