Sign in to follow this  

Shaders + fog

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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
.
.
}


Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Turns out the frustum was set further back than the fog, so pixels were being drawn further than gl_Fog.end, so fog was negative. Setting the fog end equal to the far plane fixed it.

Share this post


Link to post
Share on other sites

This topic is 4418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this