SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
OpenGLShader.cpp
Go to the documentation of this file.
2#include <Core/Logger.hpp>
3#include <fstream>
4#include <sstream>
5
6namespace Sleak {
7namespace RenderEngine {
8
10
12 if (m_program != 0) {
13 glDeleteProgram(m_program);
14 m_program = 0;
15 }
16}
17
18bool OpenGLShader::compile(const std::string& shaderPath) {
19 // Strip .hlsl extension if present, then use _gl.vert/_gl.frag
20 std::string basePath = shaderPath;
21 auto hlslPos = basePath.rfind(".hlsl");
22 if (hlslPos != std::string::npos) {
23 basePath = basePath.substr(0, hlslPos);
24 }
25 return compile(basePath + "_gl.vert", basePath + "_gl.frag");
26}
27
28bool OpenGLShader::compile(const std::string& vertPath,
29 const std::string& fragPath) {
30 std::string vertSource = ReadFile(vertPath);
31 std::string fragSource = ReadFile(fragPath);
32
33 if (vertSource.empty() || fragSource.empty()) {
34 SLEAK_ERROR("Failed to read shader files: {} and {}", vertPath,
35 fragPath);
36 return false;
37 }
38
39 GLuint vertShader = 0, fragShader = 0;
40
41 if (!CompileShaderSource(vertSource, GL_VERTEX_SHADER, vertShader)) {
42 return false;
43 }
44 if (!CompileShaderSource(fragSource, GL_FRAGMENT_SHADER, fragShader)) {
45 glDeleteShader(vertShader);
46 return false;
47 }
48
49 m_program = glCreateProgram();
50 glAttachShader(m_program, vertShader);
51 glAttachShader(m_program, fragShader);
52 glLinkProgram(m_program);
53
54 GLint success = 0;
55 glGetProgramiv(m_program, GL_LINK_STATUS, &success);
56 if (!success) {
57 char infoLog[512];
58 glGetProgramInfoLog(m_program, 512, nullptr, infoLog);
59 SLEAK_ERROR("Shader program linking failed: {}", infoLog);
60 glDeleteProgram(m_program);
61 m_program = 0;
62 glDeleteShader(vertShader);
63 glDeleteShader(fragShader);
64 return false;
65 }
66
67 glDeleteShader(vertShader);
68 glDeleteShader(fragShader);
69
70 return true;
71}
72
74 if (m_program != 0) {
75 glUseProgram(m_program);
76 }
77}
78
79bool OpenGLShader::CompileShaderSource(const std::string& source,
80 GLenum type, GLuint& shader) {
81 shader = glCreateShader(type);
82 const char* src = source.c_str();
83 glShaderSource(shader, 1, &src, nullptr);
84 glCompileShader(shader);
85
86 GLint success = 0;
87 glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
88 if (!success) {
89 char infoLog[512];
90 glGetShaderInfoLog(shader, 512, nullptr, infoLog);
91 SLEAK_ERROR("Shader compilation failed: {}", infoLog);
92 glDeleteShader(shader);
93 shader = 0;
94 return false;
95 }
96 return true;
97}
98
99std::string OpenGLShader::ReadFile(const std::string& path) {
100 std::ifstream file(path);
101 if (!file.is_open()) {
102 SLEAK_ERROR("Cannot open shader file: {}", path);
103 return "";
104 }
105 std::stringstream ss;
106 ss << file.rdbuf();
107 return ss.str();
108}
109
110} // namespace RenderEngine
111} // namespace Sleak
#define SLEAK_ERROR(...)
Definition Logger.hpp:22
void bind() override
Binds this program as the active shader for subsequent draws.
bool compile(const std::string &shaderPath) override
Splits a combined shader file into vertex/fragment stages and links them.
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10