Frame Problem with Bitmaps

Started by
6 comments, last by Brother Bob 12 years, 7 months ago
Hello Community,

I´ve got a problem with the texturing of some Quads (about 4000) in a 2D Projection.

@ first: Here´s the code

int x = 0;
int y = 0;

m_Width = 32;
m_Height = 32;
handle = new int[1];
m_Handle = handle[0];

Gl.glGenTextures(handle.Length, handle);

rect = new Rectangle(0, 0, m_Width, m_Height);

while (x < 30)
{
while (y < 30)
{
bitmap = (Bitmap)values.bmpBackgroundArray[x, y].Clone();

bdata = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);

Gl.glBindTexture(Gl.GL_TEXTURE_2D, m_Handle);
Gl.glTexImage2D(Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB, m_Width, m_Height, 0, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, bdata.Scan0);
Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR);

Gl.glEnable(Gl.GL_TEXTURE_2D);

Gl.glBegin(Gl.GL_QUADS);

Gl.glTexCoord2f(0, 0); Gl.glVertex2f(x * 32, y * 32);
Gl.glTexCoord2f(0, 1); Gl.glVertex2f(x * 32, y * 32 + m_Height);
Gl.glTexCoord2f(1, 1); Gl.glVertex2f(x * 32 + m_Width, y * 32 + m_Height);
Gl.glTexCoord2f(1, 0); Gl.glVertex2f(x * 32 + m_Width, y * 32);

Gl.glEnd();

Gl.glDisable(Gl.GL_TEXTURE_2D);
bitmap.UnlockBits(bdata);
bitmap.Dispose();

Gl.glDeleteTextures(1, handle);
y++;
}
y = 0;
x++;
}


This does draw the whole Quads with their Texture. But cause these are so many, my framerate really crashes. My Bitmaps are about 32 x 32 pixel, and are drawn 30 times to the right, and same to the bottom. Is there a better way to draw the Bitmaps? Maybe I should use a big bitmap to do so?

Thanks guys :D
Advertisement
You need to separate texture loading and rendering. You should not load the textures each frame, but once and then just refer to the using the handle to bind them. Drawing 4000 textured quads is not a problem, but recreating 4000 textures every frame is.
Well, my Textures are in an Bitmap Array, and i just draw them on the Quads.. Or do you mean, i should use Pointers?
The point of the handle returned by glGenTextures is so that you can save the handle and refer to the texture at any time instead of recreating the texture every time you need it.

Before your rendering loop starts, allocate a texture handle and upload the texture for each bitmap. This gives you an array of handles instead, and you can drop the array of bitmaps, and you can just bind the texture in your rendering loop instead of recreating the texture every time.

You should read up on how to handle textures. For example, chapter 9 in the red book (see the forum FAQ for a link to the book) is about texturing. See the first example code as a reference; the texture is created once in the init function, and referred to by the handle in the display function. You do the same, but if you have 4000 textures, you will have 4000 handles instead of just one.
uhm, i think, i´m too stupid to find the FAQ ^^

And just another question: my int-variable "handle" never gets a value, does it? Gl.glGenTextures(handle.Length, handle); That´s the only Row, where it´s been used
so, i think, it doesn´t get a value, it´s just an int- array
A link to the forum FAQ is at the top of any page within the OpenGL forum.

Your handle array is populated with handles because it is passed by reference and filled in-place.
okay, well i tryed it like that:

{
rect = new Rectangle(0, 0, 32, 32);
bitmap = new Bitmap(values.bmpBackgroundArray[0, 0]);
bdata = bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, bitmap.PixelFormat);
values.adressValuesTextures[0, 0] = bdata;
}

so, my Array "adressValuesTextures" gets the bitmap data of the Textures, so the arrays type is a "System.Drawing.Imaging.BitmapData".

And with that Array I can draw the Textures really faster.

I think, that´s the way you meant, isn´t it?
ahhh, i hadn´t understood it. so, my handle Array is getting filled with the reference of the bitmap, so i can save the value of my variable, and later i can use it like a pointer ? :D
The handle array is filled with references to the texture objects, not the bitmaps. The bitmaps only exist to contain the images on your side; once passed to OpenGL, they are no longer needed.

This topic is closed to new replies.

Advertisement