12 #include <glm/gtc/type_ptr.hpp> 25 Shader::~Shader(
void) {
26 glDeleteShader(handle);
29 GLuint Shader::getHandle() {
33 void Shader::source(std::string source) {
34 const char* c_str = source.c_str();
35 glShaderSource(handle, 1, &c_str,
nullptr);
38 void Shader::compile() {
39 glCompileShader(handle);
42 bool Shader::getCompileStatus() {
44 glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
45 return status == GL_TRUE;
48 std::string Shader::getInfoLog() {
50 glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logSize);
51 vector<char> message(logSize);
52 glGetShaderInfoLog(handle, logSize, NULL, message.data());
53 return string(message.data());
58 ShaderProgram::ShaderProgram(
void) {
59 handle = glCreateProgram();
62 ShaderProgram::~ShaderProgram(
void) {
63 glDeleteProgram(handle);
66 if (ShaderProgram::current ==
this) {
67 ShaderProgram::current =
nullptr;
71 GLuint ShaderProgram::getHandle() {
75 void ShaderProgram::attach(
Shader* shader) {
76 glAttachShader(handle, shader->
getHandle());
79 void ShaderProgram::link() {
80 glLinkProgram(handle);
83 void ShaderProgram::use() {
85 if (ShaderProgram::current ==
this) {
91 ShaderProgram::current =
this;
94 bool ShaderProgram::getLinkStatus() {
96 glGetProgramiv(handle, GL_LINK_STATUS, &status);
97 return status == GL_TRUE;
100 std::string ShaderProgram::getInfoLog() {
102 glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &logSize);
103 vector<char> message(logSize);
104 glGetProgramInfoLog(handle, logSize, NULL, message.data());
105 return string(message.data());
108 void ShaderProgram::bindFragmentDataLocation(
unsigned int colorNumber, std::string name) {
109 glBindFragDataLocation(handle, colorNumber, name.c_str());
112 int ShaderProgram::getAttributeLocation(std::string name) {
113 return glGetAttribLocation(handle, name.c_str());
116 int ShaderProgram::getUniformLocation(std::string name) {
117 return glGetUniformLocation(handle, name.c_str());
120 void ShaderProgram::setUniform(
int location,
int value) {
121 glUniform1i(location, value);
124 void ShaderProgram::setUniform(
int location,
float value) {
125 glUniform1f(location, value);
128 void ShaderProgram::setUniform(
int location, glm::vec2 value) {
129 glUniform2fv(location, 1, value_ptr(value));
132 void ShaderProgram::setUniform(
int location, glm::vec3 value) {
133 glUniform3fv(location, 1, value_ptr(value));
136 void ShaderProgram::setUniform(
int location, glm::vec4 value) {
137 glUniform4fv(location, 1, value_ptr(value));
140 void ShaderProgram::setUniform(
int location, glm::mat2 value) {
141 glUniformMatrix2fv(location, 1, GL_FALSE, value_ptr(value));
144 void ShaderProgram::setUniform(
int location, glm::mat3 value) {
145 glUniformMatrix3fv(location, 1, GL_FALSE, value_ptr(value));
148 void ShaderProgram::setUniform(
int location, glm::mat4 value) {
149 glUniformMatrix4fv(location, 1, GL_FALSE, value_ptr(value));
152 void ShaderProgram::setUniform(std::string name,
int value) {
153 int location = getUniformLocation(name);
154 setUniform(location, value);
157 void ShaderProgram::setUniform(std::string name,
float value) {
158 int location = getUniformLocation(name);
159 setUniform(location, value);
162 void ShaderProgram::setUniform(std::string name, glm::vec2 value) {
163 int location = getUniformLocation(name);
164 setUniform(location, value);
167 void ShaderProgram::setUniform(std::string name, glm::vec3 value) {
168 int location = getUniformLocation(name);
169 setUniform(location, value);
172 void ShaderProgram::setUniform(std::string name, glm::vec4 value) {
173 int location = getUniformLocation(name);
174 setUniform(location, value);
177 void ShaderProgram::setUniform(std::string name, glm::mat2 value) {
178 int location = getUniformLocation(name);
179 setUniform(location, value);
182 void ShaderProgram::setUniform(std::string name, glm::mat3 value) {
183 int location = getUniformLocation(name);
184 setUniform(location, value);
187 void ShaderProgram::setUniform(std::string name, glm::mat4 value) {
188 int location = getUniformLocation(name);
189 setUniform(location, value);
This class wraps an OpenGL shader object.
ShaderType
This enum wraps the shader types.
GLenum to_GLenum(E e)
Gets the GLenum value from an enum class.
This class wraps an OpenGL shader program.
Generic namespace for the SimpleGL framework.
GLuint getHandle()
Returns the handle of the shader object.