tinyrenderer README

The project's README from github.com/ssloy/tinyrenderer. As one markdown file: README.md.

Software rendering in 500 lines of bare C++

The code itself is of little interest. Check the course notes:

  1. Introduction
  2. Bresenham’s line drawing algorithm
  3. Triangle rasterization
  4. Primer on barycentric coordinates
  5. Hidden faces removal
  6. A crude (but simple) approach to camera handling
  7. Better camera handling
  8. Shading
  9. More data!
  10. Tangent space normal mapping
  11. Shadow mapping
  12. Indirect lighting
  13. Bonus: toon shading
  14. Afterword

In this series of articles, I aim to demonstrate how OpenGL, Vulkan, Metal, and DirectX work by writing a simplified clone from scratch. Surprisingly, many people struggle with the initial hurdle of learning a 3D graphics API. To help with this, I have prepared a short series of lectures, after which my students are able to produce quite capable renderers.

The task is as follows: using no third-party libraries (especially graphics-related ones), we will generate an image like this:

Warning: This is a training material that loosely follows the structure of modern 3D graphics libraries. It is a software renderer. I do not intend to show how to write GPU applications — I want to show how they work. I firmly believe that understanding this is essential for writing efficient applications using 3D libraries.

The starting point

The final code consists of about 500 lines. My students typically require 10 to 20 hours of programming to start producing such renderers. The input is a 3D model composed of a triangulated mesh and textures. The output is a rendering. There is no graphical interface, the program simply generates an image.

To minimize external dependencies, I provide my students with a single class for handling TGA files — one of the simplest formats supporting RGB, RGBA, and grayscale images. This serves as our foundation for image manipulation. At the beginning, the only available functionality (besides loading and saving images) is the ability to set the color of a single pixel.

There are no built-in functions for drawing line segments or triangles — we will implement all of this manually. While I provide my own source code, written alongside my students, I do not recommend using it directly, as doing the work yourself is essential to understanding the concepts. The complete code is available on github, and you can find the initial source code I provide to my students here. Behold, here is the starting point:

#include "tgaimage.h"

constexpr TGAColor white   = {255, 255, 255, 255}; // attention, BGRA order
constexpr TGAColor green   = {  0, 255,   0, 255};
constexpr TGAColor red     = {  0,   0, 255, 255};
constexpr TGAColor blue    = {255, 128,  64, 255};
constexpr TGAColor yellow  = {  0, 200, 255, 255};

int main(int argc, char** argv) {
    constexpr int width  = 64; 
    constexpr int height = 64; 
    TGAImage framebuffer(width, height, TGAImage::RGB);

    int ax =  7, ay =  3;  
    int bx = 12, by = 37; 
    int cx = 62, cy = 53; 

    framebuffer.set(ax, ay, white);
    framebuffer.set(bx, by, white);
    framebuffer.set(cx, cy, white);

    framebuffer.write_tga_file("framebuffer.tga");
    return 0;
}

It produces the 64x64 image framebuffer.tga, here I scaled it for better readability:

Teaser: few examples made with the renderer

Compilation

git clone https://github.com/ssloy/tinyrenderer.git &&
cd tinyrenderer &&
cmake -Bbuild &&
cmake --build build -j &&
build/tinyrenderer obj/diablo3_pose/diablo3_pose.obj obj/floor.obj

The rendered image is saved to framebuffer.tga.

tinyrenderer (ssloy/tinyrenderer)

Searching Database…

这是什么?这是 "tinyrenderer",一门动手实践的计算机图形学课程。它带你从零开始,用大约 500 行 C++ 编写一个软件 3D 渲染器,让你真正理解 OpenGL、Vulkan、Metal 和 DirectX 的底层工作原理。
这是现成的渲染器,还是需要跟着学的课程?这是一个教学项目,而不是一个库。你需要学习 14 篇简短的讲义笔记,并亲自编写渲染器的代码。作者提供的参考代码只作为指导,不是拿来直接调用运行的。
怎样开始学这门课?你先获取作者提供的最小起始代码(一个处理 TGA 格式的类,加上一个空的 64x64 帧缓冲),然后按照讲义笔记一步步操作,用 C++ 自己编写画线、三角形光栅化、着色等功能。
学这门课需要安装图形库吗?不需要使用任何第三方或图形库。从设置像素到画线和画三角形,全部都是用纯 C++ 手写实现的,只用到一个内置的小型 TGA 类来读取和保存图像。
它有可以交互的窗口或图形界面吗?完全没有图形界面。每次运行只是计算像素,并把一张静态的 TGA 图像写入磁盘,没有实时预览,也没有交互功能。
这门课适合哪些人?适合想了解 3D 图形底层原理、而不只是会调用 API 的程序员。作者指出,学员通常需要花 10 到 20 小时编码,才能做出一个可以运行的渲染器。
这门 14 讲的课程涵盖哪些内容?内容从 Bresenham 画线算法和三角形光栅化开始,接着讲重心坐标、隐藏面消除、相机处理、着色、切线空间法线贴图、阴影贴图,最后到间接光照与卡通光照。
为什么要自己做软件渲染器,而不直接用 OpenGL?作者认为,理解光栅化和着色在底层如何工作,对于以后编写高效的真实 GPU 应用至关重要。
输出结果采用什么图像格式?图像保存为 TGA 格式。作者选择它,是因为它是最简单的格式之一,同时仍然支持 RGB、RGBA 和灰度数据。
我能直接使用作者提供的源代码吗?可以,但作者明确不鼓励这样做。整门课程的重点就是让你亲自编写这约 500 行代码,而不是复制一个现成的渲染器。
Creator
ssloy
Size
62.5 MB
Stars
24.4K
Updated
CommitJul 29
Age
11y
Commits · 90 Days