# Making stuff fade over distance..?

## Recommended Posts

Hi there! Right now I'm using this peice of code to fade things Alpha = 1.0 - (1.0/MaxObjectDistance)* Object.Distance); it works, however objects are only fully opaque when they're right in your face.. How does one alter the function above to make them fully solid, say when they're half the MaxObjectDistance away? :/ It's probably something really simple that I'm missing.. but I'm stuck. Thanks for any help!

##### Share on other sites
Think in reverse;)

Alpha = 1.0;
// only do the alpha when its further away then half the max distance
if (Object.Distance > (MaxObjectDistance / 2.0f))
{
Alpha = 1.0 - (1.0/MaxObjectDistance)* Object.Distance);
}

##### Share on other sites
it's verry simple:

const float HalfMaxObjectDistance = MaxObjectDistance * 0.5;
Alpha = Object.Distance <

##### Share on other sites
Alpha = Object.Distance <

##### Share on other sites
sorry something wrong posting in this forum: Alpha=Object.Distance<

##### Share on other sites
I would suggest
if (Object.Distance <= (MaxObjectDistance / 2.0f)) {   Alpha = 1.0;} else if (Object.Distance < MaxObjectDistance) {   Alpha = 2.0 - 2.0/MaxObjectDistance * Object.Distance;} else {   Alpha = 0.0;}

so that for half distance (and lower)
Alpha = 2 - 2/Max * Max/2 = 1
and for full distance (and greater)
Alpha = 2 - 2/Max * Max = 0

General solution: A straight line has the definition (polynomial of 1st order)
y(x) = c0 + c1 * x
where y(x=x0) and y(x=x1) is given, so that the unknown coefficients c0 and c1 could be computed. Additionally clip the domain at x0 and x1, and you're done.

##### Share on other sites
I'm not sure if this meets your needs, but have you looked into OpenGL's built in fog system?

##### Share on other sites
hehe..

As most of you suggested, the solution was REALLY simple!!

Alpha = 2.0 - (2.0/MaxObjectDistance)* Object.Distance);

Unless someone makes a way for the fog to "paint" transparency instead of a color, I'm sticking to changing the alpha for objects far away. :)

Thanks for the help!
(although I'm feeling rather stupid at the moment.. hehe. )

##### Share on other sites
Yeah, they fade away with fog, but maybe not quite like you want. You could try using a fog color of black. It still wouldn't be the same as the transparency you are getting with the above formula.

##### Share on other sites
What I really want is blend stuff with the skydome... if I use glFog then objects turn fully white (or whatever color the fog is) but are still visibile on the sky - not wanted.. :/

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627681
• Total Posts
2978611

• 13
• 12
• 10
• 12
• 22