r/GraphicsProgramming • u/lajdjqq • Aug 07 '26
Video I added viewport clipping to my software renderer
Enable HLS to view with audio, or disable this notification
I thought it would be cool to try to figure out how to do triangle clipping myself, to come up with my own algorithm. To solve this, I solved a couple of linear equations and derived a formula that lets me find the intersection points between the triangle edges and the viewport. I don't know if I came up with something original or just accidentally reinvented one of the existing methods. Either way, my solution looks terrible in code, but it works, and I think it's a great learning experience
I render this at 1920x1080 resolution on a single thread of an i5-9400F, is this a good FPS result? I didn't use SIMD, but I tried to use every optimization trick I know.
repo and code: https://github.com/NaiNameDev/software_rasterizer
8
u/dybuk87 Aug 07 '26
Why not just clip against Z=0 and then just do 2d clipping? This create unnecessary calculations and generate new triangles
5
u/Avelina9X Aug 07 '26
Oh this is really cool.
When I saw the triangle clipping I was gonna say "ah, this probably messes with UVs" and then you showed the textured Teto.
Are you dynamically remapping the perspective corrected UV coords? Or is the triangle clipping RGB view just a rasterizer debug view to see it in action and you're still using the original unclipped coords for the UVs/other vertex data?
33
u/susosusosuso Aug 07 '26
Why do you perform triangle clipping?
3
u/forumonaut Aug 07 '26
This is the Sutherland-Hodgman algorithm right?
2
u/LUMINAL_DEV Aug 07 '26
Psp teto.
2
u/Jabba_the_Putt Aug 07 '26
So if ibunderstand it correctly, its clipping off the areas outside of the window in real time as you move it around as well as adding them back, and when that happens it kinda redraws it in real time to maintain the shape which we can see happening in the wireframe looking view but when you apply the visual texture/shader its all hidden but stull happening "behind the scenes"?
15
u/simonschreibt Aug 07 '26
i love such examples to show how exactly the triangles gets "reshaped". super cool!
8
u/seaweednomnomnom Aug 07 '26
teto teteto
2
u/juplantern Aug 08 '26
You just reminded me I have finals for this in 3 weeks and I forgot about it 😭
1
u/heypringle Aug 09 '26
Nicee!
1
u/nelmklt Aug 09 '26
Seek mentioned ×2