POV-Ray : Newsgroups : povray.advanced-users : How to avoid or remove fog from the backgound? Server Time
26 Dec 2024 02:17:37 EST (-0500)
  How to avoid or remove fog from the backgound? (Message 1 to 6 of 6)  
From: Elmar Krieger
Subject: How to avoid or remove fog from the backgound?
Date: 27 Jul 2005 02:35:01
Message: <web.42e72a35808d84e1e78d3d640@news.povray.org>
Hi!

Two things I'm chewing on:

1) Create fog that doesn't start immediately at the camera, but only at a
certain distance from it, so that some close objects are not fogged at all.
Comparable to the OpenGL GL_FOG_MODE GL_LINEAR feature that allows to
specify where the fog starts and where it reaches the maximum density.

What I am doing now is abusing ground fog, by rotating it such that the
'ground' is parallel to the viewplane (my camera is at <0,0,1024> and looks
at <0,0,-1>):

fog { distance 1500 fog_type 2 color rgbt<0,0,0,0.1> up <0,0,1> fog_offset
-5559 fog_alt 1341.000000 }

Is this the only way to emulate what I want, or am I missing something that
gets closer to OpenGL?

2) I'm trying hard to create a fogged object (that gets darker in the back)
in front of a transparent background (using +UA +FN command line options).
But I can't keep the fog from affecting the background color and alpha
value. The only hack I can currently think of is render the image twice,
once with and once without fog, and  use the alpha channel of the second
image to calculate how much fog I have to subtract from the first.

Is there any way to do it in one render pass? Can I somehow keep the fog
from modifying the background, or (also helpful) the alpha channel?

Many thanks for your help,
Elmar


Post a reply to this message

From: Warp
Subject: Re: How to avoid or remove fog from the backgound?
Date: 27 Jul 2005 14:11:07
Message: <42e7ce3b@news.povray.org>
Elmar Krieger <elm### [at] cmbirunl> wrote:
> 1) Create fog that doesn't start immediately at the camera, but only at a
> certain distance from it, so that some close objects are not fogged at all.
> Comparable to the OpenGL GL_FOG_MODE GL_LINEAR feature that allows to
> specify where the fog starts and where it reaches the maximum density.

  You have to take into account that povray, as a raytracer, can also
calculate reflections and refractions. Thus you have to make a distinction
between fog which is calculated along a ray (regardless of where the ray
starts) and a fog which is calculated in relation to the location of the
camera.

  Probably what you want is a "sphere" of fog centered on the camera so
that the "inside" of the sphere is fog-free and then, at some distance,
the fog starts getting denser.

  If I'm not complately mistaken (I haven't actually tried it) you can
get this effect by using a global media (which type? that's a good
question) using spherical mapping (just scale the media big enough).
  Even though media cannot be sampled for rays which do not hit anything,
I think that povray still calculates the color of the media correctly
assuming that it travels an infinite distance inside it (or at least
I remember encountering this phenomenon in the past).

  If it works it will give you a sphere of "fog" around the camera which
will work correctly for reflections and refractions also.

-- 

                                                          - Warp


Post a reply to this message

From: Elmar Krieger
Subject: Re: How to avoid or remove fog from the backgound?
Date: 27 Jul 2005 18:25:00
Message: <web.42e808c45ae24a7b386d56720@news.povray.org>
Warp wrote:
> Elmar Krieger wrote:
> > 1) Create fog that doesn't start immediately at the camera, but only at a
> > certain distance from it, so that some close objects are not fogged at all.
> > Comparable to the OpenGL GL_FOG_MODE GL_LINEAR feature that allows to
> > specify where the fog starts and where it reaches the maximum density.
>
>   You have to take into account that povray, as a raytracer, can also
> calculate reflections and refractions. Thus you have to make a distinction
> between fog which is calculated along a ray (regardless of where the ray
> starts) and a fog which is calculated in relation to the location of the
> camera.
>
>   Probably what you want is a "sphere" of fog centered on the camera so
> that the "inside" of the sphere is fog-free and then, at some distance,
> the fog starts getting denser.

