It is recommended to read the original blog directly for more timely updates and better reading experience

If you think I write good, give me a thumbs-up 🌟! Thank you. It really means a lot to me!

“Ten Days of Homemade Soft Renderer” is a title THAT I admit is clickbait. Trying to build a homemade soft renderer in 10 days without knowing anything about graphics is like trying to watch 30 days of C++ mastery in one night and get a perfect score at 8 a.m. the next morning after missing a class. I admit there is a genius out there, but unfortunately I’m not.

As mentioned above, this series of wheels are standing on the shoulders of giants to complete, need to have a certain understanding and learning of relevant knowledge, if you are a graphics master, this soft renderer is certainly not to do; If you’re just getting into graphics, building a soft renderer wheel is a great way to solidify your underlying knowledge.


This article mainly (90%) refers to ssloy’s TinyRenderer tutorial, implementing a soft renderer with zero dependency, Create a simplified version of OpenGL2.0 (rendering pipeline only supports Vertex Shader and Fragment Shader custom Shader types) Learn how graphics apis like OpenGL work at the bottom.

My own wheel is the toyRenderer, and I added a lot of comments on the Basis of the TinyRenderer and optimized part of the code according to my own understanding (not ruling out the reverse optimization). If you are interested in the TinyRenderer, please refer to my implementation and the tutorial I am writing now. Just click star 🌟

The directory structure of this column is the same as that of tinyRenderer for easy comparison.


Front knowledge

If you want to understand tinyRenderer code, you need to have a certain amount of knowledge. Here are some summaries of how I built the wheel, which you can refer to before learning.

1. The mathematical

For graphics, mathematics is an inescapable obstacle; There’s not a lot of math involved in this soft renderer wheel, but my personal summary is as follows:

1.1 High school Mathematics

Building a soft renderer requires you to remember some high school geometry, which is not difficult, such as the coordinate formula of a line, the coordinates of the center of gravity, etc.

1.2 calculus

In fact, there is only a small amount of calculus involved. There is a little gradient content in the original course, which has little impact on the overall learning progress.

1.3 Linear Algebra

Linear algebra involves a lot of content, from the simplest vector, to all kinds of coordinate system transformation, all require a solid understanding of linear algebra.

If you’ve forgotten all about linear algebra, I recommend 3Blue1Brown’s the Essence of Linear Algebra, which is the best introduction to linear algebra I’ve ever seen. And most of the matrix transformation involved in graphics is three-dimensional space, basically after reading this course you can start to learn graphics.


2. The graphics

I can only recommend one introductory graphics course, GAMES101- Introduction to Modern Computer Graphics by Yan Lingqi.

I won’t go into details about how powerful Yan Lingqi is, but the most important thing is that GAMES101 has a lot of advantages:

  • All Chinese explanation, greatly reduce the learning threshold of domestic partners
  • The course is very new, starting at the beginning of 2020, and there will be no outdated course/lesson plan
  • As the title of the course is “Introduction to Modern Graphics”, this course covers not only classic raster rendering, but also relatively new and cutting-edge topics such as ray tracing

What do you get out of this course? For example, the 2077 graphics Settings panel you already know what it means

If I had followed this course, I would have completed a small soft renderer after class. However, since I joined the course later, I did not follow my homework. After searching, I found that tinyRenderer was recommended by the most people, so I finally realized my own soft renderer by referring to this tutorial.

Teacher Yan’s video tutorial, you can learn with his PPT. If you’re used to reading books, I recommend two books. One is the famous Tiger book Fundamentals of Computer Graphics 4th Edition and the other is Real Time Rendering 4th Edition. They are both very classic and very famous books.

Neither of these two books has been introduced in China. I have the PDF in English here. You can read my personal profile.


3.C++

This renderer is written in C++, but uses basic syntax. A little more advanced knowledge is template programming and operator overloading. Personally, I think that as long as you have the foundation of other languages, you can start to practice by reading C++ syntax for half an hour.


If I know all three of these things, I can write a soft renderer in ten days. If you don’t get the basics right (especially graphics), ten days is a long time. Nonsense not to say, we go to take the environment!


This tutorial is a zero-dependency soft renderer, so the dependent environment is the C++ development environment.

Note: Zero dependency means that this project does not rely on any third party libraries, soft rendering means that all calculations are done on the CPU side without GPU participation

There are many ways to build and configure C++ environment. The fastest way is to directly use a highly integrated IDE. Visual Studio can be used for Windows computers, and Xcode can be used for Mac users. Of course, you can also use CMake + VSCode to build C++ runtime environment.

I am a lazy person and usually use Xcode to develop a lot, so I don’t want to do more work, so I directly use Xcode to build projects, friends should not learn my bad habit.

Xcode createC++project

1. Create a project

Creating a C++ project is very simple. After launching Xcode, click Create a new Xcode project to Create a new project


2. In the dialog box that appears, select Command Line Tool and click Next


3. In the new popup, fill in Product Name, select C++ Language, and click Next


4. In the new popover, select the project path and click Create to Create the project

At this point, the project is created.

2. Configure a relative path

The soft renderer needs to do some IO operations on some files on the hard disk, so it needs to configure the relative path of the project.

First open a popover in the order Product -> Scheme -> Edit Scheme.

Select Using Custom Working Directory from the popup box and select the path where the project file is located:

3. Drag the source code in

Because this project is zero-dependent, the rendering method is to generate an image in TGA format from source code. Since we are writing soft renderers and not image encoders, we can simply drag the tgaimage.h and tgaimage. CPP from source code into our project.

With main. CPP, there are now only three files in the project directory

.├ ── heavy exercises.cpp ├─ heavy exercises.cpp ├─ heavy exercises.cpp ├─ heavy exercisesCopy the code

Then we’ll write some simple code inside main.cpp — create a 100×100 image and draw a red dot (RGB (255, 0, 0) on the coordinate (52, 41).

#include "tgaimage.h"

const TGAColor red = TGAColor(255.0.0.255);

int main(int argc, char** argv) {
  TGAImage image(100.100, TGAImage::RGB);
  image.set(52.41, red);
  image.flip_vertically(a); image.write_tga_file("output/lesson00.tga");
  return 0;
}
Copy the code

Click the Build button in the triangle at the top left corner of Xcode. If the build is successful and an image named Lesson00.tga is generated in the Output folder, the environment is configured successfully! (The red dot is only one pixel in size, so you can’t see it clearly by clicking on the larger image)





Today we drew a dot on the picture, tomorrow we will learn how to draw a straight line efficiently.


If you think I write good, give me a thumbs-up 🌟! Thank you. It really means a lot to me!