I tried to use a FogState to let objects fade out slowly when they get too far, but my planets atmosphere which is generated by a shader is not affected by the Fog.
This makes sense to me, since the shader kinda overwrite everything else.
But I wonder if there is a workaround for such a situation.
Should i just increase the alpha inside the shader depending on the distance to the camera? That would mean, kinda duplicating the logic inside the FogState?
Or apply the Fog in a separate pass would that work? i didn't try that yet.
I don't think a new pass would help, since you would have to re-draw, say, the background on top of far away objects, which would be a lot worse than attempting to recreate the fog in the shader.
I had the same problem when I was working on my texture splatting library.
If you're using ARB shaders it's really easy, just add this line after your header:
OPTION ARB_fog_linear;
If you're using GLSL shaders, it's a bit more complicated; you have to write the fog code yourself. Assuming you are using per-pixel linear fog:
gl_FragColor = mix(color,gl_Fog.color,1.0-(gl_Fog.end-gl_Position.z)*gl_Fog.scale);
Replace "color" with whatever you were going to write to gl_FragColor.
cool thanks momoko it seems to work, i am using glsl shader.
But i get a OpenGLException: Invalid operation (1282) whenever i use gl_Position.z.
If i use hardcoded Values between 100000 and - 100000 i don't get this exception.
I am not sure how there could be invalid values in gl_Position.z.
ah, its gl_FragCoord not gl_Position in the fragment shader.
edit : nope
ok finally got it working.
i had to read the vertex position from the vertexshader.
in vertex shader:
varying vec3 vertexPos;
vertexPos = gl_Position;
in fragment shader:
varying vec3 vertexPos;
gl_FragColor = mix(gl_FragColor, gl_Fog.color, 1.0 - (gl_Fog.end - vertexPos.z) * gl_Fog.scale);
Ah yeah sorry about that.
You can write the Z to gl_FragDepth (it's a predefined varying variable), or you can use your own.
ahh gl_FragDepth it is, i knew there must be something.
i really should get a book to look up those things.
What i found pretty useful also is the: GLSL Quick Reference Guide
Well you're not necessarily forced to write into that variable, you can use your own like I said. I guess that gl_FragDepth should be used if the vertex shader is handled by the fixed pipeline.