24 Shader::Shader() : m_id(0), m_attributes(), m_uniforms(), m_shaders()
32 glShaderSource(shader, 1, &src, NULL);
33 glCompileShader(shader);
35 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
36 if (status == GL_FALSE) {
38 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
39 GLchar *info =
new GLchar[
length];
40 glGetShaderInfoLog(shader, length, NULL, info);
41 LOGE(
"Error compiling shader: %s\n", info);
50 for (
unsigned int i = 0; i < shaders.size(); ++i)
51 glAttachShader(program, shaders[i]);
53 glLinkProgram(program);
55 for (
unsigned int i = 0; i < shaders.size(); ++i)
56 glDetachShader(program, shaders[i]);
59 glGetProgramiv(program, GL_LINK_STATUS, &status);
60 if (status == GL_FALSE) {
62 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
63 GLchar *info =
new GLchar[
length];
64 glGetProgramInfoLog(program, length, NULL, info);
65 LOGE(
"Error linking program: %s\n", info);
74 m_id = glCreateProgram();
76 for (
int i = 0; i <
count; ++i)
78 GLuint shader = glCreateShader(types[i]);
88 string sources[] = { vs_src, fs_src };
89 GLenum types[] = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER };
95 m_id = glCreateProgram();
97 for (
int i = 0; i <
count; ++i)
99 GLuint shader = glCreateShader(types[i]);
112 string paths[] = { vs_path, fs_path };
113 GLenum types[] = { GL_VERTEX_SHADER, GL_FRAGMENT_SHADER };
126 for (
int i = 0; i <
m_shaders.size(); ++i)
128 glDeleteProgram(
m_id);
143 std::unordered_map<std::string, GLint>::iterator it =
m_uniforms.find(name);
150 GLint
location = glGetUniformLocation(
m_id, name.c_str());
151 string msg =
"Invalid shader uniform [" + name +
"]";
152 ASSERT(location >= 0, msg.c_str());
160 std::unordered_map<std::string, GLint>::iterator it =
m_attributes.find(name);
167 GLint location = glGetAttribLocation(
m_id, name.c_str());
168 string msg =
"Invalid shader attribute [" + name +
"]";
169 ASSERT(location >= 0, msg.c_str());
179 glEnableVertexAttribArray(loc);
180 glVertexAttribPointer(
186 (
void*)(offset *
sizeof(
GLfloat))
void set_attribfv(string name, GLsizei num_components, GLsizei stride, GLsizei offset)
bool load_from_file(const string *paths, GLenum *types, int count)
GLenum GLuint GLintptr offset
std::vector< GLuint > m_shaders
GLsizei const GLuint * paths
GLenum GLenum GLsizei count
const float * value_ptr() const
bool read_file(const std::string &path, std::string &dest)
bool link_program(GLuint program, std::vector< GLuint > shaders)
bool load_from_src(const string *sources, GLenum *types, int count)
bool compile_shader(GLuint shader, GLenum type, const char *source)
GLsizei GLsizei GLchar * source
std::unordered_map< string, GLint > m_attributes
void set_uniform(string name, const mat4 &v)
GLsizei GLenum GLenum * types
GLint get_attribute_location(string name)
GLint get_uniform_location(string name)
GLenum GLuint GLenum GLsizei length
const void GLsizei GLsizei stride
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
std::unordered_map< string, GLint > m_uniforms
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
void unset_attrib(string name)