]> git.tdb.fi Git - libs/gl.git/blob - source/shader.cpp
Get rid of the typedefs for fundamental types
[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         if(!(compiled=get_param(GL_COMPILE_STATUS)))
67                 throw CompileError(get_info_log());
68 }
69
70 int Shader::get_param(GLenum param) const
71 {
72         int value;
73         glGetObjectParameterivARB(id, param, &value);
74         return value;
75 }
76
77 string Shader::get_info_log() const
78 {
79         GLsizei len=get_param(GL_INFO_LOG_LENGTH);
80         char log[len+1];
81         glGetInfoLogARB(id, len+1, &len, log);
82         return string(log, len);
83 }
84
85 } // namespace GL
86 } // namespace Msp