work on renderer

This commit is contained in:
2026-02-08 17:29:56 -06:00
parent f277dee1f7
commit 086e5f5126
5 changed files with 128 additions and 37 deletions

6
.ccls
View File

@@ -4,5 +4,7 @@ clang++
-Ivendor/imgui -Ivendor/imgui
-Ivendor/notcurses/include -Ivendor/notcurses/include
-Ivendor/GameNetworkingSockets/include -Ivendor/GameNetworkingSockets/include
-DWINDOW=1 -DWINDOW_GLFW
-DRENDERER=1 -DWINDOW_SDL3
-DRENDERER_OPENGL
-DRENDERER_SDL3

View File

@@ -3,34 +3,110 @@
#include "pch.hpp" #include "pch.hpp"
#include "extratools.h"
namespace Archimedes { namespace Archimedes {
class VertexArray {}; class VertexArray {
class IndexArray {}; public:
VertexArray(const void* data, size_t size) : data(data), size(size) {
}
class Shader {}; ~VertexArray() {}
const void* getData() const { return data; }
size_t getSize() const { return size; }
unsigned int getId() const { return id; }
private:
const void* data;
size_t size;
unsigned int id;
};
class IndexArray {
public:
IndexArray(const unsigned int* indices, size_t count) : indices(indices), count(count) {
}
~IndexArray() {}
const void* getIndicies() const { return indices; }
size_t getCount() const { return count; }
private:
const unsigned int* indices;
size_t count;
unsigned int id;
};
class Shader {
public:
enum class LoadType {
FromSource,
FromFileSource,
FromFileBin
};
Shader(const std::string& source, LoadType loadType) {
switch(loadType) {
case LoadType::FromSource:
break;
case LoadType::FromFileSource:
break;
case LoadType::FromFileBin:
break;
default:
break;
}
}
~Shader() {}
unsigned int getId() const { return id; }
private:
unsigned int id;
};
class RenderTarget {
public:
RenderTarget(const void* data, size_t size, size_t count, const std::string& shader)
: data(data), size(size), count(count), shader(shader) {}
~RenderTarget() {}
private:
const void* data;
size_t size;
size_t count;
const std::string& shader;
};
class Renderer { class Renderer {
public: public:
int w, h; int w, h;
Renderer() {} glm::vec4 clearColor = { 0.0f, 0.0f, 0.0f, 0.0f };
virtual ~Renderer() = 0;
Renderer() : w(0), h(0) {}
virtual ~Renderer() {}
virtual bool init() = 0; virtual bool init() = 0;
virtual void render() = 0; virtual void render() = 0;
std::list<std::function<void()>>& getCmdList() { virtual void draw(const VertexArray&, const IndexArray&, const Shader&) = 0;
return rc;
}
virtual Renderer* getRendererImpl() = 0;
private:
std::list<std::function<void()>> rc;
}; };
} }

View File

@@ -1,5 +1,7 @@
#ifndef RENDERER_OPENGL
#define RENDERER_OPENGL #define RENDERER_OPENGL
#ifdef RENDERER_OPENGL
#undef RENDERER_OPENGL
#include "pch.hpp" #include "pch.hpp"
@@ -11,6 +13,12 @@
namespace Archimedes { namespace Archimedes {
class VertexArrayOpenGL : public VertexArray {};
class IndexArrayOpenGL : public IndexArray {};
class ShaderOpenGL : public Shader {};
class RendererOpenGL : public Renderer { class RendererOpenGL : public Renderer {
public: public:
@@ -19,19 +27,30 @@ namespace Archimedes {
RendererOpenGL() {}; RendererOpenGL() {};
~RendererOpenGL() {}; ~RendererOpenGL() {};
bool init() { bool init() override {
return glewInit() == GLEW_OK; return glewInit() == GLEW_OK;
}; };
void render(std::list<std::function<void()>> cmdList, int& w, int& h) { void render() override {
glViewport(0, 0, w, h); glViewport(0, 0, w, h);
glClearColor(0.2, 0.2, 0.4, 1); glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
void draw() {} void draw(const VertexArray& va, const IndexArray& ia, const Shader& shader) override {
glUseProgram(shader.id);
glBindVertexArray(va.id);
glDrawElements(GL_TRIANGLES, va.count, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
RendererOpenGL* getRendererImpl() override { return this; }
}; };
} }

View File

@@ -1,7 +1,5 @@
#if WINDOW == 1 #ifdef WINDOW_GLFW
#undef WINDOW_GLFW
#ifndef WINDOW_GLFW
#define WINDOW_GLFW
#include "pch.hpp" #include "pch.hpp"
@@ -30,11 +28,13 @@ namespace Archimedes {
std::cout << "glfwInit failed!\n"; std::cout << "glfwInit failed!\n";
std::abort(); std::abort();
} }
#if RENDRER == 1
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); if(renderer) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
#endif glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
}
window = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL); window = glfwCreateWindow(640, 480, "Archimedes", NULL, NULL);
if(!w) { if(!w) {
@@ -134,5 +134,3 @@ namespace Archimedes {
} }
#endif #endif
#endif

View File

@@ -1,7 +1,5 @@
#if WINDOW == 2 #ifdef WINDOW_SDL3
#undef WINDOW_SDL3
#ifndef WINDOW_SDL3
#define WINDOW_SDL3
#include "pch.hpp" #include "pch.hpp"
@@ -9,7 +7,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#if RENDERER == 2 #if RENDERER == 1
#include <SDL3/SDL_opengl.h> #include <SDL3/SDL_opengl.h>
@@ -136,7 +134,7 @@ namespace Archimedes {
while(SDL_PollEvent(&e)); while(SDL_PollEvent(&e));
} }
WindowSDL3* getWindowImpl() { return this; }; WindowSDL3* getWindowImpl() override { return this; };
#if RENDERER == 1 #if RENDERER == 1
void restoreContext() { SDL_GL_MakeCurrent(window, gl_context); } void restoreContext() { SDL_GL_MakeCurrent(window, gl_context); }
@@ -167,5 +165,3 @@ namespace Archimedes {
} }
#endif #endif
#endif