Graphics & Visualization

FdF: 3D Wireframe Renderer in C with MiniLibX

An interactive 3D wireframe renderer in C that turns height maps into isometric landscapes, with rotation, zoom, panning, and color gradients.

Last updated:

FdF: 3D Wireframe Renderer in C with MiniLibX

Introduction

FdF, short for fil de fer (French for wireframe), is a 42 graphics project that reads a height map from a .fdf file and draws it as a 3D wireframe landscape. Each number in the file is the altitude of a point, optionally followed by a hex color, and neighboring points are joined by lines.

The renderer uses MiniLibX, a minimal graphics library that provides only a window, an image buffer, and keyboard and mouse events. Everything else, from projection to line drawing and color interpolation, is written by hand.

The problem

Turning a grid of numbers into a readable 3D picture means projecting each point from 3D to 2D, drawing lines pixel by pixel, and fitting maps of very different sizes on screen, all without a graphics API to help. Maps range from a handful of points to large terrains, and the image has to redraw quickly while the user interacts with it.

The program also has to reject malformed maps safely and free every resource when the window closes.

Architecture

The program loads the map with get_next_line, checks that it is well formed, and stores each point's altitude and color. A scale is computed so the map fits the window, and every point then goes through the same transformation: altitude scaling, isometric or orthographic projection, rotation, zoom, and translation.

Lines between neighboring points are drawn with Bresenham's algorithm into an off-screen image buffer, coloring each pixel by interpolating between the colors of the two endpoints. Once the image is complete it is pushed to the window in one call. Keyboard and mouse hooks update the view parameters and trigger a redraw.

Implementation decisions

Draw into an image, not the window
Pixels are written directly into an image buffer that is displayed once per frame, instead of drawing each pixel with mlx_pixel_put. That is much faster and avoids flicker while the user interacts.
Bresenham's line algorithm
Lines are drawn with integer arithmetic only, so there are no rounding gaps and every line is continuous whatever its slope.
Automatic fit
The initial zoom and altitude scale are computed from the map's dimensions, so a small test map and a large terrain both appear centered and fully visible without manual tuning.
Color by interpolation
Each pixel's color blends the RGB components of the two endpoints according to its position on the line, producing smooth gradients across the whole wireframe.

Technical challenges

  • Implementing rotation and projection with correct trigonometry while keeping the map centered
  • Redrawing large maps fast enough to feel interactive
  • Parsing maps reliably, including optional hex colors and rows of inconsistent width
  • Releasing the image, the window, and all map memory on exit, whether through Escape or the window's close button

Results

  • Renders any valid .fdf map as an isometric or orthographic wireframe
  • Supports panning, zooming with the mouse wheel, rotation, altitude scaling, and a reset key
  • Smooth color gradients along every line
  • An on-screen control menu in the bonus version

Related case studies