cangine
Loading...
Searching...
No Matches
shader.hpp
Go to the documentation of this file.
1#pragma once
2#include <iostream>
3#include <fstream>
4#include <sstream>
5#include "../engine/exception.hpp"
6
7#define CANGINE_DEFAULT_VERT_SRC R"(#version 330 core
8layout (location = 0) in vec3 aPos;
9layout (location = 1) in vec4 aColor;
10layout (location = 2) in vec2 uv;
11out vec4 ColorOut;
12out vec2 vUv;
13void main()
14{
15 gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
16 ColorOut = aColor;
17 vUv = uv;
18}
19)"
20
21#define CANGINE_DEFAULT_FRAG_SRC R"(#version 330 core
22out vec4 FragColor;
23in vec4 ColorOut;
24in vec2 vUv;
25uniform sampler2D uTexture;
26uniform bool uUseTexture;
27uniform bool uMono;
28void main()
29{
30 if (uUseTexture)
31 {
32 if (uMono)
33 {
34 float alpha = texture(uTexture, vUv).r;
35 FragColor = vec4(ColorOut.rgb, alpha * ColorOut.a);
36 }
37 else
38 {
39 vec4 t = texture(uTexture, vUv) * ColorOut;
40 FragColor = t;
41 }
42 }
43 else
44 {
45 FragColor = ColorOut;
46 }
47}
48)"
49
50// "
51// ^ to fix ide coloring issues
52
53namespace cangine {
54
61 class Shader {
62 private:
63 unsigned int vs;
64 unsigned int fs;
65
66 struct SourceTag {};
67
69 void attach();
71 void cleanUp();
73 void compileShaders();
75 void compileFromSource(const std::string& vsSrc, const std::string& fsSrc);
76
77 Shader(const std::string& vsSrc, const std::string& fsSrc, SourceTag);
78
79 public:
80 std::string shaderPath;
81
82 bool valid = true;
83 unsigned int ID;
84 std::string name;
85
87 void use();
89 void unbind();
95 void setBool(const std::string& name, bool value);
101 void setInt(const std::string& name, int value);
107 void setFloat(const std::string& name, float value);
114 void setVec2(const std::string& name, float x, float y);
122 void setVec3(const std::string& name, float x, float y, float z);
131 void setVec4(const std::string& name, float x, float y, float z, float w);
137 Shader(std::string name, std::string shaderPath = "./shaders");
139 ~Shader();
140
146 static Shader* fromSource(const std::string& vsSrc, const std::string& fsSrc);
147 };
148}
Wraps a compiled and linked OpenGL shader program.
Definition shader.hpp:61
void cleanUp()
Deletes the intermediate shader stage objects after linking.
Definition shader.cpp:69
void setBool(const std::string &name, bool value)
Sets a boolean uniform.
Definition shader.cpp:170
static Shader * fromSource(const std::string &vsSrc, const std::string &fsSrc)
Creates a shader program compiled from source strings.
Definition shader.cpp:153
void setVec4(const std::string &name, float x, float y, float z, float w)
Sets a vec4 uniform.
Definition shader.cpp:195
void setFloat(const std::string &name, float value)
Sets a float uniform.
Definition shader.cpp:180
void setVec2(const std::string &name, float x, float y)
Sets a vec2 uniform.
Definition shader.cpp:185
void use()
Activates this shader program for subsequent draw calls.
Definition shader.cpp:157
~Shader()
Deletes the OpenGL shader program.
Definition shader.cpp:47
void setInt(const std::string &name, int value)
Sets an integer uniform.
Definition shader.cpp:175
unsigned int ID
OpenGL shader program handle.
Definition shader.hpp:83
unsigned int vs
Compiled vertex shader handle.
Definition shader.hpp:63
void compileShaders()
Reads and compiles the vertex and fragment shader sources.
Definition shader.cpp:87
void setVec3(const std::string &name, float x, float y, float z)
Sets a vec3 uniform.
Definition shader.cpp:190
void compileFromSource(const std::string &vsSrc, const std::string &fsSrc)
Compiles vertex and fragment shaders directly from source strings.
Definition shader.cpp:74
std::string shaderPath
Directory the shader sources are loaded from.
Definition shader.hpp:80
unsigned int fs
Compiled fragment shader handle.
Definition shader.hpp:64
void attach()
Attaches and links the compiled shader stages into the program.
Definition shader.cpp:51
std::string name
Base name of the shader source files.
Definition shader.hpp:84
void unbind()
Deactivates the current shader program.
Definition shader.cpp:166
bool valid
Whether compilation and linking succeeded.
Definition shader.hpp:82
Definition audiomanager.cpp:4
Definition shader.hpp:66