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-2023 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 {
31namespace asset {
32enum class ShaderType {
33 NONE,
34 VERT,
35 FRAG,
36 GEOM
37};
38
43class Shader : public Asset {
44 public:
45 Shader() = default;
47 Shader(const std::string& code, ShaderType type);
48
50 void operator()(const std::string& code, ShaderType type);
51 ~Shader();
52
54
56 unsigned int id;
58
59 private:
60 explicit Shader(const Shader&) = default;
61};
62
70 public:
72 ShaderProgram(const Shader& vert, const Shader& frag);
73 ShaderProgram(const Shader& vert, const Shader& frag, const Shader& geom);
75
76 void setBool(const std::string& name, bool value);
77 void setInt(const std::string& name, int value);
78 void setFloat(const std::string& name, float value);
79 void setVec2(const std::string& name, const glm::vec2& value);
80 void setVec2(const std::string& name, float x, float y);
81 void setVec3(const std::string& name, const glm::vec3& value);
82 void setVec3(const std::string& name, float x, float y, float z);
83 void setVec4(const std::string& name, const glm::vec4& value);
84 void setVec4(const std::string& name, float x, float y, float z, float w);
85 void setMat2(const std::string& name, const glm::mat2& mat);
86 void setMat3(const std::string& name, const glm::mat3& mat);
87 void setMat4(const std::string& name, const glm::mat4& mat);
88 void bindTexture(int texture, unsigned int texture_id);
89
90 // Simpler overloaded functions so that you can just say set xxx and change the type
91 // as and when you like.
92 void Set(const std::string& name, bool value);
93 void Set(const std::string& name, int value);
94 void Set(const std::string& name, float value);
95 void Set(const std::string& name, const glm::vec2& value);
96 void Set(const std::string& name, float x, float y);
97 void Set(const std::string& name, const glm::vec3& value);
98 void Set(const std::string& name, float x, float y, float z);
99 void Set(const std::string& name, const glm::vec4& value);
100 void Set(const std::string& name, float x, float y, float z, float w);
101 void Set(const std::string& name, const glm::mat2& mat);
102 void Set(const std::string& name, const glm::mat3& mat);
103 void Set(const std::string& name, const glm::mat4& mat);
104
105 void UseProgram();
106 unsigned int program;
107
108 void SetMVP(const glm::mat4& model, const glm::mat4& view, const glm::mat4& projection);
109
110 operator unsigned int() const { return program; }
111};
112
114typedef std::shared_ptr<ShaderProgram> ShaderProgram_t;
115
118 return std::make_shared<ShaderProgram>(vert, frag);
119}
120
123 return std::make_shared<ShaderProgram>(vert, frag, geom);
124}
125
147class ShaderDefinition : public Asset {
148 public:
150 std::string vert;
151
153 std::string frag;
154
156 std::string geometry;
157
158 // All the uniforms
159 Hjson::Value uniforms;
160
162
164
165 private:
166 void SetShaderUniform(cqsp::asset::ShaderProgram_t& shader, std::pair<const std::string, Hjson::Value>& value);
167};
168
169// Set of utility functions that load shaders
170unsigned int LoadShaderData(const std::string& code, int type);
171unsigned int MakeShaderProgram(int vertex, int fragment, int geometry = -1);
172
178std::string GetErrorLog(unsigned int shader);
179} // namespace asset
180} // namespace cqsp
The base class for assets.
Definition: asset.h:159
Definition: shader.h:147
std::string vert
Code for vertex shader.
Definition: shader.h:150
std::string geometry
Code for geometry file.
Definition: shader.h:156
Hjson::Value uniforms
Definition: shader.h:159
std::string frag
Code for fragment shader.
Definition: shader.h:153
void SetShaderUniform(cqsp::asset::ShaderProgram_t &shader, std::pair< const std::string, Hjson::Value > &value)
Definition: shader.cpp:437
ShaderProgram_t MakeShader()
Definition: shader.cpp:504
AssetType GetAssetType() override
Definition: shader.h:163
Definition: shader.h:43
unsigned int id
Id of the shader.
Definition: shader.h:56
ShaderType shader_type
Definition: shader.h:53
~Shader()
Definition: shader.cpp:266
AssetType GetAssetType() override
Definition: shader.h:57
Shader(const Shader &)=default
void operator()(const std::string &code, ShaderType type)
Constructs a shader from the code provided.
Definition: shader.cpp:232
A shader program.
Definition: shader.h:69
~ShaderProgram()
Definition: shader.cpp:197
void setMat2(const std::string &name, const glm::mat2 &mat)
Definition: shader.cpp:113
void setFloat(const std::string &name, float value)
Definition: shader.cpp:85
void bindTexture(int texture, unsigned int texture_id)
Definition: shader.cpp:527
void setVec3(const std::string &name, const glm::vec3 &value)
Definition: shader.cpp:97
void setVec4(const std::string &name, const glm::vec4 &value)
Definition: shader.cpp:105
void setBool(const std::string &name, bool value)
Definition: shader.cpp:77
void setMat4(const std::string &name, const glm::mat4 &mat)
Definition: shader.cpp:121
unsigned int program
Definition: shader.h:106
void setVec2(const std::string &name, const glm::vec2 &value)
Definition: shader.cpp:89
void SetMVP(const glm::mat4 &model, const glm::mat4 &view, const glm::mat4 &projection)
Definition: shader.cpp:175
void setInt(const std::string &name, int value)
Definition: shader.cpp:81
void UseProgram()
Definition: shader.cpp:173
void Set(const std::string &name, bool value)
Definition: shader.cpp:125
void setMat3(const std::string &name, const glm::mat3 &mat)
Definition: shader.cpp:117
ShaderProgram()
Definition: shader.cpp:182
std::string GetErrorLog(unsigned int shader)
Get the issues of the shader, up to 1024 characters of it.
Definition: shader.cpp:63
AssetType
Definition: asset.h:33
@ 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:114
@ SHADER
shader prototype
Definition: assetprototype.h:27
ShaderType
Definition: shader.h:32
@ VERT
Vertex shader.
@ FRAG
Fragment shader.
@ NONE
Invalid shader.
@ GEOM
Geometry shader.
ShaderProgram_t MakeShaderProgram(Shader &vert, Shader &frag)
Helper function to create a shader program.
Definition: shader.h:117
unsigned int LoadShaderData(const std::string &code, int type)
Definition: shader.cpp:29
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: clientctx.h:21