My question is not really a game development question, but since it's based on 3D programming, I thought it would fit best here.

I've got a line made out of Point3D objects where the value of x, y, and z are somewhere in those regions:

x: 0 - 640
y: 0 - 480
z: 0 - 4000

I'd like to project those lines into a plane with the size of 640 * 480 (the same size as the base x * y values were) using a oerspective projection. I found some information about the matrix I should use in an old computer graphics script (there are a lot of different matrixes around the net too), but could not get it to work.

Based on this tutorial, I'm using the following parameters:

float n = 1000; // near plane
float f = 4000; // far plane
float l = 0;    // left side
float r = 640;  // right side
float b = 480;  // bottom
float t = 0;    // top

float m11 = (2 * n) / (r - l);
float m22 = (2 * n) / (t - b);
float m31 = (r + l) / (r - l);
float m32 = (t + b) / (t - b);
float m33 = (-1 * (f + n)) / (f - n);
float m43 = (-2 * f * n) / (f - n);
float m34 = (-2 * f * n) / (f - n);

m = new Matrix3D(m11,   0,   0,   0,
                   0, m22,   0,   0,
                 m31, m32, m33,  -1,
                   0,   0, m43,   0);

Now all the result-points I receive from doing a m.Transform(myPoint3D) are in ranges like:

x = -1,015 to -1,694
y = 1,568 to 1,572

In my understanding those values should be in ranges from -1 to 1, and I would scale them to fit into my plane, but those results look totally chaotic... Also, the resulting z-values aren't "flat" but ranging from 2.903 to 2.996.

Any ideas where my error is?


Take a look at this MSDN LINK, they tell you all you need to know to create a projection matrix that you can use. And that one is really good.

For your results, the x&y seems correct. indeed the values are supposed to be inside -1 to 1, but the fact is that you also have scaling in place as well as they can endup outside of the screen. To get them into screen cordinates wich im assuming you are after, there is a simple forumla to transform them from clip space to screen space.

xy = xy * 0.5 + 0.5;
xy *= wh

Where x&y are the x and y of the point, and w is width of the screen and h is the height.
This will give you the screen coordinates of your points.

  • \$\begingroup\$ Thanks for your answer! The mentioned article did help a little and your formulars did bring at least something into the middle of my screen! \$\endgroup\$ – DIF Feb 27 '14 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.