PDA

View Full Version : Specular and pow



satan
06-01-2008, 06:03 AM
Hi all,

I am playing around with a simple phong shader and found that the results for a specular exponent (shininess) of zero look very ugly.
This is my code for the specular term:


vec4 Ispec = gl_FrontLightProduct[0].specular
*pow(max(dot(R,E),0.0),gl_FrontMaterial.shininess) ;


Obviously the problem is that pow(0.0, 0.0) is undefined. Now I can workaround this by using max(dot(R,E),epsilon) with a very small non zero epsilon value or use an IF statement. As conditional branching is expensive I like to use the epsilon idea, which looks ok to me. But I am not sure if there are any issues with this approach, meaning that it fails in some cases I did not think of.
Thanks in advance for any input.

ZbuffeR
06-01-2008, 07:52 AM
http://en.wikipedia.org/wiki/Phong_shading

It is said that the "usual" value is 50 for the power exponent.
Indeed, Blender defaults to 50, letting the user enter only integer values between 1 and 511.

Anyway very low exponent values make strongly aliased artifacts near the terminator, so it is not really useful.

ZbuffeR
06-01-2008, 08:08 AM
The Blinn-Phong specular is better in this area, much less artifacts. And the results feel less artificial to the eye.

satan
06-01-2008, 11:24 AM
Thanks for the info. As OpenGL accepts values between 0 and 128 I wanted my shader to do the same. Perhaps I should rethink this, but values near zero look ok and may be usable for special cases.
I also tried Blinn-Phong but it had a lot of trouble with large triangles. I first thought that it was my fault, but after checking everything I found the information that this is a known problem with Blinn-Phong. If this is indeed correct, then it does not fit my needs.

ZbuffeR
06-01-2008, 11:53 AM
But the fixed path lighting does use the blinn half vector method :)
Anyway, using a small epsilon should be safe enough.

Didn't you see the heavily aliased terminator whith a very low exponent, for example when the light is mostly behind the object (in a moon crescent configuration) ?

satan
06-01-2008, 01:10 PM
But the fixed path lighting does use the blinn half vector method :)

At least this I knew. :)


Didn't you see the heavily aliased terminator whith a very low exponent, for example when the light is mostly behind the object (in a moon crescent configuration) ?

Here I cannot follow you. Can you elaborate?

ZbuffeR
06-01-2008, 04:07 PM
I hope I can be more explicit with the help of an image :
http://www.highend3d.com/maya/tutorials/...lg_blinn_hi.jpg (http://www.highend3d.com/maya/tutorials/rendering_lighting/shaders/screenlarge.php?id=186&file=lg_blinn_hi.jpg)
In this setup, I will ignore the whiteish light, and only consider the blueish one, shading the top left part of the sphere.
With a classic phong specular with a very low exponent (3 or less), the specular will appear to bleed through the sphere. It will feel strange, but it will be even worse with shadow casting.

Raider
06-01-2008, 05:40 PM
Just a small correction, pow(0,0) is defined and equals to one 1.
;)

overlay
06-01-2008, 06:42 PM
In maths, conventionally, pow(0,0) is defined and equals to 1.

It is also true for the C function pow():

http://www.hmug.org/man/3/POW.php
"pow(x, +-0) returns 1 for any x, even a NaN."

But for GLSL, the result is undefined. See the GLSL spec 1.20.8, page 57, section "8.2 Exponential Functions"

"Results are undefined if x=0 and y<=0"

Ref: http://www.opengl.org/registry/doc/GLSLangSpec.Full.1.20.8.pdf

satan
06-02-2008, 07:57 AM
I hope I can be more explicit with the help of an image :
http://www.highend3d.com/maya/tutorials/...lg_blinn_hi.jpg (http://www.highend3d.com/maya/tutorials/rendering_lighting/shaders/screenlarge.php?id=186&file=lg_blinn_hi.jpg)
In this setup, I will ignore the whiteish light, and only consider the blueish one, shading the top left part of the sphere.
With a classic phong specular with a very low exponent (3 or less), the specular will appear to bleed through the sphere. It will feel strange, but it will be even worse with shadow casting.
Now I get it and it really is a problem. Testing showed that even exponents of 40 or 50 show this bleeding. So I think that you were right from the beginning and I have to use blinn-phong and better tesselated geometry.
Thanks for the input.