5 #include "opengl/shader-gl.h"
17 bool ShaderGL::create(char *buf, unsigned int bsz, const char *fname)
19 /* find shader type and create shader */
23 stype = GL_VERTEX_SHADER;
26 stype = GL_FRAGMENT_SHADER;
29 fprintf(stderr, "Unknown shader type.\n");
32 name = std::string(fname);
33 sdr = glCreateShader(stype);
36 glShaderSource(sdr, 1, (const char **)&buf, 0);
41 /* check compile status */
43 glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
45 printf("Successfully compiled shader: %s\n", fname);
47 fprintf(stderr, "Failed to compile %s shader.\n", fname);
49 /* print the info log */
51 glGetShaderiv(sdr, GL_INFO_LOG_LENGTH, &loglen);
52 if(loglen > 0 && (buf = new char[loglen + 1])) {
53 glGetShaderInfoLog(sdr, loglen, 0, buf);
68 void ShaderGL::destroy()
79 ShaderProgramGL::ShaderProgramGL()
82 memset(shaders, 0, sizeof shaders / sizeof *shaders);
87 ShaderProgramGL::~ShaderProgramGL()
92 bool ShaderProgramGL::create()
94 prog = glCreateProgram();
96 fprintf(stderr, "Failed to create shader program.\n");
102 bool ShaderProgramGL::link()
110 glGetProgramiv(prog, GL_LINK_STATUS, &status);
112 printf("Successfully linked shader program.\n");
116 printf("Failed to link shader program.\n");
119 glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &loglen);
122 if(loglen > 0 && (buf = new char[loglen + 1])) {
123 glGetProgramInfoLog(prog, loglen, 0, buf);
129 return status ? true : false;
132 bool ShaderProgramGL::use() const
146 void ShaderProgramGL::destroy()
148 glDeleteProgram(prog);
153 void ShaderProgramGL::attach_shader(Shader *shader)
155 glAttachShader(prog, ((ShaderGL *)shader)->sdr);