PDA

View Full Version : Texturing and Texturing (newbie)



Sudeepa
12-16-2008, 02:36 AM
Hi all, I am trying to load a texture my screen using Open GLES and here are my codes . Well , it is crashing ,I may be getting my graphics code wrong .Any sujjestions ?


_LIT(KTextureBitmap,"z:\\system\\apps\\Mobile\\Mobile.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 )
static const GLbyte skinvertices[4 *2] =
{
/* top */
-1, -1,
1, -1 ,
1, 1,
-1, 1
};
/* 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)
};



GLuint CMobileContainer::BindTextures(const TRect& aRect,TBool aWrap,CFbsBitmap& aTextureBitmap) const
{
GLuint texture;
// allocate a texture name
glGenTextures( 2, &texture );
// select our current texture
// glBindTexture( GL_TEXTURE_2D, texture );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, aRect.Width(), aRect.Height(),
0, GL_RGB, GL_UNSIGNED_BYTE, aTextureBitmap.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,aWrap ? GL_REPEAT : GL_CLAMP_TO_EDGE );

return texture;

}



void CMobileContainer::GenSkin(const TRect& aRect) const
{

CFbsBitmap* TextureBitmap= new (ELeave)CFbsBitmap;
TBool wrap=EFalse;

TextureBitmap->Load(KTextureBitmap,0,ETrue);
/***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 );
glBindTexture( GL_TEXTURE_2D, BindTextures(aRect,wrap,*TextureBitmap) );
glVertexPointer( 3, GL_SHORT, 0, skinvertices );
glTexCoordPointer( 2, GL_BYTE, 0, nokTexCoords );
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)
User::Panic(_L("Panic due to"),err);
// GenSelector(aRect);
/* Call eglSwapBuffers, which blit the graphics to the window */
eglSwapBuffers( iEglDisplay, iEglSurface );
}

_NK47
12-16-2008, 02:48 AM
alot of code. you have at leats an idea where it crashes?
more likely on: glDrawElements( GL_TRIANGLES, 10 * 3, GL_UNSIGNED_BYTE, triangles );

i assume you set glEnableClientState( GL_VERTEX_ARRAY ) somewhere else because you need to set it before drawing. second, what is triangles and where does it come from?

Sudeepa
12-16-2008, 03:35 AM
Hi NK47 I changed my code to this here . But still no change .
:( .

void CMobileEMailContainer::GenSkin(const TRect& aRect) const
{

CFbsBitmap* TextureBitmap= new (ELeave)CFbsBitmap;
TBool wrap=EFalse;

TextureBitmap->Load(KTextureBitmap,0,ETrue);
/***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 );
glBindTexture( GL_TEXTURE_2D, BindTextures(aRect,wrap,*TextureBitmap) );

/* Enable vertex array */
// glMatrixMode( GL_MODELVIEW );
glEnableClientState( GL_VERTEX_ARRAY );
glVertexPointer( 2, GL_SHORT, 0, skinvertices );//no of cocordinates per vertex,type,byte offset between consecutive vertexes,first vertex in array
glTexCoordPointer( 2, GL_BYTE, 0, nokTexCoords );
glShadeModel( GL_FLAT );
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glDrawElements( GL_TRIANGLES, 2* 3, GL_UNSIGNED_BYTE, triangles );
// glDeleteTextures(2, &texture);
}

_NK47
12-16-2008, 07:05 AM
Not familiar with ES. why is vertex pointer showing to GL_SHORT values and texture pointer to GL_BYTE?
first do a trivial check to see if your memory pointers are not zero on glVertexPointer, glTexCoordPointer and glDrawElements because its hard to tell where they come from. see glDrawElements (http://opengl.org/documentation/specs/man_pages/hardcopy/GL/html/gl/drawelements.html) for more details under DESCRIPTION.
p.s. any information on the crash would help alot (error message, call, exception).

Sudeepa
12-17-2008, 03:58 AM
Right.Now I have changed my codes ,so the crash is gone but still no texture :( .


START BITMAP Mobile.mbm
HEADER
TARGETPATH \system\apps\Mobile
SOURCEPATH ..\gfx
SOURCE c24 screen.bmp //pic size 128,340
SOURCE c24 screen1.bmp //pic size 128,128
END



_LIT(KTextureBitmap,"z:\\system\\apps\\Mobile\\Mobile.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 )
static const GLbyte skinvertices[4 *2] =
{
/* top */
-1, -1,
1, -1 ,
1, 1,
-1, 1
};
/* 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)
};


GLuint CMobileContainer::BindTextures(const TRect& aRect,TBool aWrap,CFbsBitmap& aTextureBitmap) const
{
GLuint texture;
TInt TextureSize=aTextureBitmap.SizeInPixels().iHeight* aTextureBitmap.SizeInPixels().iWidth;
// allocate a texture name
glGenTextures( 2, &texture );
/* The data in the texture are in RGB order but is read in BGR order.
So we have to swap the 1st and 3rd bytes. */
TUint8 * TextureData = (TUint8 *) aTextureBitmap.DataAddress();
for ( TInt i = 0; i < TextureSize; i++ )
{
TUint8 t = TextureData[i*3];
TextureData[i*3] = TextureData[i*3+2];
TextureData[i*3+2] = t;
}
/* Enable back face culling, texturing, and normalization. */
glEnable( GL_TEXTURE_2D );
/***an appropriate texture matrix has to be initialized*/
glMatrixMode( GL_TEXTURE );
glLoadIdentity();
glScalef( 1.f / 128.f, 1.f / 128.f, 1.f );
glTranslatef( 128.f, 128.f, 0.f );
glMatrixMode( GL_MODELVIEW );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
// select our current texture
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, aRect.Width(), aRect.Height(),
0, GL_RGB, GL_UNSIGNED_BYTE, aTextureBitmap.DataAddress() );
/**************Texture environment parameters are set using glTexEnvf().*/
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, /*GL_REPLACE */GL_DECAL);
// 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,aWrap ? GL_REPEAT : GL_CLAMP_TO_EDGE );

return texture;

}



void CMobileContainer::GenSkin(const TRect&amp; aRect) const
{

CFbsBitmap* TextureBitmap= new (ELeave)CFbsBitmap;
TBool wrap=ETrue;
TextureBitmap->Load(KTextureBitmap,EMbmMobileScreen1,ETrue);
glBindTexture( GL_TEXTURE_2D, BindTextures(aRect,wrap,*TextureBitmap) );
/* Enable vertex array */
glVertexPointer( 2, /*GL_SHORT*/GL_BYTE, 0, skinvertices );//no of cocordinates per vertex,type,byte offset between consecutive vertexes,first vertex in array
glTexCoordPointer( 2, GL_BYTE, 0, nokTexCoords );
glShadeModel( GL_FLAT );
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glDrawElements( GL_TRIANGLES, 2* 3, GL_UNSIGNED_BYTE, triangles );
}

_NK47
12-17-2008, 05:47 AM
"Right.Now I have changed my codes ,so the crash is gone but still no texture"
can have many reasons. call glIsTexture() on your texture object and see if it returns GL_TRUE. make sure you specify correct texture coordinates in glTexCoordPointer (they look ok i guess) and see that you use GL_LINEAR or GL_NEAREST filtering if you have no mipmaps (the code above hasnt). call glGetError after texture creation to see if everything went ok.