Sign in to follow this  

DirectX Fog w/ vertex and pixel shader

This topic is 4749 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 am rendering a large pixel/vertex shaded terrain and I want to apply fog to it so I don't have to render the whole thing. DirectX fog is supposed to work with shaders that are pre ps_3_0 but I can't get fog working. The fog works fine on the terrain when I render it without a shader. What it does: It kindof works a little with the shaded terrain. It renders the whole terrain completely covered in fog if I turn it on. I think it has something to do with the D3DDECLUSAGE_FOG render state. I don't know what it does or how it works. I googled it and just got the SDK. Heres the documentation on it: D3DDECLUSAGE_FOG Vertex data contains fog data. Use D3DDECLUSAGE_FOG with a usage index of 0 to specify a fog blend value used after pixel shading finishes. This applies to pixel shaders prior to version ps_3_0. I'm using ps_1_0! Thanks, Joe

Share this post


Link to post
Share on other sites
In shader model 3, there is no fog register or concept of fog. This is because different card vendors interpreted some fog concepts differently. By making the fog implementation up to the game programmer, not the card vendor, each game gets a consistant look, and any special custom fogging techniques they want.

Pre shader model 3, there is fog, however you still have to do some work in the shader, it doesn't magically work.

I've never seen the usage fog... perhaps if you want precomputed fog amounts, you can use that. Usually you want to compute fog on the fly based on cameraspace z.

In HLSL, Cg, etc, I have no idea, but in assembly there is an oFog.x output register. Setting this to 0 means full fog. Setting this to 1 means no fog.

After you've transformed your vertex into viewspace, subtract a fog near plane. Scale to get 1 as your fog far plane. Output 1 - this value, clamped.

ie:

fog = 1 - (pos.z - near)/(far-near)
fog = min(fog, 1)
oFog.x = max(fog, 0)

Fogging while using a W buffer changes things a bit... or it did in DX8. If you're using a W buffer and having troubles, nvidia's developer section has a paper on it.

Share this post


Link to post
Share on other sites

This topic is 4749 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