5 #include "state_manager.h"
6 #include "opengl/shader-gl.h"
18 bool ShaderGL::create(char *buf, unsigned int bsz, const char *fname)
20 /* find shader type and create shader */
24 stype = GL_VERTEX_SHADER;
27 stype = GL_FRAGMENT_SHADER;
30 fprintf(stderr, "Unknown shader type.\n");
33 name = std::string(fname);
34 sdr = glCreateShader(stype);
37 glShaderSource(sdr, 1, (const char **)&buf, 0);
42 /* check compile status */
44 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
46 printf("Successfully compiled shader: %s\n", fname);
48 fprintf(stderr, "Failed to compile %s shader.\n", fname);
50 /* print the info log */
52 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &loglen);
53 if(loglen > 0 && (buf = new char[loglen + 1])) {
54 glGetShaderInfoLog(sdr, loglen, 0, buf);
69 void ShaderGL::destroy()
80 ShaderProgramGL::ShaderProgramGL()
83 memset(shaders, 0, sizeof shaders / sizeof *shaders);
88 ShaderProgramGL::~ShaderProgramGL()
93 bool ShaderProgramGL::create()
95 prog = glCreateProgram();
97 fprintf(stderr, "Failed to create shader program.\n");
103 bool ShaderProgramGL::link()
111 glGetProgramiv(prog, GL_LINK_STATUS, &status);
113 printf("Successfully linked shader program.\n");
117 printf("Failed to link shader program.\n");
120 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &loglen);
123 if(loglen > 0 && (buf = new char[loglen + 1])) {
124 glGetProgramInfoLog(prog, loglen, 0, buf);
130 return status ? true : false;
133 bool ShaderProgramGL::use() const
147 void ShaderProgramGL::destroy()
149 glDeleteProgram(prog);
154 void ShaderProgramGL::attach_shader(Shader *shader)
156 glAttachShader(prog, ((ShaderGL *)shader)->sdr);
160 int ShaderProgramGL::get_uniform_location(const char *name) const
165 return glGetUniformLocation(prog, name);
168 void ShaderProgramGL::set_uniformi(int location, int value)
170 if(!use() || location == -1) {
174 glUniform1i(location, value);
177 void ShaderProgramGL::set_uniformi(int location, int x, int y)
179 if(!use() || location == -1) {
183 glUniform2i(location, x, y);
186 void ShaderProgramGL::set_uniformi(int location, int x, int y, int z)
188 if(!use() || location == -1) {
192 glUniform3i(location, x, y, z);
195 void ShaderProgramGL::set_uniformi(int location, int x, int y, int z, int w)
197 if(!use() || location == -1) {
201 glUniform4i(location, x, y, z, w);
204 void ShaderProgramGL::set_uniformf(int location, float value)
206 if(!use() || location == -1) {
210 glUniform1f(location, value);
213 void ShaderProgramGL::set_uniformf(int location, float x, float y)
215 if(!use() || location == -1) {
219 glUniform2f(location, x, y);
222 void ShaderProgramGL::set_uniformf(int location, float x, float y, float z)
224 if(!use() || location == -1) {
228 glUniform3f(location, x, y, z);
231 void ShaderProgramGL::set_uniformf(int location, float x, float y, float z, float w)
233 if(!use() || location == -1) {
237 glUniform4f(location, x, y, z, w);
240 void ShaderProgramGL::set_uniform_matrix(int location, const Mat4 &mat)
242 if(!use() || location == -1) {
246 glUniformMatrix4fv(location, 1, GL_FALSE, mat[0]);