Vectors and such

Started by
9 comments, last by boohillie 20 years, 11 months ago
Nathaniel Hammen has given the example regarding physics and normals, I''ll give you an example from lighting.

An important property of the dot product:
a dot b == |a||b|cos(angle)

i.e. the value is the same as the magnitude (length) of vector a multiplied by the magnitude of vector b, multiplied by the cosine of the shortest angle between the two vectors.

If both the vectors are normalised, their magnitudes are both 1. So plugged into the above you get 1*1*cos(angle) which is just the same as cos(angle).

Now imagine you''ve got a flat surface such as a table top, i.e. a plane, a side on view in crap ASCII would be:
  +--------+||      ||||      || 


The normal vector of the table top would point outwards from the plane, combined with the any point on the surface of the table top, the normal vector defines the plane:

     N     ^     |     |+--------+||      ||||      || 



Next, imagine you''re shining a spotlight at the table top, the vector representing the direction that the light is pointed in is marked below as L:

     N   L     ^  /     | /     |/+--------+||      ||||      || 



In terms of purely diffuse lighting (i.e. ignoring any shininess - assume the table is made of matte rubber or something rather than varnished or at all shiny), the more directly the light is pointing at the table, the more light will be reflected from the surface of the table (i.e. the brighter it''ll appear). So if the light is pointing directly at the top of the table, the brightness is maximum, and if it''s at the side, not much will be cast on the surface.

So the brightness level is dependent on the angle between the light and the surface of the table.

The surface of the table is defined by the normal vector, N. The light direction is defined by the direction of the light L.

The dot product gives you cosine of the angle between the two vectors, which leads to the classic Lambertian diffuse calculation of:

I=L.N

i.e. the cosine of the angle between the light and the surface of the table.

The cosine is actually better than just the angle. If you instead got the angle with more traditional trigonometry you''d have a value between 0 and 90 degrees (or the same in radians) ranging between the side and top of the table - to convert this to a light intensity would require some extra work to put the numbers into a nice range (e.g. 0 to 1) and flip them so that 0 means maximum intensity and 90 means minimum.

A cosine is just a ratio, and it gives us a nice value between -1 and 1 with 0 degrees (i.e. the light pointing straight down) being 1 and 0 being side on (i.e. 90 degrees) which is perfect for lighting.

Something I''ve deliberately glossed over is that what you''re really trying to do with I=L.N is see how much of the light can be seen FROM the top of the table rather than how much of the table can be seen from the light. So the L (light) vector needs to point out from the table so that both vectors (conceptually) start from the same point on the table and point outwards. So the L vector for a spotlight should be negated (e.g. L=-L) which will simply change the direction of the vector (swap the end that you''d draw the arrowhead on).

The reason that normals and light vectors need to be normalised should already have become clear. We want the lengths of the vectors to be 1 so that we only get the cosine of the angle between the vectors without their lengths to be taken into account.

IMO with this kind of stuff there really isn''t any substitute for:

a)drawing this stuff on paper and working through the maths by hand with some sample values. See what happens when you pass in the same vector for both, see what happens when one vector is 0,0,0, see what happens when one value or one element is negative etc. Work it through with a calculator, draw the resulting vectors.

b)find a good textbook on this stuff targeted at an appropriate level, preferably one with excercises and answers. Work through as much of it as possible.

c)implement them on a computer and start using them. Make a simple testbed that lets you draw simple things like lines, points, boxes etc and do things like show the columns of a matrix as vectors, try adding scales to the matrix, try adding two vectors together and see what the resultant vector does (try drawing the vectors end to end). A lot of things that you''ve previously thought of as just arbitrary numbers and arbitrary operations that don''t make much sense DO tend to fit into place when you can visualise them in realtime.

--
Simon O''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

This topic is closed to new replies.

Advertisement