gl.shader make the fog effect disappear

Jul 11, 2009 at 4:30pm

gl.shader make the fog effect disappear

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 v2;
#N vpatcher 0 44 1680 1046;
#P origin -19 4;
#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 954 60 29 196617 t 10;
#P newex 950 32 48 196617 loadbang;
#P flonum 1237 376 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 1237 399 97 196617 param outoffset $1;
#P message 728 746 69 196617 shader shady;
#B color 14;
#P message 921 301 79 196617 read op.abs.jxs;
#P flonum 1134 376 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 1134 398 91 196617 param inoffset $1;
#P flonum 1231 325 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 1231 348 93 196617 param outscale $1;
#P flonum 1134 325 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 1134 349 87 196617 param inscale $1;
#P newex 980 427 138 196617 jit.gl.shader lt @name shady;
#B color 5;
#P toggle 455 292 15 0;
#P window setfont “Fixedwidth Serif” 10.;
#N vpatcher 40 55 259 376;
#P window setfont “Fixedwidth Serif” 10.;
#P newex 40 234 128 1441802 prepend position 0 0;
#P outlet 40 272 15 0;
#P newex 40 188 62 1441802 expr $i1 * -0.2;
#P message 116 123 50 1441802 updown;
#P newex 40 92 62 1441802 t i b;
#N counter 10 50;
#X flags 0 0;
#P newobj 40 150 92 1441802 counter 10 50;
#P inlet 40 40 15 0;
#P newex 40 123 62 1441802 qmetro 50;
#N vpatcher 40 55 440 355;
#P pop;
#P newobj 60 327 50 1441802 p mover;
#P connect 2 0 4 0;
#P connect 4 0 1 0;
#P fasten 5 0 3 0 121 144 45 144;
#P connect 1 0 3 0;
#P connect 3 0 6 0;
#P connect 6 0 8 0;
#P connect 8 0 7 0;
#P fasten 4 1 5 0 97 116 121 116;
#P pop;
#P newobj 455 314 50 1441802 p mover;
#P window setfont Arial 12.;
#P comment 424 264 40 -1205862388 Torus;
#P comment 534 264 144 -1205862388 Light and Handle;
#P window setfont “Fixedwidth Serif” 10.;
#P newex 541 434 80 1441802 unpack f f f;
#P newex 541 411 92 1441802 route position;
#P window linecount 3;
#P newex 614 293 115 1441802 jit.gl.handle lt @scale 0.1 @position 1 0.5 0;
#P newex 614 350 150 1441802 jit.gl.gridshape lt @scale 0.05 @color 1 1 1 @position 1 0.5 0;
#B color 5;
#P toggle 28 295 15 0;
#P window linecount 1;
#P message 28 315 43 1441802 fog $1;
#P flonum 293 289 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 258 289 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 222 289 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 186 289 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 150 289 35 10 0 0 0 22 0 0 0 102 102 204 222 222 222 0 0 0;
#P flonum 114 289 35 10 0 0 0 22 0 0 0 40 204 140 222 222 222 0 0 0;
#P flonum 78 289 35 10 0 0 0 22 0 0 0 255 92 174 222 222 222 0 0 0;
#P newex 78 313 250 1441802 pak fog_params 0.2 0.2 0.2 1. 1. 1. 10.;
#P window setfont Times 10.;
#P comment 77 346 226 1310730 R , G , B , Alpha , Density , Start Distance , End Distance;
#P toggle 240 48 15 0;
#P user swatch 104 419 103 30;
#P window setfont “Fixedwidth Serif” 10.;
#P newex 104 454 103 1441802 vexpr $i1 / 255.;
#P newex 104 477 140 1441802 prepend light_ambient;
#P number 11 205 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 11 224 110 1441802 prepend shininess;
#P user swatch 381 419 103 30;
#P newex 381 454 103 1441802 vexpr $i1 / 255.;
#P newex 381 477 140 1441802 prepend light_specular;
#P user swatch 256 112 103 30;
#P newex 256 147 103 1441802 vexpr $i1 / 255.;
#P newex 256 169 128 1441802 prepend mat_specular;
#P user swatch 8 113 103 30;
#P newex 8 148 103 1441802 vexpr $i1 / 255.;
#P newex 8 170 122 1441802 prepend mat_ambient;
#P user swatch 132 113 103 30;
#P message 240 68 110 1441802 auto_material $1;
#P user swatch 246 419 103 30;
#P toggle 9 421 15 0;
#P flonum 658 465 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 622 465 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 586 465 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P flonum 550 465 35 10 0 0 0 22 0 0 0 221 221 221 222 222 222 0 0 0;
#P newex 550 487 188 1441802 pak light_position 0. 0. 0. 1.;
#P newex 246 454 103 1441802 vexpr $i1 / 255.;
#P newex 246 477 133 1441802 prepend light_diffuse;
#P newex 132 148 103 1441802 vexpr $i1 / 255.;
#P newex 132 170 122 1441802 prepend mat_diffuse;
#P toggle 126 48 15 0;
#P message 126 69 110 1441802 smooth_shading $1;
#P toggle 7 50 15 0;
#P message 7 70 113 1441802 lighting_enable $1;
#P user jit.pwindow 426 40 346 219 0 1 0 0 1 1;
#X name lt;
#P message 427 21 140 1441802 name lt , depthbuffer 1;
#P window setfont Arial 12.;
#P comment 3 28 149 -1205862388 Lighting Parameters;
#P comment 3 396 119 -1205862388 OpenGL Renderer;
#P window setfont “Fixedwidth Serif” 10.;
#P newex 9 470 62 1441802 t b erase;
#P window linecount 3;
#P newex 384 344 121 1441802 jit.gl.gridshape lt @shape torus @position 0 0 -3;
#B color 5;
#P window setfont Times 10.;
#P window linecount 1;
#P comment 25 422 72 1310730 Start Rendering;
#P window setfont “Fixedwidth Serif” 10.;
#P newex 9 446 62 1441802 qmetro 50;
#P newex 9 529 103 1441802 jit.gl.render lt;
#B color 5;
#P user panel 3 410 519 142;
#X brgb 195 209 218;
#X frgb 0 0 0;
#X border 0;
#X rounded 0;
#X shadow 0;
#X done;
#P user panel 3 42 414 216;
#X brgb 195 209 218;
#X frgb 0 0 0;
#X border 0;
#X rounded 0;
#X shadow 0;
#X done;
#P user panel 534 280 237 273;
#X brgb 195 209 218;
#X frgb 0 0 0;
#X border 0;
#X rounded 0;
#X shadow 0;
#X done;
#P window setfont Arial 12.;
#P comment 4 266 109 -1205862388 Fog Parameters;
#P user panel 4 280 359 112;
#X brgb 195 209 218;
#X frgb 0 0 0;
#X border 0;
#X rounded 0;
#X shadow 0;
#X done;
#P user panel 374 280 149 112;
#X brgb 195 209 218;
#X frgb 0 0 0;
#X border 0;
#X rounded 0;
#X shadow 0;
#X done;
#P window setfont “Arial Bold” 18.;
#P window linecount 2;
#P comment 811 733 280 -1195376622 < <<<<< When you click , then the fog effect disappears;
#P connect 78 0 17 0;
#P connect 17 0 16 0;
#P connect 35 0 34 0;
#P connect 34 0 33 0;
#P fasten 78 0 29 0 955 259 14 259;
#P connect 29 0 8 0;
#P connect 8 0 11 0;
#P fasten 24 0 7 0 555 524 14 524;
#P fasten 44 0 7 0 109 497 14 497;
#P fasten 39 0 7 0 386 503 14 503;
#P fasten 22 0 7 0 251 500 14 500;
#P fasten 11 1 7 0 66 494 14 494;
#P connect 11 0 7 0;
#P connect 43 0 42 0;
#P fasten 78 0 58 0 955 259 33 259;
#P connect 58 0 57 0;
#P connect 46 0 45 0;
#P connect 45 0 44 0;
#P fasten 50 0 49 1 83 308 117 308;
#P connect 78 0 19 0;
#P connect 19 0 18 0;
#P connect 32 0 21 0;
#P connect 21 0 20 0;
#P fasten 51 0 49 2 119 308 151 308;
#P fasten 52 0 49 3 155 308 185 308;
#P fasten 53 0 49 4 191 308 219 308;
#P connect 78 0 47 0;
#P connect 47 0 31 0;
#P connect 30 0 23 0;
#P connect 23 0 22 0;
#P fasten 54 0 49 5 227 308 253 308;
#P connect 38 0 37 0;
#P connect 37 0 36 0;
#P fasten 55 0 49 6 263 308 287 308;
#P fasten 79 0 56 0 959 259 298 259;
#P fasten 56 0 49 7 298 308 321 308;
#P connect 41 0 40 0;
#P connect 40 0 39 0;
#P connect 65 0 10 0;
#P fasten 42 0 10 0 16 245 389 245;
#P fasten 57 0 10 0 33 340 389 340;
#P fasten 20 0 10 0 137 195 389 195;
#P fasten 33 0 10 0 13 199 389 199;
#P fasten 16 0 10 0 12 95 389 95;
#P fasten 31 0 10 0 245 89 389 89;
#P fasten 18 0 10 0 131 92 389 92;
#P fasten 36 0 10 0 261 191 389 191;
#P fasten 49 0 10 0 83 336 389 336;
#P connect 75 0 10 0;
#P connect 14 0 15 0;
#P fasten 78 0 66 0 955 259 460 259;
#P connect 66 0 65 0;
#P fasten 60 0 61 0 619 341 546 341;
#P connect 61 0 62 0;
#P fasten 62 0 25 0 546 460 555 460;
#P fasten 62 1 26 0 581 460 591 460;
#P fasten 25 0 24 1 555 483 599 483;
#P connect 60 0 59 0;
#P fasten 62 2 27 0 616 460 627 460;
#P fasten 26 0 24 2 591 483 643 483;
#P fasten 27 0 24 3 627 483 687 483;
#P fasten 28 0 24 4 663 483 731 483;
#P fasten 78 0 74 0 955 175 926 175;
#P connect 78 0 79 0;
#P connect 74 0 67 0;
#P fasten 72 0 67 0 1139 420 985 420;
#P fasten 70 0 67 0 1236 371 985 371;
#P fasten 68 0 67 0 1139 371 985 371;
#P fasten 76 0 67 0 1242 420 985 420;
#P connect 69 0 68 0;
#P connect 73 0 72 0;
#P connect 71 0 70 0;
#P connect 77 0 76 0;
#P pop;

You’ll need to restart it…

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

Ad.

#44701
Jul 11, 2009 at 8:38pm

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);
#160864
Jul 11, 2009 at 9:45pm

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.

#160865
Jul 11, 2009 at 10:19pm

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.

#160866
Jul 12, 2009 at 9:18am

Here’s my jxs file:

Input scale

Input offset

Output scale

Output offset





< ![CDATA[
varying vec2 texcoord0;

uniform vec4 inscale;
uniform vec4 inoffset;
uniform vec4 outscale;
uniform vec4 outoffset;
uniform sampler2DRect texture;

void main (void)
{
gl_FogFragCoord = gl_FogCoord;
}
]]>

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’

#160867
Jul 12, 2009 at 9:20am

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

Thanks for help.

Ad.

#160868
Jul 13, 2009 at 4:30pm

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:

– Pasted Max Patch, click to expand. –

————————————–
————————————–
mat.phong.1.light.fog.falloff.jxs
————————————–

Ambient Multiplier

Diffuse Multiplier

Light Falloff Rate

Fog Density

Fog Density

Fog Color

Specular Multiplier

Specularity Adjustment

varying vec3 N;
varying vec3 P;

void main(void)
{
gl_Position = ftransform();
N = gl_NormalMatrix * gl_Normal;
P = (gl_ModelViewMatrix*gl_Vertex).xyz;
}
]]>

