Archived

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

DanielH

Transparent rendered texture

Recommended Posts

In my game I render a gun to a texture and then draw it in ortho mode. I''d like to make the textures background transparent instead of black, but i don''t know how to. Any ideas?

Share this post


Link to post
Share on other sites
Is the correct way of doing this by use some strange background color and the go through the texturedata and change that color to alpha 0? But how do I then get the pixeldata since i just copied the viewport to a texture.

Share this post


Link to post
Share on other sites
I read an article that just explained the way of doing this but didnt show any code for it.

Firstly the texture must be a 4 channel RGBA texture. And then before drawing the 3D object I clear the screen with a 0 alpha clear color. And lastly I copy the vieport using glCopyTexImage2D with GL_RGBA mode. But It still doesnt work!?

Share this post


Link to post
Share on other sites
I''ll try and put you on the right track.

You really need to create an ALPHA map, which is basically just an 8 bit texture used to store transparency values. You can do this by going through your data manually and for a black pixel, on your alpha map have a value of 0.0, and for any over value have the alpha of 1.0. You could write some code to find the edges of your texture and have an alpha of say 0.5, thus you won''t have ultra sharp unrealistic edges.

The easiest way to do this is propably to copy your RGB texture into a RGBA texture and the process the fourth byte of each pixel as I describe above.

I hope I have at least been of some help.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have the same problem, but a bit more difficult since I need to include black as a color in my texture.

Until now I''ve had to draw a quad twice using two different GL_BLEND-ings, once for a mask and once for color.
But that''s very slow when I draw it a lot.
I want to only draw once and without needing mask textures or RGBA data for everything, just "blending out" one certain color.

I''m trying multitexture commands but thats even more confusing. I wish there was a simple answer to this simple problem.. why they make gl commands for everything except basic masking?
There should be glBindTextureExcludingColor(0.0, 0.0, 0.0) which does a simple one color blend-out for you and keeps all the other colors, all without requiring RGBA values in the texture data.
(Anyone want to help me write that?!? anyone else think it''s possible?)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Nevermind I figured it out, no thanks to you

/* Copy the RGB image in to the RGBA image out, giving all pixels of color * (r,g,b) an alpha of zero and the other ones an alpha of 255.*/
void ColorKey(GLubyte *in,GLubyte *out,GLubyte r,GLubyte g,GLubyte b,
unsigned int width,unsigned int height) {
unsigned int i,j;
for(i=0;i<height;++i)
for(j=0;j<width;++j) {
out[4*(i*width+j)+0]=in[3*(i*width+j)+0];
out[4*(i*width+j)+1]=in[3*(i*width+j)+1];
out[4*(i*width+j)+2]=in[3*(i*width+j)+2];
if(r==in[3*(i*width+j)+0] &&
g==in[3*(i*width+j)+1] &&
b==in[3*(i*width+j)+2])
out[4*(i*width+j)+3]=0;
else
out[4*(i*width+j)+3]=255;
}
}

Simple, no?

Share this post


Link to post
Share on other sites
isn''t that extremly slow???

first copy the texture from AGP memory to system mem - then pass over it - send it back (i bet this takes 50% of the frame time)

Doesn''t the frame buffer have an alpha channel ? If so clear it with alpha 0.0 and render with alpha 1.0 . Or you could use a PBUFFER.



[ My Site ]
''I wish life was not so short,'' he thought. ''Languages take such a time, and so do all the things one wants to know about.'' - J.R.R Tolkien
/*ilici*/

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why does this have to be so complex?
to load a texture give me a break!

Share this post


Link to post
Share on other sites
Not just loading, making a already existing texture transparent.

I still havent been able to make it transparent


I think that when i use glCopyTexImage2D to copy the framebuffer to a texture its background should also be alpha 0 when the background is cleared with alpha 0.... But no no

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Like NEHE eh?
Sure that works, and it''s simple, but it requires you to send the geometry twice and that''s slow.

You really didn''t "solve the problem" until you can draw only once like you should be able to. (Unless you like your graphics to be slow and take more performance than they should..)
Also I''ve found it to be a bit annoying to have to make and load mask textures for EVERY thing.. something like multitexturing or Alpha masks would be a more efficiant and managable approach..

Share this post


Link to post
Share on other sites
Maybe a stupid question, but you might have overlooked it, so I''ll ask it anyway

Did you use an internal-format in glCopyTexImage2d of RGBA or equivalent? If not, the alpha value wouldn''t be returned, would it?

Hellsp4wn
-----------

I''m having deja-vu and amnesia at the same time. It''s like I think I''ve forgotten this before.

Share this post


Link to post
Share on other sites