Archived

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

transparent objects

This topic is 5430 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

Hi, I''m having problems creating two semi-transparent boxes. This is what I have: 1. Two cubes with a texture on them (the texture is made of several colors, 128x128), that appear one behind the other on screen. The color RED is only of the colors that appear in the texture, and I want ONLY THE RED to become semi-transparent... 2. I set the render states as follows: ALPHATESTENABLE is TRUE ALPHABLENDENABLE is TRUE SRCBLEND is SRCALPHA DESTBLEND is INVSRCALPHA Since the texture was created in Paint Brush, I wanted to modify the alpha channel manually, so I did the following: D3DXCreateTextureFromFileEx( pDev, "red.bmp", D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_UNKNOWN, D3DPOOL_MANAGED, D3DX_FILTER_LINEAR, D3DX_FILTER_LINEAR, 0x00000000, NULL, NULL, &MyTex); // ^^^^^^^ THIS COLORKEY WILL BE IMPORTANT LATER... MyTex->LockRect(0,&lockedrect,0,0); // ...loop and put 0x40 as the alpha channel of every texel: for (i = 0; i < 128; ++i) for (j=0; j < 128; j++) { curr_color = texdata[i*lockedrect.pitch + j]; if (IsRed(curr_color) { curr_color = curr_color & 0x00ffffff | 0x40000000; texdata[i*lockedrect.pitch + j] = curr_color; } } MyTec->UnlockRect(0); The results I got depend on the value of the ColorKey argument that I passed to CreateTextureFromFileEx: 1. If ColorKey was 0, then nothing happend. The alpha value of 0x40 did not have any affect. 2. If ColorKey was 0 BUT had some non-zero alpha channel (for example: 1,0,0,0), then the red portions of the texture were getting closer to the ambient color as I lowered the alpha value from 0x40 to 0x00. No matter what, I did NOT see one cube "through" the other in ANY CASE. Any ideas why I can''t make the cubes transparent????

Share this post


Link to post
Share on other sites
FOUND OUT SOMETHING INTERSTING:

I rendered my objects in the wrong order. I rendered the cube in the "front" first, and only then I rendered the cube in the back. So there was not yet a "previous pixel color" to blend with. When I reveresed the order of rendering, it was ok...

But this is very weird... after all, "front" and "back" are relative to the point of view! If I would look at the scene from the "other side", then I''d loose the translucency again! So what am I supposed to do?

Share this post


Link to post
Share on other sites
hi sagmam,

you have to sort the transparent objects, from nehe''s site tutorial # 32:

"For the alpha blending technique to work correctly, the transparent primitives must be drawn in back to front order and must not intersect". When drawing alpha blended objects, it is very important that objects in the distance are drawn first, and objects up close are drawn last.

The reason is simple... The Z buffer prevents OpenGL from drawing pixels that are behind things that have already been drawn. So what ends up happening is objects drawn behind transparent objects do not show up. What you end up seeing is a square shape around overlapping objects... Not pretty! ....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Your other issue with transparency not working when there is no alpha is likely that D3DX is loading the texture as X8R8G8B8 when the source is 24 bit, and loading as A8R8G8B8 when you have an alpha. If you tell D3DX to use format A8R8G8B8 in the load call you should be okay.

Share this post


Link to post
Share on other sites