fog and specular highlights problem

Hi!

I programmed a simple routine that renders 3d-object with specular highlights on the surfaces by using alpha blending (GL_SRC_ALPHA, GL_ONE). Or better, it renders the object as it is and then it re-render it with the specular texturemap in alpha blending.
The problem is that when I added the FOG feature, fog seems not to affect the specular highlights: in fact when the object is very far from the viewpoint, the “solid” part of the object correctly fades to the fog color, but the specular-highlight texture is normally rendered!..any suggestion to avoid this problem? thanx!

Well, think about your math for a second. If you wanted specular highlights with your object, and you want the entire thing to be fogged, I think it might need 3 rendering passes.

1st pass you want to render the object with your normal object texturing, lighting, whatever is supposed to be ‘painted’ on the surface, with no fog.

2nd pass is where you want to do an additive blend of your specular highlight(s) of your light sources. {edit: no fog on 2nd pass either /edit}

3rd pass would be a multiplicitive blend of the fog color/value/intinsity with everything in the 2 previous rendering passes. This means just render white polygons with fog enabled and do a multiplicitve blend.

Dan

[This message has been edited by Dan82181 (edited 07-16-2002).]

for you highlightpass check/try following:

  • is your glTexEnv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE) ?
  • use GL_SRC_COLOR instead of GL_SRC_ALPHA.

but when you get it running you will notice, that it still does not rendering what you really want…that’s because you can’t use fogging with additive blending correctly without any special tweaking.

example:

1.pass diffuse lighted/textured part. -> no blending
2.pass specular highlights -> gl_one,gl_one

when you draw the first pass, the fog is applied to the geometry and rendered into framebuffer.
in the second pass, the fog is applied again, and when blending, you blend two fogged colors together.
so when your object is nearly fully inside the fog, the parts with specular highlights are much brighter than the diffuse only parts…

let’s take a look at the color of a single pixel:

without fog:
1.pass: source 0,0,30
framebuffer 0,0,30
2.pass: source 100,0, 0 (highlight, added to FB)
framebuffer final 100,0,30

now let’s say fog is applied with a blendfactor of 0.5 and its color is 100,100,100

now let’s take a look at our pixel:
1.pass: source 0, 0,30
aplly fog 50,50,65
framebuffer 50,50,65
2.pass: source 10,0,0 (highlight, added to FB)
apply fog 60,50,50
framebuffer final 110,100,65

as you can see this isn’t what you want at all !!! what you want is to apply the fogging to the final color. and this can be only acomplished with a 3rd rendering pass…if you want to make it correctly.
however, when you are only applying specular highlights in the 2nd pass, you can do following:
render the 1st pass with fogging enabled and the second one without fogging, but with attenuated highlight intensity over the distance from the viewpoint.(computed based on the same values like in the standard-fogging)

hope, this helps you a bit.

adrian

[This message has been edited by AdrianD (edited 07-16-2002).]

Originally posted by AdrianD:
however, when you are only applying specular highlights in the 2nd pass, you can do following:
render the 1st pass with fogging enabled and the second one without fogging, but with attenuated highlight intensity over the distance from the viewpoint.(computed based on the same values like in the standard-fogging)

It’s easier to set fog color to black in the 2nd pass and leave fogging enabled. This way you eliminate the term of fog equation in which fog color appears so it’s not added to the framebuffer twice. Of course it works only with blending set to GL_ONE, GL_ONE - other blend modes require different tricks.

[This message has been edited by coop (edited 07-17-2002).]

Thank you ery much for your help!!!
All the solutions that you suggested work perfectly…I personally prefer to set the fog color to {0,0,0,0} while I render the highlights: I would have liked to avoid a third rendering pass (damn slow!) and that was the only way! What a stupid I am! How I could have not thought about that! thanx for your help m8s