Jump to content
  • Advertisement
Sign in to follow this  
dgma9271

OpenGL Why use dot product in glsl?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!