Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Texturing and Texturing (newbie)

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    3

    Texturing and Texturing (newbie)

    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 ?
    Code :
    _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)
    	};
    Code :
    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;
     
    }
    Code :
    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 );
    }

    Code :
    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 );
    	}

  2. #2
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Texturing and Texturing (newbie)

    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?


  3. #3
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    3

    Re: Texturing and Texturing (newbie)

    Hi NK47 I changed my code to this here . But still no change .
    .
    Code :
    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);
    }

  4. #4
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Texturing and Texturing (newbie)

    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 for more details under DESCRIPTION.
    p.s. any information on the crash would help alot (error message, call, exception).

  5. #5
    Junior Member Newbie
    Join Date
    Dec 2008
    Posts
    3

    Re: Texturing and Texturing (newbie)

    Right.Now I have changed my codes ,so the crash is gone but still no texture .
    Code :
    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
    Code :
    _LIT(KTextureBitmap,"z:\\system\\apps\\Mobile\\Mobile.mbm");
    [COLOR="Lime"]/* Macro for changing the input texture coordinate values from
       GLubyte [0,255] to GLbyte [-128,127]. See more info below. */[/COLOR]
    #define tex(u,v) (GLbyte)( (u) - 128 ) , (GLbyte)( (v) - 128 )
    static const GLbyte skinvertices[4 *2] =
    	{
    	   [COLOR="#00ff00"] /* top */[/COLOR]
    	    -1,  -1, 
    	    1, -1 ,	    
    	    1,	1,  
    	    -1,  1
    	};
    [COLOR="#00ff00"]/* Define indices for drawing the triangles. The color of the square
     *  is determined by the color of the last vertex of the square.*/[/COLOR]
    static const GLubyte triangles[2* 3] =
    {
    	[COLOR="#00ff00"]/* up */[/COLOR]
    	0,1,2,
    	[COLOR="#00ff00"]/*down*/[/COLOR]
    	1,2,3
    };
    static const GLbyte nokTexCoords[4 * 2] = 
    	{
    	   [COLOR="#00ff00"] /**** */[/COLOR]
    	    tex(0,0), 
    	    tex(255,0),
    	    tex(255,255), 
    	    tex(0,255)
    	};
    Code :
    GLuint CMobileContainer::BindTextures(const TRect& aRect,TBool aWrap,CFbsBitmap& aTextureBitmap) const
    {
    	GLuint texture;
    	TInt TextureSize=aTextureBitmap.SizeInPixels().iHeight*aTextureBitmap.SizeInPixels().iWidth;
    	[COLOR="#00ff00"]// allocate a texture name[/COLOR]
    	glGenTextures( 2, &texture );
    	[COLOR="#00ff00"]/* 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. */[/COLOR]
    	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;
    		}	
    	 [COLOR="#00ff00"]/* Enable back face culling, texturing, and normalization. */[/COLOR]   
        glEnable( GL_TEXTURE_2D );
    	 [COLOR="#00ff00"]/***an appropriate texture matrix has to be initialized*/[/COLOR]
    	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 );
    	[COLOR="#00ff00"]// select our current texture[/COLOR]
    	glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, aRect.Width(), aRect.Height(),
    			0, GL_RGB, GL_UNSIGNED_BYTE, aTextureBitmap.DataAddress() );
    	[COLOR="#00ff00"]/**************Texture environment parameters are set using glTexEnvf().*/[/COLOR]
    	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, [COLOR="#00ff00"]/*GL_REPLACE */[/COLOR]GL_DECAL);
    	[COLOR="#00ff00"]// if wrap is true, the texture wraps over at the edges (repeat)
        //       ... false, the texture ends at the edges (clamp)[/COLOR]
        glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,aWrap ? GL_REPEAT : GL_CLAMP_TO_EDGE );
     
        return texture;
     
    }
    Code :
    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) );
    	[COLOR="#00ff00"]/* Enable vertex array */[/COLOR]
    	glVertexPointer( 2, [COLOR="#00ff00"]/*GL_SHORT*/[/COLOR]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 );
    }

  6. #6
    Advanced Member Frequent Contributor _NK47's Avatar
    Join Date
    Mar 2008
    Posts
    574

    Re: Texturing and Texturing (newbie)

    "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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •