c++ - Transform vertex coordinate to "same" screen coordinate -
i'm using direct3d draw vertices vertex buffer vertex format fvf_xyz
.
i want implement functionality draw lines now. when drawing line in 3d space project end points 2d screen space , use function draw line 2d space. problem function.
my vertex buffer contains 2 vertices coordinates (0,0,0)
, (1,0,0)
. planing transform basic line final line basic math , transformations.
the problem when want render vertex screen coordinate (0,0)
example don't know how set transformation matrices.
from understanding should end , screen coord (0,0)
when setting world, view , projection matrix identity, (0,0,0)
ends @ center of screen.
how need set world, view , projection matrices transform (0,0,0)
(0,0)
, (1,0,0)
(1,0)
, on?
firstly worth noting in projection space (-1, 1, z, w) transforms (0,0) , (1, -1, z, w) (1, 1).
so working on basis want transform given vertex projection space matter of doing following:
t' = w * v * p
(where w world matrix, v view matrix , p projection matrix).
you can multiply homogeneous coordinate (4 ie x,y,z,1) projection space.
now if wish perform perspective divide matter of doing divide w. ie
x' = x / w; y' = y / w; z' = z / w; w' = w / w; // or 1
now have set of coordinates x,y coordinates in range -1, -1 (1, 1). if wish transform them 0 -> 1 space suggest following:
x'' = (x' + 1) / 2; y'' = (-y' + 1) / 2;
you have coordinates in space 0, 0 top left , 0, 1 bottom right. outside range off screen.
its worth noting z' value have after perspective divide can put z-buffer. if z-value less 0 (or greater can't remember off top of head) clipped front plane , greater/less +/-1 beyond far clip plane.
hope helps.
Comments
Post a Comment