Transparent rendered texture

Started by
13 comments, last by DanielH 20 years, 6 months ago
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?
Advertisement
How about setting the alpha to 0 and using alphablending?
How do I do that?
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.
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!?
I really need help with this That black quad looks awfull
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.
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?)
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?
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*/

This topic is closed to new replies.

Advertisement