Conquer Space 0.0.0
A space themed grand strategy game set in the near future, with realistic orbital mechanics, and an emphasis on economics and politics.
shader.h
Go to the documentation of this file.
1/* Conquer Space
2 * Copyright (C) 2021-2025 Conquer Space
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17#pragma once
18
19#include <hjson.h>
20
21#include <istream>
22#include <memory>
23#include <string>
24#include <utility>
25
26#include <glm/glm.hpp>
27
28#include "engine/asset/asset.h"
29
30namespace cqsp::asset {
31enum class ShaderType {
32 NONE,
33 VERT,
34 FRAG,
35 GEOM
36};
37
42class Shader : public Asset {
43 public:
44 Shader() = default;
46 Shader(const std::string& code, ShaderType type);
47
49 void operator()(const std::string& code, ShaderType type);
50 ~Shader();
51
53
55 unsigned int id;
57
58 private:
59 explicit Shader(const Shader&) = default;
60};
61
69 public:
71 ShaderProgram(const Shader& vert, const Shader& frag);
72 ShaderProgram(const Shader& vert, const Shader& frag, const Shader& geom);
74
75 void setBool(const std::string& name, bool value);
76 void setInt(const std::string& name, int value);
77 void setFloat(const std::string& name, float value);
78 void setVec2(const std::string& name, const glm::vec2& value);
79 void setVec2(const std::string& name, float x, float y);
80 void setVec3(const std::string& name, const glm::vec3& value);
81 void setVec3(const std::string& name, float x, float y, float z);
82 void setVec4(const std::string& name, const glm::vec4& value);
83 void setVec4(const std::string& name, float x, float y, float z, float w);
84 void setMat2(const std::string& name, const glm::mat2& mat);
85 void setMat3(const std::string& name, const glm::mat3& mat);
86 void setMat4(const std::string& name, const glm::mat4& mat);
87 void bindTexture(int texture, unsigned int texture_id);
88
89 // Simpler overloaded functions so that you can just say set xxx and change the type
90 // as and when you like.
91 void Set(const std::string& name, bool value);
92 void Set(const std::string& name, int value);
93 void Set(const std::string& name, float value);
94 void Set(const std::string& name, const glm::vec2& value);
95 void Set(const std::string& name, float x, float y);
96 void Set(const std::string& name, const glm::vec3& value);
97 void Set(const std::string& name, float x, float y, float z);
98 void Set(const std::string& name, const glm::vec4& value);
99 void Set(const std::string& name, float x, float y, float z, float w);
100 void Set(const std::string& name, const glm::mat2& mat);
101 void Set(const std::string& name, const glm::mat3& mat);
102 void Set(const std::string& name, const glm::mat4& mat);
103
104 void UseProgram();
105 unsigned int program;
106
107 void SetMVP(const glm::mat4& model, const glm::mat4& view, const glm::mat4& projection);
108
109 operator unsigned int() const { return program; }
110};
111
113typedef std::shared_ptr<ShaderProgram> ShaderProgram_t;
114
117 return std::make_shared<ShaderProgram>(vert, frag);
118}
119
122 return std::make_shared<ShaderProgram>(vert, frag, geom);
123}
124
146class ShaderDefinition : public Asset {
147 public:
149 std::string vert;
150
152 std::string frag;
153
155 std::string geometry;
156
157 // All the uniforms
158 Hjson::Value uniforms;
159
161
163
164 private:
165 void SetShaderUniform(ShaderProgram_t& shader, std::pair<const std::string, Hjson::Value>& value);
166};
167
168// Set of utility functions that load shaders
169unsigned int LoadShaderData(const std::string& code, int type);
170unsigned int MakeShaderProgram(int vertex, int fragment, int geometry = -1);
171
177std::string GetErrorLog(unsigned int shader);
178} // namespace cqsp::asset
The base class for assets.
Definition: asset.h:165
Definition: shader.h:146
ShaderProgram_t MakeShader()
Definition: shader.cpp:500
void SetShaderUniform(ShaderProgram_t &shader, std::pair< const std::string, Hjson::Value > &value)
Definition: shader.cpp:434
std::string vert
Code for vertex shader.
Definition: shader.h:149
std::string geometry
Code for geometry file.
Definition: shader.h:155
Hjson::Value uniforms
Definition: shader.h:158
std::string frag
Code for fragment shader.
Definition: shader.h:152
AssetType GetAssetType() override
Definition: shader.h:162
Definition: shader.h:42
unsigned int id
Id of the shader.
Definition: shader.h:55
ShaderType shader_type
Definition: shader.h:52
~Shader()
Definition: shader.cpp:265
AssetType GetAssetType() override
Definition: shader.h:56
Shader(const Shader &)=default
void operator()(const std::string &code, ShaderType type)
Constructs a shader from the code provided.
Definition: shader.cpp:231
A shader program.
Definition: shader.h:68
~ShaderProgram()
Definition: shader.cpp:196
void SetMVP(const glm::mat4 &model, const glm::mat4 &view, const glm::mat4 &projection)
Definition: shader.cpp:174
void bindTexture(int texture, unsigned int texture_id)
Definition: shader.cpp:523
void setBool(const std::string &name, bool value)
Definition: shader.cpp:76
void setInt(const std::string &name, int value)
Definition: shader.cpp:80
void setVec4(const std::string &name, const glm::vec4 &value)
Definition: shader.cpp:104
void setVec3(const std::string &name, const glm::vec3 &value)
Definition: shader.cpp:96
void setMat4(const std::string &name, const glm::mat4 &mat)
Definition: shader.cpp:120
ShaderProgram()
Definition: shader.cpp:181
unsigned int program
Definition: shader.h:105
void UseProgram()
Definition: shader.cpp:172
void Set(const std::string &name, bool value)
Definition: shader.cpp:124
void setVec2(const std::string &name, const glm::vec2 &value)
Definition: shader.cpp:88
void setMat2(const std::string &name, const glm::mat2 &mat)
Definition: shader.cpp:112
void setFloat(const std::string &name, float value)
Definition: shader.cpp:84
void setMat3(const std::string &name, const glm::mat3 &mat)
Definition: shader.cpp:116
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: asset.h:29
std::string GetErrorLog(unsigned int shader)
Get the issues of the shader, up to 1024 characters of it.
Definition: shader.cpp:64
AssetType
Definition: asset.h:32
@ SHADER_DEFINITION
shader_def as the type parameter in resource.hjson
std::shared_ptr< ShaderProgram > ShaderProgram_t
The preferred way of using a shader program.
Definition: shader.h:113
unsigned int MakeShaderProgram(int vertex, int fragment, int geometry)
Definition: shader.cpp:53
@ SHADER
shader prototype
Definition: assetprototype.h:27
ShaderType
Definition: shader.h:31
@ VERT
Vertex shader.
@ FRAG
Fragment shader.
@ NONE
Invalid shader.
@ GEOM
Geometry shader.
unsigned int LoadShaderData(const std::string &code, int type)
Definition: shader.cpp:30