Sign in to follow this  
Del Snd of Thndr

Loading Textures [C#]

Recommended Posts

Hi all! I'm currently working on a MilkShape 3D and 3DS Max model loader in C#. I have everything working correctly except when I load a texture. I have noticed that when I load a texture, it is loaded and displayed, but with a bluish tint over the bitmap's colors. I have narrowed the error location to my LoadGLTexture function, which is mostly listed below:
protected virtual uint LoadGLTexture(uint[] textures, string filename, int textureID)
		{
			try
			{
				//TODO:  Find out why there is a bluish background applied.
				Bitmap	bmp		= new Bitmap(filename);	
				bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);

				Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
				System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits
					(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, 
					System.Drawing.Imaging.PixelFormat.Format24bppRgb);

				GL.glGenTextures(1, textures);
				GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);

				GL.glBindTexture(GL.GL_TEXTURE_2D, textures[textureID]);
				GLU.gluBuild2DMipmaps(GL.GL_TEXTURE_2D, 3, bmp.Width, bmp.Height, 
					GL.GL_RGB, GL.GL_UNSIGNED_BYTE, bmpData.Scan0);

				GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,
					(int)GL.GL_LINEAR_MIPMAP_LINEAR);
				
				GL.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,
					(int)GL.GL_LINEAR);

				bmp.UnlockBits(bmpData);
				bmp.Dispose();
				
				return textures[textureID];
			}
	
			catch
			{
				if(MessageBox.Show("The specified texture was not found.  Would you like to choose one?", 
					"Texture Error", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, 
					MessageBoxDefaultButton.Button1) == DialogResult.Yes)
				{
					using(OpenFileDialog dlg = new OpenFileDialog())
					{
						dlg.Filter	= "Texture file (*.bmp) | *.bmp";
						
						if(dlg.ShowDialog() == DialogResult.OK)
							return (LoadGLTexture(textures, dlg.FileName, textureID));
					}
				}

				return 0;
			}
		}





Can anyone tell by looking at this code what I might be doing wrong? BTW, I am using the NeHeGL C# Basecode. If you need to see more source, please let me know. Thanks in advance!

Share this post


Link to post
Share on other sites
How would I go about doing that? Also, I've noticed something else: If I replace the code in the try block with the following code, the texture for the Milkshape model loads correctly, but the texture for the 3DS Max model does not load at all. Does this mean that I am handling the texture incorrectly in my MaxModel class somewhere?


Bitmap bmp = new Bitmap(filename);
bmp.RotateFlip(RotateFlipType.RotateNoneFlipNone);

Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits
(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

GL.glGenTextures(1, textures);
GL.glBindTexture(GL.GL_TEXTURE_2D, textures[textureID]);
GL.glTexImage2D(GL.GL_TEXTURE_2D, 0, (int)GL.GL_RGB8, bmp.Width,
bmp.Height, 0, GL.GL_BGR_EXT, GL.GL_UNSIGNED_BYTE,
bmpData.Scan0);

GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, (int)GL.GL_LINEAR);
GL.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, (int)GL.GL_LINEAR);

bmp.UnlockBits(bmpData);
bmp.Dispose();

return textures[textureID];



Thanks again.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this