Hacker News new | ask | show | jobs
by alejohausner 3687 days ago
The reason that 4x4 matrices are used so much in graphics is their versatility: you can use them to implement modeling (placing an object into the world), viewing (changing a world point into camera coords), projection and clipping (putting a camera-coord point into clipping coords), color transformations (after all, both x,y,z,w and r,g,b,a are coordinates of a 4D point). That's why there's matrix multipliers built into all graphics hardware.

I understand where you're coming from: translating a point is just 3 additions, so why do a full matrix x point multiply? Scaling is just 3 multiplies, and so on. But if you've got a hardware matrix-point multiplier, all those transformations cost the same, so you're not saving time by devoting special code to each kind of transformation.

Moreover, you don't have to learn full linear algebra: to do graphics, you don't have to know how to invert a matrix, nor solve a system of linear equations, nor diagonalize a matrix. Graphics just needs a tiny subset of linear algebra.

When I teach graphics, I describe points, vectors, lines, circles, matrix-point multiplication, and matrix-matrix multiplication. It takes maybe two lectures, and it provides tools for lots of other parts of the graphics pipeline.

Once you have those tools, you can talk about the viewing transformation (just a special matrix), composite transformations (matrix-matrix multiplication), hierarchical objects (more matrix-matrix multiplication), projection (another special matrix), color transformations (another special matrix).

I also find that when describing ray tracing, you need to talk about going from (row col) pixel coordinates to a 3D point in space, where the ray starts. That's partly the INVERSE view transformation, and (yay) it's just a matrix.

When I was a wee lad in 1981 I did indeed try what you suggest: I worked out the algebra of projection, knowing just where the image plane was (using some points and vectors), and where the world point was. This give me an image point. I was very proud to be able to squeeze this into 50 opcodes on a TI-57 calculator. But when I became a man, and put away childish things, I saw that matrices describe the same process much more cleanly and simply. I wish I had known about them much earlier.

Hell, I wish high school math taught some points and vectors, and some simple matrix operations as I described above (not full-blown linear algebra). Then, they could use those tools to make pictures starting from just numerical descriptions of shapes! It would give many kids more enthusiasm for mathematics, and would start them on the way towards operators, groups, linear spaces, and all those things they might see later. All we would have to do is get education departments to teach the math teachers all this stuff. It would be exhilarating for everyone involved.

1 comments

> I understand where you're coming from: translating a point is just 3 additions, so why do a full matrix x point multiply? Scaling is just 3 multiplies, and so on. But if you've got a hardware matrix-point multiplier, all those transformations cost the same, so you're not saving time by devoting special code to each kind of transformation.

Oh, I'm not arguing against the utility of matrices. I just find that they were too abstract a concept for the young me to learn at the same time while trying to understand 3d graphics. They're an useful abstraction for when you know them, but if you don't, focusing on them is a good way to frustrate a young mind who just wants to understand 3d transformation and perspective projection.

Even if you read about matrices and vectors, it takes quite a bit longer to get comfortable with them and build intuition. Instead, with weak knowledge and lack of intuition (can you expect more from a young teen who had to try learn about these things himself on the net in a foreign language?), dressing the simple core of 3d computations in that language only serves as a barrier to understanding, and you have to work hard backwards to really understand what's happening "under the hood", below these abstractions. It's a bit much to comprehend, and I don't think it helps. It didn't help me for sure. And that ended up being incredibly frustrating, crushing even. It could just be that I am dumber than the lot of you?

If anything, it would've been better if someone could've shown how simple and intuitive the underlying arithmetic is, and then from there on go on to show how one can wrap things up with useful abstractions to combine later. This would give the motivation to learn the more abstract material, and straight away show how it is useful, applying it to things you know already. To me, that would make as much sense as learning to do arithmetic before learning about functions and solving equations.

And really, I wasn't that interested in using graphics hardware or some library. For me the point was in doing it all from scratch.

I think that matrices seem difficult because they are taught by teachers who don't know how they play a role in geometry. Even points and vectors can be hard if the teacher doesn't show how to connect their algebra and their geometry.

Example: a parametric line segment: P(t) = t * A + (1 - t) * B: If t is 0, you get A. If it's 1 you get B. If it's 0.5, you get halfway between A and B. If negative, you're 'before' A. If t > 1, you're 'beyond' B. Then it's easy to see that restricting t > 0 gives you a ray. The geometry matches the algebra nicely.

Example: in ray tracing, you have to test whether a ray intersects a sphere. This gives you a quadratic equation. If the equation has two real roots, the ray hits the sphere twice. If just one real root, it touches it once (one hit point: ray is tangent to sphere). If no real roots, the ray misses (no hit points). I love how the algebra and the geometry correspond so nicely.

Once you tell a student that applying a matrix will change an object's shape, it's easy to get them to see how applying two matrices means applying two shape changes. Then if you apply the changes in opposite order, you get a different matrix product, and a different final shape. Again, the algebra corresponds to the geometry. And you've just taught them about non-commutative multiplication!

And then I show them a translation matrix, and the inverse of the translation matrix (I don't teach them how to invert matrices; why bother them?). And I show them that the inverse makes sense: the translation amounts are the negatives of the original. And I show them that the product of those two matrices is the identity, which again matches the geometric fact: move, then un-move, is the same as doing nothing.

I talk about rotations using my body: I turn around the X axis, then the Y axis. The I repeat it in opposite order, and show that rotations usually don't commute.

What I'm trying to say is that matrices are confusing because teachers SUCK at explaining them. For the small subset of linear algebra that graphics needs, there are a lot of geometric intuitions that make it simple to explain the material.

You're right about that. Much of what I was taught when I studied CS involved learning by implication. We would be shown how to do something, and were supposed to figure everything out after seeing how that was done, while getting on with the next five things we were seeing demonstrated.

It was more like teaching a child to tie a shoelace and then assuming that he can work out how to tie all the different types of knots needed on a sailing ship than it was actually teaching.