Why use dot product in glsl?

Started by
6 comments, last by dgma9271 14 years, 6 months ago
I'm new to opengl shading language. I see that dot product is used frequently in shading language. My question is: what does it mean? I mean i know what dot product is. It's simply the sum of the products of corresponding scalar components of two vectors. But sayin' that when used in a glsl shader what does it actually mean according to glsl context? Also why use it to calculate ambient, diffuse and specular intensity?
Advertisement
In general you will find that the dot product operator is used quite frequently when dealing with lighting. This is because given two unit vectors, such as a face normal and the light direction normal, then the dot product gives us cos(θ). If you look at a sample lighting equation, like Phong, then you'll note that you can directly substitute that straight into the equation to get the resulting lighting value.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Dot product between 2 normalized vectors will give you a number that ranges from -1 to 1.

1 means that they are pointed the same way

-1 means they are pointed opposite ways.

0 means they are perpendicular.

(etc)

Anyhow, since many lighting calculations are based upon reflection, you need to do things like find the difference in angle between a triangle face's normal vs the direction of light, so dot product comes in useful there.

that's the high level reasoning, if you are curious for more details hopefully someone else can explain deeper (:
Quote:Original post by dgma9271
But sayin' that when used in a glsl shader what does it actually mean according to glsl context?

It's important to realise that there's nothing particularly special about the built-in 'dot' function - it doesn't use any glsl context, nor does it change the glsl state. You'd get exactly the same results if you did the dot product manually.

The only difference is that by having it as a built-in function it allows for more efficient instructions to be generated under the hood.
Thanks for explaining it.

> Anyhow, since many lighting calculations are based upon reflection, you need to
> do things like find the difference in angle between a triangle face's normal vs
> the direction of light, so dot product comes in useful there.
>
> that's the high level reasoning, if you are curious for more details hopefully
> someone else can explain deeper (:


But how do you calculate angle difference with dot product? I could be wrong but
are you sayin that dot product of face normal and light direction is the angle
difference between them? Can you kindly explain a bit?
When you dot 2 vectors, you don't get the angle between them, you get the COSINE of the angle between them (:

so, you can do an inverse cosine to get the angle - OR, if you have need of the cosine of the angle, then you are done and can just use the dot product, it's kinda cool.

The vectors have to be normalized though for that to work.
Based on what Atrix256 said,

"Dot product between 2 normalized vectors will give you a number that ranges from -1 to 1.

1 means that they are pointed the same way

-1 means they are pointed opposite ways.

0 means they are perpendicular."


We can use this to calculate the angle between them, this is because you have the cosine between the two, so if you use inverse cos^-1(dot product between two vectors) that will give you the angle.

EDIT: Beaten to it ;)
Thanks to ya all. I get it now.


> We can use this to calculate the angle between them, this is because you have
>the cosine between the two, so if you use inverse cos^-1(dot product between two
>vectors) that will give you the angle.

Ooops. I donno how i missed that. Thanks.

This topic is closed to new replies.

Advertisement