Jump to content
  • Advertisement
Sign in to follow this  
Fickle

OpenGL Draing sprites in openGL

This topic is 4907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am beginning a pseudo-2d engine in openGL and am having trouble drawing sprites. I have completed the first 7 NeHe tutorials and have a fairly good understanding of the basics covered therein. OK, the problem : I want to map a texture to a quad and draw all pixels not of a certain color (with an rgb of 255,0,255 for example). Looking through the tutorials on NeHe's site and on the web I have not been able to find any code which will allow me to do this. I would greatly appreciate any links, advice, or code which would help me out. I have found this code which looks promising... glAlphaFunc(GL_LESS, 1); glEnable(GL_ALPHA_TEST); It seems that if I could make all of the pixels I want to blit have an alpha of 1, and the junk pixels have an alpha of 0, then this filter out the junk for me... problem is, I don't have any idea how to set the alpha values of the pixels. Is this done in the paint program? Is there some way to set the pixels to the desired alpha values in my code? SoAny links that explain the basics of the alpha value would be grealy beneficial as well. Thanks - Fickle

Share this post


Link to post
Share on other sites
Advertisement
Hi Fickle,

What you describe is called colour keying.

How are you loading the texture? Colour keying is normally done when the texture is loaded, before passing it to OpenGL, by checking each pixel, and if it is that certain colour, making that pixel transparent. Setting the alpha channel of a pixel depends on what format your image is in memory when you load it (e.g. 32-bit RGBA). If it is 32-bit, then per pixel you have 1 byte of information for the alpha channel. Bitwise operators, shifts, etc. can be your friends when setting that 1 byte. Say you have a 32-bit integer representing the current pixel:
uint32 pixelColour;
pixelColour &= 0x00FFFFFF; // or for LSB do &= 0xFFFFFF00

That bitwise-AND operation masks out the most significant byte (MSB) of the four. The alpha channel may not be that byte - I can't remember off the top of my head what the usual ordering is.

Alternatively you can load the texture from an image format that supports transparency (PNG as an example). That way you could make all (say) blue pixels transparent yourself using Photoshop or a similar app. Even better, make the texture yourself with transparency from the start.

Hope that wasn't too unclear!

Share this post


Link to post
Share on other sites
I am loading the textures using the code directly out of NeHe tutorial 7...

GLuint texture[1];
AUX_RGBImageRec *LoadBMP(char *Filename) { ... }

int world::LoadGLTextures()
{
AUX_RGBImageRec *TextureImage[1];
memset(TextureImage,0,sizeof(void *)*1);
if(TextureImage[0]=LoadBMP("Data/tile.bmp"))
{
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB,
GL_UNSIGNED_BYTE,TextureImage[0]->data);
}
}

Looking at this it seems that the this code is setting up texture[0]as a RGB texture, not a RGBA.
Im guessing that after texture[0] has been set up by this code it will contain the R value for pixel 0,0, then the G value, then the B value, then go onto the next pixel. So it would contain a total of xsize*ysize*3 GLuint's.

For the color keying, should I create a temp image from the bitmap, and then set texture[0] (where the texture is stored) as 4/3 the size of the temp, then go through and set texture[0][0] = temp[0], texture[0][1] = temp[1], texture[0][2] = temp[2], then do the check to see if the three pixel values correspond to the value of a transparent pixel, a set texture[0][4] to either 0 or 255 correspondingly?
Is there a simpler way to do this?

Sorry, I know that looked ugly.

I like the idea of just creating the image with the transparency layer much better : ) I'm looking into the help files for paint program, but no luck so far...

Thanks for the help, Neex

Share this post


Link to post
Share on other sites
Quote:
For the color keying, should I create a temp image from the bitmap, and then set texture[0] (where the texture is stored) as 4/3 the size of the temp, then go through and set texture[0][0] = temp[0], texture[0][1] = temp[1], texture[0][2] = temp[2], then do the check to see if the three pixel values correspond to the value of a transparent pixel, a set texture[0][4] to either 0 or 255 correspondingly?


That seems a pretty reasonable way of doing it yes. Remember to change the parameter in glTexImage2D from GL_RGB to GL_RGBA.

Quote:
I like the idea of just creating the image with the transparency layer much better : ) I'm looking into the help files for paint program, but no luck so far...

Sadly you're loading BMP files which don't support transparency AFAIK. If you do want proper transparency, i.e. an 8-bit alpha channel rather than the 1-bit that colour-keying provides, you could use another BMP file to store the alpha channel and merge the two in a similar way to the colour keying in your program. For example, use the red channel of the second image to represent the transparency of the first.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
OK, got everything up and running now, thanks for the help Neex

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!