DEV Community

Cover image for The experience of writing a ray tracer in Go
Bruno Baère
Bruno Baère

Posted on

The experience of writing a ray tracer in Go

Motivation

There was a lazy day on an extended weekend when I was watching The Cherno's Code Reviews series and came to meet (again) the Ray Tracing in a Weekend book series. As the code was already in C++, I thought it wouldn't be much of a challenge to write it again in the same language. It would rather be best to identify and improve bottlenecks, or to use Modern C++ techniques.

Alas, I had already written a ray tracer in C back in college as a classroom assignment and had worked on some improvements in a Computer Graphics class in my post-grad.

But as I'm learning Go (and loving it) and I was searching for projects that would be nice to have a taste of what Go has to offer, it occurred to me that writing the ray tracer1 in Go would be fun and easy.

And it was.

Go makes it a breeze

Go already has image manipulation, profiling, and flag-parsing off the shelf. So, instead of following the book's suggestion of writing the result to a text file, I could output it directly to a png file.

Go's syntax2 makes you write clean and easy-to-understand code. This project made me work with interfaces, whereas in C++ I'd probably resort to templates or inheritance.

The only external library I had to resort to was a Go implementation of glm.

What's next?

Once I finished the first book, I knew some improvements could be made to run faster and better. For example, as ray tracing is extremely parallel, I could use goroutines to use more cores.

Also, there are other two books in the series: "The next week", and "The rest of your life".

The path of others

Surely, I wasn't the only one who thought about doing this project. If you're interested in how other developers handled implementing the ray tracer in Go, I'd recommend taking a look at these:

What do we take from it?

Adapting "Ray Tracing in a Weekend - Part 1"'s code to Go was an entertaining experience. I certainly would recommend using it to get a hold of the basics of a new language you're learning. I'm just waiting for the opportunity to tackle the second and third books in the series, and going back to the start with The Ray Tracer Challenge.

GitHub logo killerasus / GoRTIW

Implementation of the book series Ray Tracing in One Weekend in Go

Go Raytracing in One Weekend

This is an implementation of Ray Tracing in One Weekend - The Book Series using Go and go-glm.

As I am learning Go, I decided to translate the book's C++ implementation to Go. The objective is to compare both implementations, trying firstly to accomplish each chapter's objectives, and secondly trying to optimize the result.

The current status of the code is shown below:

Chapter Result
Book 1 Complete Result

As stated in the book, Ray Tracing in One Weekend does not aim to create a complete ray tracer (more of a path tracer). The following books add more functionalities to the ray tracer, making it feature complete by the end of the series.




1: Actually more of a path tracer than a ray tracer.
2: Sometimes I get myself thinking that Go is C and Python's child.

Top comments (0)