-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.h
62 lines (48 loc) · 2.03 KB
/
Program.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
#pragma once
#include <GL/glew.h>
#include <glm/mat4x4.hpp>
#include <unordered_map>
#include <fstream>
#include <sstream>
enum Shaders {
VERTEX = 1,
FRAGMENT = 2,
GEOMETRY = 4,
ALL = 7
};
class Program {
private:
unsigned int _hid;
std::unordered_map<std::string, unsigned int> _cache;
void _setFun(int loc) { assert(false); }
void _setFun(int loc, float a) { glUniform1f(loc, a); }
void _setFun(int loc, float a, float b) { glUniform2f(loc, a, b); }
void _setFun(int loc, float a, float b, float c) { glUniform3f(loc, a, b, c); }
void _setFun(int loc, float a, float b, float c, float d) { glUniform4f(loc, a, b, c, d); }
void _setFun(int loc, const glm::vec2& a) { glUniform2fv(loc, 1, &a[0]); }
void _setFun(int loc, const glm::vec3& a) { glUniform3fv(loc, 1, &a[0]); }
void _setFun(int loc, const glm::vec4& a) { glUniform4fv(loc, 1, &a[0]); }
void _setFun(int loc, bool a) { glUniform1i(loc, a); }
void _setFun(int loc, int a) { glUniform1i(loc, a); };
void _setFun(int loc, unsigned int a) { glUniform1i(loc, a); }
void _setFun(int loc, glm::mat4 a) { glUniformMatrix4fv(loc, 1, GL_FALSE, &a[0][0]); }
int getUniformLocation(const std::string& name);
public:
Program(unsigned int hid) : _hid(hid) {}
~Program();
template <typename ... Args> void setUniform(const std::string& name, Args&& ... args) { _setFun(getUniformLocation(name), std::forward<Args>(args)...); }
void bindUniformBlock(const std::string& name, unsigned int index);
void bind();
void unbind();
};
class ProgramIO {
private:
using shared_handle = std::shared_ptr<Program>;
public:
static unsigned int getType(unsigned int type);
static std::string getPath(const std::string& path, unsigned int type);
static std::string readFile(const std::string& filepath);
static unsigned int buildShader(const std::string& path, unsigned int shader);
static shared_handle buildProgram(const std::string& path, unsigned int shaders);
static shared_handle& getProgram(const std::string& path, unsigned int shaders = FRAGMENT | VERTEX);
};