Skip to content

Mandelbulb: free camera with mouse look and WASD in the camera mode - #37

Merged
resetius merged 1 commit into
masterfrom
mandelbulb-fly-camera
Nov 14, 2026
Merged

resetius merged 1 commit into
masterfrom
mandelbulb-fly-camera

Conversation

@resetius

@resetius resetius commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

The camera mode of mandelbulb (key C) was an orbit camera: the arrows orbited the fractal and A/Z moved the camera closer or farther. This PR turns it into a free, shooter-like camera that can fly around and into the fractal.

Controls in the camera mode:

Input Action
Mouse, arrows turn the view (pitch is limited to about ±89°)
W / S forward / back
A / D left / right
Space / Left Shift up / down
C back to the rotating orthographic mode
Tab next formula, as before

The mouse turns the view while the cursor is over the window; the existing --input:grab_mouse=on option captures the cursor for unlimited turning. Outside the camera mode nothing changes: the arrows pan and A/Z zoom.

What changed:

  • models/mandelbulb.c: free camera state (position, yaw, pitch, held keys). Movement runs every frame from the key mask of the input events. The speed is the distance to the surface per second, taken from distance_estimate, a CPU copy of the shader DE(). The camera slows down near the surface, and a step is never longer than the distance to the surface, so it does not jump through it. A floor of 1e-3 on the distance keeps the camera movable if it still ends up inside the set, where DE is 0. The distance from the center is limited to 8 because the rays march at most 10 units.
  • In the camera mode A strafes, so A/Z no longer zoom there; Z does nothing in this mode.
  • The camera position reaches the shader as the translation of Rot; the Dist uniform is removed from the uniform block (C struct, vertex and fragment shaders).
  • models/mandelbulb.frag: the camera origin is read from Rot, and a comment asks to keep DE() and its CPU copy in sync.

Behavior change: only the camera mode, as described above.

Verification (macOS, AppleClang, CMake + Ninja):

  • Build: no warnings in the changed files.
  • ctest: 3/3 passed.
  • Camera logic, with a throwaway harness (not committed) that includes mandelbulb.c and calls the handlers directly:
    • the CPU DE copy matches a separate port of the shader on 80,000 random points of all 4 formulas (max relative difference 1.9e-6);
    • 10 s of W from the start position: no step is longer than DE·dt, the camera never gets inside the set, and DE drops from 2.19 to 0.023;
    • WASD, Space and Shift move in the expected directions; after turning, W follows the view;
    • the mouse turns the view the expected way and pitch is clamped;
    • the arrows turn only in the camera mode and A/Z zoom only outside it;
    • the distance stays within 8, and C toggles the mode.
  • Vulkan with VK_LAYER_KHRONOS_validation (load confirmed with VK_LOADER_DEBUG): 0 messages in the normal and in the camera mode. OpenGL runs without errors in both modes.
  • Speed: in the camera mode at the start position, OpenGL gives about 28 FPS (960×540 window, 1920×1080 framebuffer, 2×2 supersampling, vsync off), the same as the old orbit camera.
  • The harness did not drive real keyboard and mouse input in a window. In one OpenGL run the camera position and angles changed while the window was open, which suggests the input path works, but that was not a controlled test.

@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Test Results

9 tests  ±0   9 ✅ ±0   0s ⏱️ ±0s
3 suites ±0   0 💤 ±0 
3 files   ±0   0 ❌ ±0 

Results for commit f3bc8d3. ± Comparison against base commit d488599.

♻️ This comment has been updated with latest results.

The camera mode (key C) was an orbit camera moved by the arrows and A/Z.
It is now a free camera: the mouse and the arrows turn the view, WASD
moves, Space/Left Shift move up and down. The speed is the distance to
the surface per second, computed by a CPU copy of the shader DE, so the
camera slows down near the surface and a step never jumps over it.

The camera position reaches the shader as the translation of Rot, the
Dist uniform is removed. The orthographic mode is unchanged.
@resetius
resetius force-pushed the mandelbulb-fly-camera branch from 54ba3a7 to f3bc8d3 Compare November 14, 2026 16:24
@resetius
resetius merged commit 7278352 into master Nov 14, 2026
2 checks passed
@resetius
resetius deleted the mandelbulb-fly-camera branch November 14, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant