Computer Graphics from Scratch (2021)

(gabrielgambetta.com)

87 points | by ibobev 3 days ago

8 comments

  • kbruner 2 days ago
    I don't see clipping in homogeneous space, which seems like a big omission. Can't beat good old Foley/van Dam - "Computer Graphics Principals & Practice" for stuff like this.
    • hyperman1 3 hours ago
      I've got both 2nd and 3th edition of Foley & Van Dam. The 3d was written when basic techniques were established, so I found the 2nd much more interesting as things seemed more experimental.
  • matinmonshizade 18 hours ago
    When I studied my bachelor’s degree I had a computer graphics course which this book cover most of it and it is fabulous.
  • stevetron 5 hours ago
    But how do you actually put any of it on the screen of your computer? That seems to be an item that is lacking from all books on graphics.
  • senthil_rajasek 21 hours ago
    If you are looking for the source, like I did, to experiment then it is in the js files of the Demo links in each chapter. The demo code uses canvas and js to show the implementations. Looks fun!
  • vivzkestrel 2 days ago
    - a few major questions from a guy who is absolutely not familiar with this subject

    - is x axies increasing towards right, y axis towards top, z axis into the screen ?

    - are there other alignments for x,y,z?

    - does every object have coordinates defined in the -1 to 1 range?

    - what is local coordinate system compared to world coordinate system and does it also stick to -1 to 1 range? let us say I have a 3d Cube 5 units think, what does the local coordinate system for this look like and if I wanted to place it at 0,0,0 what do we do? multply local x world space in matrix form?

    • corysama 19 hours ago
      The mapping of axes to directions is arbitrary. The math doesn't care. Only the humans care. This has lead to nearly every possible permutation being arbitrarily selected by the various content creation and game engines over time. [1]. Hopefully moving forward, new tools will converge on the top-right corner of [1]

      [1] https://media.licdn.com/dms/image/v2/D5622AQFrgBnFEcx5cA/fee...

      When you look directly at the numbers in the vertex data structure of a 3D mesh, those verts are defined in "local" space. If you want a collection of 3D meshes to move around together as a character, you define a "local to model" transform for each mesh to place them in the space of the model (character). When you are editing and animating models in the world, you must pick an arbitrary spatial frame as common reference point relative to which all models are moving around. That's the world space. When you define a camera, you make a conceptual alternative to the world origin that has the camera at the origin. That's camera/view space.

    • Frotag 2 days ago
      (amateur explanation incoming, should probably double check elsewhere)

      I think of it as the world is biggest possible 3d-space. The camera is a 3d volume within the world, often a pyramid called the "frustrum" [0]. The screen is a 2d rectangle generated from the 3d camera frustrum. Loosely speaking, the pyramid is stretched into a rect prism and flattened. Or in other words, only objects inside the camera volume are rendered.

      > is x axies increasing towards right, y axis towards top, z axis into the screen ?

      When referring to the world, yes a lot of rendering software uses the y-up convention, with x being horizontal and z being into / out of the screen.

      https://www.gabrielgambetta.com/computer-graphics-from-scrat...

      > are there other alignments for x,y,z?

      I think y-up is the most common but sometimes its z-up like in Blender. I'm not super familiar with the history / how older software like directx does it.

      > does every object have coordinates defined in the -1 to 1 range?

      The world, camera, and screen (and object) all have different coordinate systems.

      The camera actually has a bunch of them: view space, clip space, normalized device coordinates (NDC).

      View space mostly just applies a translation (and possible rotation if camera is rotated) to the world space coordinates of each object. So the camera (the tip of the frustrum / pyramid) is at origin now, still y-up and z in/out of screen (again assuming no camera rotation).

      Clip space maps the view-space pyramid to a new pyramid where the rectangular cross-sections become square and the height of pyramid along z get's warped too. Both the final side-length of the squares and new pyramid height are usually configurable constants. This "handles" the effects of FOV (the slope of pyramid edges) and camera aspect ratio (the aspect ratio of rect cross sections in view space). [1]

      NDC maps each square xy cross-section of the clip space pyramid to the same size square and stacks these cross-sections to form a cube. This models perspective (far objects need to be bigger (occupy same % of cross-section) to render at same size as something closer to camera / tip of pyramid). This is also where the coordinates get normalized to [-1,1]. [2]

      And then screen space is rendered by flattening the NDC cube (ie draw on screen using xy, use z as order for drawing). There's also stretching involved since NDC is a cube but your screen is probably a rectangle.

      > - what is local coordinate system compared to world coordinate system and does it also stick to -1 to 1 range? let us say I have a 3d Cube 5 units think, what does the local coordinate system for this look like and if I wanted to place it at 0,0,0 what do we do? multply local x world space in matrix form?

      Hopefully the previous blurb mostly answers this, with model space -> world space being the same as world space -> view space. That is, convert coordinates such that the origin changes from middle of object to the world's origin. And again, model space doesn't require normalizing anything to [-1,1], only NDC asks for that.

      --

      [0] When modeled by pyramid volume, they're called perspective cameras. When modeled by a rectangular prism, they're called orthographic cameras.

      [1] I implied clip space is 3d but there's really a 4th dimension to make the math easy.

      [2] Not sure why z has to be normalized to same range (ie why cant result be a rect prism instead of cube) but that's the convention I guess.

      • vivzkestrel 2 days ago
        - first of all thank you for the absolute detail on this one

        - when i searched books i only see web GL, open GL, directX, vulkan etc and most resources online are also dedicated to these

        - are you aware of a resource that teaches these things from scratch like without using any of the libraries above, it explains all these coordinate spaces, then vector math associated and starts drawing / rendering 3d objects that may eventually turn into discussions of collisions, physics , raytracing, shaders etc? (eventually gamedev)

        • hingler36 20 hours ago
          The no starch press book featured in the article is where I learned a lot of this, and it's a really wonderful introduction.

          Graphics libraries are notoriously difficult if you don't have a solid foundation to start from so avoiding them at the beginning is a good call.

          I've done a lot of work with collisions, and the book "Real-Time Collision Detection" is the best resource I've found!

        • khedoros1 18 hours ago
          I know this one: https://www.amazon.com/Tricks-Programming-Gurus-Advanced-Gra...

          The examples use an old version of DirectX, but if I'm remembering correctly, it's purely to get a window that you can draw into. When I was working through it, I was translating code to SDL2, with minimal effort.

          But the focus was on the math, how to implement it efficiently, and building a full software rendering graphics pipeline from scratch.

          So, producing the image, doing animations, rendering shadows.

          Raytracing is something you can study on your own. There are a lot of tutorials out there for building a working raytracer.

          Collisions and physics would be a separate topic.

          Shaders...you're back to one of the graphics APIs, because those are the interfaces that graphics accelerator drivers give us to interface with the GPU itself.

        • Frotag 2 days ago
          No sorry, I don't remember seeing any all-in-one tutorials. For just the rendering / raytracing / shaders I remember using the OP and the github link below a lot.

          https://natethegreat2525.github.io/projects/software_rendere...

          • vivzkestrel 2 days ago
            - super appreciate, as a token of appreciation, i ll share some of my links in return

            - https://danielchasehooper.com/posts/shapeup/ Making a 3D Modeler, in C, in a Week

            - https://medium.com/@togunchan/building-a-simple-3d-graphics-... How I Built a Raycasting 3D Engine in C++ (Wolfenstein-Style)

            - i still havent found that one tutorial to be honest that makes it all click like it starts at a point on the screen, explains all coordinate spaces, explains enough vector math, linear algebra, draws cubes, explains shaders, raytracers, textures, physics, collisions and literally ends up making a quake game or something putting everything brick by brick together

            • Frotag 2 days ago
              > i still havent found that one tutorial

              Yeah I haven't either. I think what's helped me the most (both for rendering and algorithms in general) is reading about the general idea and then trying to derive the process myself. I don't worry about the math as much as the intuition and this usually reveals what steps are optional or are some kind of tradeoff (eg optimizing for memory over cpu, etc).

              In fact I went through this whole learning process recently while trying to make a DSL + interpreter for defining enemy behavior in my game. Almost forgot how satisfying it feels to rediscover an algorithm and compare notes with existing versions.

        • molybd3num 2 days ago
          this is rather 'all over the place' and only graphics, but still a very good resource https://www.scratchapixel.com/
  • AlexeyBrin 3 days ago
    This is a great tutorial. I also bought the book published by No Starch Press.
  • rramadass 7 hours ago
    Related:

    Practical Linear Algebra: A Geometry Toolbox by Gerald Farin and Dianne Hansford - https://www.farinhansford.com/books/pla/