]> git.tdb.fi Git - libs/gl.git/blob - source/shader.cpp
Don't expose the shader type enum
[libs/gl.git] / source / shader.cpp
1 #include "arb_fragment_shader.h"
2 #include "arb_shader_objects.h"
3 #include "arb_vertex_shader.h"
4 #include "error.h"
5 #include "shader.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 Shader::Shader(GLenum t)
13 {
14         init(t);
15 }
16
17 Shader::Shader(GLenum t, const string &src)
18 {
19         init(t);
20
21         source(src);
22         compile();
23 }
24
25 void Shader::init(GLenum t)
26 {
27         compiled = false;
28
29         if(t==GL_FRAGMENT_SHADER)
30                 static Require _req(ARB_fragment_shader);
31         else if(t==GL_VERTEX_SHADER)
32                 static Require _req(ARB_vertex_shader);
33
34         id = glCreateShader(t);
35 }
36
37 Shader::~Shader()
38 {
39         glDeleteShader(id);
40 }
41
42 void Shader::source(unsigned count, const char **str, const int *len)
43 {
44         glShaderSource(id, count, str, len);
45 }
46
47 void Shader::source(const string &str)
48 {
49         source(str.data(), str.size());
50 }
51
52 void Shader::source(const char *str, int len)
53 {
54         source(1, &str, &len);
55 }
56
57 void Shader::compile()
58 {
59         glCompileShader(id);
60         int value = 0;
61         glGetShaderiv(id, GL_COMPILE_STATUS, &value);
62         if(!(compiled = value))
63                 throw compile_error(get_info_log());
64 }
65
66 string Shader::get_info_log() const
67 {
68         GLsizei len = 0;
69         glGetShaderiv(id, GL_INFO_LOG_LENGTH, &len);
70         char *buf = new char[len+1];
71         glGetShaderInfoLog(id, len+1, &len, buf);
72         string log(buf, len);
73         delete[] buf;
74         return log;
75 }
76
77
78 VertexShader::VertexShader():
79         Shader(GL_VERTEX_SHADER)
80 { }
81
82 VertexShader::VertexShader(const string &src):
83         Shader(GL_VERTEX_SHADER, src)
84 { }
85
86
87 FragmentShader::FragmentShader():
88         Shader(GL_FRAGMENT_SHADER)
89 { }
90
91 FragmentShader::FragmentShader(const string &src):
92         Shader(GL_FRAGMENT_SHADER, src)
93 { }
94
95 } // namespace GL
96 } // namespace Msp