编程与软件工程github.com编辑精选
OpenGL 是如何工作的:500 行代码实现软件渲染器
How OpenGL works: software renderer in 500 lines of code
2016 — 2022,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 GitHub 上 tinyrenderer 项目的 wiki,用约 500 行代码从零实现一个软件渲染器,并配系列文章讲解三角形光栅化、着色器等图形管线的核心环节,目标是让人通过亲手构建来理解 OpenGL/GPU 渲染的基本原理。
为什么经久不衰:它把复杂的图形管线浓缩成几百行可读代码,是理解现代渲染器底层机制的经典入门材料,多年后仍被反复推荐。
HN 评论精选
指出标题有误导性,代码只是基础光栅化器,OpenGL 的功能远不止这些。
标题有点误导:这其实是图形编程课程的样板代码,和 OpenGL 关系不大。讲解原理的文章比代码本身有趣得多。它只是一个低效的三角形光栅化器,所做的是遍历覆盖三角形的矩形像素,对每个落在内部的像素调用一个“shader”函数,核心就在那 40 行里。我不知道那些漂亮图片里的纹理是怎么做的(在未包含的 shader 里),但它们没有计算正确 mipmap 纹理所需的偏导数。OpenGL 远不止一个光栅化器,还有纹理、深度模板操作、混合、计算着色器和高效内存管理。
exDM69查看英文原文
The title is a bit misleading, this is boilerplate code for a graphics programming course and it hasn't got much to do with OpenGL. The articles describing the operation are much more interesting than the code itself. It's just an inefficient triangle rasterizer. All it does is loop over the pixels in a rectangle covering a triangle, and for each pixel inside it calls a "shader" function. All the beef is in these 40 lines [0]. I don't know how they've done the texturing in all those pretty pictures (it's in the "shaders", not included here), but they don't calculate the partial derivatives required for correct, mipmapped texturing. Simple non-mipmapped perspective correct texture mapping can be computed in the shaders, with the usual caveats. OpenGL is much more than a rasterizer, there's texturing, depth-stencil operations, blending, compute shaders and efficient memory management. [0] https://github.com/ssloy/tinyrenderer/blob/master/our_gl.cpp#L42 edit: Someone in reddit pointed out that this is a translation of a Russian language course. The original Russian version looks to be a bit longer than the English translation (but I don't read Russian, so I can't tell if it is better): https://habrahabr.ru/post/249467/
软件渲染器是极好的学习方式,但教程未涉及性能优化这一维度。
我写过一个软件版 OpenGL,唯一目标是用它玩 Quake3。我可以保证那是极棒的学习经历。这个教程似乎没有触及性能问题,而那本身又是另一番学习体验。
8bitpimp查看英文原文
I wrote a software implementation of OpenGL with the only goal of being able to play Quake3 using it. I can vouch that it is an amazing learning experience. The tutorial here doesnt seem to aproach the issues of performance however, which really are another learning experience entirely.
肯定其简洁,但建议实现 OpenGL 接口,并推荐了更深入的图形管线资源。
很棒也很简洁,但让我意外的是,如果目标是展示 OpenGL 如何工作,作者没有实现 OpenGL 的接口(显然不是全部)。另一个好例子是 Trenki 为 GPX2 写的软件渲染器,它实现了着色器架构。其他有用资源还有 Michael Abrash 的 Graphics Programming Black Book,以及 ryg 的 A Trip Through the Graphics Pipeline。
sclangdon查看英文原文
This is great and so concise, but I'm surprised that the author didn't implement OpenGL's interface (obviously not all of it), if the goal is to show how OpenGL works. Another good example of this is Trenki's software renderer for the GPX2[1], which implements a shader architecture if memory serves. I haven't looked at it for many years, but I remember it being a useful resource when learning this stuff. Other useful resources are, of course, Michael Abrash's Graphics Programming Black Book[2] (despite it's age, is still a great read filled with useful information), and for a really deep dive into the graphic's pipeline, ryg's (of Farbrausch fame) A Trip Through the Graphics Pipeline[3]. [1] http://www.trenki.net/content/view/18/38/ [2] https://github.com/jagregory/abrash-black-book [3] https://fgiesen.wordpress.com/2011/07/09/a-trip-through-the-graphics-pipeline-2011-index/
实际基于教程构建了 R 包,认为动手实现管线能显著加深对渲染的理解。
我用并扩展了这个教程来写 rayvertex 包,一个 R 语言的延迟并行软件光栅化器。显式地构建渲染管线确实帮我理解了底层数据流和光栅化技术,也让我更好地理解 GLSL。给光栅化器加上顺序无关透明、多核渲染、抗锯齿等特性,让我体会到现代渲染器背后的工作量。强烈推荐。
tylermw查看英文原文
I used and extended this tutorial to write the rayvertex [1][2] package, which is a deferred parallel software rasterizer in R. Explicitly building the rendering pipeline really helped me understand the underlying data flow and rasterization techniques, which in turn made me understand GLSL much better. And extending the rasterizer with order-independent transparency, multicore rendering, antialiasing, and other niceties made me appreciate the work that goes into modern renderers. Highly recommend. [1] https://www.rayvertex.com/ [2] https://github.com/tylermorganwall/rayvertex
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选