
How OpenGL works: software rendering in 500 lines of code - WoodenChair
https://github.com/ssloy/tinyrenderer/wiki
======
taspeotis
Previous discussion [1]

    
    
        How OpenGL works: software renderer in 500 lines of code (github.com)
        363 points by gregorymichael 200 days ago
    

[1]
[https://news.ycombinator.com/item?id=11264469](https://news.ycombinator.com/item?id=11264469)

------
planteen
I can't wait to work through this. I agree so much with this philosophy: "I do
not want to show how to write applications for OpenGL. I want to show how
OpenGL works. I am deeply convinced that it is impossible to write efficient
applications using 3D libraries without understanding this."

------
Kenji
This is very impressive, I love it when code has minimal (or no) dependencies,
you really see what's going on under the hood. Could have used some more
comments in the code itself though (there are basically none but the code is
clean). I first thought the code would render into a window using OpenGL. But
this is a _plain software_ renderer with no hardware acceleration/OpenGL at
all and it simply stores the rendered image into a picture file. There is no
window or stuff like that.

EDIT: The lessons in the wiki explain things so well that comments in the code
really aren't necessary at all.

------
Jasper_
Heh, I did something similar, but in JS, rather than C++, if you want a second
opinion of "how it works":

[http://magcius.github.io/sw3dv/sw_rast.html](http://magcius.github.io/sw3dv/sw_rast.html)

[https://github.com/magcius/sw3dv/blob/gh-
pages/sw_rast.js](https://github.com/magcius/sw3dv/blob/gh-pages/sw_rast.js)

The math and geometry behind 3D projection and 3D rendering is fairly basic
once it clicks.

------
1_player
There's one piece of information about the OpenGL pipeline I never quite
understood:

Years ago when I was playing writing my own operating system, I wanted to add
accelerated video drivers, and obviously OpenGL. I now know it's out of the
reach of hobby OSes, but I could never figure out what's the relationship with
OpenGL and the actual hardware.

Is OpenGL just an API, with the actual API -> hardware command translation
done by the video driver? Or does the hardware understand some part of the
OpenGL spec? Rephrased, does the OpenGL spec mandate some ABI to talk with the
video card?

I seem to think the former's the right answer, as I never managed to find any
of the actual OpenGL -> HW command translation source code anywhere. At the
time Intel wasn't a player in the video card space, so nVidia and ATI were the
only option, but alas, closed source.

~~~
stuaxo
The former is the right answer. Now, there are open source options, e.g.
Nouveau for Nvidia originally for Linux, but has been ported to FreeBSD.

Gallium should make it easier to do what you wanted as well - providing the
glue between a video driver and APIs such as OpenGL / DirectX ( - there is a
gallium implementation of DirectX 9 for Wine).

------
starmole
I love the approach for teaching! I also just recently wrote a reference GL-SW
renderer. It's not much code.. 400 loc? But it really drives home the (simple)
math. There are some things that are hard to do in SW - derivatives. Also
writing SW today is very different than back in the day! Just Lerp everything,
no DDA, no "divide only every 8th pixel".

------
Animats
Cute. Most of the work is done in function "triangle" in [1]. 33 lines of code
which do, somewhat slowly, what GPUs do rapidly.

[1]
[https://github.com/ssloy/tinyrenderer/blob/master/our_gl.cpp](https://github.com/ssloy/tinyrenderer/blob/master/our_gl.cpp)

------
crigue
I did that in class with ssloy. It's really not that difficult, even if you're
no maths buff :)

------
rav
The link should be changed to
[https://github.com/ssloy/tinyrenderer/wiki](https://github.com/ssloy/tinyrenderer/wiki)
since that seems to be the primary entry point for the repo/tutorial.

