]> git.tdb.fi Git - libs/gl.git/blob - source/shader.cpp
Drop Shader::get_param and Program::get_param
[libs/gl.git] / source / shader.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include "arb_shader_objects.h"
9 #include "except.h"
10 #include "extension.h"
11 #include "shader.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 Shader::Shader(ShaderType t)
19 {
20         init(t);
21 }
22
23 Shader::Shader(ShaderType t, const string &src)
24 {
25         init(t);
26
27         source(src);
28         compile();
29 }
30
31 void Shader::init(ShaderType t)
32 {
33         compiled = false;
34
35         if(t==FRAGMENT_SHADER)
36                 static RequireExtension _ext("GL_ARB_fragment_shader");
37         else if(t==VERTEX_SHADER)
38                 static RequireExtension _ext("GL_ARB_vertex_shader");
39
40         id = glCreateShaderObjectARB(t);
41 }
42
43 Shader::~Shader()
44 {
45         glDeleteObjectARB(id);
46 }
47
48 void Shader::source(unsigned count, const char **str, const int *len)
49 {
50         glShaderSourceARB(id, count, str, len);
51 }
52
53 void Shader::source(const string &str)
54 {
55         source(str.data(), str.size());
56 }
57
58 void Shader::source(const char *str, int len)
59 {
60         source(1, &str, &len);
61 }
62
63 void Shader::compile()
64 {
65         glCompileShaderARB(id);
66         int value = 0;
67         glGetObjectParameterivARB(id, GL_OBJECT_COMPILE_STATUS_ARB, &value);
68         if(!(compiled = value))
69                 throw CompileError(get_info_log());
70 }
71
72 string Shader::get_info_log() const
73 {
74         GLsizei len = 0;
75         glGetObjectParameterivARB(id, GL_OBJECT_INFO_LOG_LENGTH_ARB, &len);
76         char log[len+1];
77         glGetInfoLogARB(id, len+1, &len, log);
78         return string(log, len);
79 }
80
81 } // namespace GL
82 } // namespace Msp