Thread: 3D to 2D projections via a matrix

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    3D to 2D projections via a matrix

    Thanks to my new book I just got in the mail I finally figured out my problem with 3D to 2D projection.


    I was on the right track but just couldn't sort it out.

    IM=identity matrix

    p=[p1,p2,p3,1][IM]=[p1,p2,p3,p3] or p', for p3!=0

    so w=p3.------------------->^

    This is vector in homogenous space - to map it back to three dimensions divide each component by w.

    so

    V(x,y,z,w)= (x/w,y/w,z/w,w/w)=(x/w,y/w,z/w,1)=(x/w,y/w,z/w)=x

    Pretty simple really, dunno why I couldn't derive it.
    Last edited by VirtualAce; 03-27-2004 at 01:08 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Also for the newbie to matrices here are some helpful ones - I've posted this before but it was a looooooong time ago.


    Translation matrix


    T(p)=

    [1,0,0,0]
    [0,1,0,0]
    [0,0,1,0]
    [px,py,pz,1]

    Rotation matrices

    where @ = angle

    X(@)=

    [1,0,0,0]
    [0,cos@,sin@,0]
    [0,-sin@,cos@,0]
    [0,0,0,1]


    Y(@)=

    [cos@,0,sin@,0]
    [0,1,0,0]
    [sin@,0,cos@,0]
    [0,0,0,1]


    Z(@)=

    [cos@,sin@,0,0]
    [-sin@,cos@,0,0]
    [0,0,1,0]
    [0,0,0,1]

    Scaling matrix

    S(q)=

    [qx,0,0,0]
    [0,qy,0,0]
    [0,0,qz,0]
    [0,0,0,1]

    inverse S(q)=

    [1/qx,0,0,0]
    [0,1/qy,0,0]
    [0,0,1/qz,0]
    [0,0,0,1]

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    not entirely true for rotation matrices...you just put some rotation matrix, but you don't specifiy what coordinate system it works with. It could either be left handed or right handed, or have the 'z axis up' coordinate system...GL is a right handed system

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    It is for a left handed coordinate system with z axis running into the screen. But converting to another system would not be difficult.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    By the way Silvercoord, do you program in DirectX?? I think you only code in OpenGL if memory serves me right.

    And you said you figured out how to slide against walls. I'd be interested to see the theory behind this. As well when you create a level is each object its own mesh or do you combine meshes to create one object?

    Personally my plan is to have each large object as its own mesh unless the geometry changes in real time. I'm going to use progressive meshes so that I can control the LOD on each one. For lighting and effects I'm going to use vertex and/or pixel shaders written in HLSL. For the basic lighting though, D3D should suffice - the pixel/vertex shaders will only be for stuff that either cannot be done easily in D3D or looks ugly when it is. For particle effects I'm definitely going to use point sprites and in DX9 they are easier than ever to implement.

    So from what I gleaned from this book on D3D and the information gleaned in Lamothe's book as well as some other DOS based 3D books I have, I think I should be able to have a rudimentary engine up and running. Now the problem is the darn models - how do you create them and what program do you use? I'm not and artist and certainly not a 3D modeler by any means.

    Future books I'd like to get are about sound - I can program the sound engine but learning more about the entire system might enable me to do other effects and advanced effects not supported by DirectSound. As well I'm looking into some physics-based books and the Game Programming Gems series.

    Lotsa fun, but lotsa work.
    Last edited by VirtualAce; 03-28-2004 at 08:52 AM.

  6. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I use a smart BSP compiler called GTKRadiant. It's a quake3 editor, subsequently I use quake3 BSP format. You build the world, and BSPs the world, then performs a process called CSG (Constructive Solid Geometry) to determine what meshes go with what objects. It's probably one of the most complex programs that can be written, which is why I didn't make an attempt at writing my own.

    About sliding against walls, when you hit an object you snag the plane normal that you hit. The new velocity becomes the component of the vector that is perpendicular to the normal.
    In order to do this you must remove the vector component of the original velocity that are parallel (in the same direction as) the normal. The parallel component is:

    DotProduct(velocity,normal) * normal

    assuming the normal has a length of one. This works because the DotProduct gives the scalar components of velocity, in the direction of the normal. When you multiply a unit vector by any scalar, it gives a new vector with that scalar.

    Then, once you've got the parallel component, you just take it away from the original velocity.

    It becomes:

    Velocity -= (DotProduct(Velocity, Normal) * Normal )

    EDIT:
    i don't use directx, but the engine is modular enough that I easily could. My rendering is done inside 'renderface' and 'renderfacenolightmap' and 'renderalphaface' where it makes the OpenGL calls. I could easily set those up to be function pointers, and all that changes is which drawing API is called. I have no desire to learn directx rendering though. GL works.
    Last edited by Silvercord; 03-28-2004 at 03:12 PM.

  7. #7
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    sweet so i posted that for nothing

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Well I thought I could simply add the vectors - the vector you hit with your current vector. But this prob wouldn't work. Your solution makes sense.

    I shall try it. Also - how are you getting a balanced BSP tree when you pre-compute them? My plan is to use quadtrees and/or sectors for terrain and BSPs for when inside objects/buildings, etc.

    The quadtree is easy to compute but the BSP is giving me a headache.

  9. #9
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    that's just it, I don't bother with the partitioning of worlds. I decided that (for me) it's too complicated, and if it isn't done just right then you get an unbalanced world, which is about the worst possible thing you can do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. OpenGL - Getting a Grip on Perspectives
    By Tonto in forum Game Programming
    Replies: 14
    Last Post: 11-10-2006, 12:00 AM
  4. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM