# Thread: Vector Operations in 3D Graphics

1. ## Vector Operations in 3D Graphics

"3D Math Primer for Graphics and Game Development" by Fletcher Dunn & Ian Parberry

Hi, I've been reading through the above mentioned book and I find it to be a pretty good book.
Now I'm no slouch when it comes to Math, but I've never been able to fully understand how it
relates to 3D graphics.

I learnt a bunch in the first four chapters, as the authors thoroughly explain what's going on, and why.
The chapter on vector operations seems to be quite vague though, in the sense that they don't
explain why these operations are needed.

Examples are normalization, and projecting one vector onto another.

It's not enough to tell me how to do something, I need to know why it's necessary, and the effect it has.

I don't know if it's just me, but fully grasping the concept is of utmost importance to me.
I'm thinking that perhaps too much information is brought about in this chapter which cannot be
fully understood until later chapters.

Does anyone know of another book or tutorial that covers this topic as thoroughly as Dunn and Parberry
cover the first few chapters of their book? Or even better, can anyone here explain it to me?

Thanks

2. Get a physics book and you'll find lots of applications for normalization and projection

3. >> Get a physics book and you'll find lots of applications for normalization and projection

I presume you're talking about a physics book related to game programming?

4. No, good ol' physics book. Oh and a calculus book that fully explains the projection and normalization, and what it is. Especially how you can get other vectors using a projection

5. Thanks. Will give it shot.

6. I recommend any first year university linear algebra book. Thats where i learned all my vector math.

7. ....or reading that book you have closer.

I have the same book and it tells you what to use those operations for.

8. You guys probably think that I'm a total moron, but what are vectors? I'm going to learn them at the end of my year of Pre-Calculus, but I really want to at least get brief concepts. If not, just don't flame me .

9. In the simplist, most general way they are nothing but a directed line segment. They have a direction and a magnitude. Now the application of them is where all the fun is.

10. A vector only differs from a 3D point in how we interpret it.

This is a 3D vector:

4,1,5

This can be taken two ways. If we are looking at this as a point in 3D space then we are at 4x,1y, and 5z. But if we look at this as a vector then we look at it as a [i]displacement from the origin of 4 units on x, 1 unit on y, and 5 units on z. Draw a line from the point through the origin to infinity and through the point 4,1,5 and you have a vector.

Vector's and points are so close in nature that I do not specify a new class for handling points in my code because they can just as well be expressed as vectors.

There are many operations that can be done on vectors - normalization, cross product, addition, subtraction, etc., etc.

Here are two very important operations:

Normalization and Dot product

Code:
```void Normalize(Vector3 &in,Vector3 &out)
{
float length=sqrt((in.x*in.x)+(in.y*in.y)+(in.z*in.z));
float oneoverlength=1.0f/length;
out.x=in.x*oneoverlength;
out.y=in.y*oneoverlength;
out.z=in.z*oneoverlength;
}

float DotProduct(Vector3 &v1,Vector3 &v2)
{
Normalize(v1,v1);
Normalize(v2,v2);
return (v1.x*v2.x)+(v1.y*v2.y)+(v1.z*v2.z);
}```

11. Originally Posted by Bubba
Draw a line from the point through the origin to infinity and through the point 4,1,5 and you have a vector.
Technically it would not go to infinity. Vectors have a definate length to them. Also remember that <4,1,5> is a different vector than <8, 2, 10> even though they are just scalar multiples of each other.

Also I'm curious as to your Normalize and DotProduct functions. Namely why are you normalizing the vectors prior to taking the dot product?

12. Because taking the dot product of non-normalized vectors does not yield meaningful results in computer graphics.

The geometric definition of the dot product states that the dot product is equal to the cosine of the angle between two vectors, however, you must normalize the vectors.

Try it in one of your engines. Non-normal dot products will NOT product the correct lighting coefficients.

13. That's it? Vectors are just 3D points that form line segments and view frustrums?

14. Originally Posted by Bubba
Because taking the dot product of non-normalized vectors does not yield meaningful results in computer graphics.

The geometric definition of the dot product states that the dot product is equal to the cosine of the angle between two vectors, however, you must normalize the vectors.

Try it in one of your engines. Non-normal dot products will NOT product the correct lighting coefficients.
Actually, there are far more applications for the dot product than just lighting calculations. And in those you would want the vectors unnormalized.

Originally Posted by blankstare77
That's it? Vectors are just 3D points that form line segments and view frustrums?
No, vectors are not positions in 3D space. Vectors have no position. They merely represent a direction and a magnitude.

15. Yeah, I really can't think of a worse way of implementing dotproduct. While I do it myself, dotproduct really shouldn't even be a function, rather preferably a macro (it typically takes longer just to call the function than to actually execute it). The dotproduct is supposed to be a low-level operation that *just* takes the dotproduct. If you need a normalized vector they'll already be normalized. But even if you are just trying to compute the angle between vectors, but don't want to normalize them, you can still just use the formula:

A . B = |A| |B| cos

without unnecessarily normalizing them.

And, to find the axis/angle required to rotate towards a goal I use cross product (CurrentViewVector x DestinationVector) because it gives both the angle required to rotate to until you are facing the goal and the axis to rotate about.

What if, in a computer game you collide against some plane normal, and you want to find the component of the velocity that is effected by the impulse? In your implementation it forces the velocity vector to be of length one just when you take the dotproduct between the normal and the velocity vector.