Skip to main content
gamedev.net
🔒 Locked

Texture problems.

Started by mishal153 6 replies 1.2k views
Original Post
mishal153
mishal153
Hi, I am new to opengGL. I am trying to map a texture onto a square. But its givin problems. I am givin the code:

OpenGL settings function(called once):

 Tao.OpenGl.Gl.glShadeModel ( Gl.GL_SMOOTH );
			Tao.OpenGl.Gl.glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
			Tao.OpenGl.Gl.glClearDepth ( 1.0 );
			Tao.OpenGl.Gl.glEnable     ( Gl.GL_DEPTH_TEST );
			Tao.OpenGl.Gl.glDepthFunc  ( Gl.GL_LEQUAL );
			Tao.OpenGl.Gl.glHint       ( Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST);
		    Tao.OpenGl.Gl.glEnable     ( Gl.GL_TEXTURE_2D );

			Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MIN_FILTER , Gl.GL_LINEAR );
			Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MAG_FILTER , Gl.GL_LINEAR );

================================================================
TextureClass constructor:
               public MyTexture( string Filename )
		{
			m_Texture = new uint[1];
			rect      = new Rectangle (0,0,0,0);
			m_FileName = Filename;
		}

Binding Function:(called once only)        


		public void BindTexture()
		{
			m_Image   = new Bitmap ( m_FileName );
			rect = new Rectangle( 0, 0, m_Image.Width , m_Image.Height );
			m_BitmapData = m_Image.LockBits ( rect , System.Drawing.Imaging.ImageLockMode .ReadOnly ,
			System.Drawing.Imaging.PixelFormat.Format24bppRgb );

			Tao.OpenGl.Gl.glGenTextures ( 1 , m_Texture );   //generates a unique ID of some kind
			Gl.glBindTexture            ( Gl.GL_TEXTURE_2D , m_Texture[0] );
			Gl.glTexImage2D    ( Gl.GL_TEXTURE_2D , 0 , (int)Gl.GL_RGB8 ,
				                 m_Image.Width , m_Image.Height , 0 , Gl.GL_BGR_EXT ,
                                 Gl.GL_UNSIGNED_BYTE , m_BitmapData.Scan0 ); 

            m_Image.UnlockBits ( m_BitmapData );
			m_Image.Dispose    ();
		}

=================================================================

Draw function:(repetedly called)

                        OpenGLObject.Clear();
		        Gl.glMatrixMode(Gl.GL_PROJECTION);
			Gl.glLoadIdentity();
			Gl.glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
			Gl.glMatrixMode(Gl.GL_MODELVIEW);
   	Gl.glBindTexture(Gl.GL_TEXTURE_2D ,TextureObject.m_Texture[0] );

			Gl.glBegin(Gl.GL_QUADS);
			
                        Gl.glTexCoord2f(1.0f, 1.0f);
			Gl.glVertex3f( 0.75f, 0.75f , 1.0f );
			
			Gl.glTexCoord2f(0.0f,1.0f);
			Gl.glVertex3f( 0.25f, 0.75f , 1.0f );

			Gl.glTexCoord2f(0.0f,0.0f);
			Gl.glVertex3f( 0.25f, 0.25f , 1.0f );
			
                        Gl.glTexCoord2f(1.0f,0.0f);
			Gl.glVertex3f( 0.75f, 0.25f , 1.0f );
			
			Gl.glEnd();

Added source code tags, please check the forum FAQ for details on the tags in use on this board [Edited by - _the_phantom_ on April 7, 2005 8:54:06 AM]
rick_appleton
rick_appleton
What exactly do you see (or not see) on the screen?

From a quick glimpse at your code, I think you might need/want to change
Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MIN_FILTER , Gl.GL_LINEAR );Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MAG_FILTER , Gl.GL_LINEAR );


to
Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MIN_FILTER , Gl.GL_NEAREST );Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MAG_FILTER , Gl.GL_NEAREST );


and you might want to make the last element of your vertex coordinates 0.f instead of 1.f to be certain they aren't clipped away.
mishal153
mishal153
hi...
I see a Neat white square.

I'll try changin tht code u suggested.
mishal153
mishal153
I changed the code...still it shows the same.I wanna ask one thing...I have see this code from a demo project and have implemented it here. How does m_Texture[0] link to the texture. When we do
Gl.glBindTexture( Gl.GL_TEXTURE_2D , m_Texture[0] );

we say tht the m_Texture[0] will behave as a 2D texture...am i right?

and when we do :

Gl.glTexImage2D ( Gl.GL_TEXTURE_2D , 0 , (int)Gl.GL_RGB8 ,
m_Image.Width , m_Image.Height , 0 , Gl.GL_BGR_EXT ,
Gl.GL_UNSIGNED_BYTE , m_BitmapData.Scan0 );

we are specifying from where will the 2D-texture data be xtracted. Then how does m_Texture[0] represent my texture??

i am kinda confused as u can guess....
rick_appleton
rick_appleton
Tao.OpenGl.Gl.glGenTextures ( 1 , m_Texture ); //generates a unique ID of some kindGl.glBindTexture ( Gl.GL_TEXTURE_2D , m_Texture[0] );Gl.glTexImage2D ( Gl.GL_TEXTURE_2D , 0 , (int)Gl.GL_RGB8 ,m_Image.Width , m_Image.Height , 0 , Gl.GL_BGR_EXT ,Gl.GL_UNSIGNED_BYTE , m_BitmapData.Scan0 );


The first line above generates an ID for the texture. If you are only using one texture, m_Texture[0] should be 1. This refers to a texture object in OpenGL memory. You needn't concern yourself with how it's stored or how it works, you only need to know that you can access that texture with m_Texture[0].
In the second line you tell OpenGL you want to use that texture object, and then on the third line you load the texture data into the texture.

I can't really see anything else being wrong. Are you certain that the bitmap is read correctly?
mishal153
mishal153
ya..i am sure the bitmap is found correctly atleast coz after this line:

m_Image = new Bitmap ( m_FileName );
rect = new Rectangle( 0, 0, m_Image.Width , m_Image.Height );


the m_Image.width and height are 64 each which is the actual image's size. And that m_Texture[0] also has value 1 as u said. donno whats wrong???
Kalidor
Kalidor
Quote:
Original post by mishal153 Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MIN_FILTER , Gl.GL_LINEAR );
Gl.glTexParameteri (Gl.GL_TEXTURE_2D , Gl.GL_TEXTURE_MAG_FILTER , Gl.GL_LINEAR );


glTexParameter functions operate on the currently bound texture (I'm pretty sure), so you need to call those after the texture is bound otherwise it will have no effect. The default value for GL_TEXTURE_MIN_FILTER is GL_NEAREST_MIPMAP_LINEAR which is not a valid minification filter for non-mipmapped textures, so the texture will not show up unless you are using mipmapping, which you aren't here.

If that doesn't work there's gotta be something else wrong too, so post any more code you think could be relevant to the issue. Also, put [ source ][ /source ] tags around your code to make it more readable.
mishal153
mishal153
wow,, that worked man....thanx a lot !!! Thanx a lot ...

bye
mishal

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.