Skip to main content
GameDev.net gamedev.net
🔒 Locked

3D Perspective Projection (w)

Started by adam17 Oct 8, 2012 at 4:01 AM 0 replies 3k views
Original Post
adam17
adam17
I'm working on an assignment for my graphics class and I've encountered a problem. Basically I have a set of coordinates in World space and I have to transform them into Screen space and display them. I have to manually build all of the matrices and do the transformations by myself (no opengl or d3d). I have the following 3 matrices

  • Xsp (proj to screen transform)
  • Xpi (image to projection transform)
  • Xiw (world to image transform)

    I'm rendering to a 256x256 frame buffer, so my Xsp matrix looks like this:

    128 0 0 128
    0 -128 0 128
    0 0 1 0
    0 0 0 1


    My Xpi matrix has m[3][2] set to tan(fov / 2) where my fov = 35. (In the matrix .32 was just rounded for easier viewing)

    1 0 0 0
    0 1 0 0
    0 0 1 0
    0 0 .32 0


    Lastly my Xiw matrix is just the inverse of my camera transform. The camera is at 0,0,-15, looking at 0,0,0 with an up-vector of 0,1,0

    1 0 0 0
    0 1 0 0
    0 0 1 15
    0 0 0 1


    Using just these three matrices I can multiply them against a coordinate and get it transformed into screen space just fine. The catch is when I want them to be perspective correct. I've tried figuring out what 'w' is, but I cannot find a solid definition of how to calculate it. Some places are saying that the final Z value is used to divide X and Y by, but it doesn't look right. Some places are saying the W coord that is calculated by multiplying the matrix with the coordinate (assuming 1.0 for the 4th coord) is the W to divide by. I can't seem to get either method to work.

    My question is what is W and how is it calculated?

    Thank you so much in advance!
    -Adam
Adaline
Adaline

Hello smile.png


With homogeneous coordinates, there are four components to represent a 3d vertex or a 3d vector :

  • if w != 0 , ( x , y , z , w ) represents the vertex ( x/w , y/w , z/w )
  • if w = 0 , it represents all the vectors colinear to the vector ( x , y , z ). [ i.e. : a * ( x , y , z ) , a in lR* ]

So for a vertex ( x , y , z ) transformed into ( x' , y' , z' ) with Transform :

  • ( X , Y , Z , W ) = ( x , y , z , 1 ) * Transform
  • ( x' , y' , z' ) = ( X , Y , Z ) / W

NB :
For the vertices, the w component is usually initially set to 1 so that the w division is not needed in rotations, translations, scalings, and any combination of them.
With this assumption, the w division is necessary only when Transform is (or 'contains') a projection matrix.

Nico

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.