Skip to main content
gamedev.net
🔒 Locked

Frame Problem with Bitmaps

Started by DJLaitier Aug 29, 2011 at 10:43 AM 6 replies 1.7k views
Original Post
DJLaitier
DJLaitier
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
Brother Bob
Brother Bob
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.
DJLaitier
DJLaitier
Well, my Textures are in an Bitmap Array, and i just draw them on the Quads.. Or do you mean, i should use Pointers?
Brother Bob
Brother Bob
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.
DJLaitier
DJLaitier
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
Brother Bob
Brother Bob
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.
DJLaitier
DJLaitier
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 ?
Brother Bob
Brother Bob
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.