• Advertisement
Sign in to follow this  

Making stuff fade over distance..?

This topic is 4342 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 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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
it's verry simple:

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Alpha = Object.Distance <

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sorry something wrong posting in this forum: Alpha=Object.Distance<

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.. :/

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement