Forums > Jitter

model in fog wont preserve texture?


Ad.
January 15, 2013 | 4:53 pm

Hello,
I would like to add some cool fog effects to my models.
the shader doesn’t seem to preserve texture of my duck.dae,poor duck…
how can I bind it into my shader ?
here’s the patch and the shader:

– 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;
}
]]>

thank you in advance !!


January 15, 2013 | 8:41 pm

you have to add texture sampling to your shader,

also you must provide the model’s texture as an attribute to the gl.model object, since you are bypassing the internal material by specifying an external shader.

fwiw, we are fixing a bug with the jit.gl.material fog implementation, which allows you to specify fog parameters directly to the internal materials.
this will be in the next version.

– Pasted Max Patch, click to expand. –

Ambient Multiplier

Diffuse Multiplier

Light Falloff Rate

Fog Density

Fog Density

Fog Color

Specular Multiplier

Specularity Adjustment

varying vec3 N;
varying vec3 P;
varying vec2 texcoord;

void main(void)
{
gl_Position = ftransform();
texcoord = vec2 ( gl_TextureMatrix [ 0 ] * gl_MultiTexCoord0 ) ;
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 + diffuse_tex * 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;
uniform sampler2DRect diffuse_tex;

varying vec3 N;
varying vec3 P;
varying vec2 texcoord;

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

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

MAT_VARS
vec4 diffuse_tex = texture2DRect ( diffuse_tex , texcoord ) ;
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;
}
]]>



Ad.
January 15, 2013 | 9:22 pm

really nice !!
thank you for your quick response
tiouss


Viewing 3 posts - 1 through 3 (of 3 total)