Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
shader_base.h
1#ifndef OE_RENDER_SHADER_BASE_H
2#define OE_RENDER_SHADER_BASE_H
3
4#include "common.h"
5
6#include <vector>
7#include <string>
8#include <map>
9#include <unordered_map>
10#include <glm/mat4x4.hpp>
11
12#include "../scene/material.h"
13#include "../util/non_copyable.h"
14
15namespace oe::scene
16{
17 class Camera;
18 class Node;
19 class Material;
20}
21
22namespace oe::render
23{
24 class Texture;
25 class Cubemap;
26
33 {
34 public:
40 ShaderBase(const std::string& name = "");
41
43
54 template <typename T>
55 void define(const std::string& name, const T& value)
56 {
57 _defines[name] = std::to_string(value);
58 }
59
65 void define(const std::string& name, const char* value)
66 {
67 _defines[name] = std::string(value);
68 }
69
75 void define(const std::string& name, const std::string& value)
76 {
77 _defines[name] = value;
78 }
79
87 void define(const std::string& name)
88 {
89 _defines[name] = "";
90 }
91
96 const std::string& getName() const
97 {
98 return _name;
99 }
100
107 void compile(const GeometryPass vertex_pass, const RenderingPass fragment_pass);
108
114 void bind(const GeometryPass vertex_pass, const RenderingPass fragment_pass);
115
124 void fillFromCamera(const scene::Camera& camera) noexcept;
125
129 void fillTextures(const std::map<std::string, std::pair<uint32_t, std::shared_ptr<oe::render::Texture>>>& textures);
130
134 void fillCubemaps(const std::map<std::string, std::pair<uint32_t, std::shared_ptr<oe::render::Cubemap>>>& cubemaps);
135
149 void fillFromMaterial(const scene::Material* material);
150
154 void unbind();
155
156 /*
157 *
158 */
159 //std::string getShaderContent(const VertexPass vertex_pass, const RenderingPass fragment_pass) const;
160
167 void setInt(const std::string& name, const int32_t& value);
168
175 void setUInt(const std::string& name, const uint32_t& value);
176
183 void setFloat(const std::string& name, const float& value);
184
191 void setDouble(const std::string& name, const double& value);
192
199 void setMat3(const std::string& name, const glm::mat3& value);
200
207 void setMat4(const std::string& name, const glm::mat4& value);
208
215 void setVec2i(const std::string& name, const glm::ivec2& value);
216
223 void setVec2(const std::string& name, const glm::vec2& value);
224
231 void setVec3(const std::string& name, const glm::vec3& value);
232
239 void setVec4(const std::string& name, const glm::vec4& value);
240
247 void setIntArray(const std::string& name, const std::vector<int32_t>& values);
248
255 void setUIntArray(const std::string& name, const std::vector<uint32_t>& values);
256
263 void setFloatArray(const std::string& name, const std::vector<float>& values);
264
271 void setDoubleArray(const std::string& name, const std::vector<double>& values);
272
279 void setVec2iArray(const std::string& name, const std::vector<glm::ivec2>& values);
280
287 void setVec2Array(const std::string& name, const std::vector<glm::vec2>& values);
288
295 void setVec3Array(const std::string& name, const std::vector<glm::vec3>& values);
296
303 void setVec4Array(const std::string& name, const std::vector<glm::vec4>& values);
304
311 void setMat4Array(const std::string& name, const std::vector<glm::mat4>& values);
312
313 protected:
314 std::unordered_map<GeometryPass, std::string> _vertex_shader_sources;
315 std::unordered_map<RenderingPass, std::string> _fragment_shader_sources;
316
317 std::string _header;
318
319 private:
320
321 struct PassPairHash
322 {
323 std::size_t operator ()(const std::pair<GeometryPass, RenderingPass> &p) const
324 {
325 auto h1 = std::hash<GeometryPass>{}(p.first);
326 auto h2 = std::hash<RenderingPass>{}(p.second);
327
328 return h1 ^ h2;
329 }
330 };
331
332 std::string _generate_header() const noexcept;
333
334 uint32_t _program_handle = 0;
335
336 std::string _name;
337
338 std::unordered_map<std::string, std::string> _defines;
339
340 std::unordered_map<std::string, int32_t> _locations;
341
342 std::unordered_map<std::pair<GeometryPass, RenderingPass>, std::unordered_map<std::string, int32_t>, PassPairHash> _pass_locations;
343 std::unordered_map<std::pair<GeometryPass, RenderingPass>, uint32_t, PassPairHash> _pass_programs;
344
345 static uint32_t _last_bound_shader;
346 };
347}
348
349#endif
Shader class.
Definition shader_base.h:33
void setVec4(const std::string &name, const glm::vec4 &value)
Send a vec4 uniform to the shader.
void compile(const GeometryPass vertex_pass, const RenderingPass fragment_pass)
Compile / link the shader using the graphic API.
void setUIntArray(const std::string &name, const std::vector< uint32_t > &values)
Send a array of unsigned int uniforms to the shader.
void setFloatArray(const std::string &name, const std::vector< float > &values)
Send an array of float uniforms to the shader.
void setFloat(const std::string &name, const float &value)
Send a float uniform to the shader.
const std::string & getName() const
Get shader name.
Definition shader_base.h:96
void bind(const GeometryPass vertex_pass, const RenderingPass fragment_pass)
Use the shader in graphic API.
void fillFromCamera(const scene::Camera &camera) noexcept
Fill uniforms from camera (mainly position + transform matrices)
void setInt(const std::string &name, const int32_t &value)
Send an int uniform to the shader.
void setMat3(const std::string &name, const glm::mat3 &value)
Send a 3x3 matrix uniform to the shader.
void setVec2(const std::string &name, const glm::vec2 &value)
Send a vec2 uniform to the shader.
void setMat4(const std::string &name, const glm::mat4 &value)
Send a 4x4 matrix uniform to the shader.
void define(const std::string &name, const char *value)
Define a shader constant.
Definition shader_base.h:65
void fillFromMaterial(const scene::Material *material)
Fill uniforms from material.
void setMat4Array(const std::string &name, const std::vector< glm::mat4 > &values)
Send an array of matrix uniforms to the shader.
void unbind()
Tell the graphic API to release this shader.
void setVec2iArray(const std::string &name, const std::vector< glm::ivec2 > &values)
Send an array of ivec2 uniforms to the shader.
ShaderBase(const std::string &name="")
Create a new shader instance.
void setDouble(const std::string &name, const double &value)
Send a double uniform to the shader.
void setVec3Array(const std::string &name, const std::vector< glm::vec3 > &values)
Send an array of vec3 uniforms to the shader.
void define(const std::string &name, const T &value)
Define a shader constant.
Definition shader_base.h:55
void define(const std::string &name)
Define a shader constant without value.
Definition shader_base.h:87
void setVec4Array(const std::string &name, const std::vector< glm::vec4 > &values)
Send an array of vec4 uniforms to the shader.
void setVec2Array(const std::string &name, const std::vector< glm::vec2 > &values)
Send an array of vec2 uniforms to the shader.
void fillTextures(const std::map< std::string, std::pair< uint32_t, std::shared_ptr< oe::render::Texture > > > &textures)
Fill textures uniforms + bind textures.
void setVec3(const std::string &name, const glm::vec3 &value)
Send a vec3 uniform to the shader.
void fillCubemaps(const std::map< std::string, std::pair< uint32_t, std::shared_ptr< oe::render::Cubemap > > > &cubemaps)
Fill cubemaps uniforms + bind cubemaps.
void setDoubleArray(const std::string &name, const std::vector< double > &values)
Send an array of double uniforms to the shader.
void setVec2i(const std::string &name, const glm::ivec2 &value)
Send an ivec2 uniform to the shader.
void setIntArray(const std::string &name, const std::vector< int32_t > &values)
Send an array of int uniforms to the shader.
void define(const std::string &name, const std::string &value)
Define a shader constant.
Definition shader_base.h:75
void setUInt(const std::string &name, const uint32_t &value)
Send an unsigned int uniform to the shader.
The "eye of the scene".
Definition camera.h:33
Render agnostic material.
Definition material.h:26
Prevent class to be copied.
Definition non_copyable.h:12
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures)
Definition opengl.h:10
RenderingPass
Pipeline rendering passes.
Definition common.h:26
GeometryPass
Vertex rendering passes.
Definition common.h:14
Scene related management (Render-agnostic Geometry, Manger, etc...)
Definition debug.h:19