OpenGL - How can I do my own fog without glFog()?
I’m having problems with using glFog(), so I thought I’d do it all myself - but I don’t know how. I can colour my textures from black to original color by simply using the alpha channel, but I want them to be white the further away from the camera…
Easy, a multipass solution would be to draw your textured poly, then calculate the fog color (1.0, 1.0, 1.0, fog(dist(x,y,z))) at the vertices and then blend in another copy of the poly except just gourard shaded (no texture). This example does not take into account any lighting however. You’d also have to account for lights too (light would affect the rgb channels, not the alpha).
In my example, ‘fog’ is just a function of distance. And ‘dist’ is a function that returns the distance separating point (x,y,z) and the viewpoint.
[This message has been edited by DFrey (edited 10-18-2000).]
First thanks DFrey for answering
It looks like you know your stuff.
A multi-pass is probably slower than a single. I don’t need to add extra color, because fogging my sky is enough.
At the moment I can use glColor() to add extra color to my texture. But these values are only multiplied by the texture colors.
Is there a way to multiply these with an extra value (like 2)?
It would look like simple specular lighting - but not the same.
I’ve heard that glColorMaterial can do something like this. I’ve tried it but can’t get it to work.
Is this possible? Can I also do it in 1 pass?
Is it possible to use glBlendFunc() for this case. - To generate my own fog in 1 rendering pass?
You can’t do proper fogging by setting the color of the triangle. Coloring is pre-texture, and fog is post-texture, which means that color is applied before the texture, and fog is applied after the texture.
But as far as I know, it should be possible to do fog in a signle pass using multitexturing. I’m not really sure about how to setup the two texture units and blendfunctions, but maybe someone else here knows.
Save yourself a lot of trouble and sort out whatever problems you’re having with glFog().
I agree with Don’t Disturb, unless you want to do volumetric fog that is (which doesn’t sound like what BasKuenen wants).
If you want to multiply the color by 2 you can. Simply multiply the first argument by the second, and the second by the first, this is the same as multiplying the first by the second times two. Did that make sense?
Ok, multiplicative blend. glBlendFunc(GL_DEST_COLOR,GL_ZERO) right? Multiplicative blend times two is glBlendFunc(GL_DEST_COLOR,GL_SRC_COLOR)
the first one is: srcdst + dst0
the second one is: srcdst + dstsrc == srcdst2
This is extremly useful for lightmaps since you get greater range (0.0 - 2.0) on your lightmaps
Thanks guys for all your answers
You can view this picture, and see the error when I use normal glFog():
This distortion is only on clipped polygons. It almost looks like an OpenGL bug!?
At the moment I’ve got it working somewhat:
1st - I’m clearing the color buffer.
2nd - Blend my textures over them with alpha 1 (for close) and 0 for far away and full fog.
I’m thinking about drawing a nice coloured back plane, so I can get coloured fog
I’m using it for clouds and horizon, and looks already much better than the normal fog. Good thing I’ve got this fog bug
Thanks again for all your answers. I’ll look into them in a moment…
(ps: How can I setup my texture that I can use RGB from the texture and Alpha from glColor?)
Actually, you can use a GL_RGB internal format texture and use GL_REPLACE as your environment. Only the RGB channels will be replaced, while alpha will be passed through.