News for hackers. Stuff that matters.
Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @01:48PM (#26017464)
A long time ago, due to an improbable sequence of events, I found myself teaching Computer Graphics at my alma mater, a year after I took the class myself. Over the following five years or so, my approach to the subject evolved, and I really got the hang of it. After I stopped teaching, I took my notes, handouts and slides, and made them into a series of articles that I put on my website, where they remained in relative obscurity. Hacker News managed to find it every once in a while, and it was generally well received, but nothing came out of this. Until April 2019. It made the HN front page again [0], except this time it caught the attention of an editor in No Starch Press [1].

Long story short, today I’m incredibly grateful and excited to tell you that CGFS is coming out as a real book, with pages and all [2]. The folks at NSP graciously agreed to let me publish the updated contents, the product of almost two years of hard editing and proofreading work, for free on my website [3]. But if you’d like to preorder the printed or ebook version, you can use the coupon code MAKE3DMAGIC to get a 35% discount at https://nostarch.com/computer-graphics-scratch.

I’m still somewhat in disbelief that my work is getting published as a book, and this genuinely wouldn’t have happened without your support. So once again, THANK YOU :)

[0] https://news.ycombinator.com/item?id=19584921

[1] http://nostarch.com

[2] https://nostarch.com/computer-graphics-scratch

[3] http://gabrielgambetta.com/computer-graphics-from-scratch

