Code:
#include "MobileContainer.h"
#include <fbs.h>
#include <eiklabel.h> // for example label control
_LIT(KTextureBitmap,"z:\\system\\apps\\MobileMail\\MobileMail.mbm");
/* Macro for changing the input texture coordinate values from
GLubyte [0,255] to GLbyte [-128,127]. See more info below. */
#define tex(u,v) (GLbyte)( (u) - 128 ) , (GLbyte)( (v) - 128 )
// ================= MEMBER FUNCTIONS =======================
/* Define vertice coordinates for the cube containing square*/
static const GLbyte vertices[4 * 3] = //row major
{
-1, 0, 0,
1, 0, 0,
-1, 2, 0,
1, 2, 0
};
/* Define indices for drawing the triangles. The color of the square
* is determined by the color of the last vertex of the square.*/
static const GLubyte triangles[2* 3] =
{
/* up */
0,1,2,
/*down*/
1,2,3
};
static const GLbyte nokTexCoords[4 * 2] =
{
/**** */
tex(0,0),
tex(255,0),
tex(255,255),
tex(0,255)
};
// ---------------------------------------------------------
// CMobileContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CMobileContainer::ConstructL(const TRect& /*aRect*/)
{
CreateWindowL();
SetExtentToWholeScreen(); // Take the whole screen into use
ActivateL();
// iScreenSize=aRect.Size();
/* Get the display for drawing graphics */
iEglDisplay = eglGetDisplay( EGL_DEFAULT_DISPLAY );
/* Initialize display */
eglInitialize( iEglDisplay, NULL, NULL );
EGLConfig config; // config describing properties of EGLSurface
EGLConfig *configList = NULL; // pointer for EGLConfigs
int configSize = 0; // num of configs we want EGL to
// return
int numOfConfigs = 0; // num of configs actually returned
/* Get the number of all possible EGLConfigs */
eglGetConfigs( iEglDisplay, configList, configSize, &numOfConfigs );
configSize = numOfConfigs;
/* Allocate memory for configList */
configList = (EGLConfig*)User::Alloc( sizeof(EGLConfig)*configSize );
/* Define properties for the wanted EGLSurface.
To get the best possible performance, choose
an EGLConfig with a buffer size matching
the current window's display mode*/
TDisplayMode DMode = Window().DisplayMode();
TInt BufferSize = 0;
switch(DMode)
{
case(EColor4K):
BufferSize = 12;
break;
case(EColor64K):
BufferSize = 16;
break;
case(EColor16M):
BufferSize = 24;
break;
case(EColor16MU):
BufferSize = 32;
break;
default:
_LIT(KDModeError, "unsupported displaymode");
User::Panic( KDModeError, 0 );
break;
}
const EGLint attrib_list[] = { EGL_BUFFER_SIZE,BufferSize,
EGL_DEPTH_SIZE,16,
EGL_NONE };
/* Choose configs that fulfill the requirement in attrib_list */
eglChooseConfig( iEglDisplay, attrib_list, configList, configSize,&numOfConfigs );
/* Choose the ‘best’ config and use that in the future */
config = configList[0]; // Choose the best EGLConfig. EGLConfigs
// returned by eglChooseConfig are sorted so
// that the best matching EGLconfig is first in
// the list.
User::Free( configList ); // free configList, not used anymore
/* Create a surface where the graphics are blitted */
iEglSurface = eglCreateWindowSurface( iEglDisplay, config, &Window(),NULL );
/* Create a rendering context */
iEglContext = eglCreateContext( iEglDisplay, config, NULL, NULL );
/* Make the context current. Binds to the current rendering thread and surface. Use the same surface for both drawing and reading */
eglMakeCurrent( iEglDisplay, iEglSurface, iEglSurface, iEglContext );
}
// Destructor
CMobileContainer::~CMobileContainer()
{
eglMakeCurrent( iEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT );
eglDestroySurface( iEglDisplay, iEglSurface );
eglDestroyContext( iEglDisplay, iEglContext );
eglTerminate( iEglDisplay ); // release resources associated with EGL
// & OpenGL ES
}
// ---------------------------------------------------------
// CMobileContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CMobileContainer::SizeChanged()
{
}
// ---------------------------------------------------------
// CMobileContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CMobileContainer::CountComponentControls() const
{
return 0; // return nbr of controls inside this container
}
// ---------------------------------------------------------
// CMobileContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CMobileContainer::ComponentControl(TInt /*aIndex*/) const
{
return NULL;
}
// ---------------------------------------------------------
// CMobileContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CMobileContainer::GenSkin(const TRect& /*aRect*/) const
{
GLuint texture;
CFbsBitmap* TextureBitmap= new (ELeave)CFbsBitmap;
TBool wrap=EFalse;
TextureBitmap->Load(KTextureBitmap,0,ETrue);
#define TEX_WIDTH 180
#define TEX_HEIGHT 240//320
// allocate a texture name
glGenTextures( 2, &texture );
/***Enable Texturing*/
glEnable( GL_TEXTURE_2D );
/***an appropriate texture matrix has to be initialized*/
glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glScalef( 1.f / 255.f, 1.f / 255.f, 1.f );
glTranslatef( 128.f, 128.f, 0.f );
glMatrixMode( GL_MODELVIEW );
/********texture coordinate array should then be enabled and
* the texture coordinate pointer set****/
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glTexCoordPointer( 2, GL_BYTE, 0, nokTexCoords );
// glGenTextures( 2, iNokTexObjects);
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, TEX_WIDTH, TEX_HEIGHT,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureBitmap->DataAddress() );
/**************Texture environment parameters are set using glTexEnvf().*/
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,wrap ? GL_REPEAT : GL_CLAMP_TO_EDGE );
/****************For skinning effect***************/
// glEnable( GL_BLEND );
// glBlendFunc( GL_ONE, GL_ONE );
/********************************/
glDrawElements( GL_TRIANGLES, 10 * 3, GL_UNSIGNED_BYTE, triangles );
}
void CMobileContainer::Draw(const TRect& aRect) const
{
/**************************Set the correct step of OpenGl renderer*/
/* Set the screen background color to black */
glClearColor( 0.f, 0.f, 0.f, 1.f );
/* Initialize viewport */
glViewport( 0, 0, aRect.Width(), aRect.Height());
//**************draw the object to made it translucent
/* Set the projection matrix */
glMatrixMode( GL_PROJECTION );
glFrustumf( -1.f, 1.f, -1.f, 1.f, 3.f, 1000.f );
TRAPD(err,GenSkin(aRect);)
if(err!=KErrNone) //My code exiting here
User::Panic(_L("Panic due to"),err);
// GenSelector(aRect);
/* Call eglSwapBuffers, which blit the graphics to the window */
eglSwapBuffers( iEglDisplay, iEglSurface );
}
void CMobileContainer::GenSelector(const TRect& /*aRect*/) const
{
/************************Codes to make the next object translucent **/
//draw the background
glEnable(GL_BLEND); //activate blending mode
glBlendFunc(GL_SRC_ALPHA/*GL_ONE*/, GL_ONE); //define blending factors
glMatrixMode( GL_MODELVIEW );
/* Enable vertex array */
glEnableClientState( GL_VERTEX_ARRAY );
/* Set vertex array pointer */
glVertexPointer( 3, GL_BYTE, 0, vertices );
/* Set the shading model */
glShadeModel( GL_FLAT );
/*********************draw the actual scene***************/
/* Clear the color buffer */
glClear( GL_COLOR_BUFFER_BIT );
/* Move the camera, MatrixMode == GL_MODELVIEW */
glLoadIdentity();
glTranslatex(0, 0, -100 << 16 ); //or glTranslatef(0.0, 0.0, -100.0)
/* Set current color for the triangle (red) */
glColor4f( 1.f, 0.f, 0.f, 0.25f );//RGBA
/* Scale the geometry */
glScalex( 15 << 16, 15 << 16, 15 << 16 ); // or glScalef( 15.0, 15.0,
// 15.0 )
// /* Draw the triangle */
// glDrawElements( GL_TRIANGLES, 1* 3, GL_UNSIGNED_BYTE, triangles );
/* Draw the square */
glDrawElements( GL_TRIANGLES, 2* 3, GL_UNSIGNED_BYTE, triangles );
/* Call eglSwapBuffers, which blit the graphics to the window */
}
// ---------------------------------------------------------
// CMobileContainer::HandleControlEventL(
// CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CMobileContainer::HandleControlEventL(
CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
{
// TODO: Add your control event handler code here
}