# OpenGL and Vector Math Basics

CS 481 Lecture, Dr. Lawlor

## C++ Libraries used with OpenGL

You can use many libraries along with OpenGL itself.  Here are a few we'll be using in this course:

## Calls you Should Already Know

(or else calls you need to look up right now!)

### Vectors, dot, and cross products

You should know basic 2D and 3D vectors, including dot products and cross products. Vectors are basically all we ever deal with in computer graphics, so it's important to understand these pretty well.

In particular, you should know at least:
• Vectors as positions/locations/coordinates, offsets, and unit-length directions.  Know how to calculate a vector's length, and know that "to normalize" means to make a vector have unit length by dividing it by its length.
• Homogenous vector interpretation: (x,y,z,1) is a location; (x,y,z,0) is a direction (er, usually).
• "It's just a bag of floats" vector interpretation: the color (r,g,b,a) can and is treated like as a vector, especially on the graphics hardware.
• Dot product: compute the "similarity" of two vectors
• A dot B = A.x*B.x + A.y*B.y + A.z*B.z
• For unit vectors, A dot B is the cosine of the angle between A and B.
• You can figure this out if you write down the dot product of two unit 2D vectors: A=(1,0) lying along the x axis, B=(cos theta, sin theta) in polar coordinates.  Then A dot B = cos theta.
• For unit vectors, A dot B is:
• 1 if A and B lie in the same direction (angle=0)
• 0 if A and B are orthogonal (angle=90)
• -1 if A and B lie in the opposite directions (angle=180)
• For any vector, A dot A is the square of A's length.  For a unit vector, A dot A is 1.
• In general, A dot B = length(A) * length(B) * cos(angle between A and B)
• Dot product is commutative: A dot B = B dot A
• Dot product distributes like multiplication: A dot (B + C) = A dot B + A dot C
• Dot product is friendly with scalars: c*(A dot B) = (c*A) dot B = A dot (c*B)
• "A dot B" is the math notation.  In code you usually write "float f = dot(A,B);".
• Dot product is very handy in computer graphics for lighting: for surface normal N and light direction L, "float light = dot(N,L);".
• Coordinate frames
• A set of 3 orthogonal and normalized (orthonormal) vectors (call them S, U, and V) is magical.
• The magic is if I take any point X=s*S+u*U+v*V, then
• X dot S = (s*S+u*U+v*V) dot S
• X dot S = s * (S dot S) + u * (U dot S) + v * (V dot S)
• X dot S = s*1 + u * 0 + v * 0    (because S is unit, and S, U, and V are orthogonal)
• or X dot S = s
• That is, the 's' coordinate of the (S,U,V) coordinate system is just X dot S!
• This means I can take any point into our out of an orthonormal frame really cheaply: dot product (with each axis) to go in, scales and adds to get back out.
• To put the coordinate origin at W, just subtract W from X before dotting (or subtract W dot S after dotting)
• Cross product: compute a vector orthogonal to the source vectors
• A cross B = cross (A,B) = (A.y*B.z-A.z*B.y, A.z*B.x-A.x*B.z, A.x*B.y-A.y*B.x)
• On your right hand, if your index finger is A, and your middle finger is B, your thumb points along "A cross B".
• "A cross B" is the math notation.  In code you usually write "vec3 C = cross(A,B);".
• The vector A cross B is orthogonal to both A and B.  (dot(A,C) = dot(B,C) = 0)
• length(cross(A,B)) == length(A)*length(B)*sin(angle between A and B)
• Cross product can be used to make any two non-orthogonal vectors A and B into an orthogonal frame (X,Y,Z).  The idiomatic use is:
• X = A    (pick a vector to be one axis)
• Z = A cross B  (new 'Z' axis is orthogonal to A/B plane)
• Y = Z cross X
• Note that the cross product is anti-commutative: A cross B = - B cross A
You should pick one of the thousands of "vec3" classes on the web to represent vectors in your code, or else you'll go insane writing "glSomething3f(foo_x,foo_y,foo_z); foo_x+=bar_x; foo_y+=bar_y; foo_z+=bar_z;"

With a nice vec3 class, you'll be able to say "glSomething3fv(foo); foo+=bar;"