#define MAT_VARS float light_distance;
float light_exponent;
float light_factor;
vec3 light_pos_vec;
vec3 Ldir;
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 Hn;

#define LIGHT(num) light_pos_vec = gl_LightSource[num].position.xyz – P;
light_distance = length(light_pos_vec);
light_exponent = light_distance*light_falloff;
light_factor = exp2(-abs(light_exponent));
Ldir = normalize(gl_LightSource[num].position.xyz – P);
Hn = normalize(Ldir + Vn);
ambient = gl_LightSource[num].ambient*Ka;
diffuse = gl_LightSource[num].diffuse*Kd*max(dot(Nn, Ldir), 0.);
specular = gl_LightSource[num].specular*Ks*pow(max( dot(Nn, Hn) , 0.), shininess);
color += ambient + light_factor*(diffuse + specular);

uniform vec4 Ka;
uniform vec4 Kd;
uniform float light_falloff;
uniform float fog_density;
uniform float fog_start;
uniform vec4 fog_color;
uniform vec4 Ks;
uniform float shininess;

varying vec3 N;
varying vec3 P;

void main(void)
{
vec3 Nn = normalize(N);
vec3 Vn = normalize(-P);

vec4 color = vec4(0., 0., 0., 0.);

MAT_VARS
LIGHT(0)

float distance = max(length(P)-fog_start, 0.);
float fogExponent = distance*fog_density;
float fogFactor = exp2(-abs(fogExponent));
color = mix(fog_color, color, fogFactor);

gl_FragColor = color;
}
]]>

#160869
Jul 13, 2009 at 4:56pm

Thank you so much !!
It Works just fine.

Ad.

#160870

You must be logged in to reply to this topic.