Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/rendering/floor_surface.cpp

sdl_runtime_compiler git · main

SDL3 game for running and compiling code at runtime

sdl3 c++ compiler dlopen cmake · first commit 2026-04-19 · last commit 2026-07-03 (3 months ago) · synced 3 days ago · upstream: git.ide3.de/hsnr/sdl-runtime-compiler

C++ 72.3% C 26.2%
git clone https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler.gitwget https://git.christianimmanuel.de/sdl-graphics/sdl_runtime_compiler/archive/sdl_runtime_compiler.tar.gz
src/rendering/floor_surface.cpp 2.6 KB · 90 lines raw
#include <SDL3/SDL.h>

#include "entities/camera.hpp"
#include "floor_surface.hpp"

namespace
Nimbin
{

namespace Floor
{

void
Mandelbrot::generate(float  world_scale,
                     float  floor_y,
                     double center_x,
                     double center_y,
                     double width)
{
    _verts.clear();
    _indices.clear();

    // Single quad covering the floor. We use the existing 9-floats-per-vertex
    // layout: [pos(3) | normal(3) | color(3)], but here we *abuse* the color
    // slot to carry the complex-plane coordinate (cx, cy, 0). The shader
    // reads it from vColor when in Mandelbrot mode.
    const float  ws = world_scale;
    const double half_w = width * 0.5;

    // Four corners of the floor in world space.
    // Match the layout so corners go around: BL, BR, TR, TL (looking down +Y).
    struct V { float x,y,z; double cx,cy; };
    V corners[4] = {
        { -ws, floor_y, -ws, center_x - half_w, center_y - half_w }, // BL
        {  ws, floor_y, -ws, center_x + half_w, center_y - half_w }, // BR
        {  ws, floor_y,  ws, center_x + half_w, center_y + half_w }, // TR
        { -ws, floor_y,  ws, center_x - half_w, center_y + half_w }, // TL
    };

    for (const V& v : corners) {
        _verts.push_back(v.x);
        _verts.push_back(v.y);
        _verts.push_back(v.z);
        // Normal — flat up.
        _verts.push_back(0.0f);
        _verts.push_back(1.0f);
        _verts.push_back(0.0f);
        // "Color" slot carries the complex-plane coord.
        _verts.push_back(static_cast<float>(v.cx));
        _verts.push_back(static_cast<float>(v.cy));
        _verts.push_back(0.0f);
    }

    // Two triangles: BL,BR,TR  +  BL,TR,TL
    _indices.push_back(0);
    _indices.push_back(1);
    _indices.push_back(2);
    _indices.push_back(0);
    _indices.push_back(2);
    _indices.push_back(3);
}


void
Mandelbrot::draw(GlRenderer& gl, const Camera& cam, const Light& light) const
{
    if (_verts.empty()) return;

    Mat4 view  = buildViewMat4    (cam);
    Mat4 proj  = gl.get_Projection();
    Mat4 model = Mat4::identity   ();
    Mat4 mvp   = Mat4::mul        (proj, Mat4::mul(view, model));

    gl.set_Wireframe(false);
    gl.set_MVP      (model, mvp);
    gl.set_NormalMat(model);
    gl.set_Light    (light);

    gl.enable_MandelbrotMode(true);
    gl.set_ColorUseVertex   (false);
    gl.draw_MeshColored     (_verts.data(),
                             static_cast<int>(_verts.size() / 9),
                             _indices.data(),
                             static_cast<int>(_indices.size()));
    gl.enable_MandelbrotMode(false);
}

} // namespace Floor

} // namespace Nimbin