cae  0.0.0
Cross-API graphics engine
Loading...
Searching...
No Matches
opgl.cpp
Go to the documentation of this file.
1#include "OPGL/OPGL.hpp"
2
3#ifdef __linux__
5#elifdef _WIN32
7#elifdef __APPLE__
9#endif
10
11#include <glm/ext/matrix_transform.hpp>
12
13#include <stdexcept>
14
15void cae::OPGL::initialize(const NativeWindowHandle &nativeWindowHandle, const Color &clearColor)
16{
17#ifdef __linux__
18 m_context = std::make_unique<EGLContext_>();
19#elifdef _WIN32
20 m_context = std::make_unique<WGLContext>();
21#elifdef __APPLE__
22 m_context = std::make_unique<NSGLContext>();
23#endif
24
25 m_context->initialize(nativeWindowHandle);
26 const auto &gl = m_context->gl;
27
28 gl.Enable(GL_DEPTH_TEST);
29 gl.ClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
30
31 gl.GenBuffers(1, &m_ubo);
32 gl.BindBuffer(GL_UNIFORM_BUFFER, m_ubo);
33 gl.BufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4), nullptr, GL_DYNAMIC_DRAW);
34 gl.BindBufferBase(GL_UNIFORM_BUFFER, 0, m_ubo);
35 gl.BindBuffer(GL_UNIFORM_BUFFER, 0);
36}
37
38void cae::OPGL::draw(const WindowSize &windowSize, const ShaderID &shaderId, const glm::mat4 mvp)
39{
40 const auto &gl = m_context->gl;
41 gl.Viewport(0, 0, windowSize.width, windowSize.height);
42 gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
43
44 gl.UseProgram(m_programs.at(shaderId));
45 gl.BindBuffer(GL_UNIFORM_BUFFER, m_ubo);
46 gl.BufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4), &mvp);
47
48 // binding = 0 car dans le shader: layout(binding = 0)
49 gl.BindVertexArray(m_mesh.vao);
50 gl.DrawArrays(GL_TRIANGLES, 0, m_mesh.vertexCount);
51 gl.BindVertexArray(0);
52
53 m_context->swapBuffers();
54}
55
56void cae::OPGL::createPipeline(const ShaderID &id, const ShaderIRModule &vertex, const ShaderIRModule &fragment)
57{
58 const auto &gl = m_context->gl;
59 const GLuint program = gl.CreateProgram();
60
61 const GLuint vs = createGLShader(GL_VERTEX_SHADER, vertex, gl);
62 const GLuint fs = createGLShader(GL_FRAGMENT_SHADER, fragment, gl);
63
64 gl.AttachShader(program, vs);
65 gl.AttachShader(program, fs);
66 gl.LinkProgram(program);
67
68 GLint success = 0;
69 gl.GetProgramiv(program, GL_LINK_STATUS, &success);
70 if (success == 0)
71 {
72 char log[512];
73 gl.GetProgramInfoLog(program, 512, nullptr, log);
74 throw std::runtime_error(log);
75 }
76
77 gl.DeleteShader(vs);
78 gl.DeleteShader(fs);
79
80 m_programs[id] = program;
81}
82
83void cae::OPGL::createMesh(const std::vector<float> &vertices)
84{
85 const auto &gl = m_context->gl;
86 Mesh mesh{};
87 mesh.vertexCount = static_cast<GLsizei>(vertices.size() / 5);
88
89 gl.GenVertexArrays(1, &mesh.vao);
90 gl.GenBuffers(1, &mesh.vbo);
91
92 gl.BindVertexArray(mesh.vao);
93 gl.BindBuffer(GL_ARRAY_BUFFER, mesh.vbo);
94 gl.BufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
95
96 gl.VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), static_cast<void *>(0));
97 gl.EnableVertexAttribArray(0);
98
99 gl.VertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), reinterpret_cast<void *>(3 * sizeof(float)));
100 gl.EnableVertexAttribArray(1);
101
102 gl.BindBuffer(GL_ARRAY_BUFFER, 0);
103 gl.BindVertexArray(0);
104
105 m_mesh = mesh;
106}
107
108GLuint cae::OPGL::createGLShader(const GLenum type, const ShaderIRModule &data, const GladGLContext &gl)
109{
110 const GLuint shader = gl.CreateShader(type);
111
112 gl.ShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V, data.spirv.data(),
113 static_cast<GLsizei>(data.spirv.size() * sizeof(uint32_t)));
114
115 gl.SpecializeShader(shader, data.entryPoint.c_str(), 0, nullptr, nullptr);
116
117 return shader;
118}
This file contains the EGLContext_ class declaration.
This file contains the NSGLContext class declaration.
This file contains the OPGL class declaration.
This file contains the WGLContext class declaration.
void initialize(const NativeWindowHandle &nativeWindowHandle, const Color &clearColor) override
Initialize the renderer with a native window handle and clear color.
Definition opgl.cpp:15
static GLuint createGLShader(GLenum type, const ShaderIRModule &data, const GladGLContext &gl)
Definition opgl.cpp:108
void draw(const WindowSize &windowSize, const ShaderID &shaderId, glm::mat4 mvp) override
Draw the scene using the specified shader and window size.
Definition opgl.cpp:38
GLuint m_ubo
Definition OPGL.hpp:67
void createMesh(const std::vector< float > &vertices) override
Create a mesh with the given vertex data.
Definition opgl.cpp:83
std::unique_ptr< IContext > m_context
Definition OPGL.hpp:63
void createPipeline(const ShaderID &id, const ShaderIRModule &vertex, const ShaderIRModule &fragment) override
Create a rendering pipeline with vertex and fragment shaders.
Definition opgl.cpp:56
std::string ShaderID
Struct for color.
Definition IRenderer.hpp:23
GLsizei vertexCount
Definition OPGL.hpp:24
Struct for native window handle.
Definition IWindow.hpp:34
Struct for shader intermediate representation module.
std::vector< uint32_t > spirv
Struct for window size.
Definition IWindow.hpp:23
uint16_t height
Definition IWindow.hpp:25
uint16_t width
Definition IWindow.hpp:24