I am trying to draw a simple 32x32 PNG image using SharpGL and OpenGL 2.1. Despite many hours of searching, reading answers to similar questions, nothing could solve my issue. The image is showed as a white square.
I tried debugging OpenGL using glGetError()
but it always returns 0.
I have no idea of what could be the problem.
Some help would be very appreciated!
Loading method
public static GLImage LoadImagePNG(OpenGL gl, Bitmap bmp)
{
bmp = ResizeTo(bmp, 32, 32);
uint[] textures = new uint[1];
gl.GenTextures(1, textures);
uint texture = textures[0];
gl.Enable(OpenGL.GL_TEXTURE_2D);
BitmapData data = bmp.LockBits(
new Rectangle(0, 0, bmp.Width, bmp.Height),
ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb
);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, texture);
gl.TexImage2D(
OpenGL.GL_TEXTURE_2D,
0,
4,
bmp.Width,
bmp.Height,
0,
OpenGL.GL_RGBA,
OpenGL.GL_UNSIGNED_BYTE,
data.Scan0
);
Console.WriteLine(gl.GetError());
bmp.UnlockBits(data);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_S, OpenGL.GL_CLAMP_TO_EDGE);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_WRAP_T, OpenGL.GL_CLAMP_TO_EDGE);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
// not disposing the Bitmap on purpose (for now)
return new GLImage(texture, bmp);
}
Render method
public static void DrawImage(OpenGL gl, GLImage img, double x, double y)
{
gl.PushMatrix();
gl.Translate(x, y, 0.0D);
gl.ActiveTexture(OpenGL.GL_TEXTURE0);
gl.BindTexture(OpenGL.GL_TEXTURE_2D, img.TextureID);
gl.Enable(OpenGL.GL_TEXTURE_2D);
gl.Begin(BeginMode.Quads);
gl.TexCoord(0.0f, 0.0f); gl.Vertex(0, 0);
gl.TexCoord(1.0f, 0.0f); gl.Vertex(img.Width, 0);
gl.TexCoord(1.0f, 1.0f); gl.Vertex(img.Width, img.Height);
gl.TexCoord(0.0f, 1.0f); gl.Vertex(0, img.Height);
gl.End();
gl.Disable(OpenGL.GL_TEXTURE_2D);
gl.PopMatrix();
}
and GLImage
being a custom class
GLImage
public class GLImage
{
public uint TextureID { get; private set; }
public Bitmap Bitmap { get; private set; }
public double Width
{
get
{
return this.Bitmap.Width;
}
}
public double Height
{
get
{
return this.Bitmap.Height;
}
}
public GLImage(uint textureID, Bitmap bmp)
{
this.TextureID = textureID;
this.Bitmap = bmp;
}
}
Thank you!