Chapter 5, cone direction

Hello! Thank you for this amazing book, really enjoying it so far!
I’ve got a question though about this line of fragment shader in Chapter 5:

float3 coneDirection = normalize(-light.coneDirection);

Why cone direction should be negated here? Aren’t both light and cone direction vectors originate from the same point? I guess it’s a silly question but I just can’t get my head around it.
Thanks!

It is confusing. Especially when we’re using position to calculate the direction.

Take a look at this picture, which I hope helps.

The light position (pink) is a positive direction. The cone direction is negative. To get them to point in the same direction, we have to negate one of them. Here I chose to negate the cone direction, but I could have negated the lightDirection instead, which probably would have been more “true”.

In that case, I’d have to negate the normal direction too, when calculating the diffuse intensity, which is also confusing! Consider the dot product result you want to achieve. Full intensity is where the dot product is 1.0. That’s when the two vectors point in the same direction. Intuitively, and in the chapter diagram, the direction vectors of the normal and the light direction point in different directions, which gives you a dot product of -1.0. However, to get full intensity of 1.0, you want the normal vector and the light direction vector pointing in the same direction.

I would suggest taking a look at the results in the shader debugger and drawing quick diagrams like the one I just did, to see what the directions actually are.

Once you can visualise the direction vectors, you can use whichever vector directions and dot products make most sense to you.

2 Likes

Wow, thanks for this elaborated answer, it certainly helped a lot! Now I get it! Many thanks!

1 Like

Hi
I have a related problem concerning Specular Reflection. The code, in chapter 5 is:

float specularIntensity =  pow(saturate(-dot(reflection, cameraDirection)), 
                               materialShininess);
specularColor +=  light.specularColor * materialSpecularColor * specularIntensity;

So specularIntensity is a number between 0 and 1 raised to the power of a large number (32 in the example given.

now it seems to me that a shinier material will give rise to greater specular intensity, but the more we raise the materialShininess number, the smaller will be the specularIntensity

e.g 0.9^2 = 0.81. 0.9^3 = 0.729

Is this the effect we’re looking for?

The higher the shininess, the smaller the specular will be.

I’m not sure whether this guy uses quite the same math as I do, but he has some good diagrams:

http://learnwebgl.brown37.net/09_lights/lights_specular.html

Also, note that he says:

If the exponent is large, such as 100, then the cos(angle)exp shrinks around the Y axis and only very small angles will return a significant percentage value. If the exponent is small, such as 1.0, a broad amount of light around the reflection ray will be simulated.

Scratch-a-pixel is also a good resource to know about:

https://www.scratchapixel.com/lessons/3d-basic-rendering/phong-shader-BRDF

In this pic, n is the shininess:

https://www.scratchapixel.com/images/upload/shading-intro2/shad2-phong2.png?

Thank you. It’s very clear. The dot product of the normalised vectors is effectively the cos of the angle and the exponentiation’s job is to attenuate it.

1 Like