Thread: Vector Operations in 3D Graphics

  1. #1
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    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
    Last edited by The Dog; 09-02-2005 at 06:33 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Get a physics book and you'll find lots of applications for normalization and projection

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> 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. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Thanks. Will give it shot.

  6. #6
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    I recommend any first year university linear algebra book. Thats where i learned all my vector math.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    ....or reading that book you have closer.

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

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    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. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote 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. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    100
    That's it? Vectors are just 3D points that form line segments and view frustrums?

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote 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.

    Quote 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.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  15. #15

    Join Date
    May 2005
    Posts
    1,042
    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.
    Last edited by BobMcGee123; 09-07-2005 at 06:10 PM.
    I'm not immature, I'm refined in the opposite direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3d graphics without API
    By h3ro in forum Game Programming
    Replies: 6
    Last Post: 05-31-2008, 11:51 AM
  2. 3D graphics for a noob?
    By Crazy Glue in forum Game Programming
    Replies: 8
    Last Post: 12-17-2006, 01:38 AM
  3. Beginning Game Programming Type Books
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 09-13-2006, 04:15 PM
  4. 3D Graphics -- How?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-07-2002, 02:49 AM
  5. 3D Modelling + Graphics Cards
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-17-2002, 10:29 PM