Archive · R&D

Impact, a small 3D engine

Built to learn what goes into a game engine, using DirectX 11 without the Effects system, with the goal of recreating the Geoscape from X-COM (1994).

Tech
C++17, DirectX 11, HLSL, Win32
Tools
Dear ImGui, GDI+
Goal
X-COM's Geoscape

Approach

I learned from Microsoft's Win32 documentation and from how others had solved the same problems. I also studied hw3d, a render engine by PlanetChili, took inspiration from what I liked, and came up with my own solutions for what I didn't.

The engine is a static library with its own entry point. A game only subclasses Application and pushes its layers, so everything in the demo lives in a separate SandBox project rather than in the engine.

Layers instead of hard-coded order

One thing I didn't like was the hard-coded order of objects in the application's render function. My solution was a layered structure, inspired by The Cherno's Hazel engine, that keeps each concern in its own domain, with rendered objects handled elsewhere.

An event handler passes events through the layers like a sift: you register an event and what triggers it, and when the trigger happens the event gets sent through. Rendering and updating run in the reverse order of event handling: Game, UI, Debug, Application.

  • Event types are bit flags. Each of the 16 types is one bit, so a single callback can register for several with a bitwise OR.
  • Unwanted events exit early. The handler keeps a mask of every type anything has registered for, and drops an event straight away if nobody wants it.
  • Handling stops the sift. Callbacks are stored per layer, in layer order, and the first one to report an event as handled ends its trip.

Wrapping the Earth around an icosphere

The Geoscape needs a globe, so the main object in the demo is an Earth, built as an icosphere from Andreas Kahler's method. Its radius of 63.711 units puts it at Earth's real size, at one unit per 100 km. The subdivision level can be changed at runtime from level 0 to 6, where it reaches 81,920 triangles.

An icosphere has no natural place for an equirectangular texture, so UVs are calculated from each vertex's position on the unit sphere, with help from an article on mft-dev.dk:

u = atan2(z, x) / 2π    v = acos(y) / π

That leaves two problems, both solved when the mesh is built:

  • The seam. Triangles crossing the edge of the texture would smear the whole map across themselves. Those triangles have reversed winding in UV space, so a cross product finds them, and their low-u vertices are duplicated with 1 added to u.
  • The poles. A single vertex at a pole can't have one correct u for every triangle touching it. Each of those triangles gets its own copy of the pole, with u halfway between its other two vertices.

Rendering and lighting

  • Bindables. Buffers, shaders, textures and other pipeline state share one Bindable interface, an idea I took from hw3d. A templated base class keeps one shared set per object type, so instances only carry what's unique to them.
  • Lights. The pixel shader loops over up to 100 point and directional lights from a single constant buffer, with no, linear or quadratic falloff per light. In the demo, a point light follows the moon as it moves.
  • Textures. Images load through GDI+ and go straight to the GPU, including the 8K Earth and moon maps.

Tools for working in it

  • Live tuning. Dear ImGui panels edit local and world transforms, rotation speeds, cameras, subdivision level and light settings while it runs.
  • Debug controls. Keys toggle wireframe, V-sync, pause and the ImGui layer, and the demo can switch between free-look cameras.
  • Readable failures. Failed DirectX and Win32 calls become exceptions carrying the file, line and error description, shown in a message box instead of a silent crash. Debug builds also enable memory leak checks.

Next steps

Objects are built from a very simple component system. The engine renders primitive objects with diffuse textures, point and directional lighting, and a dynamic camera. Asset importing, more texture maps and terrain displacement were next on the list.