Thanks for the hint. Trying to mimic OpenGL, the distance from the viewplane
is more important for me, so I just put my objects into a hollow box and
filled it with absorbing media. This way I got indeed a nice fog (at least
black, other fog colors seem difficult), but rendering took a factor 3
longer. Anyway, the original trick with fast rotated ground fog along the
Z-axis also worked.

My main problem is still to keep the fog from affecting the background (I
want to insert my own non-ray-traced background using the alpha channel of
the +UA +FN output).

>
>   If I'm not complately mistaken (I haven't actually tried it) you can
> get this effect by using a global media (which type? that's a good
> question) using spherical mapping (just scale the media big enough).
>   Even though media cannot be sampled for rays which do not hit anything,
> I think that povray still calculates the color of the media correctly
> assuming that it travels an infinite distance inside it (or at least
> I remember encountering this phenomenon in the past).

Unfortunately yes, I need a way to just ignore these rays traveling an
infinite distance.

Maybe a solution: I just found a mod called MegaPOV which can also output a
depth map in a single render pass. Then I could render my scene without any
fog (and get the alpha channel I need), and write a little program to add
fog myself using the data from the depth map?

Lots of hacks...

Ciao,
Elmar


Post a reply to this message

From: Abe
Subject: Re: How to avoid or remove fog from the backgound?
Date: 28 Jul 2005 07:15:01
Message: <web.42e8bd495ae24a7bda0ab25c0@news.povray.org>
The limitations abound, but:

1. Function based texture applied to all objects but which doesn't affect
the background. E.g. function(sqrt(x*x+y*y+z*z)) appropriately translated
and normalized to taste. This method will fall flat with reflections and so
forth.

2. Vahur Krouverk's Povman may give more access to the ray length
information, including reflections, for shader writing. Not sure though.

3. A questionable alternative to not affecting the background color with
fog, is to render a dual colored fog which is, say white, in the foreground
and cyan in the extreme distance, effectively creating a different colored
background suitable as a mask. This requires two fog statements: foreground
colored fog and backgound fog set to extreme distance with a
subtractive/addative color which will result in the desired background
color when applied to the forground color.

Regarding media fog, you should be able to duplicate colored fog using
emission media (fog color) and absorption media (white). You may also have
to set the absorption intensity to about 10x that of the emission media.

Abe


Post a reply to this message

From: Elmar Krieger
Subject: Re: How to avoid or remove fog from the backgound?
Date: 2 Aug 2005 09:05:04
Message: <web.42ef6e8b5ae24a7b5006563e0@news.povray.org>
Thanks to everyone for all the hints, I got it working now.
In case someone else googles this page, I post the solution:

1) Create a depthmap of the scene. First I wanted to use the built-in
depthmap support of MegaPOV, but it turned out that this takes much longer
than rendering the image a second time in PovRay using a Z-gradient texture
like this one for all objects:

fprintf(ft,"#declare DephTex = texture { pigment { gradient z scale %d
color_map { [0.0 color rgb<0,0,0>] [%.5f color rgb<0,0,0>] [%.5f color
rgb<1,1,1>] [1.0 color rgb<1,1,1>] } } finish { ambient 1.0 }
}n",ply_clipfarz, (double)fogzmin/ply_clipfarz,
(double)fogzmax/ply_clipfarz);

ply_clipfarz: the Z-coordinate of the far clipping plane
fogzmin: the Z-coordinate where the fogging and thus the depth map should
start.
fogzmax: the Z-coordinate where the fogging reaches the maximum and the
depth map ends.

2) Render both the real image and the depthmap with transparent background
+UA +FN

3) Load the image (srcsrf), the depthmap (zbfsrf) and the background
(dstsrf), add fog to the image using the info from the depthmap and blend
it on top of the background:

