• FEATURED

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

## Why use dot product in glsl?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1dgma9271  Members

Posted 28 September 2009 - 12:10 PM

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?

### #2Washu  Senior Moderators

Posted 28 September 2009 - 12:18 PM

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.

### #3Atrix256  Members

Posted 28 September 2009 - 12:19 PM

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 (:

### #4OrangyTang  Members

Posted 28 September 2009 - 12:33 PM

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.

### #5dgma9271  Members

Posted 28 September 2009 - 12:38 PM

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?

### #6Atrix256  Members

Posted 28 September 2009 - 12:40 PM

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.

### #7leet bix  Members

Posted 28 September 2009 - 12:43 PM

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 ;)

### #8dgma9271  Members

Posted 28 September 2009 - 12:48 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.