Enabling OGL fog in shaders

Member
Posts: 20
Joined: 2006.06
Post: #1
I have not gotten any assistance on gdev so perhaps someone here can be of assistance.

I have modified the particle system example from the orange book and found that the built-in fog did not work with the shaders. I altered them and found that it worked, but only if I commented out all the lines assigning values to Color except where Background is assigned.

What exactly am I missing as to why assigning a color in the vertex shader breaks fog support?

Vertex Shader:
Code:
uniform float Time;
uniform vec4 Background;

attribute vec3 Velocity;
attribute float StartTime;

varying vec4 Color;

void main( void ) {
  vec4 vert;
  vec4 ecPosition = ftransform( );
  
  gl_FogFragCoord = length( ecPosition );  
  float t = Time - StartTime;
  
  if ( t >= 0.0 ) {
    vert = gl_Vertex + vec4( Velocity * t, 0.0 );
    vert.y -= 4.9 * t * t;
    
    float highestTime = ( Velocity.y / 9.8 );
    // Particle is falling?
    if ( t > highestTime ) {
      float diff = 1.0 - (( t - highestTime )/highestTime );
      if ( diff <= .9 ) diff += .1;
      //Color = vec4( gl_Color.rgb * diff, diff );
    }
    else {
      //Color = gl_Color;
    }
  }
  // Animation has not yet started
  else {
    vert = gl_Vertex;
    Color = Background;
  }
  gl_Position = gl_ModelViewProjectionMatrix * vert;
}

Fragment Shader:
Code:
varying vec4 Color;

void main( void ) {
  float fog;
  
  const float LOG2E = 1.442695;
  fog = exp2( -gl_Fog.density * gl_Fog.density *
              gl_FogFragCoord * gl_FogFragCoord * LOG2E );
              
  fog = clamp( fog, 0.0, 1.0 );
  //fog = 0.0; // Uncomment this to prove that fog _can_ work
  
  vec3 color = mix( vec3( gl_Fog.color ), Color.rgb, fog );
  gl_FragColor = vec4( color, Color.a );
}
Thanks for any assistance.
Quote this message in a reply
Member
Posts: 68
Joined: 2005.09
Post: #2
I have to admit, I haven't picked up my copy of the orange book, and I've only written one shader... which I never saw because my card doesn't suport shaders after all Grin

So take this with a grain of salt / advil. My understanding of the pipeline is that if you invoke your own shader, it bypasses the built in pipeline, which includes fog. You have to include a line in your shader that adds fog, in the same way you have to do all the coordinate space /eye view space etc transforms yourself. I'll see if I can dig up a link.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
Using a vertex shader disables fog. If it is working despite you having a shader enabled, *that* is a bug. If you want a vertex shader, and want fog, you need to calculate the fog yourself in the vertex shader.
Quote this message in a reply
Member
Posts: 68
Joined: 2005.09
Post: #4
from "GLSL Binding":
I.3.2 Fragment shader

The fragment shader replaces the fixed functionality of the fragment processor. The GLSL specification (see [GLSL]) states that the following functionality is disabled if a fragment shader is supplied:

-Textures are not applied.
-Fog is not applied.


I don't know enough about GLSL to know if what you're doing with the passed-in fog variables is proper. So, if none of this is what's happening, I apologize!
Quote this message in a reply
Member
Posts: 68
Joined: 2005.09
Post: #5
Ah, OSC snuck in there with the save. Okay so I wasn't really off track.
Good luck!
Quote this message in a reply
Member
Posts: 20
Joined: 2006.06
Post: #6
Forgive me if my understanding of OpenGL fog/shaders is not correct, but am I not implementing it?

I am aware that I have to implement it. Did you look at the shader code I posted? I get some results with tweaking but not as I would like. I am clearly doing something (or things) incorrectly.

The lines from the vertex shader:
Code:
vec4 ecPosition = ftransform( );
gl_FogFragCoord = length( ecPosition );
Would determine the distance from the eye to the point, and the fragment shader code:
Code:
const float LOG2E = 1.442695;
fog = exp2( -gl_Fog.density * gl_Fog.density *
                    gl_FogFragCoord * gl_FogFragCoord * LOG2E );
              
fog = clamp( fog, 0.0, 1.0 );
  
vec3 color = mix( vec3( gl_Fog.color ), Color.rgb, fog );
gl_FragColor = vec4( color, Color.a );
Would calculate the amount of fog depending upon the distance. (That code is taken almost verbatim from the Orange book, though the book is not 100% clear about the scenario.)

Thanks all.
Quote this message in a reply
Member
Posts: 68
Joined: 2005.09
Post: #7
I did see that... but I'm out on this one, because I don't know enough GLSL to know if that's all you need. But I am really curious now to see how you'd impliment fog in GLSL since I'll be learning it soon.
Quote this message in a reply
Member
Posts: 20
Joined: 2006.06
Post: #8
dfmoore Wrote:I did see that... but I'm out on this one, because I don't know enough GLSL to know if that's all you need. But I am really curious now to see how you'd impliment fog in GLSL since I'll be learning it soon.
Ok, thanks. I have disabled fog in the application for the time being for other reasons and will be revisiting this after its submission in a few weeks.

What I have partially works in that setting that fog = x.x; does blend the fog in. I must be incorrectly determining one of the values used to calculate it.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Enabling vsync with Pygame/PyOpenGL cecilkorik 4 7,363 Apr 12, 2010 07:40 PM
Last Post: manifest020
  GLSL geometry- and multipass-shaders (nogo?) mcMike 3 5,867 May 2, 2008 05:51 AM
Last Post: mcMike
  Program for previewing / editing textures &amp; shaders? haxolotl 4 4,592 Jun 7, 2006 12:48 PM
Last Post: ravuya
  cubemaps with shaders akb825 3 3,198 Apr 5, 2006 06:24 PM
Last Post: akb825
  Shaders for OpenGL 1.5? Nick 14 6,705 Mar 25, 2006 11:23 AM
Last Post: DanLab