-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsk-gl-shaderprogram.cpp
More file actions
123 lines (99 loc) · 2.87 KB
/
msk-gl-shaderprogram.cpp
File metadata and controls
123 lines (99 loc) · 2.87 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
114
115
116
117
118
119
120
121
122
123
#include "stdafx.h"
#include "msk-gl-shaderprogram.h"
using namespace TexGen;
TgShaderProgram::TgShaderProgram() :
m_shaderCount(0),
m_program(glCreateProgram())
{
}
TgShaderProgram::~TgShaderProgram()
{
glDeleteProgram(m_program);
}
void TgShaderProgram::attachShader(const TgShader& shader)
{
glAttachShader(m_program, shader.getId());
// Increment the number of shaders we have associated with the program
m_shaderCount++;
}
void TgShaderProgram::link()
{
if (m_shaderCount >= 2) {
// Perform the linking process
glLinkProgram(m_program);
// Check the status
GLint linkStatus;
glGetProgramiv(m_program, GL_LINK_STATUS, &linkStatus);
if (linkStatus == GL_FALSE)
{
GLint maxLength = 0;
glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
char* infoLog = new char[maxLength];
glGetProgramInfoLog(m_program, maxLength, &maxLength, infoLog);
// Output error
OutputDebugString(infoLog);
// The program is useless now. So delete it.
glDeleteProgram(m_program);
std::cout << "Shader program linking failed." << std::endl;
}
else
{
std::cout << "Shader program linking OK." << std::endl;
}
}
else
{
std::cout << "Can't link shaders - you need at least 2, but attached shader count is only: " << m_shaderCount << std::endl;
}
}
void TgShaderProgram::use()
{
glUseProgram(m_program);
}
void TgShaderProgram::disable()
{
glUseProgram(0);
}
int TgShaderProgram::addAttribute(const std::string& name)
{
m_attributeLocations[name] = glGetAttribLocation(m_program, name.c_str());
return m_attributeLocations[name];
}
int TgShaderProgram::addUniform(const std::string& name)
{
m_uniformLocations[name] = glGetUniformLocation(m_program, name.c_str());
return m_uniformLocations[name];
}
void TgShaderProgram::setUniform(const std::string& name, float x, float y, float z)
{
glUniform3f(m_uniformLocations[name], x, y, z);
}
void TgShaderProgram::setUniform(const std::string& name, const glm::vec3 & v)
{
glUniform3fv(m_uniformLocations[name], 1, glm::value_ptr(v));
}
void TgShaderProgram::setUniform(const std::string& name, const glm::vec4 & v)
{
glUniform4fv(m_uniformLocations[name], 1, glm::value_ptr(v));
}
void TgShaderProgram::setUniform(const std::string& name, const glm::mat3 & m)
{
glUniformMatrix3fv(m_uniformLocations[name], 1, GL_FALSE, glm::value_ptr(m));
}
void TgShaderProgram::setUniform(const std::string& name, const glm::mat4 & m)
{
glUniformMatrix4fv(m_uniformLocations[name], 1, GL_FALSE, glm::value_ptr(m));
}
void TgShaderProgram::setUniform(const std::string& name, float value)
{
glUniform1f(m_uniformLocations[name], value);
}
void TgShaderProgram::setUniform(const std::string& name, int value)
{
glUniform1i(m_uniformLocations[name], value);
}
void TgShaderProgram::setUniform(const std::string& name, bool value)
{
glUniform1i(m_uniformLocations[name], value);
}