Hi,
First of all, forgive me if these questions sound stupid. I have a fair
amount of experience with 2D SDL programming, but very little experience
in 3D, and alpha channels. So, bear with me
Ok. I’ve started work on a GUI for a game project I’m working on. I’ve
planned the classes (C++) and have now moved into the implementation
phase. What I want to do is have a 2D GUI that can be drawn on top of
the 3D opengl rendering. From what I read on this list, SDL_OPENGLBLIT
is NOT and option. No problems - I’ve been experimenting with
glDrawPixels as an alternative.
Using this method, I can load a BMP and draw it using the GL_BGR format,
and I can load a PNG and draw it using the GL_RGBA format. My main
problem comes when I want to create my own GUI pixel data in memory
(with alpha support). To do this I have tried the following:
—//CUT HERE//—
data = SDL_CreateRGBSurface(
SDL_SWSURFACE,
100,
100,
screen->format->BitsPerPixel,
screen->format->Rmask,
screen->format->Gmask,
screen->format->Bmask,
screen->format->Amask
);
SDL_SetAlpha(data, SDL_SRCALPHA, 255); //Make the surface opaque
—//CUT HERE//–
I then try to draw it using the following code (adapted from testgl.c):
—//CUT HERE//–
SDL_GL_Enter2DMode();
glRasterPos2i(200,200);
glPixelZoom(1.0f, -1.0f); //Flip the surface so it’s the right way up
glDrawPixels(data->w, data->h, GL_RGBA, GL_UNSIGNED_BYTE, data->pixels);
SDL_GL_Leave2DMode();
—//CUT HERE//—
Now when I run the app, the area where the surface should be displayed
is corrupted (like noise on a tv). I originally had this problem with
images (i.e. trying to draw a PNG as an RGB instead of RGBA), but
changing the format flag makes no difference.
With this in mind, I’m looking for answers to the following:
-
Is glDrawPixels appropriate for my needs? Keep in mind that I need to
change the pixel data after creation (for example render a real time
game map). I had considered using textures, but I thought constant
re-binding would be inefficient. Am I right to assume this? -
Is there a way to add an alpha channel to a surface that originally
had none? This would allow me to draw all images/surfaces using the RGBA
format flag. -
Is there a way to find out if an image is BGR (or BGRA) and then
convert it to the more supported RGBA - or do I have to write my own
functions (if so any tips?)? -
Do I need to convert the image surface data to the screen format
using SDL_DisplayFormat? -
If I use SDL_SetAlpha to set the alpha, will these changes be
displayed by OpenGL, or does SDL keep alpha info separate from pixel data? -
Can someone clarify something for me concerning OpenGL pixel types. I
know that GL_UNSIGNED_BYTE is basically a 16-bit pixel representation,
but what would I use signed (such as GL_BYTE) pixel formats for (as far
as I understand, pixels can’t have negative colour values). In fact, now
that I’ve got myself confused - perhaps someone could also refresh my on
how pixel data is stored for each bit depth. For example, when using 32
bit colour, are 8 bits allocated for each of the R, G, B, and Alpha
channels? If so how is does this work for 16 bit colour (4 bits each?)
Whew! I appreciate the time anyone takes to read this (and reply), and
hopefully you can help me.
Thanks,
Lance