Discussion:
inverted fog ?
(too old to reply)
Luc Moulinier
2013-09-24 14:25:30 UTC
Permalink
Hello,

I'm writing an app which should display biological macromolecules. My viewer should handle zooming, panning, an Arcball rotation model and recentering, picking and fogging. The panning and zooming are implemented in projection mode through glOrtho parameters :

glMatrixMode GL_PROJECTION
glLoadIdentity
glOrtho \
-$zoom * ($Rad + $xTra) \
$zoom * ($Rad - $xTra) \
-$zoom * ($Rad + $yTra) \
$zoom * ($Rad - $yTra) \
-$Rad \
$Rad

where Rad is the radius of the virtual sphere enclosing all objects, xTra,yTra the translation vector.

The display function is :

glClearColor 0.0 0.0 0.0 0.0
glClear [expr $::GL_COLOR_BUFFER_BIT | $::GL_DEPTH_BUFFER_BIT]

glMatrixMode GL_MODELVIEW
glLoadIdentity
glTranslatef [expr {-1.0*$::pgl(xCen)}] [expr {-1.0*$::pgl(yCen)}] [expr {-1.0*$::pgl(zCen)}]

# Retreive the arcball rotation
set mat [build_rotmatrix $::cQuat]
glMultMatrixf $mat

DrawObjects

Everythinhg is fine, rotate, zoom and translate well. BUT when I apply a fog the objects at the front seem to be dark and the one at the back more lighted. I hypothesize an inversion of the z-axis, but in this case the rotation would be wrong I presume ...

Any idea ??

Many thanks in advance !

Luc
Andy V
2013-09-25 01:28:05 UTC
Permalink
Post by Luc Moulinier
Everythinhg is fine, rotate, zoom and translate well. BUT when I apply a fog the objects at the front seem to be dark and the one at the back more lighted. I hypothesize an inversion of the z-axis, but in this case the rotation would be wrong I presume ...
Perhaps you would care to post the calls that set up the fog?

However, glOrtho does indeed invert the z axis:

glOrtho describes a perspective matrix that produces a parallel projection.
(left, bottom, -near) and (right, top, -near) specify the points on the
near clipping plane that are mapped to the lower left and upper right
corners of the window, respectively, assuming that the eye is located at
(0, 0, 0). -far specifies the location of the far clipping plane. Both
near and far can be either positive or negative. The corresponding matrix
is

| 2 |
|---------- 0 0 t |
|right-left x |
| |
| 2 |
| 0 ---------- 0 t |
| top-bottom y |
| |
| |
| 0 0 -2 |
| -------- t |
| far-near z |
| |
| 0 0 0 1 |

where
right+left
t = - ----------
x right-left

top+bottom
t = - ----------
y top-bottom

far+near
t = - --------
z far-near

I hope the formatting survives posting, and that it is helpful to you.

--
Andy V
Luc Moulinier
2013-09-25 15:29:29 UTC
Permalink
Hello Andy !

Thanks for your help !

The fog is set up like this :
glEnable GL_FOG
set fogColor {0.0 0.0 0.0 1.0}
glFogfv GL_FOG_COLOR $fogColor
glFogi GL_FOG_MODE $::GL_EXP2
glFogf GL_FOG_DENSITY 0.02
glFogi GL_FOG_COORD_SRC $::GL_FRAGMENT_DEPTH
glHint GL_FOG_HINT $::GL_NICEST


If I understand well your comments, as the fog is computed using the fragment depth which is dependent of the z-coordinate, and knowing that glOrtho invert the z axis, this could explain the strange looking of my scene.

What I don't understand then is :
- wy my arcball seems not to be affected ?
- what is the workaround ?


Thanks again !
Luc
Andy V
2013-09-26 01:17:17 UTC
Permalink
Post by Luc Moulinier
If I understand well your comments, as the fog is computed using the fragment depth which is dependent of the z-coordinate, and knowing that glOrtho invert the z axis, this could explain the strange looking of my scene.
Thinking out loud here, the fog is computed using either the distance from the eye to the fragment or, more usually, |Ze| -- the absolute value of the z coordinate in eye space. Eye space, as you may recall, is the coordinate space after applying the model-view. The eye is looking from 0,0,0 towards 0,0,-infinity, but |Ze| increases as you go away from the eye. Increasing |Ze| means an exponentially decreasing f factor. If f is 1 (at the eye), the fragment color is unchanged. When f gets close to zero, the fragment color comes from the fog color.

I don't see your problem yet. Are you sure that you know what is close to the eye and what is far? If you rotate your view matrix (change your look-at point, for example), do the far away things move faster than the near ones?

The fastest thing to do is change your call to glOrtho to use $Rad for near and -$Rad for far to see what happens :-)
Nobody
2013-09-26 08:37:11 UTC
Permalink
Post by Luc Moulinier
If I understand well your comments, as the fog is computed using the
fragment depth which is dependent of the z-coordinate, and knowing that
glOrtho invert the z axis, this could explain the strange looking of my
scene.
glOrtho is supposed to invert the Z axis. In eye space, positive Z is
toward the viewer, negative Z is away from the viewer. In normalised
device coordinates, it's the other way around, with Z=-1 at the near plane
and Z=1 at the far plane. Z is then mapped to the 0..1 range of depth
values based upon glDepthRange() (the defaults have depth=(Z+1)/2).

If you inverted the depth range with e.g. glDepthRange(1, 0), that would
mess up the fog when using GL_FRAGMENT_DEPTH fog.

Similarly, if you rendered the object inside-out using
glDepthFunc(GL_GREATER) (or GL_GEQUAL), that wouldn't necessarily be
obvious with an orthographic projection (other than rotating the wrong
way), until you add fog.

Luc Moulinier
2013-09-25 20:10:25 UTC
Permalink
Sorry I forget to mention that I should have an orthogonal projection.
Luc
Loading...