Archived

This topic is now archived and is closed to further replies.

gtlpcl

Texture with a transparent color

Recommended Posts

gtlpcl    122
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
Sander    1332
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!

Share this post


Link to post
Share on other sites
gtlpcl    122
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

Share this post


Link to post
Share on other sites
Intamin AG    122
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...

Share this post


Link to post
Share on other sites
HellRiZZer    308
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 here

glDisable(GL_ALPHA_TEST);


" Do we need us? "


Ionware Productions - Games and Game Tools Development

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
grimes    122
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

Share this post


Link to post
Share on other sites
gtlpcl    122
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

Share this post


Link to post
Share on other sites
dogan    122
That''s too complicated for me. All I need to do is:
-Load a RBGA bitmap
-Mask out the alpha channel

I looked at the Alpha Blending tutorial (#32). Impossible to read.

Share this post


Link to post
Share on other sites
grimes    122
Thanks very much gtlpcl.

Lessons 24,32, and 33 all have the TGA loading code, but I''d say that it''s explained best in #24.

Share this post


Link to post
Share on other sites
dogan    122
yes, I got the code from #24, but I can''t find out how to use the texture I get. it''s something like:
glBindTexture(GL_TEXTURE_2D,textures[0].&imageData)
right?

Share this post


Link to post
Share on other sites
gtlpcl    122
Hi,

Yes, you''re right, the function glBindTexture tells to the program which texture you eant to use, but your parameters are false.

First, I guess you use the structure Texture given by Nehe in his tutorials. If it''s the case, when you use the function glBindTexture, you use the value texture[0].texID and not imageData. imageData is empty now, after you attach an image to the Texture structure, you free this value (pointer to a part of memory) which is now obsolete (see the command imageData->free.)

Second, if you use the Texture structure as a pointer (ie you implement like this : Texture *texture), it''s not a point and/or ''&'' caracter for accessing value, but an array, like this : texture->texID

So, your source could seems like that :

glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();

Remark : don''t forget to enable the textures.

Kind Regards.

PCL

Share this post


Link to post
Share on other sites