Skip to content

Commit 596d838

Browse files
committed
Fix: improve usage of renderer
remove hardcoded things like clearcolor, vertices, etc ...
1 parent 50c5b99 commit 596d838

5 files changed

Lines changed: 78 additions & 47 deletions

File tree

modules/Engine/src/engine.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ cae::Engine::Engine(const EngineConfig &config, const std::function<std::shared_
4343

4444
m_windowPlugin->create(config.window_name, {.width = config.window_width, .height = config.window_height});
4545
m_rendererPlugin->initialize(m_windowPlugin->getNativeHandle());
46+
m_rendererPlugin->createMesh({
47+
-0.5F, -0.5F, 1.F, 0.F, 0.F,
48+
0.5F, -0.5F, 0.F, 1.F, 0.F,
49+
0.F, 0.5F, 0.F, 0.F, 1.F
50+
});
4651
initShaders(shaderIRFactory, shaderFrontendFactories);
4752
}
4853

@@ -52,7 +57,7 @@ void cae::Engine::run() const
5257
int fpsIndex = 0;
5358
while (!m_windowPlugin->shouldClose())
5459
{
55-
m_rendererPlugin->draw(m_windowPlugin->getWindowSize());
60+
m_rendererPlugin->draw(m_windowPlugin->getWindowSize(), "basic");
5661
m_windowPlugin->pollEvents();
5762
printFps(fpsBuffer, fpsIndex, m_clock->getDeltaSeconds());
5863
m_clock->restart();

modules/Interfaces/include/Interfaces/Renderer/IRenderer.hpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
namespace cae
1313
{
1414

15+
struct Color
16+
{
17+
float r;
18+
float g;
19+
float b;
20+
float a;
21+
};
22+
1523
///
1624
/// @interface IRenderer
1725
/// @brief Interface for renderer
@@ -23,15 +31,17 @@ namespace cae
2331
public:
2432
~IRenderer() override = default;
2533

26-
virtual void initialize(const NativeWindowHandle &nativeWindowHandle) = 0;
27-
virtual void createPipeline(const ShaderID &id, const ShaderIRModule &vertex,
28-
const ShaderIRModule &fragment) = 0;
29-
30-
virtual void draw(const WindowSize &windowSize) = 0;
31-
3234
virtual void setVSyncEnabled(bool enabled) = 0;
33-
virtual bool isVSyncEnabled() const = 0;
35+
virtual void setClearColor(const Color &color) = 0;
36+
37+
[[nodiscard]] virtual bool isVSyncEnabled() const = 0;
3438

39+
virtual void initialize(const NativeWindowHandle &nativeWindowHandle,
40+
const Color &clearColor = {.r = 1.F, .g = 1.F, .b = 1.F, .a = 1.F}) = 0;
41+
virtual void createPipeline(const ShaderID &id, const ShaderIRModule &vertex,
42+
const ShaderIRModule &fragment) = 0;
43+
virtual void draw(const WindowSize &windowSize, const ShaderID &shaderId) = 0;
44+
virtual void createMesh(const std::vector<float>& vertices) = 0;
3545
}; // interface IRenderer
3646

3747
} // namespace cae

plugins/Renderer/OpenGL/include/OPGL/OPGL.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
namespace cae
1717
{
1818

19+
struct Mesh
20+
{
21+
GLuint vao = 0;
22+
GLuint vbo = 0;
23+
GLuint ebo = 0; // facultatif, si on a des indices
24+
GLsizei vertexCount = 0;
25+
};
26+
1927
///
2028
/// @class OPGL
2129
/// @brief Class for the OpenGL plugin
@@ -36,22 +44,23 @@ namespace cae
3644
[[nodiscard]] utl::PluginType getType() const override { return utl::PluginType::RENDERER; }
3745
[[nodiscard]] utl::PluginPlatform getPlatform() const override { return utl::PluginPlatform::ALL; }
3846

39-
void initialize(const NativeWindowHandle &nativeWindowHandle) override;
40-
void createPipeline(const ShaderID &id, const ShaderIRModule &vertex,
41-
const ShaderIRModule &fragment) override;
42-
void draw(const WindowSize &windowSize) override;
43-
4447
void setVSyncEnabled(bool enabled) override;
48+
void setClearColor(const Color &color) override { glClearColor(color.r, color.g, color.b, color.a); }
49+
4550
[[nodiscard]] bool isVSyncEnabled() const override;
4651

52+
void initialize(const NativeWindowHandle &nativeWindowHandle, const Color &clearColor) override;
53+
void createPipeline(const ShaderID &id, const ShaderIRModule &vertex,
54+
const ShaderIRModule &fragment) override;
55+
void draw(const WindowSize &windowSize, const ShaderID &shaderId) override;
56+
void createMesh(const std::vector<float>& vertices) override;
57+
4758
private:
4859
std::unique_ptr<IContext> m_context;
4960
std::unordered_map<ShaderID, GLuint> m_programs;
50-
GLuint gVAO = 0;
51-
GLuint gVBO = 0;
61+
Mesh m_mesh;
5262

5363
static GLuint createGLShader(GLenum type, const ShaderIRModule &data);
54-
void createTriangle();
5564

5665
}; // class OPGL
5766

