Shaders + fog

Started by
13 comments, last by BradDaBug 18 years, 5 months ago
I've been struggling with this and all the stuff I've found about it hasn't quite helped. I've got a vertex shader in GLSL that looks like this:
void main()
{
	gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_TexCoord[1] = gl_MultiTexCoord0;
		
	gl_Position = ftransform();
	gl_FogFragCoord = gl_FogCoord;
} 
And a fragment shader that looks like this:
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
	
void main()
{
	float fog;
	vec4 color;
	vec4 alphacolor = texture2D(texture2, gl_TexCoord[0].st / 64.0);

	color = texture2D(texture0,gl_TexCoord[0].st) * alphacolor.r + 
			texture2D(texture1,gl_TexCoord[0].st) *	(1.0 - alphacolor.r );	

	fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;

	gl_FragColor = color * fog;
}
The texture blending works fine, but I cannot figure out how to get fog to work. Apparently I'm supposed to be setting gl_FogFragCoord to something inside the vertex shader, but what?
I like the DARK layout!
Advertisement
Do you pass fog coordiantes from your OpenGL application? If not and you want to have the standard distance fog you should not write gl_FogCoord to gl_FogFragCoord but the distance from the eye to the current vertex.

mod42
i could not understand what do you do with the shader. do you use a sampler for fog?
you may want to look here: http://www.gamedev.net/community/forums/topic.asp?topic_id=257153
+-+-+-+-+-STR
Quote:Original post by mod42
Do you pass fog coordiantes from your OpenGL application? If not and you want to have the standard distance fog you should not write gl_FogCoord to gl_FogFragCoord but the distance from the eye to the current vertex.

mod42

So I have to calculate that myself? How would I do that? Is the result of ftransform() a vertex in screen space, so that I could just use the z coordinate of gl_Position?
I like the DARK layout!
in glsl you can do something like this
uniform vec3 camera;void main(void) {   gl_Position = ftransform();		   gl_TexCoord[0] = gl_MultiTexCoord0;    float dist = distance(camera,gl_Vertex.xyz);   .   .   other stuff   .   .}

Quote:Original post by BradDaBug
Quote:Original post by mod42
Do you pass fog coordiantes from your OpenGL application? If not and you want to have the standard distance fog you should not write gl_FogCoord to gl_FogFragCoord but the distance from the eye to the current vertex.

mod42

So I have to calculate that myself? How would I do that? Is the result of ftransform() a vertex in screen space, so that I could just use the z coordinate of gl_Position?


What mod42 meant was you need fog to be used before you can start using gl_FogCoord. This envolves either using the normal fog, or volumetric fog (glFogCoordfEXT() or glFogCoordPointerEXT() (although I'm not sure if this is the correct name, it's something like this)) to set the fog coordinate for the vertices/fragments. Alternatively, you can set the fog coordinate in the shader code, usually setting the vertex/fragment fog coordinate to some value based upon the distance from the eye (the bottom row of the modelview matrix).
for standard depthbased fog u dont need to use fogcoord per se
u can also find a fragments depth value by IIRC gl_FragCoord.z
use this number to add an amount of fog
Ok, I changed the line to this in the vertex shader:
gl_FogFragCoord = gl_Position.z;

And the fragment shader looks like this now:
uniform sampler2D texture0;uniform sampler2D texture1;uniform sampler2D texture2;	void main(){	float fog;	vec4 color;	vec4 alphacolor = texture2D(texture2, gl_TexCoord[0].st / 64.0);	color = mix(texture2D(texture1,gl_TexCoord[0].st),texture2D(texture0,gl_TexCoord[0].st), alphacolor.r);	fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;	if (fog >= 0.0)		gl_FragColor = mix(gl_Fog.color,color, fog);	else		gl_FragColor = gl_Fog.color;}

It works now! But you can see that I'm having to use a special case to detect whether or not fog > 0. Is that okay, or am I still doing something wrong?

Also, without using this shader on my terrain I'm getting 200 FPS, but with it I'm getting about 115 FPS. Is that normal?
I like the DARK layout!
you shouldnt need that if statement, just doing gl_FragColor = mix(gl_Fog.color,color, fog); should give you the right result.
Without the if the distant terrain looks like it's glowing. Here's a screen shot to show what I mean.
I like the DARK layout!

This topic is closed to new replies.

Advertisement