diff options
Diffstat (limited to 'src/renderer')
| -rw-r--r-- | src/renderer/RenderGroup.cpp | 22 | ||||
| -rw-r--r-- | src/renderer/RenderGroup.hpp | 31 | ||||
| -rw-r--r-- | src/renderer/Renderer.cpp | 233 | ||||
| -rw-r--r-- | src/renderer/Renderer.hpp | 37 | ||||
| -rw-r--r-- | src/renderer/opengl/GlIndexBuffer.cpp | 44 | ||||
| -rw-r--r-- | src/renderer/opengl/GlIndexBuffer.hpp | 23 | ||||
| -rw-r--r-- | src/renderer/opengl/GlRenderer.cpp | 220 | ||||
| -rw-r--r-- | src/renderer/opengl/GlRenderer.hpp | 34 | ||||
| -rw-r--r-- | src/renderer/opengl/GlShader.cpp | 80 | ||||
| -rw-r--r-- | src/renderer/opengl/GlShader.hpp | 15 | ||||
| -rw-r--r-- | src/renderer/opengl/GlVertexBuffer.cpp | 54 | ||||
| -rw-r--r-- | src/renderer/opengl/GlVertexBuffer.hpp | 27 | 
12 files changed, 283 insertions, 537 deletions
diff --git a/src/renderer/RenderGroup.cpp b/src/renderer/RenderGroup.cpp index 56e7ec5..e393393 100644 --- a/src/renderer/RenderGroup.cpp +++ b/src/renderer/RenderGroup.cpp @@ -71,17 +71,29 @@ ImVec2 RenderGroup::ViewSizeToScreenSizeImGui(V2F32 view_size) {      return result;  } -void RenderGroup::Clear(V3F32 color) { +void RenderGroup::Clear(Color color) {      m_ClearColor = color;  } -void RenderGroup::PushRectangle(V3F32 pos, V2F32 dim, V3F32 color) { -    m_REntities.emplace_back(REntity{.rect{REntityType_Rectangle, pos, dim, color}}); -    m_RSortEntries.emplace_back(pos.z, m_REntities.size()-1); +void RenderGroup::PushRectangle(RectF32 rect, float z, Color color) { +    m_REntities.emplace_back(REntity{.rect{ +            REntityType_Rectangle, +            rect.x0, rect.y0, +            rect.x1, rect.y1, +            z, +            color} +    }); +    m_RSortEntries.emplace_back(z, m_REntities.size()-1);  }  void RenderGroup::PushBitmap(V3F32 pos, int w, int h, void *data) { -    m_REntities.emplace_back(REntity{.bitmap{REntityType_Bitmap, pos, w, h, data}}); +    m_REntities.emplace_back(REntity{.bitmap{ +            REntityType_Bitmap, +            pos.x, pos.y, +            w, h, +            pos.z, +            data +    }});      m_RSortEntries.emplace_back(pos.z, m_REntities.size()-1);  } diff --git a/src/renderer/RenderGroup.hpp b/src/renderer/RenderGroup.hpp index 4904220..ddaa9ff 100644 --- a/src/renderer/RenderGroup.hpp +++ b/src/renderer/RenderGroup.hpp @@ -12,16 +12,21 @@ enum REntityType : int32_t {  struct REntity_Rectangle {      REntityType type; -    V3F32 pos; -    V2F32 dim; -    V3F32 color; +    float x0; +    float y0; +    float x1; +    float y1; +    float z; +    Color color;  };  struct REntity_Bitmap {      REntityType type; -    V3F32 pos; -    int32_t width; -    int32_t height; +    float x; +    float y; +    int32_t w; +    int32_t h; +    float z;      void *data;  }; @@ -41,7 +46,7 @@ struct RSortEntry {  class RenderGroup {  public:      RenderGroup(); -    void Clear(V3F32 color); +    void Clear(Color color);      void Reset();      void SetCameraSize(float width, float height); @@ -53,11 +58,8 @@ public:  public: -    void PushRectangle(V3F32 pos, V2F32 dim, V3F32 color); -    void PushBitmap(V3F32 pos, int width, int height, void *bitmap); - - -private: +    void PushRectangle(RectF32 rect, float z, Color color); +    void PushBitmap(V3F32 pos, int w, int h, void *bitmap);      void Sort(); @@ -67,14 +69,13 @@ public:  private: -    friend class GlRenderer; - +    friend class Renderer;      float m_CameraWidth;      float m_CameraHeight; -    V3F32 m_ClearColor; +    Color m_ClearColor;      std::vector<REntity> m_REntities;      std::vector<RSortEntry> m_RSortEntries; diff --git a/src/renderer/Renderer.cpp b/src/renderer/Renderer.cpp index 13f5feb..9217f5c 100644 --- a/src/renderer/Renderer.cpp +++ b/src/renderer/Renderer.cpp @@ -1,18 +1,233 @@  #include <renderer/Renderer.hpp> -#include <renderer/opengl/GlRenderer.hpp> +#include <GL/glew.h> +#include <cstdio> +#include <cstdlib> -std::unique_ptr<Renderer> -Renderer::Select(Api api, SDL_Window *window) { -    switch (api) { -        case API_OPENGL: { -            return std::make_unique<GlRenderer>(window); + +Renderer::Renderer(SDL_Window *window) : m_Window(window), m_Canvas{} +{ +} + + +bool +Renderer::Init() +{ +    GLenum err = glewInit(); +    if (err) { +        printf("glewInit() error\n"); +        return false; +    } + +    int32_t window_width; +    int32_t window_height; +    if (!SDL_GetWindowSize(m_Window, &window_width, &window_height)) { +        printf("Error: SDL_GetWindowSize: %s\n", SDL_GetError()); +        return false; +    } + + +    m_Canvas.rshift = 0; +    m_Canvas.gshift = 8; +    m_Canvas.bshift = 16; +    m_Canvas.ashift = 24; +    ResizeCanvas(window_width, window_height); + + +    glEnable(GL_TEXTURE_2D); +    glActiveTexture(GL_TEXTURE0); + +    glGenTextures(1, &m_GlTexId); +    glBindTexture(GL_TEXTURE_2D, m_GlTexId); + +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	 +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); +    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + +    return true; +} + + +void +Renderer::ResizeCanvas(int32_t w, int32_t h) +{ +    size_t realloc_size = (size_t)(w * h) * sizeof(m_Canvas.pixels[0]); +    void *realloc_data = realloc(m_Canvas.pixels, realloc_size); +    if (!realloc_data) { +        printf("could not resize offscreen buffer\n"); +        return; +    } + +    m_Canvas.w = w; +    m_Canvas.h = h; +    m_Canvas.pixels = (uint32_t*)realloc_data; +} + + +void +Renderer::Draw(RenderGroup& rgroup) +{ +    rgroup.Sort(); +    REntity_Rectangle clear_rect = { +        REntityType_Rectangle, +        0, 0, +        (float)(m_Canvas.w-1), (float)(m_Canvas.h-1), +        0.0f, +        rgroup.m_ClearColor +    }; +    DrawRectangle(rgroup, clear_rect); + + +    float z = -1; +    for (RSortEntry sort_entry : rgroup.m_RSortEntries) { +        if (sort_entry.z >= z) { +            z = sort_entry.z; +        } + +        REntity& entity = rgroup.m_REntities[sort_entry.entity_index]; +        switch (entity.type) { +            case REntityType_Rectangle: { +                DrawRectangle(rgroup, entity.rect); +            } break; + +            case REntityType_Bitmap: { +                Color color = {0.0f, 0.0f, 0.0f, 1.0f}; +                DrawMonoBitmap(entity.bitmap, color); +            } break; + +            default:;          } -        InvalidDefaultCase;      } -    return nullptr; + +    glActiveTexture(GL_TEXTURE0); +    glBindTexture(GL_TEXTURE_2D, m_GlTexId); +    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_Canvas.w, m_Canvas.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_Canvas.pixels); + +    glBegin(GL_QUADS); +    glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); +    glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0f, -1.0f); +    glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f,  1.0f); +    glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f,  1.0f); +    glEnd(); + +    glBindTexture(GL_TEXTURE_2D, 0);  } -Renderer::~Renderer() {} +void +Renderer::DrawRectangle(RenderGroup& rgroup, REntity_Rectangle& rect) +{ +    int32_t xmin = WorldXToScreenX(rgroup, rect.x0); +    int32_t ymin = WorldYToScreenY(rgroup, rect.y0); +    int32_t xmax = WorldXToScreenX(rgroup, rect.x1); +    int32_t ymax = WorldYToScreenY(rgroup, rect.y1); + +    if (xmin < 0) { +        xmin = 0; +    } +    if (ymin < 0) { +        ymin = 0; +    } +    if (xmax >= m_Canvas.w) { +        xmax = m_Canvas.w - 1; +    } +    if (ymax >= m_Canvas.h) { +        ymax = m_Canvas.h - 1; +    } + +    uint32_t rshift = m_Canvas.rshift; +    uint32_t gshift = m_Canvas.gshift; +    uint32_t bshift = m_Canvas.bshift; +    for (int32_t y = ymin; y <= ymax; ++y) { +        uint32_t *pixel = m_Canvas.pixels + y * m_Canvas.w + xmin; +        for (int32_t x = xmin; x <= xmax; ++x) { +            uint32_t r = (uint32_t)(rect.color.r * 255.0f); +            uint32_t g = (uint32_t)(rect.color.g * 255.0f); +            uint32_t b = (uint32_t)(rect.color.b * 255.0f); +            uint32_t val = r << rshift | g << gshift | b << bshift; +            *pixel++ = val; +        } +    } +} + + +void +Renderer::DrawMonoBitmap(REntity_Bitmap& bitmap, Color color) +{ +    int32_t x0 = (int32_t)bitmap.x; +    int32_t y0 = (int32_t)bitmap.y; +    int32_t x1 = (int32_t)bitmap.x + bitmap.w - 1; +    int32_t y1 = (int32_t)bitmap.y + bitmap.h - 1; + +    int32_t cut_left = 0; +    int32_t cut_bot = 0; + +    if (x0 < 0) { +        cut_left = x0; +        x0 = 0; +    } +    if (y0 < 0) { +        cut_bot = y0; +        y0 = 0; +    } +    if (x1 >= m_Canvas.w) { +        x1 = m_Canvas.w - 1; +    } +    if (y1 >= m_Canvas.h) { +        y1 = m_Canvas.h - 1; +    } + + +    uint32_t rshift = m_Canvas.rshift; +    uint32_t gshift = m_Canvas.gshift; +    uint32_t bshift = m_Canvas.bshift; + +    uint8_t* grayscale = (uint8_t*)bitmap.data + (-cut_bot * bitmap.w) + (-cut_left); +    uint32_t* rgba = m_Canvas.pixels + y0 * m_Canvas.w + x0; +    for (int32_t y = y0; y <= y1; y++) { +        uint8_t *grayscale_pixels = grayscale; +        uint32_t *rgba_pixels = rgba; +        for (int32_t x = x0; x <= x1; x++) { +            float alpha = *grayscale_pixels / 255.0f; + +            // Todo: we do not want to blend with existing color! + +            uint32_t rgba_result = *rgba_pixels; +            float r0 = (rgba_result >> rshift) & 0xff; +            float g0 = (rgba_result >> gshift) & 0xff; +            float b0 = (rgba_result >> bshift) & 0xff; + +            float r1 = r0 + (color.r - r0)*alpha; +            float g1 = g0 + (color.g - g0)*alpha; +            float b1 = b0 + (color.b - b0)*alpha; + +            rgba_result = (uint32_t)r1 << rshift | (uint32_t)g1 << gshift | (uint32_t)b1 << bshift; +            *rgba_pixels = rgba_result; + +            grayscale_pixels++; +            rgba_pixels++; +        } + +        grayscale += bitmap.w; +        rgba += m_Canvas.w; +    } +} + + +int32_t +Renderer::WorldXToScreenX(RenderGroup& rgroup, float x) +{ +    float xscreen = (x / rgroup.m_CameraWidth) * (float)rgroup.m_ScreenWidth; +    return (int32_t)xscreen; +} + +int32_t +Renderer::WorldYToScreenY(RenderGroup& rgroup, float y) +{ +    float yscreen = (y / rgroup.m_CameraHeight) * (float)rgroup.m_ScreenHeight; +    return (int32_t)yscreen; +} + diff --git a/src/renderer/Renderer.hpp b/src/renderer/Renderer.hpp index a3e0f98..24d4b71 100644 --- a/src/renderer/Renderer.hpp +++ b/src/renderer/Renderer.hpp @@ -5,22 +5,37 @@  #include <SDL3/SDL.h>  #include <SDL3/SDL_video.h> -#include <memory> +struct RCanvas { +    uint32_t rshift; +    uint32_t gshift; +    uint32_t bshift; +    uint32_t ashift; +    int32_t w; +    int32_t h; +    uint32_t *pixels; +}; +  class Renderer {  public: -    enum Api { -        API_OPENGL -    }; +    Renderer(SDL_Window *window); -    Renderer() = default; -    static std::unique_ptr<Renderer> Select(Api api, SDL_Window *window); +    bool Init(); +    void ResizeCanvas(int32_t w, int32_t h); +    void Draw(RenderGroup &render_group); -public: -    virtual ~Renderer() = 0; -    virtual bool Init() = 0; -    virtual void Draw(RenderGroup &render_group) = 0; -    virtual void Present() = 0; +private: +    void DrawRectangle(RenderGroup& rgroup, REntity_Rectangle& rect); +    void DrawMonoBitmap(REntity_Bitmap& bitmap, Color color); + +    int32_t WorldXToScreenX(RenderGroup& rgroup, float x); +    int32_t WorldYToScreenY(RenderGroup& rgroup, float y); + + +private: +    SDL_Window *m_Window; +    uint32_t m_GlTexId; +    RCanvas m_Canvas;  }; diff --git a/src/renderer/opengl/GlIndexBuffer.cpp b/src/renderer/opengl/GlIndexBuffer.cpp deleted file mode 100644 index c5bd713..0000000 --- a/src/renderer/opengl/GlIndexBuffer.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include <renderer/opengl/GlIndexBuffer.hpp> - -#include <GL/glew.h> -#include <string.h> -#include <stdio.h> -#include <assert.h> - -void GlIndexBuffer::Init() { -    m_CurrentIndex = 0; -    m_Indices.reserve(16384); -    glGenBuffers(1, &m_Id); -    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Id); -} - -uint32_t *GlIndexBuffer::GetData() { -    return m_Indices.data(); -} - -uint32_t GlIndexBuffer::GetCount() { -    uint32_t count = static_cast<uint32_t>(m_Indices.size()); -    return count; -} - -void GlIndexBuffer::Reset() { -    m_CurrentIndex = 0; -    m_Indices.clear(); -} - -void GlIndexBuffer::PushRectangle() { -    uint32_t current_index = m_CurrentIndex; -    m_Indices.push_back(current_index + 0); -    m_Indices.push_back(current_index + 1); -    m_Indices.push_back(current_index + 2); -    m_Indices.push_back(current_index + 0); -    m_Indices.push_back(current_index + 2); -    m_Indices.push_back(current_index + 3); -    m_CurrentIndex += 4; -} - -void GlIndexBuffer::TransferData() { -    size_t size = m_Indices.size() * sizeof(uint32_t); -    glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, m_Indices.data(), GL_STATIC_DRAW); -} - diff --git a/src/renderer/opengl/GlIndexBuffer.hpp b/src/renderer/opengl/GlIndexBuffer.hpp deleted file mode 100644 index 7805460..0000000 --- a/src/renderer/opengl/GlIndexBuffer.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#pragma once - -#include <renderer/Renderer.hpp> - -class GlIndexBuffer { -public: -    GlIndexBuffer() = default; -    void Init(); -    void Reset(); - -public: -    uint32_t GetCount(); -    uint32_t *GetData(); - -    void PushRectangle(); -    void TransferData(); - -private: -    uint32_t m_Id; -    uint32_t m_CurrentIndex; -    std::vector<uint32_t> m_Indices; -}; - diff --git a/src/renderer/opengl/GlRenderer.cpp b/src/renderer/opengl/GlRenderer.cpp deleted file mode 100644 index 5d7c985..0000000 --- a/src/renderer/opengl/GlRenderer.cpp +++ /dev/null @@ -1,220 +0,0 @@ -#include <basic/defs.hpp> -#include <iterator> -#include <renderer/RenderGroup.hpp> -#include <renderer/opengl/GlRenderer.hpp> -#include <basic/math.hpp> -#include <renderer/Renderer.hpp> -#include <renderer/opengl/GlVertexBuffer.hpp> - -#include <SDL3/SDL.h> -#include <SDL3/SDL_video.h> -#include <GL/glew.h> -#include <GL/gl.h> -#include <assert.h> -#include <cstdio> - -void GLAPIENTRY -MessageCallback(GLenum source, -                GLenum type, -                GLuint id, -                GLenum severity, -                GLsizei length, -                const GLchar* message, -                const void* userParam) -{ -    fprintf(stderr, "GL CALLBACK: %s, type = 0x%x, severity = 0x%x, message = %s\n", -            type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "", -            type, -            severity, -            message -    ); -} - -GlRenderer::~GlRenderer() { -    SDL_GL_DestroyContext(m_Context); -} - -GlRenderer::GlRenderer(SDL_Window *window) -        : m_Window(window), m_Context(nullptr) { -} - -bool GlRenderer::Init() { -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); -    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); -    SDL_GLContext sdl_gl_context = SDL_GL_CreateContext(m_Window); -    if (!sdl_gl_context) { -        return false; -    } - -    GLenum err = glewInit(); -    if (GLEW_OK != err) -    { -        fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); -        SDL_GL_DestroyContext(sdl_gl_context); -        return false; -    } - -#if 0 -    glEnable(GL_DEBUG_OUTPUT); -    glDebugMessageCallback(MessageCallback, 0); -#endif - -    GLuint vertex_array; -    glGenVertexArrays(1, &vertex_array); -    glBindVertexArray(vertex_array); - - -    // init rectangle rendering -    if (!m_RectangleShader.InitProgram()) { -        return false; -    } -    m_RectangleVertexBuffer.Init(); -    m_RectangleIndexBuffer.Init(); - - -    // init text rendering -    if (!m_TextShader.InitProgram()) { -        return false; -    } -    m_TextVertexBuffer.Init(); -    m_TextIndexBuffer.Init(); - - -    m_Context = sdl_gl_context; -    return true; -} - -void GlRenderer::Draw(RenderGroup& render_group) { -    render_group.Sort(); - -    float camera_width = render_group.m_CameraWidth; -    float camera_height = render_group.m_CameraHeight; - - -    glViewport(0, 0, render_group.m_ScreenWidth, render_group.m_ScreenHeight); -    glClearColor(render_group.m_ClearColor.x, -                 render_group.m_ClearColor.y, -                 render_group.m_ClearColor.z, -                 1.0f); -    glClear(GL_COLOR_BUFFER_BIT); - - -    // viewport space -    float scale = render_group.GetScale(); -    int32_t viewport_width  = static_cast<int32_t>(camera_width * scale); -    int32_t viewport_height = static_cast<int32_t>(camera_height * scale); -    int32_t viewport_x0 = (render_group.m_ScreenWidth - viewport_width) / 2; -    int32_t viewport_y0 = (render_group.m_ScreenHeight - viewport_height) / 2; -    glViewport(viewport_x0, viewport_y0, viewport_width, viewport_height); - - -    // draw batches -    float last_z = -1; -    for (auto [z, entity_index] : render_group.m_RSortEntries) { -        REntity& render_entity = render_group.m_REntities[entity_index]; -        switch (render_entity.type) { -            case REntityType_Rectangle: { -                // clip space (from camera space to [-1, 1] for pos and [0, 2] for dim) -                V3F32 pos = render_entity.rect.pos; -                V2F32 dim = render_entity.rect.dim; -                pos.x = 2*(pos.x / camera_width) - 1; -                pos.y = 2*(pos.y / camera_height) - 1; -                dim.x = 2*(dim.x / camera_width); -                dim.y = 2*(dim.y / camera_height); - -                if (render_entity.rect.pos.z > last_z) { -                    DrawBatch(); -                    last_z = z; -                } - -                m_RectangleVertexBuffer.PushRectangle(pos, dim, render_entity.rect.color); -                m_RectangleIndexBuffer.PushRectangle(); -            } - -            case REntityType_Bitmap: { -                REntity_Bitmap& bitmap = render_entity.bitmap; - -                // clip space (from camera space to [-1, 1] for pos and [0, 2] for dim) -                V3F32 pos = render_entity.bitmap.pos; -                pos.x = 2*(pos.x / camera_width) - 1; -                pos.y = 2*(pos.y / camera_height) - 1; - -#if 0 - -                // create setup texture -                unsigned int texture_id; -                glGenTextures(1, &texture_id);   -                glBindTexture(GL_TEXTURE_2D, texture_id);   -                glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, bitmap.width, bitmap.height, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmap.data); - -                glGenerateMipmap(GL_TEXTURE_2D); - -                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	 -                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); -                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); -                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - -                // apply texture -                glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); -                glEnableVertexAttribArray(2);   - -                // delete texture -                glDeleteTextures(1, &texture_id); - - - -                // 1) set vertex buffer -                // 2) set texture coordinates -                pos.x = 2*(pos.x / camera_width) - 1; -                pos.y = 2*(pos.y / camera_height) - 1; -                dim.x = 2*(dim.x / camera_width); -                dim.y = 2*(dim.y / camera_height); -                if (render_entity.rect.pos.z > last_z) { -                    DrawBatch(); -                    last_z = z; -                } -#endif - - -            } break; - -            InvalidDefaultCase; -        } -    } - -    DrawBatch(); -} - -void GlRenderer::Present() { -    SDL_GL_SwapWindow(m_Window); -} - -void GlRenderer::DrawBatch() { -    int32_t rectangle_index_count = static_cast<int32_t>(m_RectangleIndexBuffer.GetCount()); -    int32_t text_index_count = static_cast<int32_t>(m_TextIndexBuffer.GetCount()); - -    if (rectangle_index_count) { -        m_RectangleVertexBuffer.TransferData(); -        m_RectangleIndexBuffer.TransferData(); -        m_RectangleShader.UseProgram(); - -        glDrawElements(GL_TRIANGLES, rectangle_index_count, GL_UNSIGNED_INT, 0); - -        m_RectangleVertexBuffer.Reset(); -        m_RectangleIndexBuffer.Reset(); -    } -     -    if (text_index_count) { -        m_TextVertexBuffer.TransferData(); -        m_TextIndexBuffer.TransferData(); -        m_TextShader.UseProgram(); - -        glDrawElements(GL_TRIANGLES, text_index_count, GL_UNSIGNED_INT, 0); - -        m_TextVertexBuffer.Reset(); -        m_TextIndexBuffer.Reset(); -    } -} - - diff --git a/src/renderer/opengl/GlRenderer.hpp b/src/renderer/opengl/GlRenderer.hpp deleted file mode 100644 index d59ba45..0000000 --- a/src/renderer/opengl/GlRenderer.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#pragma once - -#include <renderer/Renderer.hpp> -#include <renderer/opengl/GlVertexBuffer.hpp> -#include <renderer/opengl/GlIndexBuffer.hpp> -#include <renderer/opengl/GlShader.hpp> - -class GlRenderer : public Renderer { -public: -    GlRenderer(SDL_Window *window); -    ~GlRenderer() override; - -    bool Init() override; -    void Draw(RenderGroup& render_group) override; -    void Present() override; - -    void InitTexture(); - -private: -    void DrawBatch(); - -private: -    SDL_Window *m_Window; -    SDL_GLContext m_Context; - -    GlVertexBuffer m_RectangleVertexBuffer; -    GlIndexBuffer m_RectangleIndexBuffer; -    GlShader m_RectangleShader; - -    GlVertexBuffer m_TextVertexBuffer; -    GlVertexBuffer m_TextIndexBuffer; -    GlShader m_TextShader; -}; - diff --git a/src/renderer/opengl/GlShader.cpp b/src/renderer/opengl/GlShader.cpp deleted file mode 100644 index cf7c6c5..0000000 --- a/src/renderer/opengl/GlShader.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include <renderer/opengl/GlShader.hpp> -#include <GL/glew.h> -#include <stdio.h> - -static const char *s_vertex_shader_src =  -"#version 330 core\n" -"layout (location = 0) in vec3 aPos;\n" -"layout (location = 1) in vec3 aColor;\n" -"\n" -"out vec4 vertexColor;\n" -"\n" -"void main()\n" -"{\n" -"    gl_Position = vec4(aPos.xy, 0.0, 1.0);\n" -"    vertexColor = vec4(aColor, 1.0);\n" -"}\n" -; - -static const char *s_fragment_shader_src = -"#version 330 core\n" -"in vec4 vertexColor;\n" -"\n" -"out vec4 FragColor;\n" -"\n" -"void main()\n" -"{\n" -"    FragColor = vertexColor;\n" -"}\n" -; - -bool GlShader::InitProgram() { -    GLuint vertex_shader; -    GLuint fragment_shader; -    if (!CompileShader(&vertex_shader, s_vertex_shader_src, GL_VERTEX_SHADER) || -        !CompileShader(&fragment_shader, s_fragment_shader_src, GL_FRAGMENT_SHADER)) { -        return false; -    } - -    GLuint program = glCreateProgram(); -    glAttachShader(program, vertex_shader); -    glAttachShader(program, fragment_shader); -    glLinkProgram(program); - -    glDeleteShader(vertex_shader); -    glDeleteShader(fragment_shader); - -    int success; -    char info_log[512]; -    glGetProgramiv(program, GL_LINK_STATUS, &success); -    if (!success) { -        printf("error linking shader program\n%s\n", info_log); -        return false; -    } - -    m_ProgramId = program; -    return true; -} - -void GlShader::UseProgram() { -    glUseProgram(m_ProgramId); -} - -bool GlShader::CompileShader(GLuint *id, const char *src, GLenum type) { -    GLuint shader = glCreateShader(type); -    glShaderSource(shader, 1, &src, 0); -    glCompileShader(shader); - -    int  success; -    char info_log[512]; -    glGetShaderiv(shader, GL_COMPILE_STATUS, &success); -    if(!success) { -        glGetShaderInfoLog(shader, 512, 0, info_log); -        printf("error %s shader compilation failed\n%s\n", type == GL_VERTEX_SHADER ? "vertex" : "fragment", info_log); -        glDeleteShader(shader); -        return false; -    } - -    *id = shader; -    return true; -} diff --git a/src/renderer/opengl/GlShader.hpp b/src/renderer/opengl/GlShader.hpp deleted file mode 100644 index d195a8a..0000000 --- a/src/renderer/opengl/GlShader.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include <GL/glew.h> - -class GlShader { -public: -    GlShader() = default; -    bool InitProgram(); -    void UseProgram(); -private: -    bool CompileShader(GLuint *id, const char *src, GLenum type); - -private: -    GLuint m_ProgramId; -}; diff --git a/src/renderer/opengl/GlVertexBuffer.cpp b/src/renderer/opengl/GlVertexBuffer.cpp deleted file mode 100644 index f716c50..0000000 --- a/src/renderer/opengl/GlVertexBuffer.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include <renderer/opengl/GlVertexBuffer.hpp> - -#include <GL/glew.h> -#include <string.h> - -#include <stdio.h> - -void GlVertexBuffer::Init() { -    m_Vertices.reserve(16384); -    glGenBuffers(1, &m_Id); -    glBindBuffer(GL_ARRAY_BUFFER, m_Id); -} - -void GlVertexBuffer::Reset() { -    glDisableVertexAttribArray(0); -    glDisableVertexAttribArray(1); -    m_Vertices.clear(); -} - -float *GlVertexBuffer::GetData() { -    return reinterpret_cast<float*>(m_Vertices.data()); -} - -uint32_t GlVertexBuffer::GetCount() { -    return static_cast<uint32_t>(m_Vertices.size()); -} - -void GlVertexBuffer::PushRectangle(V3F32 pos, V2F32 dim, V3F32 color) { -    V3F32 positions[4] = { -        V3F32(pos.x,         pos.y,         pos.z), -        V3F32(pos.x + dim.x, pos.y,         pos.z), -        V3F32(pos.x + dim.x, pos.y + dim.y, pos.z), -        V3F32(pos.x,         pos.y + dim.y, pos.z), -    }; - -    for (int i = 0; i < 4; i++) { -        GlVertex vertex = {}; -        vertex.pos = positions[i]; -        vertex.color = color; -        m_Vertices.push_back(vertex); -    } -} - -void GlVertexBuffer::TransferData() { -    size_t size = m_Vertices.size() * sizeof(GlVertex); -    GLsizei stride = sizeof(GlVertex); -    const void *offset_color = (const void*)(3*sizeof(float)); -    glEnableVertexAttribArray(0); -    glEnableVertexAttribArray(1); -    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, 0); -    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, offset_color); -    glBufferData(GL_ARRAY_BUFFER, size, m_Vertices.data(), GL_STATIC_DRAW); -} - diff --git a/src/renderer/opengl/GlVertexBuffer.hpp b/src/renderer/opengl/GlVertexBuffer.hpp deleted file mode 100644 index 4099ca2..0000000 --- a/src/renderer/opengl/GlVertexBuffer.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include <renderer/Renderer.hpp> - -struct GlVertex { -    V3F32 pos; -    V3F32 color; -}; - -class GlVertexBuffer { -public: -    GlVertexBuffer() = default; -    void Init(); -    void Reset(); - -public: -    float *GetData(); -    uint32_t GetCount(); - -    void PushRectangle(V3F32 pos, V2F32 dim, V3F32 color); -    void TransferData(); - -private: -    uint32_t m_Id; -    std::vector<GlVertex> m_Vertices; -}; -  | 