plugins/Renderer/OpenGL/src/opgl.cpp

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <memory>
1515
#include <stdexcept>
1616

17-
void cae::OPGL::initialize(const NativeWindowHandle &nativeWindowHandle)
17+
void cae::OPGL::initialize(const NativeWindowHandle &nativeWindowHandle, const Color &clearColor)
1818
{
1919
#ifdef __linux__
2020
m_context = std::make_unique<EGLContextLinux>();
@@ -27,18 +27,17 @@ void cae::OPGL::initialize(const NativeWindowHandle &nativeWindowHandle)
2727
m_context->initialize(nativeWindowHandle);
2828

2929
glEnable(GL_DEPTH_TEST);
30-
glClearColor(1.F, 1.F, 1.F, 1.F);
31-
createTriangle();
30+
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
3231
}
3332

34-
void cae::OPGL::draw(const WindowSize &windowSize)
33+
void cae::OPGL::draw(const WindowSize &windowSize, const ShaderID &shaderId)
3534
{
3635
glViewport(0, 0, windowSize.width, windowSize.height);
3736
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3837

39-
glUseProgram(m_programs.at("basic"));
40-
glBindVertexArray(gVAO);
41-
glDrawArrays(GL_TRIANGLES, 0, 3);
38+
glUseProgram(m_programs.at(shaderId));
39+
glBindVertexArray(m_mesh.vao);
40+
glDrawArrays(GL_TRIANGLES, 0, m_mesh.vertexCount);
4241
glBindVertexArray(0);
4342

4443
m_context->swapBuffers();
@@ -75,26 +74,6 @@ void cae::OPGL::createPipeline(const ShaderID &id, const ShaderIRModule &vertex,
7574
m_programs[id] = program;
7675
}
7776

78-
void cae::OPGL::createTriangle()
79-
{
80-
constexpr std::array vertices = {-0.5F, -0.5F, 1.F, 0.F, 0.F, 0.5F, -0.5F, 0.F,
81-
1.F, 0.F, 0.0F, 0.5F, 0.F, 0.F, 1.F};
82-
83-
glGenVertexArrays(1, &gVAO);
84-
glGenBuffers(1, &gVBO);
85-
86-
glBindVertexArray(gVAO);
87-
glBindBuffer(GL_ARRAY_BUFFER, gVBO);
88-
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices.data(), GL_STATIC_DRAW);
89-
90-
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), static_cast<void *>(0));
91-
glEnableVertexAttribArray(0);
92-
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void *)(2 * sizeof(float)));
93-
glEnableVertexAttribArray(1);
94-
95-
glBindBuffer(GL_ARRAY_BUFFER, 0);
96-
glBindVertexArray(0);
97-
}
9877

9978
GLuint cae::OPGL::createGLShader(const GLenum type, const ShaderIRModule &data)
10079
{
@@ -107,3 +86,27 @@ GLuint cae::OPGL::createGLShader(const GLenum type, const ShaderIRModule &data)
10786

10887
return shader;
10988
}
89+
90+
void cae::OPGL::createMesh(const std::vector<float>& vertices)
91+
{
92+
Mesh mesh{};
93+
mesh.vertexCount = static_cast<GLsizei>(vertices.size() / 5);
94+
95+
glGenVertexArrays(1, &mesh.vao);
96+
glGenBuffers(1, &mesh.vbo);
97+
98+
glBindVertexArray(mesh.vao);
99+
glBindBuffer(GL_ARRAY_BUFFER, mesh.vbo);
100+
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
101+
102+
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
103+
glEnableVertexAttribArray(0);
104+
105+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));
106+
glEnableVertexAttribArray(1);
107+
108+
glBindBuffer(GL_ARRAY_BUFFER, 0);
109+
glBindVertexArray(0);
110+
111+
m_mesh = mesh;
112+
}

plugins/Renderer/Vulkan/include/VULKN/VULKN.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,19 @@ namespace cae
3232
[[nodiscard]] utl::PluginType getType() const override { return utl::PluginType::RENDERER; }
3333
[[nodiscard]] utl::PluginPlatform getPlatform() const override { return utl::PluginPlatform::ALL; }
3434

35-
void initialize(const NativeWindowHandle &nativeWindowHandle) override {}
35+
void setVSyncEnabled(bool enabled) override {}
36+
void setClearColor(const Color &color) override {}
37+
38+
[[nodiscard]] bool isVSyncEnabled() const override { return false; }
39+
40+
void initialize(const NativeWindowHandle &nativeWindowHandle, const Color &clearColor) override {}
3641
void createPipeline(const ShaderID &id, const ShaderIRModule &vertex,
3742
const ShaderIRModule &fragment) override
3843
{
3944
}
40-
void draw(const WindowSize &windowSize) override {}
45+
void draw(const WindowSize &windowSize, const ShaderID &shaderId) override {}
46+
void createMesh(const std::vector<float>& vertices) override {}
4147

42-
void setVSyncEnabled(bool enabled) override {}
43-
[[nodiscard]] bool isVSyncEnabled() const override { return false; }
4448
}; // class VULKN
4549

4650
} // namespace cae

0 commit comments

Comments
 (0)