gl.shader make the fog effect disappear

Ad.'s icon

hi all.

I would like to use a shader on a gl.model.
There 's already fog effect apply to this model.
And when I tell the shader object to apply its shade, the fog (and light) effect disappears.
Here's the patch which shows the thing:

Max Patch
Copy patch and select New From Clipboard in Max.

You'll need to restart it...

Has anyone know the way to avoid this issue ?
Thx.

Ad.

Rob Ramirez's icon

fog must be computed in the fragment shader.

relevant code from the orange book for linear fog computation with clamping:

gl_FogFragCoord = gl_FogCoord;
float fog = (gl_Fog.end - gl_FogFragCoord)) * gl_Fog.scale;
fog = clamp(fog, 0.0, 1.0);
//Applying fog to compute final color value
color = mix(vec3(gl_Fog.color), color, fog);

Ad.'s icon

Thank you.

I must have done something wrong in the jxs ' structure:

Here's what Max told me:

• error: -- START GLSL INFO LOG: fp --
ERROR: 0:17: 'gl_FogCoord' : undeclared identifier
ERROR: 0:17: 'assign' : l-value required "gl_FogFragCoord" (can't modify a varying)
ERROR: 0:18: '=' : cannot convert from 'float' to '4-component vector of float'
ERROR: 0:18: ')' : syntax error syntax error
ERROR: Parser found no code to compile in source strings.
• error: -- END GLSL INFO LOG: fp --
• error: jit.gl.shader: GLSL program failed to compile.

Rob Ramirez's icon

whoops, it looks like fog should be a float, not a vec4.

i just posted some snippets from the book without testing, but the general idea is there.

Ad.'s icon

Here's my jxs file:

        Input scale
        Input offset
        Output scale
        Output offset

void main( void )
{
    vec4 v0 = texture2DRect(texture, texcoord0);

    v0 = (v0*inscale)+inoffset;    
    vec4 result = abs(v0);
    gl_FragColor = (result*outscale)+outoffset;
    float fog = (gl_Fog.end - gl_FogFragCoord)) * gl_Fog.scale;
    fog = clamp(fog, 0.0, 1.0);
    vec4 color = mix(vec3(gl_Fog.color), color, fog);
}
]]>

Max is not yet happy:

• error: jit_xml_document: error reading file at byte offset 1303 / not well-formed (invalid token)
• error: line (48): >
• error: jit.gl.shader: error reading shader xml file 'Macintosh HD:/Users/adrienfontaine/Desktop/op.abs.jxs'

Ad.'s icon

I am searching in a red book, but can't find anything about that !

Thanks for help.

Ad.

Wesley Smith's icon

I don't know what the shader you posted is supposed to do, but if you're trying to have fog with a shader, here's an example:

Max Patch
Copy patch and select New From Clipboard in Max.

--------------------------------------
--------------------------------------
mat.phong.1.light.fog.falloff.jxs
--------------------------------------

        Ambient Multiplier
        Diffuse Multiplier
        Light Falloff Rate
        Fog Density
        Fog Density
        Fog Color
        Specular Multiplier
        Specularity Adjustment

Ad.'s icon

Thank you so much !!
It Works just fine.

Ad.

thomas.goepfer's icon

Hi Wesley,

Any chance to have a copy of your shader mat.phong.1.light.fog.falloff.jxs ?

[EDIT] Thanks to archive.org !!! https://web.archive.org/web/20130322030458/http://cycling74.com/forums/topic.php?id=44686

mat.phong.1.light.fog.falloff.jxs
jxs 3.09 KB