(code is based on www.libsdl.org).


  /* ADD RAYTRACED IMAGE, CREATING FOG FROM ZBUFFER */
  for (i=0;i<dstsrf->h;i++)
  { dstpixel=(int32*)((char*)dstsrf->pixels+i*dstsrf->pitch);
    srcpixel=(int32*)((char*)srcsrf->pixels+i*srcsrf->pitch);
    for (j=0;j<dstsrf->w;j++)
    { /* SCALE WITH 1.008 TO MAKE SURE THAT POVRAY'S ALMOST WHITE (0xfdfdfd)
BECOMES TRUE WHITE (0xffffff) */
      alpha=col_alpha(*srcpixel);
      color=col_scale(*srcpixel,1.008);
      if (alpha)
      { /* PIXEL NOT EMPTY, GET DEPTH */
        depth=((int32*)((char*)zbfsrf->pixels+(i*zbfsrf->pitch)))[j];
        if (col_alpha(depth)) depth=col_red(depth);
        else
        { /* AS THE ZBUFFER IS RENDERED WITHOUT ANTIALIASING, IT MAY HAPPEN
THAT
             ANTIALIASED PIXELS AT THE IMAGE EDGES DO NOT HAVE A
CORRESPONDING PIXEL IN
             THE ZBUFFER - IN THIS CASE AVERAGE THE 4 SURROUNDING PIXELS */
          depths=0;
          depthsum=0;
          for (k=-1;k<=1;k++)
          { if (i+k>=0&&i+k<dstsrf->h)
            { for (l=-1;l<=1;l++)
              { if (j+l>=0&&j+l<dstsrf->w&&k*l==0)
                {
depth=((int32*)((char*)zbfsrf->pixels+((i+k)*zbfsrf->pitch)))[j+l];
                  if (col_alpha(depth))
                  { depthsum+=col_red(depth);
                    depths++; } } } } }
          if (depths) depth=depthsum/depths; }
        /* ADD FOG */
        color=col_blended(color,ply_fogcolorrgb,(255.0-depth)/255); }
      /* BLEND WITH THE BACKGROUND, SET ALPHA TO 255 */
      color=col_blended(color,*dstpixel,alpha/255.0)|COL_ALPHAMASK32;
      *(dstpixel++)=color;
      srcpixel++; } }

Hope it helps,
Elmar



"Abe" <bul### [at] taconicnet> wrote:
> The limitations abound, but:
>
> 1. Function based texture applied to all objects but which doesn't affect
> the background. E.g. function(sqrt(x*x+y*y+z*z)) appropriately translated
> and normalized to taste. This method will fall flat with reflections and so
> forth.
>
> 2. Vahur Krouverk's Povman may give more access to the ray length
> information, including reflections, for shader writing. Not sure though.
>
> 3. A questionable alternative to not affecting the background color with
> fog, is to render a dual colored fog which is, say white, in the foreground
> and cyan in the extreme distance, effectively creating a different colored
> background suitable as a mask. This requires two fog statements: foreground
> colored fog and backgound fog set to extreme distance with a
> subtractive/addative color which will result in the desired background
> color when applied to the forground color.
>
> Regarding media fog, you should be able to duplicate colored fog using
> emission media (fog color) and absorption media (white). You may also have
> to set the absorption intensity to about 10x that of the emission media.
>
> Abe


Post a reply to this message

From: Thomas de Groot
Subject: Re: How to avoid or remove fog from the backgound?
Date: 3 Aug 2005 04:02:35
Message: <42f07a1b$1@news.povray.org>
"Elmar Krieger" <elm### [at] cmbirunl> schreef in bericht
news:web.42e72a35808d84e1e78d3d640@news.povray.org...
> Hi!
>
> Two things I'm chewing on:
>
> 1) Create fog that doesn't start immediately at the camera, but only at a
> certain distance from it, so that some close objects are not fogged at
all.
> Comparable to the OpenGL GL_FOG_MODE GL_LINEAR feature that allows to
> specify where the fog starts and where it reaches the maximum density.
>

One very simple solution is using a completely transparant, not hollow,
vertical plane between the camera and the background. The orientation of the
plane's normal determines where the fog will be invisible, on the camera
side or the background side. I used this trick in the past and it works to
complete satisfaction.

Thomas


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.