Live data from Hacker News

Scratchapixel: Computer Graphics Programming from Scratch

scratchapixel.com

51–60 of 73 posts

Re: Scratchapixel: Computer Graphics Programming from Scratch

#51
post #6

What I don't get is why nearly all resources on 3D graphics assume the reader is familiar with matrices and linear algebra. For those who don't speak that language, what you're doing is plotting numbers into a magic box, doing a magic multiply, and hey presto, we got 3D! Magic. Meanwhile a teenager could easily get an intuitive grasp of 3D graphics if you just explained how translation is nothing besides addition, an…

In addition to the recommendation by lastofus, I highly highly recommend this presentation:

https://www.youtube.com/watch?v=GNO_CYUjMK8

Despite the title, the talk is NOT WebGL specific. It's a fantastic, FANTASTIC tour of 3D graphics fundamentals.

This is, IMHO, quite simply the best presentation I've ever seen on how matrix math relates to graphics programming.

Also see Stevens' site: http://acko.net

Side note - There are some great pages there, be sure to check this one out if you're at all interested in how math is applied to animation:

http://acko.net/blog/animate-your-way-to-glory/

Re: Scratchapixel: Computer Graphics Programming from Scratch

#52
post #48

Earlier quoted context omitted.

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…

> 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 we…

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.

Re: Scratchapixel: Computer Graphics Programming from Scratch

#53

Earlier quoted context omitted.

We learned basic vector arithmetic back in highschool doing our own gamedev stuff, and it's pretty straightforward. The use of matrices as "mapping" one space to another likewise so. Any approach that ignores the relevant parts of linear algebra, though, is probably going to collapse under its own weight as things get more complicated--experiencing that collapse may be interesting from a learning perspective, but I s…

This. You went to the right high school. Matrices and vectors are just a subset of linear algebra, and can definitely be taught to teenagers. When I teach graphics to 3rd-year college undergraduates, I tell them "this is what all that point-and-vector stuff you learnt in high school was really meant for". They've usually forgotten it all, but it comes back to them quickly, and the matrix stuff on top of that isn't ve…

For learning comp-sci-focused linear algebra with programmatic exercises in Python, see http://codingthematrix.com

Re: Scratchapixel: Computer Graphics Programming from Scratch

#54

Earlier quoted context omitted.

We learned basic vector arithmetic back in highschool doing our own gamedev stuff, and it's pretty straightforward. The use of matrices as "mapping" one space to another likewise so. Any approach that ignores the relevant parts of linear algebra, though, is probably going to collapse under its own weight as things get more complicated--experiencing that collapse may be interesting from a learning perspective, but I s…

This. You went to the right high school. Matrices and vectors are just a subset of linear algebra, and can definitely be taught to teenagers. When I teach graphics to 3rd-year college undergraduates, I tell them "this is what all that point-and-vector stuff you learnt in high school was really meant for". They've usually forgotten it all, but it comes back to them quickly, and the matrix stuff on top of that isn't ve…

A minor clarification: the vector/matrix stuff we picked up outside of class by hanging out at Barnes & Nobles and reading books. :)

I was fortunate in college to have a professor teaching graphics (Ron Goldman) whose training was in differential geometry. So, graphics to him was a beautiful if trivial application of mathematics and linear algebra and he taught it as such.

I will always value his teaching of mass-point stuff as an approach, because it just is so nice in how it ends up.

Re: Scratchapixel: Computer Graphics Programming from Scratch

#55

Earlier quoted context omitted.

I remember doing something similar (ignoring matrix transforms and hand-calculating everything with trig because I wasn't comfortable with it). It works fine, and it's great for a theoretical understanding of how rasterization works. I like starting from the basics that way too, then building on it. I think that most tutorials and such have a more practical focus though, with the goal of getting you to practical use…

Ignoring matrix transformations is easy, as long as the eye point is at the origin. Then yes, the algebra is almost trivial: projection is just divide-by-z. But the moment you move the camera, or stretch and turn objects, or worry about clipping, you need the full machinery that matrices provide. They're not that mysterious. I wish they were taught in high school, because they would motivate kids to learn graphics an…

> I wish they were taught in high school, because they would motivate kids to learn graphics and programming.

Learned Linear Algebra (matrix calculations and some analytical geometry) + Calculus in eleventh grade, around 1995. I think it was the standard curriculum in all Eastern Europe and the countries from the ex Soviet Union.

Re: Scratchapixel: Computer Graphics Programming from Scratch

#56
post #6

What I don't get is why nearly all resources on 3D graphics assume the reader is familiar with matrices and linear algebra. For those who don't speak that language, what you're doing is plotting numbers into a magic box, doing a magic multiply, and hey presto, we got 3D! Magic. Meanwhile a teenager could easily get an intuitive grasp of 3D graphics if you just explained how translation is nothing besides addition, an…

Yes. I was able to make a Quake-like clone when I was 17 with no knowledge of how matrices worked or linear algebra. This was before Unity et al, so I had to build it from scratch. You can get surprisingly far by stringing magical utility functions together. The reason I know those things now is because I wasn't forced to learn them then. Detail comes after engagement.

Would love to see the source for that, if you still had it.

Re: Scratchapixel: Computer Graphics Programming from Scratch

#57

Earlier quoted context omitted.

What you are wanting is this book: "Tricks of the 3D Game Programming Gurus" by Andre LaMothe. The book teaches you how to write a 3D software rasterizer from first principles. The first part of the book is nothing but a well written linear algebra primer. The book then assumes you have nothing but a C++ compiler and a pointer to the frame buffer like you might get using SDL. Unfortunately the book appears to be out…

http://portal.aauj.edu/portal_resources/downloads/programmin...

Seems to be the same author, but an earlier book?

Re: Scratchapixel: Computer Graphics Programming from Scratch

#59

Earlier quoted context omitted.

Actually, what's really replaced Bresenham is fixed-point. Dead-simple and non-branchy code with less than a dozen instructions in the main loop. Floating-point is still not exactly as fast as integer arithmetic: https://hbfs.wordpress.com/2009/07/28/faster-than-bresenhams...

Fixed point is a good approach for line drawing but doing a sqrt for circles with integer arithmetics does not work really that well even if you don't know how to convert floats to ints fast (like author of the article you linked :)).

This one shows the branchy and branchless variations of bresenham being nearly equal in speed, within 5% on the older 32 bit CPU, with the branchy old school way being 1% faster on a more recent 64 bit CPU.

Both are significantly faster than the naive floating point version.

That is in line with my experience that floating point can still be quite slow on the CPU; the advantage from using fixed point isn't necessarily in avoiding branches (though it usually helps), but in avoiding floating point computation and conversion between floats & ints. Which is something something you have to deal with as soon as you're addressing memory or trying to pack data e.g. in video coding.

Re: Scratchapixel: Computer Graphics Programming from Scratch

#60
post #48

Earlier quoted context omitted.

> 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 we…

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.…

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.

Post reply on HN