View Full Version : OpenGL - How can I do my own fog without glFog()?

10-18-2000, 11:37 AM
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...

Thanks ahead,
- Bas

10-18-2000, 12:58 PM
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).]

10-18-2000, 02:44 PM
First thanks DFrey for answering http://www.opengl.org/discussion_boards/ubb/smile.gif
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?

- Bas

10-18-2000, 09:03 PM
Is it possible to use glBlendFunc() for this case. - To generate my own fog in 1 rendering pass?

10-19-2000, 12:20 AM
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.

Don't Disturb
10-19-2000, 06:46 AM
Save yourself a lot of trouble and sort out whatever problems you're having with glFog().

10-19-2000, 07:44 AM
I agree with Don't Disturb, unless you want to do volumetric fog that is (which doesn't sound like what BasKuenen wants).

10-19-2000, 12:36 PM
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: src*dst + dst*0

the second one is: src*dst + dst*src == src*dst*2

This is extremly useful for lightmaps since you get greater range (0.0 - 2.0) on your lightmaps

10-19-2000, 02:47 PM
Thanks guys for all your answers http://www.opengl.org/discussion_boards/ubb/smile.gif

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 http://www.opengl.org/discussion_boards/ubb/smile.gif
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 http://www.opengl.org/discussion_boards/ubb/wink.gif

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?)

Don't Disturb
10-22-2000, 02:19 PM
Originally posted by BasKuenen:
(ps: How can I setup my texture that I can use RGB from the texture and Alpha from glColor?)[/B]Only way I can think of is using GL_MODULATE, making sure that the texture's alpha is all 1 and setting glColor to 1,1,1,alpha

10-22-2000, 06:18 PM
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.

- Matt