For various reasons, I’ve been trying to teach myself about 3D computer graphics lately.

Reading theory is all well and good, but you aren’t going to learn a thing if you don’t actually program anything. Graphics code, which has to be very fast, is often written in C or C++, so it’s time to dust off those C++ skills from uni. After several years coding in much easier languages like Javascript and Python, they’re very dusty skills.

## The plan

My first objective is to handle none of the shading side, just the bare bones mathematical aspects of transforming triangles into normalised device coordinates and displaying a 2D image of the points. This doesn’t necessarily need to involve graphics card APIs yet. I will be following this well-explained tutorial from Scratchapixel.

The next goal will be to learn how to send data to OpenGL (or Vulkan!), write simple shaders, and get results out along the lines of less theory-heavy OpenGL tutorials. A reasonable objective here is to have a nice revolving Suzanne or Utah Teapot with a nice-ish shader on it.

From there I can start learning how various graphics algorithms work. I’m not planning to make my own game engine, just understand how they work a bit better.

## C++ is a royal pain innit

I’m developing on my Windows desktop machine with the fancy graphics card and all that.

First problem was to get a compiler. I don’t really like Visual Studio because it’s a huge ponderous program and I learned C++ on Linux, where you just invoke the compiler on the command line. It turns out there is a port of the Gnu C Compiler to Windows called MinGW… but I may not end up actually using that because of other tools I ended up using.

The first question I had was, am I supposed to implement my own linear algebra library? While this entire project is reinventing the wheel to some degree, linear algebra isn’t a particular wheel I care to reinvent. It turns out that once I figured out what to actually search for (even with a qualifier like ‘Euclidean’, ‘vector’ is not a good search term for C++, but ‘matrix’ is, and in fact what I should have been searching is ‘linear algebra’) it turns out there are actually loads of them.

So I hesitantly settled on GMTL.

The next problem I had was, how the heck do you manage packages and dependencies in C++? Other languages have a nice convenient package manager which lets you list the dependencies of your project in your source code repository, and when someone else downloads your git repo, they can just call some command to automatically download and install the packages you need.

C++, by virtue of being much older than the trend towards every language having its own package manager, is a much messier situation. On Linux, most distros provide various C++ libraries in their package manager, but there’s no such luck on Windows, and in any case either way there’s no easy way to specify dependencies.

So should I just dump all the code of the library in my project’s folder, which sounds pretty bloated? This seems to be what people actually do. Should I upload the library code to github along with my project? I really didn’t want to do that.

One possible solution is to use a git feature called submodules. That lets me mark part of my module as a ‘submodule’ based on another git repository, and if I push the module to github, the submodule contents wouldn’t be included but can be installed with a console command. That’s exactly what I want… if the library I want has a git repository associated with it. GMTL doesn’t.

There is a relatively recent attempt to make a C++ package manager/dependency system, called Conan. There’s also another, no-longer-maintained one called CPM. Conan is very flexible and proved easy to set up, albeit defaulting to the Visual Studio compiler through CMake. Well, as long as I can run it from the command line, I don’t really mind whether it’s VS or MinGW. But… GMTL wasn’t on their list.

On the other hand, GLM, another graphics-targeted library with linear algebra code was present. It has a lot of downloads. However, this one didn’t come highly recommended on the above Stack Overflow thread on linear algebra libraries. Someone called GameDeveloper said:

well, it provide graphics programming vector and matrices. it introduces nice amount of overhead to keep compliant on GLSL (if you can do it in GLSL, most times doing it in GLSL is better especially with GL 4.x), and miss many graphics programming primitives (frustum, AABB, BB, ellipsoid). Its swizzle interface it’s obese. Much better alternative would be if it had “.xyzz()” functions generated with some code generation. It is perfect when you have to prototype opengl applications and starts showing its negative sides on bigger projects. never code a math library.

That said, since making a rasterisation program is essentially “prototyping an openGL application", maybe that isn’t so bad. And it will make it easier to learn GLSL later. (AABB is Axis-Aligned Bounding Box, BB is therefore most likely Bounding Box, frustums are of course important in the perspective transform; I’m not sure I’ll need ellipsoids).

Another option for a much more generic linear algebra library also available on Conan is Eigen. That may be overkill for what I want to do, thoguh, which is probably covered in GLM. I’m going to stick with GLM for now.

Next post, some info on rasterisation, and hopefully the details of an actual attempt to implement this thing with code.