<p>The way to do the projection is the following:</p>
<pre><code>Matrix4x4 projection;
Matrix4x4 modelView;
Vector4D position4D;

//fist the position's 4th value (w) need to be set to value &quot;1&quot;
position4D = Vector4D(position3D, 1); //the 1 in the 4th component is very important!
Vector4D result = projection * modelView * position4D;
//at the end, the result need to be divided by its 4th component (w) 
result = result / result.w;
</code></pre>
<p>The last line is of major importance, because the projection (perspective) is not linear. the division by the 4th component can not be applied within the matrix.</p>

CC BY-SA 4.0
2022-12-23T12:22:57.670Z
2022-12-23T12:22:57.670Z
Thomas
<p>I can't for the life of me figure out how to use a perspective projection matrix.  My understanding is that, once objects are in view space, the the perspective transform uses the z-coordinate to scale the x and y coordinates.</p>
<p>Here's the code I'm using (C#, dotnet 7).</p>
<pre><code>var viewToScreen = Matrix4x4.CreatePerspectiveFieldOfView((float)(Math.Tau / 4), 1.0f, 0.1f, 100f);

var square1 = new Vector3[]
{
    new(+1, +1, 1),
    new(-1, +1, 1),
    new(+1, +1, 1),
    new(+1, -1, 1),
};

var square2 = Array.ConvertAll(square1, v =&gt;
{
    v.Z += 3;
    return v;
}).Dump(&quot;Square 2 (World Space)&quot;);

var screen1m1 = Array.ConvertAll(square1, v =&gt; Vector3.Transform(v, viewToScreen));

var screen2m1 = Array.ConvertAll(square2, v =&gt; Vector3.Transform(v, viewToScreen))
</code></pre>
<p>However, after the transform, the X and Y coordinates remain unchanged.  Only the Z coordinate is changed, and then only slightly.  This is consistent with an orthogonal projection, not a perspective projection.</p>
<p>What am I doing wrong here?</p>

CC BY-SA 4.0
2022-12-23T09:50:03.247Z
I don't get the `v.Z +=3` part... but projection matrices only work with 4D vectors. After multiplication the result vector needs to be divided by its 4th `w` component. so: 
`Vector4D result = projectionMatrix * viewMatric * position4D` where the 4th component of `position4D` should be `1`. At the end you need to calculate: $result = result / result.w$
Thomas
CC BY-SA 4.0
2022-12-23T10:23:11.360Z
I'm creating a second set of points, further from the screen along the z coordinate.
John Gietzen
CC BY-SA 4.0
2022-12-23T10:39:39.963Z
Please add that as an answer.  It worked!  And, the simple C# solution is just to use Vector4 instead of Vector3, which automatically appends the 1.
John Gietzen
<matrix><rasterizer><vectors>
Trouble transforming vectors from view space to screen space using a perspective projection matrix
John Gietzen
# Trouble transforming vectors from view space to screen space using a perspective projection matrix

(Asked by: username_0 on 2022-12-23)

I can't for the life of me figure out how to use a perspective projection matrix. My understanding is that, once objects are in view space, the the perspective transform uses the z-coordinate to scale the x and y coordinates.

Here's the code I'm using (C#, dotnet 7).

```
var viewToScreen = Matrix4x4.CreatePerspectiveFieldOfView((float)(Math.Tau / 4), 1.0f, 0.1f, 100f);

var square1 = new Vector3[]
{
    new(+1, +1, 1),
    new(-1, +1, 1),
    new(+1, +1, 1),
    new(+1, -1, 1),
};

var square2 = Array.ConvertAll(square1, v =>
{
    v.Z += 3;
    return v;
}).Dump("Square 2 (World Space)");

var screen1m1 = Array.ConvertAll(square1, v => Vector3.Transform(v, viewToScreen));

var screen2m1 = Array.ConvertAll(square2, v => Vector3.Transform(v, viewToScreen))

```

However, after the transform, the X and Y coordinates remain unchanged. Only the Z coordinate is changed, and then only slightly. This is consistent with an orthogonal projection, not a perspective projection.

What am I doing wrong here?

**Tags:** matrix, rasterizer, vectors

> **Comments:**
> **username_1**: I don't get the `v.Z +=3` part... but projection matrices only work with 4D vectors. After multiplication the result vector needs to be divided by its 4th `w` component. so:  `Vector4D result = projectionMatrix * viewMatric * position4D` where the 4th component of `position4D` should be `1`. At the end you need to calculate: $result = result / result.w$  
> **username_0**: I'm creating a second set of points, further from the screen along the z coordinate.  
> **username_0**: Please add that as an answer.  It worked!  And, the simple C# solution is just to use Vector4 instead of Vector3, which automatically appends the 1.  

---

## 1 answers

### Answer by username_1 on 2022-12-23. Score: 2 | Accepted answer

The way to do the projection is the following:

```
Matrix4x4 projection;
Matrix4x4 modelView;
Vector4D position4D;

//fist the position's 4th value (w) need to be set to value "1"
position4D = Vector4D(position3D, 1); //the 1 in the 4th component is very important!
Vector4D result = projection * modelView * position4D;
//at the end, the result need to be divided by its 4th component (w) 
result = result / result.w;

```

The last line is of major importance, because the projection (perspective) is not linear. the division by the 4th component can not be applied within the matrix.


---

