Skip to main content
gamedev.net
🔒 Locked

transparent objects

Started by sagmam Jan 30, 2003 at 6:12 AM 2 replies 350+ views
Original Post
sagmam
sagmam
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????
sagmam
sagmam
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?
mickey
mickey
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! ....
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.