PDA

View Full Version : Enabling OGL fog in shaders


kordova
2006.07.10, 12:53 PM
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:
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:
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.

dfmoore
2006.07.10, 06:22 PM
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 :D

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.

OneSadCookie
2006.07.10, 06:23 PM
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.

dfmoore
2006.07.10, 06:31 PM
from "GLSL Binding (http://www.web3d.org/x3d/specifications/ISO-IEC-19775-Amendment1-X3DAbstractSpecification/Part01/shaders_glsl.html)":
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!

dfmoore
2006.07.10, 06:31 PM
Ah, OSC snuck in there with the save. Okay so I wasn't really off track.
Good luck!

kordova
2006.07.10, 07:21 PM
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:

vec4 ecPosition = ftransform( );
gl_FogFragCoord = length( ecPosition );
Would determine the distance from the eye to the point, and the fragment shader 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.

dfmoore
2006.07.12, 04:48 AM
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.

kordova
2006.07.12, 02:12 PM
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.