In case you missed the first entry: We’re here to talk about how OpenGL has changed and why that’s importantTo me, anyway.. The odd colorful screenshots are from one of my many half-baked OpenGL-based engine prototypes, presented here simply to break up the monotony of the words.
Before we can talk about where OpenGL went, we have to talk about where it started. So let’s talk about how rendering works on a fundamental level.
|It’s a good thing Pythagoras is dead, or he’d be insufferably smug right now.|
Our videogames are based on triangles. Everything is triangles. Even cube-based Minecraft is made by creating rectangles from pairs of triangles. Even text and icons on-screen are made by putting pictures of words and symbols onto triangle pairs.
I suppose there are a few other ways our graphics technology might have developed if history had played out just a little differently. We might have wound up with voxels, for example. But triangles was always a likely path for us to take.
Why triangles and not rectangles? Because triangles are mathematically more fundamental than rectangles. You can make rectanglesOr any other 2D polygon, for that matter. from triangles, but you can’t make triangles from rectangles. Computers hate ambiguity, and there’s a certain ambiguity to rendering with rectangles. Like your geometry teacher was busy telling you while you were drawing Power Rangers in your notebook, “3 points form a plane.” More informally, a 3-legged stool is inherently stable but a 4-legged stool might wobble. That wobble introduces a certain ambiguity. If you try to draw a rectangle and all 4 points aren’t on the same plane, that wobble needs to be resolved one way or another before it can begin drawing. And it turns out that the solution to that problem involves breaking the rectangle… into triangles.
The point is: It’s triangles all the way down.
3D rendering today consists of taking a bunch of 3D data and mushing it down to the 2D plane of your screen. From the Minecraft image above, you can see a cube is made from rectangles and rectangles are made from triangles. So while your brain uses its magical perspective detection to see the 3D world, to the computer it’s just a big pile of triangles sitting next to each other, with no more meaning than this lone triangle:
|For some reason nobody is interested in my design for a graphics card that’s based on rendering with tetradecagons.|
So whenever we draw polygons, we need to specify specify points in groups of threes. If you’re a hardcore trigonometry badassFor the purposes of this discussion, this is not an oxymoron. then I guess you can do it all yourself. Just draw nothing but 2D triangles. But over the years we’ve invented a bunch of techniques to do this for you, and the graphics hardware has been specially designed to do that sort of work really efficiently.
So you give OpenGL 3 points. Assuming those points wind up on the screenAnd not off to one side or behind the camera. then we get a triangle. Once the triangle is calculated, the graphics hardware fills in the space with pixels. This is called rasterization.
We don’t generally want to fill those pixels in with a solid color. I mean, you can, but you wind up with something like this:
|I don’t want to come off like some kind of graphics snob, but this probably isn’t good enough to ship.|
So while you’re defining the positions of your vertices, you can also give each one a color.
|Here is how to meet the public’s insatiable demand for red/green/blue triangles. You’re welcome.|
That’s nice. Gradient colors are much better than flat colors. It’s enough to give you something that looks like Race the Sun. But if you’re not going for a minimalist style like that then you probably want to use a texture map.
I imagine most people understand what a texture map is, even if they don’t get how it works. But for the sake of completeness: A texture map is when you take an image and use it to color your triangles like so:
|I wish I’d drawn this diagram a bit differently. Imagine the Mona Lisa not once, but as an infinite plane of that face repeating over and over like endless tiling wallpaper. Now picture putting the A, B, and C points anywhere you like on that plane. This will, of course, form a triangle. The image within that triangle will be mapped to the shape of the 3D triangle we’re drawing on screen. (Even if they’re wildly different proportions.)|
The math to do this is actually pretty straightforward, thanks to the use of triangles.
It was common to combine the vertex coloring with texture mapping. Make some corners of the triangle light and some dark, and it will fade between the two. Thus you get “lighting” on your textured walls.
Using nothing but these tools you could easilyBy “easily” I mean: Assuming you’ve got the budget, a skilled team, and you’re one of the few people at the time who understood all this stuff. And assuming you didn’t have anything else to do, I guess. make a AAA game in the 1990’s. I’m pretty sure this is everything you need to make Quake work. (Although Quake did shadows by storing the shadows as texture maps. So it would draw the whole world at full brightness, and then draw over the same polygons again with the shadows, thus making the appropriate areas dark. That’s why the shadows often looked kind of rough and jagged. The shadow textures were very low resolution.)
To be clear: The original Quake was actually a bit too early to ship with OpenGL support, and it wasn’t hardware accelerated at first. It’s complicated. But ignoring the strange way OpenGL was added to the game later, it serves as a really interesting snapshot of the technology of the day.
|The big blobby shadows in that corner are probably a couple of black pixels stretched over a couple of meters worth of wall. This image was taken using the much newer GL Quake. Modern graphics cards work pretty hard to smooth these edges out as much as possible. I remember the effect being a lot more obvious and ugly back in the day.|
And that’s it. That’s 90% of everything you need to know about how “classic” OpenGL worked.
The code to do this was pretty simple:
1 2 3 4 5 6 7 8 9 10 11
// This will make a triangle shaped like so: // 3---2 // | / // | / // |/ // 1 glBegin (GL_TRIANGLES); glVertex3f (0.0, 0.0, 0.0); glVertex3f (1.0, 1.0, 0.0); glVertex3f (0.0, 1.0, 0.0); glEnd ();
That code makes three vertices. Which makes one triangle. All I did was define three positions. Let’s add some color data:
1 2 3 4 5 6 7
glBegin (GL_TRIANGLES); glColor3f (1, 0, 0); //red glVertex3f (0.0, 0.0, 0.0); glColor3f (0, 1, 0); //green glVertex3f (1.0, 1.0, 0.0); glVertex3f (0.0, 1.0, 0.0); glEnd ();
In line two I tell OpenGL that I’m setting the color to red. Then in line 3 I give it a vertex. When it’s finally rendered, that vertex will be red. In line 4 I change the color to green. You’ll notice I didn’t set a color for the third vertex. When you set a color, it applies to every vertex from that point on, until you change it to something else.
This probably looks like a very “raw” way of making graphics. Obviously we wouldn’t want to try and construct Lara Croft’s face like this, by manually defining thousands and thousands of triangle positions in code.
Next time we’ll talk about how we got from this raw triangle access in the 90’s to the way we do things now.
 To me, anyway.
 Or any other 2D polygon, for that matter.
 For the purposes of this discussion, this is not an oxymoron.
 And not off to one side or behind the camera.
 By “easily” I mean: Assuming you’ve got the budget, a skilled team, and you’re one of the few people at the time who understood all this stuff. And assuming you didn’t have anything else to do, I guess.
The Middle Ages
Would you have survived in the middle ages?
The true story of three strange days in 1989, when the last months of my adolescence ran out and the first few sparks of adulthood appeared.
Silent Hill 2 Plot Analysis
A long-form analysis on one of the greatest horror games ever made.
The Strange Evolution of OpenGL
Sometimes software is engineered. Sometimes it grows organically. And sometimes it's thrown together seemingly at random over two decades.
The product of fandom run unchecked, this novel began as a short story and grew into something of a cult hit.