-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shader.h
191 lines (160 loc) · 4.68 KB
/
Shader.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <string>
#include <vector>
#include <iostream>
#define PRAGMALIB
#ifndef GLEW
#define GLEW_STATIC
#include <GL/glew.h>
#ifdef PRAGMALIB
#pragma comment(lib, "glew32s.lib")
#pragma comment (lib, "OpenGL32.lib")
#pragma comment(lib, "GLU32.lib")
#endif
#endif
#include "Deletors.h"
#include "Util.h"
#include "RessourceHandler.h"
NSP_UTIL
NSP_STD
#pragma once
class Shader
{
friend class ShaderCode;
public: //Public structures
enum ShaderType
{
ERR,
VERTEX,
TESSELATION_CONTROL,
TESSELATION_EVALUATION,
GEOMETRY,
FRAGMENT
};
class Uniform
{
public:
Uniform() : _name("emptyUniform"), _data(nullptr), _size(0), pos(-1) {}
Uniform(string& name, unsigned int size) : _name(name), _data(new float[size]), _size(size), pos(-1) {}
Uniform(string& name, float*& data, unsigned size) : _name(name), _data(new float[size]), _size(0), pos(-1)
{
memcpy(_data.get(), data, size * sizeof(float));
_size = size;
}
Uniform(const Uniform& other) :
_name(other._name),
_size(other._size),
pos(other.pos),
_data(new float[other._size])
{
memcpy(_data.get(), other._data.get(), other._size * sizeof(float));
}
Uniform& operator=(const Uniform& other)
{
_name = other._name;
_size = other._size;
pos = other.pos;
_data.reset(new float[other._size]);
memcpy(_data.get(), other._data.get(), other._size * sizeof(float));
return *this;
}
void create(GLuint& prgm);
void copy(Uniform& other);
void write(GLuint& other);
string& getName() { return _name; }
bool operator==(const Uniform& other) { return _name == other._name; }
bool operator==(const string& name) { return _name == name; }
bool operator!=(const Uniform& other) { return _name != other._name; }
bool operator!=(const string& name) { return _name != name; }
bool operator< (const Uniform& other) { return _name.compare(other._name) < 0; }
bool operator< (const string& name) { return _name.compare(name) < 0; }
bool operator> (const Uniform& other) { return _name.compare(other._name) > 0; }
bool operator> (const string& name) { return _name.compare(name) > 0; }
static int getUniformSize(string& name);
void set(float *val)
{
memcpy(_data.get(),val, _size * sizeof(float));
}
float* getPtr()
{
return _data.get();
}
~Uniform()
{
_size = 0;
}
private:
string _name;
unique_ptr<float[]> _data;
unsigned _size;
GLuint pos;
};
class ShaderCode : public RessourceLoader
{
string _code;
unique_ptr<GLuint, deleteGLShader> _pos;
ShaderType _type;
string _path;
Shader* _owner;
string _loadingErr;
vector<Uniform> uniforms;
public:
//ShaderCode() : _owner(), _type(TESSELATION_EVALUATION), _path(""), _pos( new GLuint(), deleteGLShader()) {}
ShaderCode(Shader* owner, ShaderType type, string& path) : _owner(owner), _type(type), _path(path), _pos( new GLuint(-1), deleteGLShader()) {}
ShaderCode(Shader* owner, ShaderType type, char* path) : _owner(owner), _type(type), _path(path), _pos( new GLuint(-1), deleteGLShader()) {}
ShaderCode(const ShaderCode& other) :
_code(other._code),
_type(other._type),
_path(other._path),
_owner(other._owner),
_loadingErr(other._loadingErr),
uniforms(other.uniforms),
_pos(new GLuint(-1), deleteGLShader())
{
}
void load(ifstream&);
void* get();
void makeShader();
GLuint& getPos() { return *_pos; }
string& getPath() { return _path; }
vector<Uniform>& getUniforms() { return uniforms; }
ShaderCode& operator=(const ShaderCode& other);
bool operator==(const ShaderCode& other) { return _type == other._type && _path == other._path && _pos == other._pos; }
bool operator!=(const ShaderCode& other) { return !(*this == other); }
private:
GLenum getShaderType(ShaderType);
vector<vector<string>> structVariables;
void addUniform(string&);
vector<string> resolveStructVariable(string&);
};
public:
//construction / destruction
Shader();
Shader(string&, string&);
~Shader();
//getter / setter
void addShader(ShaderCode&);
float* getUniformMemPos(string);
//functions
void load(RessourceHandler& loader);
void build();
void bind();
void setUniform(const string&, float*);
void setUniforms();
//operators
Shader& operator= (const Shader&);
Shader& operator= (const vector<ShaderCode>&);
bool operator== (const Shader&);
bool operator== (const ShaderCode&);
bool operator== (const vector<ShaderCode>&);
private:
//variables
//OpenGL
unique_ptr<GLuint, deleteGLProgram> program;
vector<ShaderCode> Code; //contains Shader variable
vector<Uniform> Uniforms;
vector<RessourceHandler::Ressource> reqArr;
//functions
string checkProgram();
int findUniform(const Uniform&, int, int);
int findUniform(const string&, int, int);
};