Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

closed as off-topic by Josh Petrie Jul 23 '15 at 16:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

    
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 to glGenTextures and glBindTexture, and has nothing to do with glTexImage2D. The correct place to check with glIsTexture would be right after glBindTexture. I've also had some trouble with GL.GenTextures in the past. How about using GL.GenTexture instead? – Panda Pajama May 14 '15 at 10:44
    
So I've tried to use TextureID = GL.GenTexture();, it didn't worked. I also tried to put my GL.IsTexture right after my GL.GenTexture and I still get my exception thrown. I put my GL.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 '15 at 10:52
    
What value are you getting from glGenTexture? – Panda Pajama May 14 '15 at 10:53
    
The idea is that, if glIsTexture is returning false, then your culprit is not related at all with glTexImage2D. 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 '15 at 10:54
    
My GL.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 '15 at 10:57