DEV Community

Cover image for My Journey into the Fundamentals of Computer Graphics - Part 2: Linear Algebra Strikes Back
Eric Buitrón López
Eric Buitrón López

Posted on • Updated on • Originally published at eric-buitron.hashnode.dev

My Journey into the Fundamentals of Computer Graphics - Part 2: Linear Algebra Strikes Back

Overview

Hi everyone! This week I continued reading and doing the exercises of the books that I started last week. I finished part 2 of the book by Penny de Byl, where I saw practical applications of essential trigonometry. Meanwhile, in the 3D Math Primer book, I reviewed linear algebra concepts which I go into more detail in the corresponding section. So let's jump straight into what I learned!

Top 3 things I learned

Upright space

The authors of 3D Math Primer introduce a coordinate space that they call "upright space". It is halfway between world space and a particular object space. Besides, the axes are parallel with the axes of the world space and the origin coincides with the origin of the object space. It'll probably make a lot more sense if you look at this figure from the book:

I found it very useful because it makes it easier to transform a point between the world and object space by separating the operation into 2 steps, as shown in this figure:

Normals are very useful

I went from learning how to draw the normals of a cube to calculating the normals of the triangles of each polygon to draw it correctly.

Drawing normals of a cube:

Drawing normals on each triangle face of a cube

Displaying normals on a loaded mesh

Besides, I also learned these two uses of normals:

  • Dictating which side of a polygon is visible through culling, which is removing polygons by not rendering the backface (opposite side of the polygon from which the normal is projected). This means that you only see the polygons that have their normal projected into the camera.

  • Drawing billboards, which are flat planes that represent something solid from both sides. This is mostly used for faking distant geometry/buildings or individual leaves/branches on vegetation and grass.

Specify the order of your vertices!

Normals are also used to calculate how light falls across the surface of a polygon. However, you must specify the order of your vertices to avoid having problems with external files. This is because if OpenGL doesn't receive information about how to calculate the normals, it will automatically calculate them assuming that they are given in a counter-clockwise order. This can result in incorrect lighting of an object. Hopefully, these two GIFs will clarify this point:

Not calculating the normals will give the impression that the colors randomly change and not provide any indication of the direction of the light source.

When the normals are calculated correctly, it's easy to see that the cube is lit from a single direction.

This week's math concepts

Multiple Coordinate Spaces

This chapter was very important for understanding the different coordinate spaces (world, object, camera, and upright). Besides, it explains the usefulness of basis vectors when performing transformations between coordinate spaces.

Intro to Matrices

This chapter served as a review of the basics of matrices. Most notably, it allowed me to understand how a square matrix can describe any linear transformation (which I'll see in more detail in the next chapters). I also found very useful the explanation of how, by interpreting the row vectors of a matrix as basis vectors, you can construct or deconstruct the matrix and visualize the transformation.

For example, it explains how sketching a skew box (the complete parallelogram that is formed by 2 basis vectors) and drawing the object inside the box can help with the visualization, as shown in these figures:

Skew box.

Drawing the object inside the skew box.

Useful resources

This week, there are just a couple of useful resources provided by the 3D Math Primer book. The authors explain how the concepts of linear algebra in the book only focus on the geometric applications of vectors and matrices. They explain how solving larger systems of equations is something that is usually involved in the development of these technologies:

  • Physics engine

  • Fluid simulation

  • Cloth simulation

  • Hair simulation

  • Robust procedural animation

  • Real-time global illumination

  • Machine vision

  • Gesture recognition

Since the book doesn't cover how to solve these bigger parts of linear algebra and scientific computation, the authors recommend a pair of courses from MIT OpenCourseWare given by Professor Gilbert Strang:

What's next

Next week, I'll continue my deep dive into linear algebra by learning the theory and practical applications of the essential linear transformations. Meanwhile, I invite you to tell me your thoughts on the use of "upright space"!

Top comments (2)

Collapse
 
tandrieu profile image
Thibaut Andrieu

Hey, nice to see some 3D enthusiaste 😊

I've never heard the name "upright space" before, but it is actually a good concept for beginer. It actually show that translation and rotation are not commutative. And most of the time, what you want is actually translate first, and then rotate. One can even introduce another space to handle scale:
World Space --translate--> UpRight Space --rotate--> "Rotate Space" --scale--> Model Space.

Keep somewhere in your head that all transformation matrices cannot be decomposed into Translation/Rotation/Scale. You will face this issue when you will start dealing with Projection matrix and perspective 😉

Collapse
 
ericbl3 profile image
Eric Buitrón López

Hey Thibaut, sorry for the late reply. Yeah, that makes a lot of sense! And as you say, it makes it easier to add more spaces to visualize the transformations. Thanks for the tips!