Skip to content

Instantly share code, notes, and snippets.

@StonedXander
Created October 21, 2012 17:42
Show Gist options
  • Save StonedXander/3927758 to your computer and use it in GitHub Desktop.
Save StonedXander/3927758 to your computer and use it in GitHub Desktop.
Shader support
#include <shader.hpp>
#include <iostream>
namespace Graphic {
Shader::Shader(const char *vs, const char *fs) {
vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShaderID, 1, &vs, NULL);
glCompileShader(vertexShaderID);
printLog(vertexShaderID);
fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderID, 1, &fs, NULL);
glCompileShader(fragmentShaderID);
printLog(fragmentShaderID);
shaderProgramID = glCreateProgram();
glAttachShader(shaderProgramID, vertexShaderID);
glAttachShader(shaderProgramID, fragmentShaderID);
glLinkProgram(shaderProgramID);
printLog(shaderProgramID);
}
GLint Shader::getUniformLocation(const char *loc) {
GLint result = glGetUniformLocation(shaderProgramID, loc);
return result;
}
Shader::~Shader() {
glDeleteShader(vertexShaderID);
glDeleteShader(fragmentShaderID);
glDeleteProgram(shaderProgramID);
}
void Shader::printLog(GLuint obj)
{
int infologLength = 0;
char infoLog[1024];
if (glIsShader(obj))
glGetShaderInfoLog(obj, 1024, &infologLength, infoLog);
else
glGetProgramInfoLog(obj, 1024, &infologLength, infoLog);
if (infologLength > 0)
std::cout << infoLog << std::endl;
}
}
#ifndef SHADER_HPP_
#define SHADER_HPP_
#include <GL/glew.h>
namespace Graphic {
/**
* Shader program support.
*/
class Shader {
private:
/**
* Vertex shader identifier.
*/
GLuint vertexShaderID;
/**
* Fragment shader identifier.
*/
GLuint fragmentShaderID;
/**
* Shader program identifier.
*/
GLuint shaderProgramID;
public:
/**
* Constructor.
* @param vs Vertex shader.
* @param fs Fragment shader.
*/
Shader(const char *vs, const char *fs);
/**
* Set the program to be used.
*/
inline void use() {
glUseProgram(shaderProgramID);
}
/**
* Provide access to a uniform variable.
* @param loc Variable location name.
* @return Variable identifier.
*/
GLint getUniformLocation(const char *loc);
~Shader();
private:
void printLog(GLuint);
};
}
#endif /* SHADER_HPP_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment