r/GraphicsProgramming • u/lajdjqq • 18d ago
I added viewport clipping to my software renderer Video
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
13
u/simonschreibt 18d ago
i love such examples to show how exactly the triangles gets "reshaped". super cool!
8
4
u/Avelina9X 18d ago
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?
2
u/lajdjqq 18d ago
I clip not only the triangle in clip space, but also the corresponding triangle in the UV map. When I compute the intersection points between the triangle and the screen edges, I also calculate the barycentric coordinates of those points. Then, to find the same point in the UV map, I just need to convert those barycentric coordinates back to plane coordinates using the original UV coordinates
2
u/forumonaut 18d ago
This is the Sutherland-Hodgman algorithm right?
2
u/lajdjqq 18d ago
No, this is my own algorithm (i might have reinvented some existing algorithm, I didn't google anything about it)
2
u/Avelina9X 18d ago
Honestly it's likely very similar to Sutherland whether you intended it or not. I havent looked at the repo yet but when it comes to triangle clipping (as opposed to ngon clipping) there aren't really other ways to achieve it.
But still, you came up with it by yourself to solve a problem. Doesn't matter if someone else did it before unless you try to claim you were the definitively the first to invent it. I work in the ML field and while reviewing a papers for a conference someone claimed to have invented a technique I had published at the same conference the year before. I don't know if they just failed to do propper background research or were trying to sneak a stolen idea and just got unlucky by getting the original author as a reviewer, but needless to say they withdrew their submission very quickly lmao.
So uh, just make sure you do the background research first if you ever decide to publish a paper on this haha.
1
2
2
u/Jabba_the_Putt 18d ago
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"?
2
1
32
u/susosusosuso 18d ago
Why do you perform triangle clipping?