<p>I have a simple test scene setup in a toy software rasterizer.  I have a plane facing the +Z axis at the origin and the camera is facing -Z at Z=2.  Everything works fine and looks good at that orientation but if I turn the camera to face -X it  renders a distorted triangle long after the plane has left the view of the camera.  I have been trying to debug this for weeks and can't find the problem.  I assume I am missing somthing silly, maths are not my strong suit.  I have check my matrix/vector math and it seems good to me.</p>

<p>Here is the realevent code: NOTE it's emited from the "ReadableC backend" of a toy language so the formating is a little strange.  Also I am fully aware there are important parts of software rasterization that are missing i the implementation.  It has been simplified to isolate the bug.</p>

<p><a href="https://gist.github.com/Twiebs/3278d2e58d0a51b2d86a101172b2904e" rel="nofollow noreferrer">Relevant Source Code</a></p>

CC BY-SA 3.0
2017-06-27T12:28:33.567Z
It sounds like your triangles are being transformed so they can have Z < 0 when rotated into eye space. Usually you need to clip the triangle against a near plane to avoid this.
PaulHK
CC BY-SA 3.0
2017-06-27T12:47:00.513Z
@PaulHK You should post that as an answer.
Dan Hulme
CC BY-SA 3.0
2017-06-27T16:24:03.327Z
@PaulHK that should indeed be right. The clipping should happen before the perspective divide and after the projection transformation. So, I would indeed post that as an answer as Dan said.
bram0101
<rendering>
Strange behaviour in simple software rasterizer
Torin Wiebelt
# Strange behaviour in simple software rasterizer

(Asked by: username_0 on 2017-06-27)

I have a simple test scene setup in a toy software rasterizer. I have a plane facing the +Z axis at the origin and the camera is facing -Z at Z=2. Everything works fine and looks good at that orientation but if I turn the camera to face -X it renders a distorted triangle long after the plane has left the view of the camera. I have been trying to debug this for weeks and can't find the problem. I assume I am missing somthing silly, maths are not my strong suit. I have check my matrix/vector math and it seems good to me.

Here is the realevent code: NOTE it's emited from the "ReadableC backend" of a toy language so the formating is a little strange. Also I am fully aware there are important parts of software rasterization that are missing i the implementation. It has been simplified to isolate the bug.

[Relevant Source Code](https://gist.github.com/Twiebs/3278d2e58d0a51b2d86a101172b2904e)

**Tags:** rendering

> **Comments:**
> **username_1**: It sounds like your triangles are being transformed so they can have Z < 0 when rotated into eye space. Usually you need to clip the triangle against a near plane to avoid this.  
> **username_2**: @username_1 You should post that as an answer.  
> **username_3**: @username_1 that should indeed be right. The clipping should happen before the perspective divide and after the projection transformation. So, I would indeed post that as an answer as Dan said.  

---

## 0 answers

