SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
OpenGLShader.cpp
Go to the documentation of this file.
1
#include "
../../include/private/Graphics/OpenGL/OpenGLShader.hpp
"
2
#include <
Core/Logger.hpp
>
3
#include <fstream>
4
#include <sstream>
5
6
namespace
Sleak
{
7
namespace
RenderEngine
{
8
9
OpenGLShader::OpenGLShader
() {}
10
11
OpenGLShader::~OpenGLShader
() {
12
if
(m_program != 0) {
13
glDeleteProgram(m_program);
14
m_program = 0;
15
}
16
}
17
18
bool
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
28
bool
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
73
void
OpenGLShader::bind
() {
74
if
(m_program != 0) {
75
glUseProgram(m_program);
76
}
77
}
78
79
bool
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
99
std::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
Logger.hpp
SLEAK_ERROR
#define SLEAK_ERROR(...)
Definition
Logger.hpp:22
OpenGLShader.hpp
Sleak::RenderEngine::OpenGLShader::OpenGLShader
OpenGLShader()
Definition
OpenGLShader.cpp:9
Sleak::RenderEngine::OpenGLShader::~OpenGLShader
~OpenGLShader() override
Definition
OpenGLShader.cpp:11
Sleak::RenderEngine::OpenGLShader::bind
void bind() override
Binds this program as the active shader for subsequent draws.
Definition
OpenGLShader.cpp:73
Sleak::RenderEngine::OpenGLShader::compile
bool compile(const std::string &shaderPath) override
Splits a combined shader file into vertex/fragment stages and links them.
Definition
OpenGLShader.cpp:18
Sleak::RenderEngine
Backend-facing rendering layer shared by the four graphics backends.
Definition
Application.hpp:60
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
src
Graphics
OpenGL
OpenGLShader.cpp
Generated on
for SleakEngine by
1.18.0