This project was made as an introduction to computer graphics, together with Mitch van den Broek. We wanted to build a small graphics engine with OpenGLA standard way for a program to talk to the graphics card. It gives the commands to upload shapes and textures and to draw them with shaders. ourselves, to create one scene with it: a small rollercoaster in a grassy field. A cart rides on rails along a track of curves, through a day that turns into night. When the sun goes down, lamp posts light up along the track, and a red button at the station sends the cart onto a second loop. We used libraries only to load files. Everything that ends up on screen is our own OpenGL code.
We wrote it in C++20, with the shaders in GLSLThe OpenGL Shading Language. Shaders are small programs in this language that run on the graphics card, for example to compute the color of every pixel. and GLFWA small library that opens a window, creates the OpenGL context to draw in, and reads the keyboard and mouse. for the window and input, and our own small classes around the OpenGL objects (shaders, textures, framebuffers, VAOVertex Array Object. It remembers which buffers belong to a shape and how the graphics card must read them., VBOVertex Buffer Object. It holds the data of every corner point of a shape on the graphics card, such as the position, normal and texture coordinates. and EBOElement Buffer Object. It holds the order in which the corner points form triangles, so a point that several triangles share is stored only once.). The code is split in two parts: a small engine library with the window, the cameras, the meshes, the scene and the render passes, and a rollercoaster demo that is built on top of it.
The track is two loops that share a station. Each loop is a list of waypoints, and between every two waypoints the code fits a cubic BΓ©zier curveA smooth curve defined by a few control points. A cubic one has four: it starts at the first, ends at the last, and the two in the middle pull the curve toward them.. The control points of each curve come from its neighbours, so the track flows from one curve into the next without a kink. We sample the curves with forward differencingA way to step along a curve by adding a few precomputed differences, instead of calculating the full formula again for every point., which only needs a few additions per point. From those samples, each loop builds an arc-lengthThe real distance along a curve. A table from distance to curve parameter lets an object move at a constant speed. table, so the cart moves at the same speed on the straight parts and in the curves. The same table places the rail objects.


A cart rides on the rails and turns with the curves. At the station stands a control post with a red button. Click the button, and it starts to pulse. At the end of the lap, the cart switches to the other loop.
That button works with color pickingFinding out which object is under the mouse by drawing each object in its own color off-screen and reading back the pixel.. On a click, the clickable objects are drawn a second time to a hidden framebuffer, each in its own flat color. We then read the color under the cursor and know exactly what you hit.
The scene has two cameras. A free camera flies around with the keyboard and mouse to look at the whole track, and a second camera rides in the cart to follow the ride in first person. The arrow keys change the speed, and when the speed drops below zero, the ride goes in reverse.
The engine can also load 3D models from glTFAn open file format for 3D models, often called 'the JPEG of 3D'. files. I wrote the loader on top of a JSON parser: it reads the accessors and buffer views by hand and turns them into vertex, normal, UV and index arrays for our own mesh class.

The scene goes through a full day in two minutes. The sun moves across the sky and sets in orange, and then the moon and the stars come out. The sky colors, the fog and the ambient light all follow the time of day.
Lighting is done per pixel in the fragment shaders, with Blinn-PhongA common lighting model. It adds the shiny highlight you see where a light reflects off a surface toward the camera. highlights. Each surface gets ambient light from the sky and the ground, light from the sun or the moon, and fog in the color of the sky that hides the far edge of the world. When it gets dark, the lamp posts along the track turn on. Each lamp is a point light that fades out with distance, and it flickers a little like a flame.

The finished frame never goes straight to the screen. It is first rendered in HDRHigh dynamic range. Colors can be brighter than white, so a lamp can be many times brighter than the grass next to it. to a framebuffer object with two outputs: the full scene, and only the parts that glow. Then a few passes draw it again on a quad that fills the screen:
- Bloom: the glowing parts get blurred back and forth between two buffers at half resolution, and are added back on top so the lamps, the button and the sun glow.
- Tone mapping: a filmic curve brings the HDR colors back to what a screen can show, with a higher exposure at night.
- Gaussian blur and a Laplacian edge filter, both with a convolution kernelA small grid of numbers. Each pixel becomes a weighted mix of its neighbours, which can blur the image or bring out edges., on keys 2 and 3.
- Chroma keying: a decorated frame with a green centre is drawn over the scene, and the shader removes the green by comparing colors in the YCbCrA way to store color as brightness plus two color channels. This makes it easier to pick out 'green' regardless of how light or dark it is. color space.


Aerostats