Software rendering in 500 lines of bare C++

(haqr.eu)

46 points | by mpweiher 1 hour ago

4 comments

  • articulatepang 29 minutes ago
    I went through this a few months ago in Rust. I wrote all the code by hand, no LLMs. Then I went ahead and added a small "game" on top, plus some special effects like pixelization shaders and chromatic aberration at the edge of a flashlight.

    https://github.com/kshitijl/tinyrenderer-rs

    if anyone is interested! The repo has lots and lots of in-progress screenshots so you can see the renderer come to life, plus all the hilarious visual bugs along the way.

    I learned a lot! My biggest lesson, other than the specifics of how rendering works, was that modern CPUs are really fast: a single-threaded CPU renderer can definitely run an interactive 3D game with some fancy special effects.

  • bob1029 34 minutes ago
    I wish we could have just one of these tutorials properly cover the concern of triangle clipping. This is the part that I struggle with the most in a software renderer. If you are going to be building a practical one, this is something you will eventually have to deal with, even for super basic scenes. Any time geometry intersects the view frustum you need to clip those triangles.
    • thechao 8 minutes ago
      You only need to clip triangles is you're worried about attribute interpolation for very large triangles. There's two ways to handle this: (1) discard (fast but not a great user experience); or, (2) primitive synthesis. Just frustum clipping is enabled by point picking in the local tile. Primitive synthesis requires some FP kung fu; but, is easiest done in barycentric space against a reverse transformed clipping rectangle. This lets you carefully control clip rounding error using either doubles or (better) fixed point. Abrash likes to use integer fixed point, but that is historical — modern fixed point can be handled with careful control of the fp unit in the mantissa. The major issue is regenerating the Z and the 1/Z values for the new vertices of the synthesized primitives. Everything else should flow down the pipe naturally, assuming a deferred attribute synthesis rasterizer.

      There are examples in the open source version of my rasterizer: OpenSWR.org.

    • ggambetta 18 minutes ago
  • t1234s 48 minutes ago
    I jumped when the first image loaded on the page.. didn't expect that.
    • hnlmorg 34 minutes ago
      What image was that? The first image I get is a headshot.

      Personally I’d have gone with a teapot as my demo but I don’t see anything shocking about the model the author chose.

    • mito88 23 minutes ago
      > I jumped when the first image loaded on the page.. didn't expect that.

      mbappe'?

  • chrisjj 19 minutes ago
    "Comments

    An error occurred: API rate limit already exceeded "