My Journey into the Fundamentals of Computer Graphics - Part 1: A New Project
Tags: gamedev, learning, computergraphics, mathematics
Published: 2023-08-28 19:14:46

## Preamble

Hello everyone! For the last year, I've been trying to learn, without much progress, about computer graphics. My failure has been mainly due to not giving myself enough time to learn while also trying to go right into intermediate or even advanced topics without having a good grasp on the fundamentals. I decided that it's finally time for that to change and I'm starting a series of (hopefully weekly) dev articles to document my progress. I invite you to join me in my journey into the fundamentals of Computer Graphics!

## Overview

If I want to get into the fun part of computer graphics, which for me at the moment would be to create cool shaders and recreate natural effects such as "god rays" and water caustics, I need to really understand the fundamental topics. But, what are those fundamental topics? Well, for starters, I need to understand (mostly remember) math concepts, such as trigonometry and linear algebra, and also some basic physics. I learned the majority of these topics during university, but it's been over a year since I had to do any kind of math that involved more than the basic operations (I guess that's a quirk of working as a Full-Stack Developer), and even longer since I've seen or used the math that is used for computer graphics.

Having a firm grasp of these concepts will be very useful as I learn about computer graphics because it will allow me to understand what's going on behind the scenes. This means that I'll have an easier time in the future when I want to create any shader or learn more advanced techniques. To start my journey into the fundamentals of computer graphics, I'll review the math concepts covered in these two books, which will be my focus for the next couple of weeks:

* [Mathematics for Game Programming and Computer Graphics](https://www.goodreads.com/book/show/66087612) by Penny de Byl
    
* [3D Math Primer for Graphics and Game Development](https://gamemath.com/) by Fletcher Dunn & Ian Parbery
    

## Top 3 things I learned

It's important to point out that they aren't in any particular order of importance. This week, my top 3 are all from Penny de Byl's book (or research I made during the exercises of the book as you'll see at the end).

### Bresenham's algorithm

Jack Bresenham designed an algorithm that can be used for plotting lines and circles that aren't disconnected from each other, which usually happens if you try to draw them with a naive approach.

Drawing lines with the naive approach. Notice how vertical lines get disconnected:

![Drawing lines with a naive approach. Some of them are disconnected and look like dashed lines.](https://cdn.hashnode.com/res/hashnode/image/upload/v1693198517435/ca9712b4-9425-439e-a490-24671b270171.png?auto=compress,format&format=webp align="left")

Drawing lines with Bresenham's algorithm:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693198999411/57b35787-f78a-4925-8ba5-aa79f33165ac.png?auto=compress,format&format=webp align="center")

Drawing circles with the naive approach:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693199114549/d6f99fd2-950f-4a4b-963f-e78bd788b808.png?auto=compress,format&format=webp align="center")

Drawing circles with Bresenham's algorithm:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693199132846/5bd6e5df-a8c3-4d90-a1bc-f9db2309268f.png?auto=compress,format&format=webp align="center")

### Intro to OpenGL

I started learning OpenGL and went from drawing a square mesh to drawing a textured cube.

Square mesh:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693199356871/b679abed-fe93-43c6-8d1a-9a526b4cbaaa.png?auto=compress,format&format=webp align="center")

Perspective view of wireframe cube:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693199391842/6fe7cd8b-025a-42b7-9fed-98ffab87483f.png?auto=compress,format&format=webp align="center")

Cube with very basic lighting

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693199431789/b989ab4b-636c-427f-a4a0-136f871e3044.png?auto=compress,format&format=webp align="center")

Cube with textures:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693199459292/814dbeff-635c-4cb7-ad53-1587527ea1df.png?auto=compress,format&format=webp align="center")

As you can see, the texture didn't look right the first time I finished this exercise, which is what led to probably my biggest discovery of the week.

### Cubes need 24 vertices, not 8

During the first exercise of drawing a cube, I had to define the vertices and triangles that would form the mesh. The most obvious approach, which is what I did, would be to define just 8 vertices since that's what a cube needs right? Well, everything seemed right at first but when I got to the exercise of texturing the cube, I defined 8 UV coordinates, one per vertex of my cube, and something was completely off as you saw in the previous subsection. I'll include the picture here again so you don't have to go back to see it:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693199459292/814dbeff-635c-4cb7-ad53-1587527ea1df.png?auto=compress,format&format=webp align="center")

Two faces of the cube (the picture only shows one) had a deformed version of the texture. After some research, I found out that in computer graphics, you need to define a cube using 24 vertices instead of 8. This is mostly done to correctly define the normals of the cube, which is something that I'll see later in the book, but it also helped with the texturing of the cube.

I already see you screaming at your screen "The numbers Eric! What do they Mean?". So put simply, the 24 vertices are the result of having each plane of the cube (6 planes) have its own set of vertices (4 vertices each). This means that some vertices will have the same position but their UV coordinates and normal could be different, so they aren't necessarily duplicates. If you want to dig deeper into this topic you can look at these posts that I found during my research:

* [Stack Overflow](https://stackoverflow.com/questions/5046579/vertex-normals-for-a-cube)
    
* [Unity Forum](https://forum.unity.com/threads/explained-how-many-verices-cube-needs-8-16-nah-24-what.546219/)
    
* [Reddit](https://www.reddit.com/r/opengl/comments/119hvze/im_confused_about_vertex_layouts_uvs_and_normals/)
    

After fixing my code and having 24 vertices, and therefore also 24 UV coordinates, my cube finally had all faces with the correct texture:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693201218593/97745ce0-60bc-4d72-a634-3b855587aaea.png?auto=compress,format&format=webp align="center")

I know the tiling is still off but that wasn't part of the exercise or what I'm learning right now, so I decided to just leave it like that. If I decide someday to make my own Minecraft clone I might come back and learn how to properly fix the texture of this cube.

## This week's math concepts

This week, I read the first two chapters of the 3D Math Primer book, where I reviewed the following topics:

* Cartesian Coordinate Systems:
    
    * I think the most important part for me was learning how to convert from one hand coordinate space to another since nobody seems to agree on which to use. I mean, this book uses the left-hand coordinate system while OpenGL uses the right-hand.
        
* Vectors:
    
    * The exercises helped me understand the practical applications of the operations of vectors for game development in general. For example:
        
        * **Dot product**: Knowing if a point is in front or behind a character and if the character can see that point considering their field of view and maximum viewing distance.
            
        * **Cross product**: Knowing if a character rotates clockwise or counterclockwise when moving between 3 points.
            

## Useful resources

The chapters I've read from Penny de Byl's book are full of very useful resources for different purposes, so I decided to share them here in case they're useful to anyone.

* Use the [Desmos calculator](http://www.desmos.com/calculator) to double-check how the line (function) you're trying to draw should look like.
    
* Get free fonts at [Dafont](http://www.dafont.com).
    
* Create your own font from scratch at [FontSctruct](http://www.fontstruct.com).
    
* Get free 2D icons from [IconArchive](http://www.iconarchive.com).
    
* Get free textures from [textures.com](http://www.textures.com).
    

## What's next?

Next week, I'll take a deep dive into linear algebra in both books with topics such as matrices and coordinate spaces. Meanwhile, I invite you to tell me if you already knew that cubes must have 24 vertices or if you have any other resources that might be useful for my journey!