I'm doing a little C# / OpenGL project and I can't manage to load textures properly.
So here is my texture class :
public class Texture
{
public String Filepath;
private int TextureID;
public Texture(byte[] Filename)
{
Stream s = FileManager.FindTexture(Filename);
if (s == null) return;
Bitmap b = new Bitmap(FileManager.FindTexture(Filename));
b.MakeTransparent(Color.FromArgb(255,0,255));
//b = BitmapEx.Resize(b, b.Width * 2, b.Height * 2);
//b.Save(DateTime.Now.ToFileTimeUtc().ToString() + ".png");
//GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out TextureID);
GL.BindTexture(TextureTarget.Texture2D, TextureID);
BitmapData data = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
b.UnlockBits(data);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
if(!GL.IsTexture(TextureID))
{
throw new Exception();
}
}
public void Apply()
{
if (TextureID != 0)
{
GL.BindTexture(TextureTarget.Texture2D, TextureID);
}
}
}
As you can see, my bitmap loads well from the stream. I tried to save it after it loaded into a my debug folder and every texture loaded saves. So I'm pretty sure this part is not the problem. But everytime I try to do GL.IsTexture it returns false, I tried to apply my textures either it doesn't work.
I've used a similar method in another project where it was working.
Bitmap bitmap = new Bitmap("Data/UI/" + File);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);
GL.GenTextures(1, out Texture);
GL.BindTexture(TextureTarget.Texture2D, Texture);
BitmapData data = bitmap.LockBits(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0,
OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
bitmap.UnlockBits(data);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Repeat);
I can't see why it is not working right now.
My textures are loaded way after OpenGL is initialized.
I'm using C# .NET framework 3.5 with OpenTK 1.1 ( Runtime 2.0.5, fresh out of their websites ).
Can someone give me a hint on why it is not working, please ?
Warm regards, Chaussette.
glIsTexture
is used to determine if the provided name is indeed a texture. It has nothing to do with whether the data inside is correct or not. In other words,glIsTexture
is related toglGenTextures
andglBindTexture
, and has nothing to do withglTexImage2D
. The correct place to check withglIsTexture
would be right afterglBindTexture
. I've also had some trouble withGL.GenTextures
in the past. How about usingGL.GenTexture
instead? – Panda Pajama May 14 at 10:44TextureID = GL.GenTexture();
, it didn't worked. I also tried to put myGL.IsTexture
right after myGL.GenTexture
and I still get my exception thrown. I put myGL.IsTexture
in my old project and it worked fine. I've also tried using the same textures just in case the fileformat would be wrong but it didn't changed anything. – Chaussette May 14 at 10:52glGenTexture
? – Panda Pajama May 14 at 10:53glIsTexture
is returningfalse
, then your culprit is not related at all withglTexImage2D
. It has nothing to do with your data. There is some reason why you're not getting a valid texture name, and improperly initialized OpenGL comes to mind. You say you're initializing it properly though... – Panda Pajama May 14 at 10:54GL.GenTexture
returns me 1. OpenGL is initialized by OpenTK. It displays me polygons. Here's my OnLoad function ` game.Load += (sender, e) => { game.Title = "Test"; game.Icon = r.favicon; game.VSync = VSyncMode.Off; GL.ClearColor(Color.Lavender); GL.Disable(EnableCap.Lighting); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); };` – Chaussette May 14 at 10:57