Texture with a transparent color

Started by
15 comments, last by gtlpcl 20 years, 7 months ago
Hi, I begin to program in opengl and I have a few ideas. I would like to try making a game in opengl. Since my first day in opengl, I learned a lot, but There are still some problems. Actually, the most important is that I would like to display a quad with a texture. Until here, it''s work very well. But, I would like that only one color in the texture is transparent and the rest display. How can I do this??? I think that I''m not the first trying this operation, but I don''t find anything on Internet about it. Thanks. PCL
Advertisement
Well, you can do it two ways to my knowledge.

Firstly I think there''s a command in openGL to only show a certain colour, but I dunno it, so you could do it another way.

Alpha blending - make the texture in Photoshop or something and make an alpha channel with the bits you want in, of course if it''s loads of pixels in different places on the screen it''ll be hard to do, but im sure PS7 or whatever has a command that''ll do this too. There''s a nehe tut on alpha blending on the site.


- Squalion
Get NeHe''s TGA loader. Open up paint shop (or photoshop or whatever) and open your image. Select only the part that should be visible in the texture. Click "save to alpha channel" and save your image as a 32 bit uncompressed TGA.

In your game, load the TGA and create a texture from it. Then:

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0f);

and render your texture.

At Anonymous: There is no OpenGL command to prevent rendering of a certain color (unless you create some fragment shader to do it for you). All you can do in openGL is block an entire color *channel* (red, green, blue or alpha). That''s something different, because it would remove e.g. all red components from the texture.

Sander Maréchal
[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]


GSACP: GameDev Society Against Crap Posting
To join: Put these lines in your signature and don''t post crap!

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Thanks to you.

It works very well with TARGA files and Alpha channel. It''s exactly what I''m looking for.

Thanks again and kind regards.

GTLPCL
U could also u a BMP (RGB) and write a routine that loads the pixels from the file, and create a RGBA array yourself and check for that colour that means "invisible" and write in alpha corresponding to that.

Just an idea tho...
The world isn't unpredictable. It's CHAOTIC.
Here''s an example of what I do when I want to create an alpha channel for a color I want to be transparent. Please note that the data must be of RGB format and 24 bits (or you can change that). After you used this code, you have to create a usual OpenGL texture using glGenTextures, glTexParametri etc.

	GLubyte *newData = new GLubyte[mat->m_iWidth * mat->m_iHeight * 4];	GLubyte *oldData = new GLubyte[mat->m_iWidth * mat->m_iHeight * 3];	GLuint textureType = GL_RGB;	if(mat->m_iBpp == 32)		textureType = GL_RGBA;	else if(mat->m_iBpp == 24)		textureType = GL_RGB;	glGetTexImage(GL_TEXTURE_2D, 0, textureType, GL_UNSIGNED_BYTE, oldData);	int t = 0;	for (int i=0; i<mat->m_iWidth * mat->m_iHeight * mat->m_iBpp / 8; i+=3)	{		newData[t] = oldData[i];		newData[t+1] = oldData[i+1];		newData[t+2] = oldData[i+2];		if(oldData[i] == color.r && oldData[i+1] == color.g && oldData[i+2] == color.b)			newData[t+3] = 0;		else			newData[t+3] = 255;		t+=4;	}	mat->m_cData = newData; // Here'' s the data you need	mat->m_iBpp = 32;	CreateGLTexture(mat); // This one just goes thru usual OpenGL texture generation - GenTextures, TexParametri, etc	SAFEDEL(oldData);




When you want to actually render a texture on a polygon/tri/quad/etc, use this code:

glEnable(GL_ALPHA_TEST);glAlphaFunc(GL_GREATER, 0.0f);glBindTexture(//your texture);// Your glBegin() code hereglDisable(GL_ALPHA_TEST);


" Do we need us? "


Ionware Productions - Games and Game Tools Development

I don''t have much experience with these graphics programs, so maybe you can tell me what i''m doing wrong. I''ve tried this with PSP 5, GIMP, and now I''m trying it in PSP 8.

With PSP 8, I do this:

I make a new 256x256 24 bit color image with a transparent background, draw a few lines and circles on it, make a new mask layer from image using any non-zero number, save to alpha channel, hit save as, call it Font.tga, and put it in the Data file of Lesson 24. When I run the .exe it says that initialization failed. Any reply would be greatly appreciated, I''ve spent a good amount of time trying to figure out what i''m doing wrong.
I see where is your problem.
If you want to use an alpha channel int your picture, it must be in 32 bits.

Kind regards.

PCL
Hey, just registered. I''m anonymous up there.

for color depth, it seems to offer 24 bit at the highest. I assumed the extra 8 would be added once i saved the mask to the alpha channel. When I looked at save options, it had compressed set to default, so i switched that, but it still doesn''t load.

when I save though, it says this:

Because of the limitations of the specified file format (and possibly the save options you''ve selected), the saved file will be limited to a merged image. Would you like to continue?

Thanks again for any help
Hi,

This is the way I use for making mw TARGA texture with an alpha channel :

1/ I use Photoshop 5.0 and I open the picture which interest me (the format doesn''t mind)
2/ Then, I had the alpha channel by clicking on the menu "select -> Color range..."
3/ A dialog box will be opened. Click on the picture on the color you want to be transparent. Check the option "invert". you will see a "mask" back and white into the dialog box. The black represents the transparent information (alpha = 0) and the white represents the opac information (alpha = 1.) You can now click on OK for close the dialog box.
4/ Now, wou will see a selection on the picture. Now you will create the alpha channel by clicking on the menu "Select -> Save the selection..." Name it as you want.
5/ that''s done, you have an alpha channel on your picture.
6/ Now, we coult save the picture. Make a "Save as..." Choose the format TARGA(*.tga). Name your picture and click on save.
7/ Then, Photoshop will open a dialog box named "TARGA option" where you can choose the color depth. Choose here 32 bits.

Now, there qre two remarks I would like to make :
* First, all layers have to be merged if you want to choose the picture format when you save the picture. So, be sure to merge your layers before making these steps.
* Second, the size (width and height) of the picture is very important for TARGA textures on opengl. for the width and the height, you can choose one of these values : 2, 4, 8, 16, 32, 64, 128, 256, 512.

I hope this will help you.

Kind Regards.

PCL

This topic is closed to new replies.

Advertisement