1
votes

I'm using Direct3D to draw vertices from a vertex buffer with vertex format FVF_XYZ.

I want to implement the functionality to draw lines now. When drawing a line in 3D space I project the end points to 2D screen space and use my function to draw a line from 2D space. My problem is with this function.

My vertex buffer contains two vertices with coordinates (0,0,0) and (1,0,0). I am planing to transform this basic line to my final line with basic math and transformations.

The problem is that when I want to render a vertex to screen coordinate (0,0) for example I don't know how to set up the transformation matrices.

From my understanding I should end up and screen coord (0,0) when setting world, view and projection matrix to identity, but (0,0,0) then ends up at the center of the screen.

How would I need to set up world, view and projection matrices so that I transform from (0,0,0) to (0,0) and from (1,0,0) to (1,0) and so on?

1

1 Answers

1
votes

Firstly its worth noting that in projection space (-1, 1, z, w) transforms to (0,0) and (1, -1, z, w) to (1, 1).

So working on the basis you want to transform a given vertex to its projection space it is simply a matter of doing the following:

T' = W * V * P

(Where W is world matrix, V is view matrix and P is projection matrix).

You can now multiply any homogeneous coordinate (4 ie x,y,z,1) to the projection space.

Now if you wish to perform the perspective divide it is simply a matter of doing the divide by w. ie

x' = x / w;
y' = y / w;
z' = z / w;
w' = w / w; // or 1

Now you have a set of coordinates where the x,y coordinates are in the range -1, -1 to (1, 1). If you thus wish to transform them into he 0 -> 1 space you suggest you would do the following:

x'' = (x'  + 1) / 2;
y'' = (-y' + 1) / 2;

You now have the your coordinates in the space where 0, 0 is the top left and 0, 1 is bottom right. Anything outside that range is off screen.

Its also worth noting that the z' value you have after the perspective divide can be put into the Z-buffer. If the Z-value is less than 0 (or is it greater than can't remember off the top of my head) then it is clipped by the front plane and greater/less than +/-1 is beyond the far clip plane.

Hope that helps.