21 comments

[ 2.7 ms ] story [ 36.8 ms ] thread
Part of a fairly old OpenGL tutorial, about 2002.

The age doesn't affect this matrix part, but just FYI that any specific APIs discussed will probably be out of date compared to modern GPU programming.

I don't think there's any mathematical reason to lay out the elements in memory that way. Sure given no context I would probably use i = row + n col as index, but it doesn't really matter much me.

If I had to pick between a matrix being a row of vectors or a column of covectors, I'd pick the latter. And M[i][j] should be the element in row i column j, which is nonnegotiable.

This was answered in the article.

>> It is often fortunate that the OpenGL matrix array is laid out the way it is because it results in those three elements being consecutive in memory.

Matlab deliberately notes that its matrices are laid out like this since most matrix operations occur on columns, a whole column can be loaded on a cache line.
He lost me at

> Well, if we neglect the translation part (the bottom row), then the pure rotation part simply describes the new location of the points on the cube:

Even though im very good at visual with say complex graphs. I really didn’t get where the ‘simply’ part really comes from..

I think the fundamental reason it's laid out like this is because it results in elements with the same major index being close to each other for caching. The assumption is that users will change their minor index much more often than the major one while accessing data, which means that cache prefetching will just work.

Obviously there are situations where this breaks down, but if you have to pick one way to do it I think this makes more sense.

yellow text on green background... my eyes!
> Mathematicians like to see their matrices laid out on paper this way (with the array indices increasing down the columns instead of across the rows as a programmer would usually write them).

Could a mathematician please confirm of disconfirm this?

I think that different branches of mathematics have different rules about this, which is why careful writers make it explicit.

This is one of those things that's perennially annoying in computer graphics. Depending on the API you can have different conventions for:

- data layout (row-major vs. column major)

- pre-multiplication vs. post-multiplication of matrices

Switching either of these conventions results in a transposition of the data in memory, but knowing which one you're switching is important to getting the implementation of the math right.

And then on top of that for even more fun you have:

- Left- vs. right-handed coordinate systems

- Y-up or Z-up

- Winding order for inside/outside

There are lots of small parity things like this where you can get it right by accident but then have weird issues down the line if you aren't scrupulous. (I once had a very tedious time tracking down some inconsistencies in inside/outside determination in a production rendering system.)

People must get taught math terribly if they think "I don't need to worry about piles of abstract math to understand a rotation, all I have to do is think about what happens to the XYZ axes under the matrix rotation". That is what you should learn in the math class!

Anyone who has taken linear algebra should know that (1) a rotation is a linear operation, (2) the result of a linear operation is calculated with matrix multiplication, (3) the result of a matrix multiplication is determined by what it does to the standard basis vectors, the results of which form the columns of the matrix.

This guy makes it sound like he had to come up with these concepts from scratch, and it's some sort of pure visual genius rather than math. But... it's just math.

> This guy makes it sound like he had to come up with these concepts from scratch, and it's some sort of pure visual genius rather than math. But... it's just math.

The problem with this kind of thinking is that it encourages the exact kind of teaching you disparage. It's very easy to get lost in the sauce of arbitrary notational choices that the underlying concepts end up completely lost. Before you know it, matrices are opaque self-justified atoms in an ecosystem rather than an arbitrary tabular shorthand. Mathematics is not a singular immutable dogma that dropped out of the sky as natural law, and rediscovery is the most powerful tool for understanding.

> Anyone who has taken linear algebra should know that [...]

My university level linear algebra class didn't touch practical applications at all, which was frustrating to me because I knew that it could be very useful to some background doing hobbyist game dev. I still wish I had a better understanding of the use cases for things like eigenvectors/values.

I took a linear algebra class, as well as many others. It didn't work.

Most math classes I've taken granted me some kind of intuition for the subject material. Like I could understand the concept independent from the name of the thing.

In linear algebra, it was all a series of arbitrary facts without reason for existing. I memorized them for the final exam, and probably forgot them all the next day, as they weren't attached to anything in my mind.

