Acheron
Loading...
Searching...
No Matches
shader.hpp
1#pragma once
2
3#include <print>
4#include <string>
5
6#include "agl/agl.hpp"
7
8using namespace acheron::agl;
9
10namespace acheron::renderer {
14 struct Shader {
15 unsigned int id;
16
25 void Compile(std::string vsSrc, std::string fsSrc) {
26 auto compileStage = [](unsigned int type, const char* src) {
27 unsigned int shader = aglCreateShader(type);
28 aglShaderSource(shader, 1, &src, nullptr);
29 aglCompileShader(shader);
30
31 int success;
32
33 aglGetShaderiv(shader, COMPILE_STATUS, &success);
34 if(!success) {
35 char log[512];
36 aglGetShaderInfoLog(shader, 512, nullptr, log);
37 throw std::runtime_error(log);
38 }
39 return shader;
40 };
41
42 const char* vsSrccstr = vsSrc.c_str();
43 const char* fsSrccstr = fsSrc.c_str();
44 unsigned int vs = compileStage(VERTEX_SHADER, vsSrccstr);
45 unsigned int fs = compileStage(FRAGMENT_SHADER, fsSrccstr);
46
47 id = aglCreateProgram();
48 aglAttachShader(id, vs);
49 aglAttachShader(id, fs);
50 aglLinkProgram(id);
51
52 int success;
53 aglGetProgramiv(id, LINK_STATUS, &success);
54 if(!success) {
55 char log[512];
56 aglGetProgramInfoLog(id, 512, nullptr, log);
57 throw std::runtime_error(log);
58 }
59
60 aglDeleteShader(vs);
61 aglDeleteShader(fs);
62 }
63
67 void Bind() {
68 aglUseProgram(id);
69 }
70
71
78 void SetUniform(const char* name, float value[4]) {
79 int location = aglGetUniformLocation(id, name);
80 if(location != -1) {
81 aglUniform4fv(location, 1, value);
82 }
83 }
84
91 void SetUniformMat4(const char* name, const float mat[16]) const {
92 int location = aglGetUniformLocation(id, name);
93 if (location != -1) aglUniformMatrix4fv(location, 1, false, mat);
94 }
95
99 bool IsCompiled() { return id != 0; }
100 };
101}
Wrapper for OpenGL shaders.
Definition shader.hpp:14
void Compile(std::string vsSrc, std::string fsSrc)
Compile a shader with the source.
Definition shader.hpp:25
void SetUniformMat4(const char *name, const float mat[16]) const
Set a mat4 uniform.
Definition shader.hpp:91
void Bind()
Bind the shader.
Definition shader.hpp:67
bool IsCompiled()
Returns if the Shader was compiled.
Definition shader.hpp:99
void SetUniform(const char *name, float value[4])
Sets a vec4 uniform.
Definition shader.hpp:78