# OpenGL Why use dot product in glsl?

This topic is 3677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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 (:

##### Share on other sites
Quote:
 Original post by dgma9271But 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.

##### Share on other sites
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?

##### Share on other sites
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.

##### Share on other sites
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 ;)

##### Share on other sites
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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11