Skip to main content
GameDev.net gamedev.net
🔒 Locked

Depth Transparecy issues.

Started by ThePointingMan Jun 18, 2013 at 3:43 PM 4 replies 1.6k views
Original Post
ThePointingMan
ThePointingMan

I've been working on a Direct3D game run for a while now and I've come across an issue with alpha blending that I don't quite know how to solve.

I've set my render states like this


// both sides of the triangles
    d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);    
	//Set render states
	d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);// turn off the 3D lighting
	//make it so things color can fade
	d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
	d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
	d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
	//-------------------------------------------------------------
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
	d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

Now at first I thought everything was perfectly fine, things that where transparent in my png file would go on screen transparent, but I later found out that they will only be transparent to things they have been rendered after, so if i render a see through a space ship, and then a red cube and then an asteroid and put the red cube in front of both the space ship and the asteroid, only the space ship will be visible with a red tinge, the asteroid will be completely blocked out. Does anyone know why this is happening and how it can be solved?

belfegor
belfegor

You need to sort your items back to front in order for transparency to work correctly.

ThePointingMan
ThePointingMan

So I literally need to just make sure that when something is rendered it is rendered before everything that it needs to cover?

iMalc
iMalc

So I literally need to just make sure that when something is rendered it is rendered before everything that it needs to cover?

The opposite, make sure it is rendered before everything that needs to be able to cover it.

phil_t
phil_t

Generally, you render all opaque objects first (ideally front-to-back for performance, but visually it doesn't matter), and then all transparent objects from back-to-front in order that the transparency looks correct.

Khatharr
Khatharr

Blending works by reading the existing pixel's color value and then combining it with the color value being written. This is why back-to-front rendering order is necessary when using blending.

The blending mode you have set up there is 'normal' blending (not vertex normal, which is a lighting thing, but normal in the sense of usual). The formula works like so:

S = source color (the pixel value being rendered)

D = destination color (the color already present in the buffer being rendered to)

R = result color (the color you end up with)

First you extract the alpha value from S and turn it into a float such that transparent is 0.0 and fully visible is 1.0.

We'll call that value A. We also derive its inverse, which is 1.0 - A. We'll call that N (for iNverse).

For each channel, r, g, and b we do the following, where c is the channel:

R(c ) = (A * S(c )) + (N * D(c ))

So, for instance, if S is half-alpha red: 0xFF00007F, and D is green: 0x00FF00FF, then we get:

A = 0.5, N = 0.5

R(r ) = (0.5 * 0xFF) + (0.5 * 0) = 0x7F

R(g) = (0.5 * 0) + (0.5 * 0xFF) = 0x7F

R(b) = (0.5 * 0) + (0.5 * 0) = 0

R = 0x7F7F00FF

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

Topic Locked

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

Sign in to reply to this topic.