"The inverse of the eigen-something is the determinant of the abelian".

It was just a list of facts like this to memorize by rote.

I passed the class with a decent grade I think. But I really understood nothing. At this point, I can't remember how to multiply matrices. Specifically do the rows go with the columns or do the columns go with the rows?

I don't know if there's something about linear algebra or I just didn't connect with the instructor. But I've taken a lot of other math classes, and usually been able to understand the subject material readily. Maybe linear algebra is different. It was completely impenetrable for me.

Rotations:

SO(3) is the special orthogonal group for 3 dimensions, which is the set of rotation matrices. Rotation matrices have the determinant 1, which means that they keep the length of a vector the same (forcing it to change direction). Orthogonal group means that the rotation R times its transpose R^T yields the identity matrix aka R^T R=RR^T=I. This means that rotations are trivially reversible. You can obtain rotation matrices either by explicit use of cosine/sine along your desired axis or you can use the Rodrigues rotation formula to describe a rotation along an axis with an angle.

so(3) is the Lie algebra to the Lie group SO(3). The capitalization here is critical. In particular, so(3) is the set of skew symmetric matrices. 3 by 3 skew-symmetric matrices essentially describe the cross product a x b for a constant a. Given a skew symmetric matrix and a rotation angle theta you can use the exponential function to map so(3) to SO(3) through e^(theta S). Note that the exponential is a general mapping from a Lie algebra to its Lie group.

Transformations:

SE(3) is the special euclidean group for 3 dimensions. Its structure consists of T = [[R, t], [0, 1]].

if R = I, then T is a pure translation by t. If R != I and t = 0, then it is a pure rotation.

When you have both a rotation R and a translation t transformation then translating first and rotating second will give you T, but rotating first and translating second will give you T' = [[R, Rt], [0, 1]].

se(3) is the Lie algebra to the Lie group SE(3). One way to represent so(3) is through twists.

Revolute joints are represented by a vector Xi = [ omega, q cross-product omega] where omega is a vector that represents the parameters of a skew symmetric matrix and q is a 3d point on the axis of rotation.

Prismatic joints are represented by Xi = [0, v] where v is pointing in the direction of the movement. The transformation of a twist can be obtained as usual through the exponential function: exp(theta Xi)=T.

https://en.wikipedia.org/wiki/3D_rotation_group

https://en.wikipedia.org/wiki/Orthogonal_group

https://en.wikipedia.org/wiki/Euclidean_group

https://en.wikipedia.org/wiki/Screw_theory

There are a lot more ways to look at and understand these mysterious beasts called matrices. They seem to represent a more fundamental primordial truth. I'm not sure what it is. Determinant of a matrix indicate the area of or volume spanned by its component vectors. Complex matrices used in Fourier transform are beautiful. Quantum mechanics and AI seem to be built on matrices. There is hardly any area of mathematics that doesn't utilize matrices as tools. What exactly is a matrix? Just a grid of numbers? don't think so.
With the advent of things like r/MyBoyfriendIsAI I was expecting a substantially different article than the one I clicked into.
> What stops most novice graphics programmers from getting friendly with matrices is that they look like 16 utterly random numbers.

Wait until they see how physicists and chemists treat matrices! They will pine for the day when a transformation can be described by 16 numbers in a table.

For my fellow visual thinkers who might be looking for a linear algebra book that focuses more on developing the geometric intuition for stuff like this, rather than just pure numeric linear system solving, let me recommend:

"Practical Linear Algebra: A Geometry Toolbox" by Farin and Hansford.

Anecdote from someone does a lot of graphics programming. (Building a molecule viewer/editor):

I've only needed matrices exactly once, when building the engine. It does a few standard transforms (model view matrices etc).

The rest is all len-3 Vecs, and unit quaternions. You can use matrices for these, but I'm on team Vec+Quaternion!

The only thing that ever made this click for me was that the columns can be interpreted as the values for the new, post-transform basis vectors