📄 texture.cpp
字号:
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
/* Set array pointers. */
glVertexPointer( 3, GL_BYTE, 0, vertices );
glTexCoordPointer( 2, GL_BYTE, 0, nokTexCoords );
glNormalPointer( GL_BYTE, 0, normals );
/* Set up global ambient light. */
glLightModelxv( GL_LIGHT_MODEL_AMBIENT, globalAmbient );
/* Set up lamp. */
glEnable( GL_LIGHT0 );
glLightxv( GL_LIGHT0, GL_DIFFUSE, lightDiffuseLamp );
glLightxv( GL_LIGHT0, GL_AMBIENT, lightAmbientLamp );
glLightxv( GL_LIGHT0, GL_SPECULAR, lightSpecularLamp );
glLightxv( GL_LIGHT0, GL_POSITION, lightPositionLamp );
/* Set shading mode*/
glShadeModel( GL_FLAT );
/* Set the blending function for translucency. */
glBlendFunc( GL_ONE, GL_ONE );
/* Do not use perspective correction, can be enabled from the menu */
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
/* Symbian menu item initialization. See CTextureAppUi.cpp for more. */
iPerspectiveCorrEnabled = EFalse; // no texture perspective correction
iTextureWrapEnabled = EFalse; // no texture wrapping
iLightingEnabled = EFalse; // no lighting
iBlendingEnabled = EFalse; // no blending
// Pushing the textures into the loading queue.
_LIT(KIcons1, "icons1.jpg");
_LIT(KIcons2, "icons2.jpg");
_LIT(KIcons3, "icons3.jpg");
_LIT(KIcons4, "icons4.jpg");
iTextureManager->RequestToLoad(KIcons1, &iIcons1 );
iTextureManager->RequestToLoad(KIcons2, &iIcons2 );
iTextureManager->RequestToLoad(KIcons3, &iIcons3 );
iTextureManager->RequestToLoad(KIcons4, &iIcons4 );
//Start to load the textures.
TRAPD( err, iTextureManager->DoLoadL() );
if ( err != KErrNone )
{
_LIT(KImagesMissing, "Images missing");
User::Panic( KImagesMissing, 0 );
}
}
// -----------------------------------------------------------------------------
// CTexture::AppExit
// Relase any allocations made in AppInit
// -----------------------------------------------------------------------------
//
void CTexture::AppExit( void )
{
delete iTextureManager;
}
// -----------------------------------------------------------------------------
// CTexture::AppCycle
// OpenGL ES main rendering loop.
// -----------------------------------------------------------------------------
//
void CTexture::AppCycle( TInt aFrame, TRotationDirection aDirection )
{
glClear( GL_COLOR_BUFFER_BIT );
/* Animate and draw the cube. */
glLoadIdentity();
gluLookAt( 0.f, 0.f, 120.f, // Camera position
0.f, 0.f, 0.f, // Look at point
0.f, 1.f, 0.f ); // Up direction
switch (aDirection)
{
case ERotationRight: glRotatex( aFrame << 16, 0 , 1 << 16, 0 ); break;
case ERotationLeft: glRotatex( aFrame << 16, 0 , 1 << 16, 0 ); break;
};
/* Scale the cube coordinates to fit the screen. */
glScalex( 16 << 16, 16 << 16, 16 << 16 );
glTranslatex(0, 1<<16,0);
/* Draw the remaining side of the cube with the ogles.jpg texture. */
glBindTexture( GL_TEXTURE_2D, iIcons2.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles2 );
glBindTexture( GL_TEXTURE_2D, iIcons3.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles3 );
glBindTexture( GL_TEXTURE_2D, iIcons2.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles4 );
glBindTexture( GL_TEXTURE_2D, iIcons3.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles5 );
glTranslatex(0, -2<<16,0);
/* Draw the remaining side of the cube with the ogles.jpg texture. */
glBindTexture( GL_TEXTURE_2D, iIcons1.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles2 );
glBindTexture( GL_TEXTURE_2D, iIcons4.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles3 );
glBindTexture( GL_TEXTURE_2D, iIcons1.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles4 );
glBindTexture( GL_TEXTURE_2D, iIcons4.iID );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, triangles5 );
}
// -------------------------------------------------------------------------------------------------------
// CBillboard::OnStartLoadingTextures()
// Called for a MTextureLoadingListener by the texture manager when texture loading operation starts
// -------------------------------------------------------------------------------------------------------
void CTexture::OnStartLoadingTexturesL()
{
SetStateL( ELoadingTextures );
}
// ------------------------------------------------------------------------------------------------------------
// CBillboard::OnEndLoadingTextures()
// Called for a MTextureLoadingListener by the texture manager when texture loading operation is completed
// ------------------------------------------------------------------------------------------------------------
void CTexture::OnEndLoadingTexturesL()
{
if ( GetState() == ELoadingTextures )
{
SetStateL( ERunning );
AppCycle( 0, ERotationRight );
}
}
// ---------------------------------------------------------
// CBillboard::OnEnterState( TInt aState )
// Called by TFiniteStateMachine when the f.s.m enters a new state
// ---------------------------------------------------------
void CTexture::OnEnterStateL( TInt /*aState*/ )
{
// Nothing to do here...
}
//----------------------------------------------------------
// The following methods are called by the CTextureAppUi
// class when handling the incoming menu events.
//----------------------------------------------------------
// -----------------------------------------------------------------------------
// CTexture::TogglePerspective
// User defines whether or not to use perspective correction when rendering
// -----------------------------------------------------------------------------
//
void CTexture::TogglePerspective( void )
{
if( iPerspectiveCorrEnabled)
{
iPerspectiveCorrEnabled = EFalse;
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST );
}
else
{
iPerspectiveCorrEnabled = ETrue;
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
}
}
/**
* Enable/Disable texture wrapping.
*/
// -----------------------------------------------------------------------------
// CTexture::ToggleWrapping
// Implement texture wrapping by changing the texture matrix scale
// -----------------------------------------------------------------------------
//
void CTexture::ToggleWrapping( void )
{
if( iTextureWrapEnabled )
{
iTextureWrapEnabled = EFalse;
glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glScalef( 1.0f/255.0f, 1.0f/255.0f, 1.0f );
glTranslatef( 128.0f, 128.0f, 1.0f );
glMatrixMode( GL_MODELVIEW );
}
/* Texture wrapping is implemented here simply by changing the scale of
texture matrix. Now the coordinate values are between [0,2], which
causes the texture to wrap one time. */
else
{
iTextureWrapEnabled = ETrue;
glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glScalef( 1.0f/128.0f, 1.0f/128.0f, 1.0f );
glTranslatef( 128.0f, 128.0f, 1.0f );
glMatrixMode( GL_MODELVIEW );
}
}
/**
* Enable/Disable lighting.
*/
// -----------------------------------------------------------------------------
// CTexture::ToggleLighting
// When using textures with lights the texenv must be set to GL_MODULATE,
// and shade model must be GL_SMOOTH.
// -----------------------------------------------------------------------------
//
void CTexture::ToggleLighting( void )
{
// If texture loading is still going on, return from this method without doing anything.
if ( GetState() == ELoadingTextures )
{
return;
}
if( iLightingEnabled )
{
iLightingEnabled = EFalse;
glShadeModel( GL_FLAT ); // Don't need smooth shading when no lights
glDisable( GL_LIGHTING ); // Disable lighting
/* Set both textures environments to GL_REPLACE. */
glBindTexture( GL_TEXTURE_2D, iIcons1.iID );
glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glBindTexture( GL_TEXTURE_2D, iIcons2.iID );
glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
}
else
{
iLightingEnabled = ETrue;
glShadeModel( GL_SMOOTH ); // For the lighting to work properly
glEnable( GL_LIGHTING ); // Enable lighting
/* Set both textures environments to GL_MODULATE. */
glBindTexture( GL_TEXTURE_2D, iIcons1.iID );
glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glBindTexture( GL_TEXTURE_2D, iIcons2.iID );
glTexEnvx( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
}
}
// -----------------------------------------------------------------------------
// CTexture::ToggleBlending
// User defines whether or not blending is enabled from the menu.
// -----------------------------------------------------------------------------
//
void CTexture::ToggleBlending( void )
{
if( iBlendingEnabled )
{
// Blending was enabled, disable blending
iBlendingEnabled = EFalse;
glDisable( GL_BLEND );
glEnable( GL_CULL_FACE );
}
else
{
// Blending was disabled, enable blending
iBlendingEnabled = ETrue;
glEnable( GL_BLEND );
glDisable( GL_CULL_FACE );
}
}
// -----------------------------------------------------------------------------
// CTexture::SetScreenSize
// Reacts to the dynamic screen size change during execution of this program.
// -----------------------------------------------------------------------------
//
void CTexture::SetScreenSize( TUint aWidth, TUint aHeight )
{
iScreenWidth = aWidth;
iScreenHeight = aHeight;
// Notify the texture manager of screen size change
iTextureManager->SetScreenSize( aWidth, aHeight );
// Reinitialize viewport and projection.
glViewport( 0, 0, iScreenWidth, iScreenHeight );
// Recalculate the view frustrum
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight);
glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio,
FRUSTUM_BOTTOM, FRUSTUM_TOP,
FRUSTUM_NEAR, FRUSTUM_FAR );
glMatrixMode( GL_MODELVIEW );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -