-
Notifications
You must be signed in to change notification settings - Fork 2
/
OpenGLShaderProgram.cpp
98 lines (78 loc) · 2.1 KB
/
OpenGLShaderProgram.cpp
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
#include "OpenGLShaderProgram.h"
#include "OpenGLUtils.h" // TODO Remove?
#include <iostream>
using namespace std;
using namespace dawn;
OpenGLShaderProgram::OpenGLShaderProgram() : program(0)
{
}
OpenGLShaderProgram::OpenGLShaderProgram(const std::string &vs, const std::string &fs) : program(OpenGLUtils::CreateShaderProgram(vs, fs))
{
}
OpenGLShaderProgram::OpenGLShaderProgram(GLuint program) : program(program)
{
}
OpenGLShaderProgram::~OpenGLShaderProgram()
{
if (program)
glDeleteProgram(program);
}
void OpenGLShaderProgram::Bind()
{
glUseProgram(program);
}
void OpenGLShaderProgram::uniform(const std::string &name, GLint v0)
{
GLint loc = GetUniformLocation(name);
glUniform1i(loc, v0);
}
void OpenGLShaderProgram::uniform(const std::string &name, GLfloat v0)
{
GLint loc = GetUniformLocation(name);
glUniform1f(loc, v0);
}
void OpenGLShaderProgram::uniform(const std::string &name, vec2f v)
{
GLint loc = GetUniformLocation(name);
glUniform2f(loc, v[0], v[1]);
}
void OpenGLShaderProgram::uniform(const std::string &name, vec3f v)
{
GLint loc = GetUniformLocation(name);
glUniform3f(loc, v[0], v[1], v[2]);
}
void OpenGLShaderProgram::uniform(const std::string &name, vec4f v)
{
GLint loc = GetUniformLocation(name);
glUniform4f(loc, v[0], v[1], v[2], v[3]);
}
void OpenGLShaderProgram::uniform(const std::string &name, mat3f m)
{
GLint loc = GetUniformLocation(name);
GLfloat fv[9];
for (unsigned int i = 0; i < 9; i++) {
fv[i] = m(i);
}
glUniformMatrix3fv(loc, 1, GL_FALSE, fv);
}
void OpenGLShaderProgram::uniform(const std::string &name, mat4f m)
{
GLint loc = GetUniformLocation(name);
GLfloat fv[16];
for (unsigned int i = 0; i < 16; i++) {
fv[i] = m(i);
}
glUniformMatrix4fv(loc, 1, GL_FALSE, fv);
}
GLint OpenGLShaderProgram::GetUniformLocation(const string &name)
{
UniformLUTMap::iterator itr = uniformLUT.find(name);
if (itr == uniformLUT.end())
{
std::cout << "Looking up uniform " << name << std::endl;
GLint loc = glGetUniformLocation(program, name.c_str());
uniformLUT[name] = loc;
return loc;
}
return itr->second;
}