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
25#include <glm/glm.hpp>
26
27#include "engine/asset/asset.h"
28
29namespace cqsp {
30namespace 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
88 // Simpler overloaded functions so that you can just say set xxx and change the type
89 // as and when you like.
90 void Set(const std::string& name, bool value);
91 void Set(const std::string& name, int value);
92 void Set(const std::string& name, float value);
93 void Set(const std::string& name, const glm::vec2& value);
94 void Set(const std::string& name, float x, float y);
95 void Set(const std::string& name, const glm::vec3& value);
96 void Set(const std::string& name, float x, float y, float z);
97 void Set(const std::string& name, const glm::vec4& value);
98 void Set(const std::string& name, float x, float y, float z, float w);
99 void Set(const std::string& name, const glm::mat2& mat);
100 void Set(const std::string& name, const glm::mat3& mat);
101 void Set(const std::string& name, const glm::mat4& mat);
102
103 void UseProgram();
104 unsigned int program;
105
106 void SetMVP(const glm::mat4& model, const glm::mat4& view, const glm::mat4& projection);
107
108 operator unsigned int() const { return program; }
109};
110
112typedef std::shared_ptr<ShaderProgram> ShaderProgram_t;
113
116 return std::make_shared<ShaderProgram>(vert, frag);
117}
118
121 return std::make_shared<ShaderProgram>(vert, frag, geom);
122}
123
145class ShaderDefinition : public Asset {
146 public:
148 std::string vert;
149
151 std::string frag;
152
154 std::string geometry;
155
156 // All the uniforms
157 Hjson::Value uniforms;
158
160
162};
163
164// Set of utility functions that load shaders
165unsigned int LoadShaderData(const std::string& code, int type);
166unsigned int MakeShaderProgram(int vertex, int fragment, int geometry = -1);
167
173std::string GetErrorLog(unsigned int shader);
174} // namespace asset
175} // namespace cqsp
The base class for assets.
Definition: asset.h:157
Definition: shader.h:145
std::string vert
Code for vertex shader.
Definition: shader.h:148
std::string geometry
Code for geometry file.
Definition: shader.h:154
Hjson::Value uniforms
Definition: shader.h:157
std::string frag
Code for fragment shader.
Definition: shader.h:151
ShaderProgram_t MakeShader()
Definition: shader.cpp:435
AssetType GetAssetType() override
Definition: shader.h:161
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:264
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:230
A shader program.
Definition: shader.h:68
~ShaderProgram()
Definition: shader.cpp:195
void setMat2(const std::string &name, const glm::mat2 &mat)
Definition: shader.cpp:111
void setFloat(const std::string &name, float value)
Definition: shader.cpp:83
void setVec3(const std::string &name, const glm::vec3 &value)
Definition: shader.cpp:95
void setVec4(const std::string &name, const glm::vec4 &value)
Definition: shader.cpp:103
void setBool(const std::string &name, bool value)
Definition: shader.cpp:75
void setMat4(const std::string &name, const glm::mat4 &mat)
Definition: shader.cpp:119
unsigned int program
Definition: shader.h:104
void setVec2(const std::string &name, const glm::vec2 &value)
Definition: shader.cpp:87
void SetMVP(const glm::mat4 &model, const glm::mat4 &view, const glm::mat4 &projection)
Definition: shader.cpp:173
void setInt(const std::string &name, int value)
Definition: shader.cpp:79
void UseProgram()
Definition: shader.cpp:171
void Set(const std::string &name, bool value)
Definition: shader.cpp:123
void setMat3(const std::string &name, const glm::mat3 &mat)
Definition: shader.cpp:115
ShaderProgram()
Definition: shader.cpp:180
std::string GetErrorLog(unsigned int shader)
Get the issues of the shader, up to 1024 characters of it.
Definition: shader.cpp:61
AssetType
Definition: asset.h:31
@ 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:112
@ SHADER
shader prototype
Definition: assetprototype.h:27
ShaderType
Definition: shader.h:31
@ 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:115
unsigned int LoadShaderData(const std::string &code, int type)
Definition: shader.cpp:27
When adding assets, it is extremely crucial that you read cqsp::asset::AssetLoader::LoadResources to ...
Definition: clientctx.h:21