Re:Computer Graphics from Scratch by mstade on Wednesday February 03, 2021 @03:43PM (#26018846)
Hey Gabe – great work and huge congrats!

I'm sure you don't remember me, but we used to work together at Improbable – you sent me such a wonderful email when I left to go back to Sweden, thank you!

Anyway, just wanted to say congrats and hope you're doing great buddy. All the best! :o)

Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @03:52PM (#26018940)
Of course I remember you! Could never forget such an impeccably dressed coworker :) I think we were super close to meeting in Miami that one time I had a long layover?
Re:Computer Graphics from Scratch by mstade on Wednesday February 03, 2021 @04:21PM (#26019262)
Aww – thank you! <3

That's right, I was in Florida visiting family. Would love to reconnect, don't hesitate to ping me an email at [email protected] if the feeling is mutual! :o)

Re:Computer Graphics from Scratch by mcdevilkiller on Friday February 05, 2021 @05:20AM (#26034804)
If I were you, I'd edit that email, there are spambots crawling everywhere :)
Re:Computer Graphics from Scratch by mstade on Friday February 05, 2021 @12:27PM (#26038752)
It's ok, my spam filter seems to do its job (knock on wood!) Either way I'm past the edit time limit now anyway..

What matters is I got the opportunity to reconnect with an old pal, I'll gladly pay the spam tax if it comes to that. Worst case scenario I guess I'm getting a new email address eventually. :o)

Good tip though, thanks!

Re:Computer Graphics from Scratch by enriquto on Wednesday February 03, 2021 @05:06PM (#26019794)
I love this resource, thank you!

My favorite chapters are those about drawing raster lines and triangles. One of my first programs was writing (in 386 16-bit asm) the routine to draw a triangle, and I love these algorithms dearly.

An important and beautiful thing that is missing in the book is the drawing of anti-aliased lines (easy) and triangles (not trivial!). I find that rendering carefully shaded smooth objects with a pixelized boundary loses a big part of the magic.

Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @05:53PM (#26020311)
Yep, good point. The reason is that the objective of the book is to cover as much material as possible, in the shortest time possible, and in the simplest way possible; so I had to build the shortest path between putPixel() and filtered textures, and that leaves things like antialiasing behind.

I explain a bit of this in the introduction, most of the time I present not the best/fastest algorithm, but the one that it's the simplest to understand. For example, I don't even mention Bresenham's algorithm; instead I present a super simple but inefficient one, which achieves two critical things: (1) you're drawing lines in no time, (2) it motivates the linear interpolation method that is then used for shading, z-buffering and texturing.

Re:Computer Graphics from Scratch by gauku on Wednesday February 03, 2021 @02:03PM (#26017663)
This looks great. I have been developing interest in CG recently and this looks like a great way to start. Thank you.

And finding your Imdb profile was a nice addition on top ;)

All the best.

Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @03:24PM (#26018644)
Thanks! For better or worse, I'm interested in a lot of different things
Re:Computer Graphics from Scratch by kayoone on Wednesday February 03, 2021 @02:32PM (#26018061)
> due to an improbable sequence of events

is that why you ended up working for improbable? ;)

Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @05:58PM (#26020345)
That was a semi-intentional pun, good catch ;)

You're not far off the mark, though! It was my other series of articles about client-side prediction [0] that brought me to Improbable's attention :)

[0] https://gabrielgambetta.com/client-server-game-architecture....

Re:Computer Graphics from Scratch by kayoone on Thursday February 04, 2021 @01:12AM (#26023105)
excellent articles, i quoted them in my bachelor thesis some time ago :)
Re:Computer Graphics from Scratch by breck on Wednesday February 03, 2021 @02:27PM (#26017990)
I'm always in the market for great graphics books, and NSP is my favorite publisher, so excited to get this. Thanks for the coupon!
Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @04:27PM (#26019348)
You're welcome! I have to say they're also amazing to work with :) Special thanks to Alex and Kassie <3
Re:Computer Graphics from Scratch by billpollock on Thursday February 04, 2021 @04:11AM (#26023928)
That's great to hear!
Re:Computer Graphics from Scratch by 2pEXgD0fZ5cF on Thursday February 04, 2021 @04:00AM (#26023879)
I wanted to learn about rendering and graphics for a while.

Almost every modern source however uses engines or big frameworks and skips the parts I actually want to look into. Compiling a reading list or figuring things out myself of course would have been an option, but I guess others can relate to the problem of having too little time for hobby projects :)

Big bonus points also for trying to remain programming language agnostic! I immediately preordered your book and find it hard to express my excitement, can't wait.

Re:Computer Graphics from Scratch by garmaine on Thursday February 04, 2021 @05:08AM (#26024168)
Way back in the day I read either the 1st or 2nd edition of this book: https://www.amazon.com/Computer-Graphics-Principles-Practice...

It's a classic in the computer graphics field, like Knuth is for algorithms. I'd recommend it alongside the OP's new book.

EDIT: But for anyone who is reading this wanting to learn about practical computer graphics, CG:P&P is NOT the resource to use! Learning the low-level rasterization algorithms used in computer graphics is an important thing to learn at some point, just like learning assembly language provides valuable insights even if you never touch a line of assembler again. But if you actually want to write graphics code on modern hardware with GPUs, I'd highly recommend Real Time Rendering instead: https://www.amazon.com/Real-Time-Rendering-Fourth-Tomas-Aken...

Re:Computer Graphics from Scratch by 2pEXgD0fZ5cF on Thursday February 04, 2021 @05:47AM (#26024321)
Thanks a lot for the recommendation!

What would you say are the prerequisites for this one?

Also from the description it sounds as if it also goes a bit more into 2D graphics too, is that correct?

Re:Computer Graphics from Scratch by garmaine on Thursday February 04, 2021 @06:14AM (#26024429)
Again, I read an ancient edition which predated the existence of consumer GPUs. But the edition I read was more than half on the topic of 2D graphics and what you might consider to be mundane things like font rasterization.

You might think this has nothing to do with 3D graphics, but in fact there is a big overlap. The graphics driver + GPU takes a description of a 3D scene and projects it into 2D primitives whichs are rasterized onto a bitmap (the display) just as one might render a font glyph.

So IIRC the book begins with the basic theory and techniques of 2D graphics, and then shows how projective geometry can be used to render 3D scenes onto 2D displays, and progressively adds various corrections which we take for granted, such as perspective-correct interpolation of texture values, which these days is done automatically by the hardware.

I don't think there's any pre-requisites other than a basic understanding of beginner computer science, coding, and algorithms. If you can read Knuth, you can read CG:P&P.

Also, see my edit to the grandparent comment.

Re:Computer Graphics from Scratch by michaelg7x on Thursday February 04, 2021 @08:57AM (#26025328)
Hi Gabriel, is there a European site from which we can order? The delivery costs from the US are quite punchy, and it makes no sense to send the paper copy when a printer could do as good a job locally. Too many paper-miles? Looking forward to getting hold of it, great work! Mike
Re:Computer Graphics from Scratch by ggambetta on Thursday February 04, 2021 @09:07AM (#26025402)
Hi Mike! I have nothing to do with distribution, but the book is also available from Amazon [0], B&N [1], Bookshop [2], and Penguin [3], maybe one of these has better shipping to Europe?

[0] https://www.amazon.com/Computer-Graphics-Scratch-Gabriel-Gam...

[1] https://www.barnesandnoble.com/w/computer-graphics-from-scra...

[2] https://bookshop.org/books/computer-graphics-from-scratch/97...

[3] https://www.penguinrandomhouse.com/books/645952/computer-gra...

Re:Computer Graphics from Scratch by michaelg7x on Thursday February 04, 2021 @09:48AM (#26025729)
Hey, thanks for this. I did go on to find it on Amazon.co.uk, but of course the e-book + paper-book bundle is different. I'll check out the other sites as well. Thanks again.
Re:Computer Graphics from Scratch by trilinearnz on Wednesday February 03, 2021 @05:36PM (#26020109)
Congratulations! It's a wonderful resource, and great to see it make it's way into library-form.
Re:Computer Graphics from Scratch by jzer0cool on Wednesday February 03, 2021 @08:40PM (#26021569)
Congrats on this book! I did a computer graphics class for my undergraduate and we used this fat OpenGL book bible to try and implement some functions.

Things got very hard with ray tracing and later u,v texture mapping, etc. Making a note here so I can try your book out sometime!

Re:Computer Graphics from Scratch by aperrien on Wednesday February 03, 2021 @02:29PM (#26018022)
This is awesome, but as a kid back in the Pong era, I always wondered how the basic squares of pixels worked. In your travels writing this book, did you ever come across one that explained that in a way friendly to 12 year olds?
Re:Computer Graphics from Scratch by gamacodre on Wednesday February 03, 2021 @04:03PM (#26019070)
I was a 12 year old when I learned about them :)

The idea of a pixel is that it's the smallest area of the screen that can be independently controlled, thus the designation "pixel" for "picture element".

In most modern phone screens or monitors, each pixel is formed by a group of three smaller elements with fixed colors (usually red, green, and blue) but _variable brightness_. By controlling the brightness of these sub-elements, we can control what the overall color of the pixel appears to be - once you get more than an inch or two away from the screen, the light from the element group blends into what we see as a single color - so 100% green + 100% red + 0% blue looks like bright yellow. 50% each for red, green, and blue looks like a middling grey. You can usually see the structure of the pixel with a magnifying glass, though this is easier with an old TV or monitor than a modern phone.

These pixels are laid out on a regular rectangular grid, and your display controller will offer some way to set the color of each pixel and to then update them all at some (usually) regular interval, for example 60 times per second. In computers, it's common to keep a "frame buffer" around that stores separate values for the red, green, and blue components of every pixel on the screen. If these are 8-bit values, that implies that each R/G/B component can have 256 different levels of brightness, and in combination they allow each pixel to take on one of about 16,000,000 possible colors. So, a program can change pixel colors by writing different values into this buffer and waiting for the updated buffer to be processed by the display controller to change what the display is showing.

Of course, the electronics, physics, chemistry, timing, and logic of display generation have changed quite a bit since Pong. And not all displays even have pixels - vector displays used to be a thing, and are still used in some very specialized applications.

Re:Computer Graphics from Scratch by a9h74j on Wednesday February 03, 2021 @08:10PM (#26021381)
I once bought a schematic for a DIY pong. IIRC it used monostables and integrators to control timings -- scan frequencies yes but no strict pixels needed.
Re:Computer Graphics from Scratch by aperrien on Wednesday February 03, 2021 @04:12PM (#26019179)
I appreciate the explanation, but it's not for me. I'm trying to find a good book to recommend to my grandkids. I've had fun teaching them how to use old retro computers (c64, Apple II, and such). It's been a fun experience, but I'd like to give them some references that aren't me! I've personally come a long way in my computing skills since I was staring at shiny new Pong screens :)
Re:Computer Graphics from Scratch by gamacodre on Wednesday February 03, 2021 @05:19PM (#26019930)
That's trickier - somewhere along the way I stopped buying books and just do tactical forays onto the net when I need a new bit of domain knowledge.

Details like how and why pixels work are much easier to understand and retain if they're in some kind of context. Gamers learn about pixels to better understand the games or their gear. Devs learn about them in order to control them. Artists learn about them to better understand why computers mangle their art. So maybe choose an area the grandkids are engaged with and look for something there that also touches on display tech.

Re:Computer Graphics from Scratch by TimBurr on Wednesday February 03, 2021 @05:04PM (#26019760)
Thinking of when I was 12 (2005?), microscopes and magnifying glasses helped me understand. Depends on device DPI (cell phones have an insane number of pixels per inch; desktop screens are usually less dense).

If you can zoom in enough to see subpixel elements, and pull up a color wheel, it's very intuitive to see that the screen is made of pixels, and the pixels are made of three elements.

https://en.wikipedia.org/wiki/RGB_color_model#/media/File:Ad...

The Pico-8 might be a approachable way to explore retrocomputing. It's an in-browser console modelled after GameBoy-era consoles.

https://news.ycombinator.com/item?id=18240375

I don't know about books, though. I remember reading the binary/computers chapter of How Things Work and being thoroughly confused at that age. There was some extended allegory about white mammoths and black mammoths.

Re:Computer Graphics from Scratch by drewzero1 on Thursday February 04, 2021 @10:56AM (#26026446)
Did you have The Way Things Work or The New Way Things Work? I had the latter around the same time. I thought the extended computer section (Bill's Gates!) was very helpful. I still really like the visual in which the image of a mammoth is turned into a series of pumpkin/no pumpkin signals being launched through the air, then becomes a picture of a mammoth on the other side of the field. I think it got a bit too technical for me when it started showing how transistors are constructed-- I guess I should've stuck with the pumpkins.

Now I have both editions and have looked through them side-by-side. It's a bit unfortunate that some pages were dropped to make room for the expanded digital section in the newer edition, but well worth the trade-off for more/better explanations in the digital realm.

Re:Computer Graphics from Scratch by TimBurr on Thursday February 04, 2021 @03:52PM (#26029627)
I'm not sure. Possibly both?

Just took a look over my bookshelf, but they haven't made it with me through moves.

I do still have Incredible Cross-Sections. That was another of my favorites to flip through. It looks like there are reprints and new versions. Would definitely recommend for kids.

https://www.amazon.com/Stephen-Biestys-Incredible-Cross-Sect...

Re:Computer Graphics from Scratch by exDM69 on Thursday February 04, 2021 @05:14AM (#26024194)
Graphics was somewhat more complicated back in the day. In the days before we had enough memory to hold a full screen framebuffer, there was a lot of magic going on with cathode ray tubes, video signals, dedicated hardware sprite engines, memory access clock cycles, using interrupt handling to "race the beam" and so on.

It's not like modern display panels and HDMI signals and digital protocols are simple, but you don't have to go into the details to understand how pixels and framebuffers work.

If you're trying to explain how pixels work, going back to 1980s era technology is a detour into arcane technical details that aren't really relevant any more. They're pretty cool to read about if you're into retro computer technology but they don't have much educational value or relevance to the modern day.

Re:Computer Graphics from Scratch by II2II on Wednesday February 03, 2021 @08:17PM (#26021415)
If they're interested in retro computers, you can always try finding some books from the 1980's. Many titles were written for children. The Apple II was also a fun playground for computer graphics since it was supported in BASIC.
Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @05:56PM (#26020327)
To some extent, I guess that's what I've tried to do here. The linear algebra might be too advanced for a 12 year old (I didn't pick it up until much later!), but on the other hand you don't need to follow all the derivations - a 12 year old can learn a lot just by following the results and the resulting algorithms.

There's also a linear algebra appendix [0] that presents the operations, explains how to use them, and how they can be interpreted, without going in any theoretical depth about why these things are the way they are.

[0] https://gabrielgambetta.com/computer-graphics-from-scratch/A...

Re:Computer Graphics from Scratch by kevin_thibedeau on Wednesday February 03, 2021 @02:45PM (#26018206)
Pong uses discrete logic gates and no microprocessor for its game logic and graphics. It isn't comparable to much else.
Re:Computer Graphics from Scratch by corysama on Wednesday February 03, 2021 @03:23PM (#26018625)
Can you be specific of the context you are interested in?

On modern machines, a pixel is just a set of three numbers indicating how much red, blue and green light should be shown at a particular point. Ex: {0.75 red, 0.0 blue, 0.5 green} for a kinda-dark, orange pixel. The GPU keeps a big 2D grid of these number-triples in memory and on a regular schedule sends out a copy over the DVI cable to your monitor. The monitor has a bit of memory to hold it's copy. And, it has hardware to scan over the grid of numbers to produce a sequence of voltage levels that are used to change the color of the points on the LCD.

There's a bit of math involved in how to do a good job representing colors with numbers and how to convert those numbers to voltages. But, at the most basic level, an image is just a big 2D grid of numbers. If you want to change the image, poke the grid. People want to change images a whole lot. So, we've developed pretty sophisticated hardware and software around poking 2D grid... But, that's a whole other topic.

Re:Computer Graphics from Scratch by foobarian on Wednesday February 03, 2021 @02:54PM (#26018325)
I was dismayed to learn that pixels are not square at all. It got worse with the switch to LCD.
Re:Computer Graphics from Scratch by vram22 on Thursday February 04, 2021 @11:52AM (#26027125)
Interesting, didn't know that. What shape are they, then? Round? Or other?
Re:Computer Graphics from Scratch by mmcgaha on Wednesday February 03, 2021 @09:52PM (#26022025)
Graphics programming in C by Roger T Stevens chapters 9-12.
Re:Computer Graphics from Scratch by Shared404 on Wednesday February 03, 2021 @06:54PM (#26020861)
I've wanted to learn this for a while, and never got around to it.

I think this is the final kick I needed, so thanks!

Re:Computer Graphics from Scratch by legerdemain on Thursday February 04, 2021 @05:56PM (#26030751)
Thanks for keeping the material free online!

This is one of the few tutorials that covers raster rendering. Lots of introductory material on ray-tracing, not a lot on raster graphics.

Where can students look for more stuff on rasterization after they finish this book?

Re:Computer Graphics from Scratch by mysterydip on Wednesday February 03, 2021 @07:20PM (#26021048)
Great news! Please make sure the links to demos work for a while or you have some archival/preservation in mind. I've read through older technical books whose examples went offline and depending on the subject matter it can be quite a loss.
Re:Computer Graphics from Scratch by q_andrew on Wednesday February 03, 2021 @06:53PM (#26020859)
This is awesome. You should be proud!
Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @07:13PM (#26021000)
Thank you! Right now it's 1:13am here and I'm proud, overwhelmed by the reaction, still in disbelief,... good luck sleeping tonight XD
Re:Computer Graphics from Scratch by cshenton on Thursday February 04, 2021 @04:09AM (#26023918)
Congrats on getting your book published! I will definitely be ordering a copy :)
Re:Computer Graphics from Scratch by strangeattractr on Thursday February 04, 2021 @04:11AM (#26023931)
Congratulations on getting published, I have been looking for a book like this.
Re:Computer Graphics from Scratch by runald on Wednesday February 03, 2021 @10:26PM (#26022241)
What's the difference between the ebook and the one free one in the website?
Re:Computer Graphics from Scratch by ggambetta on Thursday February 04, 2021 @03:05AM (#26023589)
The contents are the same; the formatting, presentation, accessibility, etc, are much better in the ebook (an ebook isn't just a printed website).
Re:Computer Graphics from Scratch by LegitShady on Thursday February 04, 2021 @12:05AM (#26022811)
one costs money
Re:Computer Graphics from Scratch by adamnemecek on Wednesday February 03, 2021 @03:31PM (#26018714)
How has your approach changed?
Re:Computer Graphics from Scratch by ggambetta on Wednesday February 03, 2021 @06:52PM (#26020850)
At first I mostly followed the lecture structure of the previous professor, but over the years I streamlined it a lot, emphasized a "motivated" approach to each topic (e.g. "last chapter we did this, now we try to do this thing, it doesn't quite work, how can we fix it?"), and generally refined and simplified the explanations of everything. Teaching the same material year after year was a fantastic opportunity to identify the recurring sticking points and finding ways around them.
Re:Computer Graphics from Scratch by sriram_sun on Wednesday February 03, 2021 @01:56PM (#26017581)
Awesome! Thanks for sharing!