Introduction 简介
Computer graphics is a fascinating topic. How do you go from a few algorithms and some geometric data to the special effects for movies like Star Wars and The Avengers, animated movies like Toy Story and Frozen, or the graphics of popular video games like Fortnite or Call of Duty?
计算机图形学是一个迷人的话题。你如何从一些算法和一些几何数据到电影的特效,如《星球大战》和《复仇者联盟》,动画电影如《玩具总动员》和《冰雪奇缘》,或流行视频游戏如《堡垒之夜》或《使命召唤》的图形?
Computer graphics is also a frighteningly broad topic: from rendering 3D scenes to creating image filters, from digital typography to simulating particle systems, there are a multitude of disciplines that can be thought of as part of computer graphics. One book couldn’t hope to cover all these subjects; it would take a library. This book focuses exclusively on the topic of rendering 3D scenes.
计算机图形学也是一个广得吓人的话题:从渲染3D场景到创建图像过滤器,从数字排版到模拟粒子系统,有许多学科都可以被认为是计算机图形学的一部分。一本书不可能涵盖所有这些主题;这将需要一个图书馆。本书只专注于渲染3D场景的主题。
Computer Graphics from Scratch is my humble attempt to present this one slice of computer graphics in an accessible way. It is written to be easily understood by high-school students, while staying rigorous enough for professional engineers. It covers the same topics as a full university course—it is, in fact, based on my years of teaching the subject at university.
计算机图形学从零开始》是我试图以一种易懂的方式来介绍计算机图形学的这一领域。它的编写是为了让高中生容易理解,同时对专业工程师来说又足够严谨。它所涵盖的主题与完整的大学课程相同--事实上,它是基于我多年来在大学教授这一科目的经验。
Who This Book Is For
这本书是为谁写的
This book is for anyone with an interest in computer graphics, from high-school students to seasoned professionals.
本书适用于任何对计算机图形感兴趣的人,从高中生到经验丰富的专业人士。
I have made a conscious choice to favor simplicity and clarity in the presentation. This is reflected in the choice of ideas and algorithms within the book. While the algorithms are industry-standard, whenever there’s more than one way to achieve a certain result, I have chosen the one that is easiest to understand. At the same time, I’ve put considerable effort into making sure there’s no hand-waving or trickery. I tried to keep in mind Albert Einstein’s advice: “Everything should be made as simple as possible, but no simpler.”
我做了一个有意识的选择,在表述上倾向于简单和清晰。这反映在书中的观点和算法的选择上。虽然算法是行业标准,但只要有一种以上的方法来实现某种结果,我就会选择最容易理解的一种。同时,我花了相当大的力气来确保没有摆弄或欺骗的成分。我试图牢记阿尔伯特-爱因斯坦的建议。"每件事都应该尽可能简单,但不能更简单"。
There’s little prerequisite knowledge and no hardware or software dependencies. The only primitive used in this book is a method that lets us set the color of a pixel—as close to from scratch as we can get. The algorithms are conceptually simple, and the math is straightforward—at most, a tiny bit of high-school trigonometry. We also use some linear algebra, but the book includes a short appendix presenting everything we need in a very practical way.
没有什么先决条件的知识,也没有硬件或软件的依赖性。本书中使用的唯一基元是一种让我们设置像素颜色的方法--就像我们可以从头开始一样。算法在概念上很简单,数学也很直接--最多就是一点点高中三角函数。我们还使用了一些线性代数,但书中有一个简短的附录,以非常实用的方式介绍了我们需要的一切。
What This Book Covers
本书的内容
This book starts from scratch and builds up to two complete, fully functional renderers: a raytracer and a rasterizer. Although they follow very different approaches, they produce similar results when used to render a simple scene. Figure 1 shows a comparison.
本书从零开始,建立了两个完整的、功能齐全的渲染器:一个光线跟踪器和一个光栅器。尽管它们采用了非常不同的方法,但在用于渲染一个简单的场景时,它们产生了相似的结果。图1显示了一个比较。
While the features of the raytracer and rasterizer have considerable overlap, they are not identical, and this book explores their specific strengths, some of which can be seen in Figure 2.
虽然raytracer和rasterizer的功能有相当多的重叠,但它们并不完全相同,本书探讨了它们的具体优势,其中一些可以在图2中看到。
The book provides informal pseudocode throughout the text, as well as links to fully working implementations written in JavaScript that can run directly in any web browser.
该书在整个文本中提供了非正式的伪代码,以及用JavaScript编写的可以直接在任何网络浏览器中运行的完全工作的实现的链接。
Why Read This Book?
为什么要读这本书?
This book should give you all the knowledge you need to write software renderers. It does not make use of, or teach you how to use, existing rendering APIs such as OpenGL, Vulkan, Metal, or DirectX.
本书应该为你提供编写软件渲染器所需的所有知识。它并不利用或教你如何使用现有的渲染API,如OpenGL、Vulkan、Metal或DirectX。
Modern GPUs are powerful and ubiquitous, and few people have good reason to write a pure software renderer. However, the experience of writing one is valuable for the following reasons:
现代的GPU功能强大且无处不在,很少有人有充分的理由去编写一个纯软件渲染器。然而,由于以下原因,编写一个渲染器的经验是很有价值的。
- Shaders are software. 着色器是软件。
The first, ancient GPUs of the early 1990s implemented their rendering algorithms directly in hardware, so you could use them but not modify them (which is why most games from the mid-1990s look so similar to each other). Today, you write your own rendering algorithms (called shaders in this context) and they run in the specialized chips of a GPU.
20世纪90年代初的第一批古老的GPU直接在硬件中实现了它们的渲染算法,因此你可以使用它们,但不能修改它们(这就是为什么90年代中期的大多数游戏看起来如此相似)。今天,你可以编写自己的渲染算法(在这里称为着色器),它们在GPU的专用芯片中运行。- Knowledge is power. 知识就是力量。
Understanding the theory behind the different rendering techniques, rather than copying and pasting half-understood fragments of code or cargo-culting popular approaches, lets you write better shaders and rendering pipelines.
了解不同的渲染技术背后的理论,而不是复制和粘贴半懂不懂的代码片段或对流行的方法的崇拜,可以让你写出更好的着色器和渲染管道。- Graphics are fun. 图形很有趣。
Few areas of computer science provide the kind of instant gratification offered by computer graphics. The sense of accomplishment you get when your SQL query runs just right is nothing compared to what you feel the first time you get raytraced reflections right. I taught computer graphics at university for five years, and I often wondered why I enjoyed teaching the same thing semester after semester for so long; in the end, what made it worth it was seeing the faces of my students light up and seeing them use their first rendered scenes as their desktop backgrounds.
计算机科学中很少有领域能提供像计算机图形学那样的即时满足感。当你的SQL查询运行得恰到好处时,你所获得的成就感与你第一次获得光线跟踪反射的感觉相比,简直不值一提。我在大学里教了五年的计算机图形学,我经常想,为什么我喜欢一个学期又一个学期地教同样的东西这么久;最后,使它值得的是看到我的学生的脸亮起来,看到他们使用他们的第一个渲染的场景作为他们的桌面背景。
About This Book 关于此书
This book is divided into two parts, Raytracing and Rasterization, corresponding to the two renderers we are going to build.
本书分为两部分,即光线追踪和光栅化,与我们将要构建的两种渲染器相对应。
The first chapter introduces some basic knowledge necessary to understand these two parts. I suggest you read the chapters in order, but both parts of the book are self-contained enough that they can be read mostly independently.
第一章介绍了理解这两部分所需的一些基本知识。我建议你按顺序阅读这几章,但这本书的两部分都是自成一体的,基本上可以独立阅读。
Here’s a brief overview of what you’ll find in each chapter.
以下是每一章中你会发现的简要概述。
- Chapter 1: Introductory Concepts 第1章:介绍性概念
We define the canvas, the abstract surface we’ll be drawing on, and
PutPixel, our only tool to draw on it. We also learn to represent and manipulate colors.
我们定义画布,即我们要画的抽象表面,以及PutPixel,我们在上面作画的唯一工具。我们还学习如何表示和处理颜色。
- Part I: Raytracing 第一部分:光线追踪
- Chapter 2: Basic Raytracing 第2章:基本光线追踪
We develop a basic raytracing algorithm capable of rendering a few spheres, which look like colored circles.
我们开发了一种基本的光线跟踪算法,能够渲染一些球体,看起来像彩色的圆。- Chapter 3: Light 第三章:光
We establish a model of how light interacts with objects and extend the raytracer to simulate light. The spheres now look like spheres.
我们建立了一个光与物体相互作用的模型,并扩展了光线追踪器来模拟光线。现在球体看起来像球体。- Chapter 4: Shadows and Reflections
第四章:阴影和反射 We improve the appearance of the spheres: they cast shadows on each other and can have mirror-like surfaces where we can see reflections of other spheres.
我们改进了球体的外观:它们互相投下阴影,可以有类似镜子的表面,我们可以看到其他球体的反射。- Chapter 5: Extending the Raytracer
第5章:扩展光线追踪器 We present an overview of additional features that can be added to the raytracer, but which are beyond the scope of this book.
我们介绍了可以添加到光线追踪器中的额外功能的概述,但这些功能超出了本书的范围。
- Part II: Rasterization 第二部分:光栅化
- Chapter 6: Lines 第6章:线
We start from a blank canvas and develop an algorithm to draw line segments.
我们从一张空白的画布开始,开发一种算法来绘制线段。- Chapter 7: Filled Triangles 第7章:填充三角形
We reuse some core ideas from the previous chapter to develop an algorithm to draw triangles filled with a single color.
我们重新使用前一章的一些核心思想来开发一种算法,以绘制充满单一颜色的三角形。- Chapter 8: Shaded Triangles 第8章:阴影三角形
We extend the algorithm from the previous chapter to fill our triangles with a smooth color gradient.
我们扩展上一章的算法,用平滑的颜色梯度填充我们的三角形。- Chapter 9: Perspective Projection
第9章:透视投影 We take a break from drawing 2D shapes to look at the geometry and math we need to convert a 3D point into a 2D point we can draw on the canvas.
我们从绘制二维图形中抽身出来,看看将三维点转换为我们可以在画布上绘制的二维点所需的几何学和数学。- Chapter 10: Describing and Rendering a Scene
第十章:描述和渲染一个场景 We develop a representation for objects in the scene and explore how to use perspective projection to draw them on the canvas.
我们为场景中的物体建立一个表征,并探索如何使用透视投影将它们画在画布上。- Chapter 11: Clipping 第11章:剪裁
We develop an algorithm to remove the parts of the scene that the camera can’t see. Now we can safely render the scene from any camera position.
我们开发了一种算法来移除摄像机看不到的场景部分。现在我们可以从任何摄像机的位置安全地渲染场景。- Chapter 12: Hidden Surface Removal
第12章:隐藏的表面清除 We combine perspective projection and shaded triangles to render solid-looking objects; for this to work correctly, we need to ensure distant objects don’t cover closer objects.
我们将透视投影和阴影三角形结合起来,以渲染看起来很结实的物体;为了使其正常工作,我们需要确保远处的物体不会覆盖近处的物体。- Chapter 13: Shading 第13章:阴影
We explore how to apply the lighting equation developed in Chapter 3 to entire triangles.
我们探讨如何将第三章中开发的照明方程应用于整个三角形。- Chapter 14: Textures 第14章:纹理
We develop an algorithm to “paint” images on our triangles as a way to fake surface detail.
我们开发了一种算法,在我们的三角形上 "绘制 "图像,作为伪造表面细节的一种方式。- Chapter 15: Extending the Rasterizer
第15章:扩展光栅器 We present an overview of features that can be added to the rasterizer, but which are beyond the scope of this book.
我们对可以添加到光栅器中的功能进行了概述,但这些功能超出了本书的范围。
- Appendix: Linear Algebra 附录。线性代数
We introduce the basic concepts from linear algebra that are used throughout this book: points, vectors, and matrices. We present the operations we can do with them and provide some examples of what we can use them for.
我们介绍了贯穿本书的线性代数的基本概念:点、向量和矩阵。我们介绍了我们可以用它们进行的操作,并提供了一些我们可以使用它们的例子。
About the Author 关于作者
I’m a senior software engineer at Google. In the past, I’ve worked at Improbable (http://improbable.io), who have a good shot at building the Matrix for real (or at the very least revolutionizing multiplayer game development), and at Mystery Studio (http://mysterystudio.com), a game development company I founded and ran for about a decade and which released almost 20 games you’ve probably never heard of.
我是谷歌的一名高级软件工程师。过去,我曾在Improbable ( http://improbable.io )工作过,他们很有可能建造出真正的黑客帝国(或者至少彻底改变了多人游戏的开发),也曾在Mystery Studio ( http://mysterystudio.com )工作过,这是一家我创立并经营了大约十年的游戏开发公司,它发行了近20款你可能从未听说过的游戏。
I taught computer graphics for five years at university, where it was a semester-long third-year subject. I am grateful to all of my students, who served as unwitting guinea pigs for the materials that inspired this book.
我在大学教了五年的计算机图形学,那是一个学期的三年级科目。我很感谢我所有的学生,他们在不知情的情况下充当了激发本书的材料的小白鼠。
除了计算机图形,我还有其他的兴趣,与工程有关的和其他的。请看我的网站, http://gabrielgambetta.com ,了解更多细节和联系信息。