Nimbin[12]?SDL & Graphics / sdl_runtime_compiler / src/rendering/gl_renderer.hpp

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/gl_renderer.hpp 5.7 KB · 192 lines raw
#pragma once

#include <SDL3/SDL.h>
#include <glad/glad.h>

#include "entities/camera.hpp"
#include "utils/globals.hpp"
#include "utils/light.hpp"

namespace
Nimbin
{

struct Mat4
{
    float m[16]{};
    static Mat4 identity();
    static Mat4 perspective(float fovY_rad, float aspect, float near, float far);
    static Mat4 mul(const Mat4& a, const Mat4& b);
};

Mat4 buildViewMat4 (const Camera& cam);
Mat4 buildModelMat4(const Vec3& pos, float yaw);
Mat4 buildModelMat4(const Vec3& pos, float yaw, float pitch);


class GlRenderer
{
    GLuint        _prog{};

    GLint _uMVP       {-1},
          _uNormalMat {-1},
          _uColor     {-1};

    GLint _uBodyColor {-1},
          _uBackDim   {-1},
          _uUseFront  {-1};

    GLint _uLightDir  {-1},
          _uAmbient   {-1},
          _uIntensity {-1},
          _uSunColor  {-1};

    GLint _uFlashPos       {-1},
          _uFlashDir       {-1},
          _uFlashColor     {-1},
          _uFlashRange     {-1},
          _uFlashInnerCos  {-1},
          _uFlashOuterCos  {-1},
          _uFlashIntensity {-1};

    GLint _uModel          {-1};
    GLint _uWireframe      {-1};
    GLint _uMandelbrot     {-1};
    GLint _uWallPaper      {-1};
    GLint _uTextLineSpacing{-1};
    GLint _uUseVertexColor {-1};

    GLuint _emissive_prog  {},
           _emissive_vao   {},
           _emissive_vbo   {},
           _emissive_ebo   {};

    GLint  _uEmissiveMVP   {-1};

    GLuint _vao{},   _vbo{},   _ebo{};
    GLuint _vao_c{}, _vbo_c{}, _ebo_c{};   // VAO/VBO/EBO for colored mesh

    GLuint _line_vao{}, _line_vbo{};
    GLuint _menu_vao{}, _menu_vbo{};


    GLuint _scene_fbo       {0},
           _scene_color_tex {0},
           _scene_depth_rbo {0},
           _scene_fbo_ms       {0},
           _scene_color_rbo_ms {0},
           _scene_depth_rbo_ms {0};

    GLuint _fullscreen_vao  {0},
           _fullscreen_vbo  {0};

    GLuint _blit_prog       {0};
    GLint  _uBlitTex        {-1};

    GLuint _bright_fbo      {0},
           _bright_tex      {0};
    GLuint _blur_fbo[2]     {0, 0},
           _blur_tex[2]     {0, 0};

    GLuint _crosshair_prog {0},
           _crosshair_vao  {0},
           _crosshair_vbo  {0};

    GLuint _extract_prog    {0},
           _blur_prog       {0},
           _composite_prog  {0};

    GLint  _uExtractTex     {-1};
    GLint  _uBlurTex        {-1};
    GLint  _uBlurDir        {-1};
    GLint  _uBlurTexelSize  {-1};
    GLint  _uComposeScene   {-1},
           _uComposeBloom   {-1},
           _uComposeStrength{-1};

    GLint  _uEmissiveColor    {-1};
    GLint  _uEmissiveStrength {-1};

    float _bloom_strength { 0.6f };
    float _aspect         {1.0f};
    int   _view_w         {1};
    int   _view_h         {1};
    int   _fbo_w          {0};
    int   _fbo_h          {0};
    int   _msaa_samples   {4};
    int   _blur_w         {0};
    int   _blur_h         {0};
    bool  _crosshair_enabled {true};

    Mat4  _proj;
    SDL_GLContext _ctx{};

    void create_FBO_             (int w, int h);
    void resize_FBO_             (int w, int h);
    void destroy_FBO_            ();
    void create_FullscreenQuad_  ();
    void create_BlitProgram_     ();
    void create_BloomFBOs_       (int scene_w, int scene_h);
    void resize_BloomFBOs_       (int scene_w, int scene_h);
    void destroy_BloomFBOs_      ();
    void create_ExtractProgram_  ();
    void create_BlurProgram_     ();
    void create_CompositeProgram_();
    void run_BloomPipeline_      ();

    void draw_Crosshair_();

    GLuint compile_Shader(GLenum type, const char* src);
    bool   link_Program  (GLuint vert, GLuint frag);
public:
    static void free_MeshBuffers(unsigned int vbo, unsigned int ebo);


    void pre_Config     ();
    bool init           (SDL_Window* window);
    void begin_Frame    (int viewport_w, int viewport_h);
    void end_Frame      (SDL_Window* window);
    void clear_Depth    ();

    void set_MVP        (const Mat4& model, const Mat4& mvp);
    void set_NormalMat  (const Mat4& model);
    void set_Color      (float  r, float  g, float  b);
    void set_FaceShade  (float r, float g, float b,
                        float back_dim, bool use_front);
    void set_Light      (const Light& light);
    void set_Wireframe  (bool on);
    void set_Emissive   (float r, float g, float b, float strength);
    void set_Crosshair  (bool on) { _crosshair_enabled = on; }
    void set_DepthTest  (bool on);
    void set_CullBack   (bool on);
    void set_ColorUseVertex (bool on);
    void set_TextLineSpacing(float m);
    void set_BloomStrength  (float s) { _bloom_strength = s; }

    void enable_MandelbrotMode(bool on);
    void enable_WallPaperMode (bool on);

    float       get_Fov_y     () const { return SDL_PI_F / 3.0f; }
    float       get_Aspect    () const;
    const Mat4& get_Projection() const;
    GLuint      get_Program   () const { return _prog; }

    void draw_Mesh       (const float* verts,   int vert_count,
                          const int*   indices, int index_count);
    void draw_MeshColored(const float* verts,   int vert_count,
                          const int*   indices, int index_count);
    void draw_MeshCached (unsigned int& vbo, unsigned int& ebo, int& index_count,
                          bool dirty,
                          const float* verts, int vert_count,
                          const int* indices, int idx_count);

    void draw_Lines      (const float* points, int point_count);
    void draw_Box        (float hx, float hy, float hz);
    void draw_Menu       ();
    void draw_Emissive   (const Camera& cam,
                          const float* verts,   int vert_count,
                          const int*   indices, int index_count,
                          float r, float g, float b, float strength);
};

} // namespace Nimbin