-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShaderLib.h
63 lines (58 loc) · 3.79 KB
/
ShaderLib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#ifndef SHADERLIB_H
#define SHADERLIB_H
#include <vector>
#include <unordered_map>
#include <QOpenGLShaderProgram>
#include <memory>
class ShaderLib
{
public:
//-----------------------------------------------------------------------------------------------------
/// @brief Creates a shader program from a json file, by extracting the path of all required glsl
/// shaders for that program, compiling, attaching and linking them.
/// @param [in] _jsonFileName is the path to the json file.
/// @return is the name that this shader is stored under.
//-----------------------------------------------------------------------------------------------------
std::string loadShaderProg(const QString &_jsonFileName);
//-----------------------------------------------------------------------------------------------------
/// @brief Creates a shader program and loads a vertex and fragment shader, attaching both.
/// @param [in] _name is the name that this shader program should be stored under.
/// @param [in] _shaderPaths contains paths to the vertex, fragment, and geometry shaders in that order,
/// any paths left blank are ignored.
//-----------------------------------------------------------------------------------------------------
void createShader(const std::string &_name, const std::array<QString, 5> &_shaderPaths);
//-----------------------------------------------------------------------------------------------------
/// @brief Binds a stored shader.
/// @param [in] _name is the name of the shader program that should be bound.
//-----------------------------------------------------------------------------------------------------
void useShader(const std::string& _name);
//-----------------------------------------------------------------------------------------------------
/// @brief Accesses a stored shader program.
/// @param [in] _name is the name of the shader program should be accessed.
/// @return a pointer to the specified shader.
//-----------------------------------------------------------------------------------------------------
QOpenGLShaderProgram* getShader(const std::string& _name);
//-----------------------------------------------------------------------------------------------------
/// @brief Accesses the currently bound shader program.
/// @return a pointer to the currently bound shader program.
//-----------------------------------------------------------------------------------------------------
QOpenGLShaderProgram* getCurrentShader();
private:
std::string loadFileToString(const std::string &_path);
void parseIncludes(std::string &io_shaderString);
private:
enum SHADER_TYPES {VERTEX, FRAGMENT, GEOMETRY, TESSCONTROL, TESSEVAL};
//-----------------------------------------------------------------------------------------------------
/// @brief A map from shader name to shader program, so that they can be reused and easily bound.
//-----------------------------------------------------------------------------------------------------
std::unordered_map<std::string, std::unique_ptr<QOpenGLShaderProgram>> m_shaderPrograms;
//-----------------------------------------------------------------------------------------------------
/// @brief A map from shader name to shader, so that they can be reused by shader programs.
//-----------------------------------------------------------------------------------------------------
std::unordered_map<std::string, std::unique_ptr<QOpenGLShader>> m_shaderParts;
//-----------------------------------------------------------------------------------------------------
/// @brief A pointer to the currently bound shader program.
//-----------------------------------------------------------------------------------------------------
QOpenGLShaderProgram* m_currentShader;
};
#endif // SHADERLIB_H