TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Implementing a Tiny CPU Rasterizer

117 点作者 atan26 个月前

10 条评论

magicalhippo6 个月前
Nostalgia flashback. Anyone else implemented a renderer based on fatmap2.txt[1]?<p>I came up with my own approach using bresenham and storing spans, but it was slow and sucked.<p>Then my buddy found fatmap2.txt on a BBS and gave it to me, as I didn&#x27;t have a modem at the time. It was a revelation.<p>Programming in Turbo Pascal I was hampered by it being 16bit, but discovered I could prepend the assembly opcodes with 66h to turn them into 32bit instructions.<p>Later I tried converting the code to C, being only 300 lines and at that point well understood I figured it was a good task to get started with C. I was self-taught at programming but had read enough C to feel confident enough to try.<p>Converted it all line by line to C before compiling... and got literally over 3000 errors and warnings, and none of them made any sense.<p>Discouraged I left it, but a week later I was determined to get it working. After a few hours staring and bashing my head against the wall, I saw that the constant for the texture width which I had converted to a #define in C, had a semi-colon at the end...<p>So I removed it, and voila it complied flawlessly...<p>Only later did I realize my C compiler, djgpp, was actually a C++ compiler and that&#x27;s probably why I got so many seemingly weird errors.<p>Anyway, good times...<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;rcoscali&#x2F;ftke&#x2F;blob&#x2F;master&#x2F;ogles&#x2F;doc&#x2F;fatmap2.txt">https:&#x2F;&#x2F;github.com&#x2F;rcoscali&#x2F;ftke&#x2F;blob&#x2F;master&#x2F;ogles&#x2F;doc&#x2F;fatma...</a>
评论 #42026405 未加载
评论 #42026183 未加载
评论 #42024816 未加载
评论 #42029330 未加载
评论 #42030616 未加载
erwincoumans6 个月前
If you like this, there is also the 500 lines of C++ TinyRenderer, loading Obj files, texture mapping, clipping, and vertex&#x2F;pixel shaders on CPU only: <a href="https:&#x2F;&#x2F;github.com&#x2F;ssloy&#x2F;tinyrenderer">https:&#x2F;&#x2F;github.com&#x2F;ssloy&#x2F;tinyrenderer</a>
评论 #42030624 未加载
bob10296 个月前
I was super into this sort of thing until I hit triangle clipping concerns in homogeneous coordinate space.<p>Filling the triangle and spinning the cube are the perfect level of difficulty if you want to get your hands a bit dirty and achieve that sense of accomplishment. Anything beyond this starts to feel like a very risky time-reward tradeoff.
评论 #42025551 未加载
hakilebara6 个月前
I strongly recommend this course “3D Computer Graphics Programming”[1] from Gustavo Pezzi. It walks you through the creation of CPU rasterizer from scratch in C. I am working through it right now and I enjoy it a lot.<p>[1] <a href="https:&#x2F;&#x2F;pikuma.com&#x2F;courses&#x2F;learn-3d-computer-graphics-programming" rel="nofollow">https:&#x2F;&#x2F;pikuma.com&#x2F;courses&#x2F;learn-3d-computer-graphics-progra...</a>
评论 #42043240 未加载
评论 #42030607 未加载
Joker_vD6 个月前
By the way, does anyone know how to modify the standard perspective projection to have a spherical cap instead of the flat rectangle as its far clipping plane? Having the flat rectangle means that you can see further from the corner of your eye than in front of you which is definitely not how the human vision works, and there are some games where this bizarre behaviour is very noticeable: you can barely see some thing poking out of the fog before you, then you turn the camera to the left, and now the thing is visible well and clearly near the screen border. Turn camera back, and it goes back into the fog.
评论 #42039208 未加载
评论 #42026638 未加载
评论 #42029826 未加载
Cieric6 个月前
I love looking at stuff like this, working in the GPU space has only ever renewed my ambitions to work on similar projects. The hardest thing I always ran into with the more optimized fill algorithms was working around the single pixel holes that appear when doing everything with integers.<p>Small nitpick though, there seems to be an issue with the source code views, the file names and the first line of the code are on the same line with no spacing. looks like it might be a static site generation issue since there aren&#x27;t any nodes to separate the name and the code in the raw html.<p>Edit: turns out the issue I&#x27;m seeing is somehow due to Firefox, seems to work correctly in edge.
评论 #42022635 未加载
the5avage6 个月前
I once wrote a tiny cpu rasterizer in dlang. It is more efficient than what is shown here, since it uses only integer arithmetic (based on bresenham&#x27;s algorithm). E.g. for a triangle it outputs the points in the triangle without ever considering a single pixel outside the triangle.<p>It&#x27;s based on ranges (similiar to the ones added to C++) and the concept works very well imo. Could be a good exercise to translate it to C++23 (using coroutines with the range &quot;generator&quot;).<p><a href="https:&#x2F;&#x2F;github.com&#x2F;the5avage&#x2F;shapes">https:&#x2F;&#x2F;github.com&#x2F;the5avage&#x2F;shapes</a>
saw-lau6 个月前
This might also be of interest to anybody who enjoyed these articles: <a href="https:&#x2F;&#x2F;www.scratchapixel.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.scratchapixel.com&#x2F;</a>.
评论 #42027973 未加载
smokel6 个月前
Wait, what? They rasterize a triangle by checking <i>for each pixel</i> if it intersects with the triangle? Where are the days when you learned Bresenham [1] or fixed-point arithmetic [2] to determine the extents of a scan line, and then fill it with an ordinary for-loop?<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bresenham%27s_line_algorithm" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bresenham%27s_line_algorithm</a><p>[2] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fixed-point_arithmetic" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Fixed-point_arithmetic</a>
评论 #42021138 未加载
评论 #42021100 未加载
评论 #42021304 未加载
评论 #42022834 未加载
评论 #42027231 未加载
评论 #42026253 未加载
评论 #42027348 未加载
toolslive6 个月前
I went down this rabbit hole a few times as well. It&#x27;s great fun.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;toolslive&#x2F;NFJE">https:&#x2F;&#x2F;github.com&#x2F;toolslive&#x2F;NFJE</a><p>You used to be able to run it in a browser until they kicked Java out.