-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShaderProgram.cpp
More file actions
113 lines (88 loc) · 2.71 KB
/
Copy pathShaderProgram.cpp
File metadata and controls
113 lines (88 loc) · 2.71 KB
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
#include "ShaderProgram.h"
#include <glm/gtc/type_ptr.hpp>
ShaderProgram::ShaderProgram()
{
programId = 0;
linked = false;
}
void ShaderProgram::init()
{
programId = glCreateProgram();
}
void ShaderProgram::addShader(const Shader &shader)
{
glAttachShader(programId, shader.getId());
}
void ShaderProgram::bindFragmentOutput(const std::string &outputName)
{
glBindAttribLocation(programId, 0, outputName.c_str());
}
GLint ShaderProgram::bindVertexAttribute(const std::string &attribName, GLint size, GLsizei stride, GLvoid *firstPointer)
{
GLint attribPos;
attribPos = glGetAttribLocation(programId, attribName.c_str());
glVertexAttribPointer(attribPos, size, GL_FLOAT, GL_FALSE, stride, firstPointer);
return attribPos;
}
void ShaderProgram::link()
{
GLint status;
char buffer[512];
glLinkProgram(programId);
glGetProgramiv(programId, GL_LINK_STATUS, &status);
linked = (status == GL_TRUE);
glGetProgramInfoLog(programId, 512, NULL, buffer);
errorLog.assign(buffer);
}
void ShaderProgram::free()
{
glDeleteProgram(programId);
}
void ShaderProgram::use()
{
glUseProgram(programId);
}
bool ShaderProgram::isLinked()
{
return linked;
}
const std::string &ShaderProgram::log() const
{
return errorLog;
}
void ShaderProgram::setUniform1i(const std::string &uniformName, int v)
{
GLint location = glGetUniformLocation(programId, uniformName.c_str());
if (location != -1)
glUniform1i(location, v);
}
void ShaderProgram::setUniform2f(const std::string &uniformName, float v0, float v1)
{
GLint location = glGetUniformLocation(programId, uniformName.c_str());
if (location != -1)
glUniform2f(location, v0, v1);
}
void ShaderProgram::setUniform3f(const std::string &uniformName, float v0, float v1, float v2)
{
GLint location = glGetUniformLocation(programId, uniformName.c_str());
if (location != -1)
glUniform3f(location, v0, v1, v2);
}
void ShaderProgram::setUniform4f(const std::string &uniformName, float v0, float v1, float v2, float v3)
{
GLint location = glGetUniformLocation(programId, uniformName.c_str());
if (location != -1)
glUniform4f(location, v0, v1, v2, v3);
}
void ShaderProgram::setUniformMatrix3f(const std::string &uniformName, const glm::mat3 &mat)
{
GLint location = glGetUniformLocation(programId, uniformName.c_str());
if (location != -1)
glUniformMatrix3fv(location, 1, GL_FALSE, glm::value_ptr(mat));
}
void ShaderProgram::setUniformMatrix4f(const std::string &uniformName, const glm::mat4 &mat)
{
GLint location = glGetUniformLocation(programId, uniformName.c_str());
if (location != -1)
glUniformMatrix4fv(location, 1, GL_FALSE, glm::value_ptr(mat));
}