SolarSim
Loading...
Searching...
No Matches
shader.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <glad/glad.h>
4#include <glm/gtc/type_ptr.hpp>
5#include <glm/mat4x4.hpp>
6
7#include <iostream>
8#include <string>
9
10namespace solarsim {
11
18 struct Shader
19 {
21 uint32_t programID = 0;
22
26 void use() {
27 glUseProgram(programID);
28 }
29
36 void bindBlocks() {
37 int loc = glGetUniformBlockIndex(programID, "CameraBuffer");
38 if (loc != GL_INVALID_INDEX) {
39 glUniformBlockBinding(programID, loc, 0);
40 }
41 loc = glGetUniformBlockIndex(programID, "LightsBuffer");
42 if (loc != GL_INVALID_INDEX) {
43 glUniformBlockBinding(programID, loc, 1);
44 }
45 loc = glGetUniformBlockIndex(programID, "RigidbodyBuffer");
46 if (loc != GL_INVALID_INDEX) {
47 glUniformBlockBinding(programID, loc, 2);
48 }
49 }
50
51 // Uniform setting methods
52
58 void setUniform(const std::string& name, const glm::mat4& value) {
59 int loc = glGetUniformLocation(programID, name.c_str());
60 if (loc != -1) {
61 glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(value));
62 }
63 }
64
69 void setUniform(const std::string& name, const glm::vec3& value) {
70 int loc = glGetUniformLocation(programID, name.c_str());
71 if (loc != -1) {
72 glUniform3fv(loc, 1, glm::value_ptr(value));
73 }
74 }
75
80 void setUniform(const std::string& name, float value) {
81 int loc = glGetUniformLocation(programID, name.c_str());
82 if (loc != -1) {
83 glUniform1f(loc, value);
84 }
85 }
86
91 void setUniform(const std::string& name, int value) {
92 int loc = glGetUniformLocation(programID, name.c_str());
93 if (loc != -1) {
94 glUniform1i(loc, value);
95 }
96 }
97
102 void setUniform(const std::string& name, bool value) {
103 int loc = glGetUniformLocation(programID, name.c_str());
104 if (loc != -1) {
105 glUniform1i(loc, value);
106 }
107 }
108
116 void compile(const std::string& vertexSrc, const std::string& fragmentSrc) {
117 uint32_t vertex = glCreateShader(GL_VERTEX_SHADER);
118 const char* vSrc = vertexSrc.c_str();
119 glShaderSource(vertex, 1, &vSrc, nullptr);
120 glCompileShader(vertex);
121 checkCompileErrors(vertex, "VERTEX");
122
123 uint32_t fragment = glCreateShader(GL_FRAGMENT_SHADER);
124 const char* fSrc = fragmentSrc.c_str();
125 glShaderSource(fragment, 1, &fSrc, nullptr);
126 glCompileShader(fragment);
127 checkCompileErrors(fragment, "FRAGMENT");
128
129 uint32_t program = glCreateProgram();
130 glAttachShader(program, vertex);
131 glAttachShader(program, fragment);
132 glLinkProgram(program);
133 checkCompileErrors(program, "PROGRAM");
134
135 glDeleteShader(vertex);
136 glDeleteShader(fragment);
137
138 programID = program;
139
140 // Bind blocks once
141 bindBlocks();
142 }
143
151 void checkCompileErrors(uint32_t shader, const std::string& type) {
152 int success;
153 char infoLog[1024];
154 if (type != "PROGRAM") {
155 glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
156 if (!success) {
157 glGetShaderInfoLog(shader, 1024, nullptr, infoLog);
158 std::cerr << "Shader Compilation Error (" << type << "): " << infoLog << "\n";
159 }
160 } else {
161 glGetProgramiv(shader, GL_LINK_STATUS, &success);
162 if (!success) {
163 glGetProgramInfoLog(shader, 1024, nullptr, infoLog);
164 std::cerr << "Shader Linking Error (" << type << "): " << infoLog << "\n";
165 }
166 }
167 }
168
169 };
170}
Definition engine.cpp:23
Manages OpenGL shader program compilation and uniform setting.
Definition shader.hpp:19
uint32_t programID
Definition shader.hpp:21
void setUniform(const std::string &name, bool value)
Set a boolean uniform value.
Definition shader.hpp:102
void checkCompileErrors(uint32_t shader, const std::string &type)
Check for shader compilation or linking errors.
Definition shader.hpp:151
void setUniform(const std::string &name, int value)
Set an integer uniform value.
Definition shader.hpp:91
void bindBlocks()
Bind uniform buffer blocks to predefined binding points.
Definition shader.hpp:36
void setUniform(const std::string &name, const glm::vec3 &value)
Set a vec3 uniform value.
Definition shader.hpp:69
void compile(const std::string &vertexSrc, const std::string &fragmentSrc)
Compile and link shader program from source code.
Definition shader.hpp:116
void use()
Activate this shader for rendering.
Definition shader.hpp:26
void setUniform(const std::string &name, float value)
Set a float uniform value.
Definition shader.hpp:80
void setUniform(const std::string &name, const glm::mat4 &value)
Set a mat4 uniform value.
Definition shader.hpp:58