rays 0.1.12 → 0.1.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +5 -5
  2. data/.doc/ext/rays/bitmap.cpp +22 -76
  3. data/.doc/ext/rays/bounds.cpp +95 -125
  4. data/.doc/ext/rays/camera.cpp +171 -0
  5. data/.doc/ext/rays/color.cpp +223 -45
  6. data/.doc/ext/rays/color_space.cpp +146 -46
  7. data/.doc/ext/rays/defs.cpp +183 -0
  8. data/.doc/ext/rays/font.cpp +69 -21
  9. data/.doc/ext/rays/image.cpp +26 -37
  10. data/.doc/ext/rays/matrix.cpp +186 -29
  11. data/.doc/ext/rays/native.cpp +14 -8
  12. data/.doc/ext/rays/noise.cpp +53 -0
  13. data/.doc/ext/rays/painter.cpp +187 -292
  14. data/.doc/ext/rays/point.cpp +96 -77
  15. data/.doc/ext/rays/polygon.cpp +313 -0
  16. data/.doc/ext/rays/polygon_line.cpp +96 -0
  17. data/.doc/ext/rays/polyline.cpp +167 -0
  18. data/.doc/ext/rays/rays.cpp +103 -12
  19. data/.doc/ext/rays/shader.cpp +83 -9
  20. data/LICENSE +21 -0
  21. data/README.md +1 -1
  22. data/Rakefile +24 -9
  23. data/VERSION +1 -1
  24. data/ext/rays/bitmap.cpp +23 -81
  25. data/ext/rays/bounds.cpp +100 -128
  26. data/ext/rays/camera.cpp +186 -0
  27. data/ext/rays/color.cpp +231 -51
  28. data/ext/rays/color_space.cpp +149 -47
  29. data/ext/rays/defs.cpp +183 -0
  30. data/ext/rays/defs.h +26 -2
  31. data/ext/rays/extconf.rb +2 -3
  32. data/ext/rays/font.cpp +74 -24
  33. data/ext/rays/image.cpp +28 -40
  34. data/ext/rays/matrix.cpp +198 -30
  35. data/ext/rays/native.cpp +14 -8
  36. data/ext/rays/noise.cpp +55 -0
  37. data/ext/rays/painter.cpp +203 -298
  38. data/ext/rays/point.cpp +105 -81
  39. data/ext/rays/polygon.cpp +329 -0
  40. data/ext/rays/polygon_line.cpp +99 -0
  41. data/ext/rays/polyline.cpp +176 -0
  42. data/ext/rays/rays.cpp +103 -13
  43. data/ext/rays/shader.cpp +84 -9
  44. data/include/rays.h +10 -2
  45. data/include/rays/bitmap.h +14 -26
  46. data/include/rays/bounds.h +21 -4
  47. data/include/rays/camera.h +74 -0
  48. data/include/rays/color.h +25 -14
  49. data/include/rays/color_space.h +15 -10
  50. data/include/rays/coord.h +114 -0
  51. data/include/rays/debug.h +22 -0
  52. data/include/rays/defs.h +36 -0
  53. data/include/rays/exception.h +6 -2
  54. data/include/rays/font.h +4 -4
  55. data/include/rays/image.h +12 -18
  56. data/include/rays/matrix.h +50 -24
  57. data/include/rays/noise.h +42 -0
  58. data/include/rays/opengl.h +2 -50
  59. data/include/rays/painter.h +89 -93
  60. data/include/rays/point.h +44 -51
  61. data/include/rays/polygon.h +198 -0
  62. data/include/rays/polyline.h +71 -0
  63. data/include/rays/rays.h +3 -0
  64. data/include/rays/ruby.h +7 -1
  65. data/include/rays/ruby/bounds.h +1 -1
  66. data/include/rays/ruby/camera.h +41 -0
  67. data/include/rays/ruby/color.h +1 -1
  68. data/include/rays/ruby/color_space.h +1 -1
  69. data/include/rays/ruby/font.h +1 -1
  70. data/include/rays/ruby/matrix.h +1 -1
  71. data/include/rays/ruby/point.h +1 -1
  72. data/include/rays/ruby/polygon.h +52 -0
  73. data/include/rays/ruby/polyline.h +41 -0
  74. data/include/rays/ruby/rays.h +8 -0
  75. data/include/rays/ruby/shader.h +1 -1
  76. data/include/rays/shader.h +36 -8
  77. data/lib/rays.rb +7 -2
  78. data/lib/rays/bitmap.rb +0 -15
  79. data/lib/rays/bounds.rb +17 -23
  80. data/lib/rays/camera.rb +24 -0
  81. data/lib/rays/color.rb +20 -47
  82. data/lib/rays/color_space.rb +13 -13
  83. data/lib/rays/image.rb +3 -7
  84. data/lib/rays/matrix.rb +28 -0
  85. data/lib/rays/module.rb +4 -19
  86. data/lib/rays/painter.rb +78 -93
  87. data/lib/rays/point.rb +13 -21
  88. data/lib/rays/polygon.rb +58 -0
  89. data/lib/rays/polygon_line.rb +36 -0
  90. data/lib/rays/polyline.rb +32 -0
  91. data/lib/rays/shader.rb +20 -1
  92. data/rays.gemspec +5 -7
  93. data/src/bitmap.h +36 -0
  94. data/src/bounds.cpp +74 -11
  95. data/src/color.cpp +58 -23
  96. data/src/color_space.cpp +52 -34
  97. data/src/color_space.h +22 -0
  98. data/src/coord.cpp +170 -0
  99. data/src/coord.h +35 -0
  100. data/src/font.cpp +118 -0
  101. data/src/font.h +64 -0
  102. data/src/frame_buffer.cpp +37 -71
  103. data/src/frame_buffer.h +4 -4
  104. data/src/image.cpp +172 -98
  105. data/src/image.h +25 -0
  106. data/src/ios/bitmap.h +23 -0
  107. data/src/ios/bitmap.mm +133 -110
  108. data/src/ios/camera.mm +510 -0
  109. data/src/ios/font.mm +50 -62
  110. data/src/ios/helper.h +4 -4
  111. data/src/ios/opengl.mm +19 -4
  112. data/src/ios/rays.mm +3 -0
  113. data/src/matrix.cpp +111 -26
  114. data/src/matrix.h +30 -0
  115. data/src/noise.cpp +74 -0
  116. data/src/opengl.cpp +9 -27
  117. data/src/opengl.h +37 -0
  118. data/src/osx/bitmap.h +23 -0
  119. data/src/osx/bitmap.mm +133 -110
  120. data/src/osx/camera.mm +451 -0
  121. data/src/osx/font.mm +49 -62
  122. data/src/osx/helper.h +2 -2
  123. data/src/osx/opengl.mm +19 -83
  124. data/src/osx/rays.mm +3 -0
  125. data/src/painter.cpp +845 -671
  126. data/src/painter.h +24 -0
  127. data/src/point.cpp +140 -119
  128. data/src/polygon.cpp +1266 -0
  129. data/src/polygon.h +32 -0
  130. data/src/polyline.cpp +160 -0
  131. data/src/polyline.h +69 -0
  132. data/src/render_buffer.cpp +11 -4
  133. data/src/render_buffer.h +2 -2
  134. data/src/shader.cpp +163 -106
  135. data/src/shader.h +38 -0
  136. data/src/shader_program.cpp +533 -0
  137. data/src/{program.h → shader_program.h} +28 -16
  138. data/src/shader_source.cpp +140 -0
  139. data/src/shader_source.h +52 -0
  140. data/src/texture.cpp +136 -160
  141. data/src/texture.h +65 -0
  142. data/src/win32/bitmap.cpp +62 -52
  143. data/src/win32/font.cpp +11 -13
  144. data/src/win32/font.h +24 -0
  145. data/src/win32/gdi.h +6 -6
  146. data/test/helper.rb +0 -3
  147. data/test/test_bitmap.rb +31 -7
  148. data/test/test_bounds.rb +36 -0
  149. data/test/test_color.rb +59 -19
  150. data/test/test_color_space.rb +95 -0
  151. data/test/test_font.rb +5 -0
  152. data/test/test_image.rb +24 -20
  153. data/test/test_matrix.rb +106 -0
  154. data/test/test_painter.rb +157 -51
  155. data/test/test_painter_shape.rb +102 -0
  156. data/test/test_point.rb +29 -0
  157. data/test/test_polygon.rb +234 -0
  158. data/test/test_polygon_line.rb +167 -0
  159. data/test/test_polyline.rb +171 -0
  160. data/test/test_shader.rb +9 -9
  161. metadata +102 -70
  162. data/.doc/ext/rays/texture.cpp +0 -138
  163. data/ext/rays/texture.cpp +0 -149
  164. data/include/rays/ruby/texture.h +0 -41
  165. data/include/rays/texture.h +0 -71
  166. data/lib/rays/texture.rb +0 -24
  167. data/src/program.cpp +0 -648
  168. data/test/test_texture.rb +0 -27
@@ -1,138 +0,0 @@
1
- #include "rays/ruby/texture.h"
2
-
3
-
4
- #include <rucy.h>
5
- #include "rays/ruby/color_space.h"
6
- #include "rays/ruby/bitmap.h"
7
- #include "defs.h"
8
-
9
-
10
- using namespace Rucy;
11
-
12
- using Rays::coord;
13
-
14
-
15
- RUCY_DEFINE_VALUE_FROM_TO(Rays::Texture)
16
-
17
- #define THIS to<Rays::Texture*>(self)
18
-
19
- #define CHECK RUCY_CHECK_OBJECT(Rays::Texture, self)
20
-
21
-
22
- static
23
- VALUE alloc(VALUE klass)
24
- {
25
- return new_type<Rays::Texture>(klass);
26
- }
27
-
28
- static
29
- VALUE setup(VALUE self, VALUE width, VALUE height, VALUE color_space, VALUE alpha_only)
30
- {
31
- RUCY_CHECK_OBJ(Rays::Texture, self);
32
-
33
- Rays::ColorSpace* cs = to<Rays::ColorSpace*>(color_space);
34
- if (!cs) argument_error(__FILE__, __LINE__);
35
-
36
- *THIS = Rays::Texture(
37
- to<int>(width), to<int>(height), *cs, to<bool>(alpha_only));
38
- return self;
39
- }
40
-
41
- static
42
- VALUE width(VALUE self)
43
- {
44
- CHECK;
45
- return value(THIS->width());
46
- }
47
-
48
- static
49
- VALUE height(VALUE self)
50
- {
51
- CHECK;
52
- return value(THIS->height());
53
- }
54
-
55
- static
56
- VALUE color_space(VALUE self)
57
- {
58
- CHECK;
59
- return value(THIS->color_space());
60
- }
61
-
62
- static
63
- VALUE alpha_only(VALUE self)
64
- {
65
- CHECK;
66
- return value(THIS->alpha_only());
67
- }
68
-
69
- static
70
- VALUE s(VALUE self, VALUE x)
71
- {
72
- CHECK;
73
- return value(THIS->s(x.as_f(true)));
74
- }
75
-
76
- static
77
- VALUE t(VALUE self, VALUE y)
78
- {
79
- CHECK;
80
- return value(THIS->t(y.as_f(true)));
81
- }
82
-
83
- static
84
- VALUE s_max(VALUE self)
85
- {
86
- CHECK;
87
- return value(THIS->s_max());
88
- }
89
-
90
- static
91
- VALUE t_max(VALUE self)
92
- {
93
- CHECK;
94
- return value(THIS->t_max());
95
- }
96
-
97
- static
98
- VALUE to_bitmap(VALUE self)
99
- {
100
- CHECK;
101
- return value(Rays::Bitmap(*THIS));
102
- }
103
-
104
-
105
- static Class cTexture;
106
-
107
- void
108
- Init_texture ()
109
- {
110
- Module mRays = rb_define_module("Rays");
111
-
112
- cTexture = rb_define_class_under(mRays, "Texture", rb_cObject);
113
- rb_define_alloc_func(cTexture, alloc);
114
- rb_define_private_method(cTexture, "setup", RUBY_METHOD_FUNC(setup), 4);
115
- rb_define_method(cTexture, "width", RUBY_METHOD_FUNC(width), 0);
116
- rb_define_method(cTexture, "height", RUBY_METHOD_FUNC(height), 0);
117
- rb_define_method(cTexture, "color_space", RUBY_METHOD_FUNC(color_space), 0);
118
- rb_define_method(cTexture, "alpha_only", RUBY_METHOD_FUNC(alpha_only), 0);
119
- rb_define_method(cTexture, "s", RUBY_METHOD_FUNC(s), 1);
120
- rb_define_method(cTexture, "t", RUBY_METHOD_FUNC(t), 1);
121
- rb_define_method(cTexture, "s_max", RUBY_METHOD_FUNC(s_max), 0);
122
- rb_define_method(cTexture, "t_max", RUBY_METHOD_FUNC(t_max), 0);
123
- rb_define_method(cTexture, "to_bitmap", RUBY_METHOD_FUNC(to_bitmap), 0);
124
- }
125
-
126
-
127
- namespace Rays
128
- {
129
-
130
-
131
- Class
132
- texture_class ()
133
- {
134
- return cTexture;
135
- }
136
-
137
-
138
- }// Rays
@@ -1,149 +0,0 @@
1
- #include "rays/ruby/texture.h"
2
-
3
-
4
- #include <rucy.h>
5
- #include "rays/ruby/color_space.h"
6
- #include "rays/ruby/bitmap.h"
7
- #include "defs.h"
8
-
9
-
10
- using namespace Rucy;
11
-
12
- using Rays::coord;
13
-
14
-
15
- RUCY_DEFINE_VALUE_FROM_TO(Rays::Texture)
16
-
17
- #define THIS to<Rays::Texture*>(self)
18
-
19
- #define CHECK RUCY_CHECK_OBJECT(Rays::Texture, self)
20
-
21
-
22
- static
23
- RUCY_DEF_ALLOC(alloc, klass)
24
- {
25
- return new_type<Rays::Texture>(klass);
26
- }
27
- RUCY_END
28
-
29
- static
30
- RUCY_DEF4(setup, width, height, color_space, alpha_only)
31
- {
32
- RUCY_CHECK_OBJ(Rays::Texture, self);
33
-
34
- Rays::ColorSpace* cs = to<Rays::ColorSpace*>(color_space);
35
- if (!cs) argument_error(__FILE__, __LINE__);
36
-
37
- *THIS = Rays::Texture(
38
- to<int>(width), to<int>(height), *cs, to<bool>(alpha_only));
39
- return self;
40
- }
41
- RUCY_END
42
-
43
- static
44
- RUCY_DEF0(width)
45
- {
46
- CHECK;
47
- return value(THIS->width());
48
- }
49
- RUCY_END
50
-
51
- static
52
- RUCY_DEF0(height)
53
- {
54
- CHECK;
55
- return value(THIS->height());
56
- }
57
- RUCY_END
58
-
59
- static
60
- RUCY_DEF0(color_space)
61
- {
62
- CHECK;
63
- return value(THIS->color_space());
64
- }
65
- RUCY_END
66
-
67
- static
68
- RUCY_DEF0(alpha_only)
69
- {
70
- CHECK;
71
- return value(THIS->alpha_only());
72
- }
73
- RUCY_END
74
-
75
- static
76
- RUCY_DEF1(s, x)
77
- {
78
- CHECK;
79
- return value(THIS->s(x.as_f(true)));
80
- }
81
- RUCY_END
82
-
83
- static
84
- RUCY_DEF1(t, y)
85
- {
86
- CHECK;
87
- return value(THIS->t(y.as_f(true)));
88
- }
89
- RUCY_END
90
-
91
- static
92
- RUCY_DEF0(s_max)
93
- {
94
- CHECK;
95
- return value(THIS->s_max());
96
- }
97
- RUCY_END
98
-
99
- static
100
- RUCY_DEF0(t_max)
101
- {
102
- CHECK;
103
- return value(THIS->t_max());
104
- }
105
- RUCY_END
106
-
107
- static
108
- RUCY_DEF0(to_bitmap)
109
- {
110
- CHECK;
111
- return value(Rays::Bitmap(*THIS));
112
- }
113
- RUCY_END
114
-
115
-
116
- static Class cTexture;
117
-
118
- void
119
- Init_texture ()
120
- {
121
- Module mRays = define_module("Rays");
122
-
123
- cTexture = mRays.define_class("Texture");
124
- cTexture.define_alloc_func(alloc);
125
- cTexture.define_private_method("setup", setup);
126
- cTexture.define_method("width", width);
127
- cTexture.define_method("height", height);
128
- cTexture.define_method("color_space", color_space);
129
- cTexture.define_method("alpha_only", alpha_only);
130
- cTexture.define_method("s", s);
131
- cTexture.define_method("t", t);
132
- cTexture.define_method("s_max", s_max);
133
- cTexture.define_method("t_max", t_max);
134
- cTexture.define_method("to_bitmap", to_bitmap);
135
- }
136
-
137
-
138
- namespace Rays
139
- {
140
-
141
-
142
- Class
143
- texture_class ()
144
- {
145
- return cTexture;
146
- }
147
-
148
-
149
- }// Rays
@@ -1,41 +0,0 @@
1
- // -*- c++ -*-
2
- #pragma once
3
- #ifndef __RAYS_RUBY_TEXTURE_H__
4
- #define __RAYS_RUBY_TEXTURE_H__
5
-
6
-
7
- #include <rucy/rucy.h>
8
- #include <rucy/class.h>
9
- #include <rucy/extension.h>
10
- #include <rays/texture.h>
11
-
12
-
13
- namespace Rays
14
- {
15
-
16
-
17
- Rucy::Class texture_class ();
18
- // class Rays::Texture
19
-
20
-
21
- }// Rays
22
-
23
-
24
- RUCY_DECLARE_VALUE_FROM_TO(Rays::Texture)
25
-
26
-
27
- namespace Rucy
28
- {
29
-
30
-
31
- template <> inline Class
32
- get_ruby_class<Rays::Texture> ()
33
- {
34
- return Rays::texture_class();
35
- }
36
-
37
-
38
- }// Rucy
39
-
40
-
41
- #endif//EOH
@@ -1,71 +0,0 @@
1
- // -*- c++ -*-
2
- #pragma once
3
- #ifndef __RAYS_TEXTURE_H__
4
- #define __RAYS_TEXTURE_H__
5
-
6
-
7
- #include <xot/pimpl.h>
8
- #include <rays/defs.h>
9
- #include <rays/color_space.h>
10
- #include <rays/opengl.h>
11
-
12
-
13
- namespace Rays
14
- {
15
-
16
-
17
- class Bitmap;
18
-
19
-
20
- class Texture
21
- {
22
-
23
- public:
24
-
25
- Texture ();
26
-
27
- Texture (
28
- int width, int height, const ColorSpace& cs = RGBA,
29
- bool alpha_only = false);
30
-
31
- Texture (const Bitmap& bitmap, bool alpha_only = false);
32
-
33
- ~Texture ();
34
-
35
- GLuint id () const;
36
-
37
- int width () const;
38
-
39
- int height () const;
40
-
41
- const ColorSpace& color_space () const;
42
-
43
- bool alpha_only () const;
44
-
45
- float s (float x) const;
46
-
47
- float t (float y) const;
48
-
49
- float s_max () const;
50
-
51
- float t_max () const;
52
-
53
- bool dirty () const;
54
-
55
- void set_dirty (bool b = true);
56
-
57
- operator bool () const;
58
-
59
- bool operator ! () const;
60
-
61
- struct Data;
62
-
63
- Xot::PImpl<Data, true> self;
64
-
65
- };// Texture
66
-
67
-
68
- }// Rays
69
-
70
-
71
- #endif//EOH
@@ -1,24 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
-
3
-
4
- require 'rays/ext'
5
- require 'rays/color_space'
6
-
7
-
8
- module Rays
9
-
10
-
11
- class Texture
12
-
13
- def initialize (width, height, color_space = :RGBA, alpha_only = false)
14
- setup width, height, ColorSpace.get_color_space(color_space), alpha_only
15
- end
16
-
17
- def bounds ()
18
- Bounew.new 0, 0, width, height
19
- end
20
-
21
- end# Texture
22
-
23
-
24
- end# Rays
@@ -1,648 +0,0 @@
1
- #include "program.h"
2
-
3
-
4
- #ifndef IOS
5
- #define USE_SHADER
6
- #endif
7
-
8
-
9
- #ifdef USE_SHADER
10
-
11
-
12
- #include <vector>
13
- #include <algorithm>
14
- #include <boost/scoped_ptr.hpp>
15
- #include <boost/scoped_array.hpp>
16
- #include "rays/exception.h"
17
- #include "rays/shader.h"
18
-
19
-
20
- namespace Rays
21
- {
22
-
23
-
24
- class UniformValue
25
- {
26
-
27
- public:
28
-
29
- virtual ~UniformValue () {}
30
-
31
- virtual void apply (GLint location) const =0;
32
-
33
- };// UniformValue
34
-
35
-
36
- template <typename T, int DIMENSION>
37
- class UniformValueT : public UniformValue
38
- {
39
-
40
- public:
41
-
42
- UniformValueT (T arg1)
43
- {
44
- assert(DIMENSION == 1);
45
- array[0] = arg1;
46
- }
47
-
48
- UniformValueT (T arg1, T arg2)
49
- {
50
- assert(DIMENSION == 2);
51
- array[0] = arg1;
52
- array[1] = arg2;
53
- }
54
-
55
- UniformValueT (T arg1, T arg2, T arg3)
56
- {
57
- assert(DIMENSION == 3);
58
- array[0] = arg1;
59
- array[1] = arg2;
60
- array[2] = arg3;
61
- }
62
-
63
- UniformValueT (T arg1, T arg2, T arg3, T arg4)
64
- {
65
- assert(DIMENSION == 4);
66
- array[0] = arg1;
67
- array[1] = arg2;
68
- array[2] = arg3;
69
- array[3] = arg4;
70
- }
71
-
72
- UniformValueT (const T* args, size_t size)
73
- {
74
- assert(size == DIMENSION);
75
- for (size_t i = 0; i < size; ++i)
76
- array[i] = args[i];
77
- }
78
-
79
- void apply (GLint location) const;
80
-
81
- private:
82
-
83
- T array[DIMENSION];
84
-
85
- };// UniformValueT
86
-
87
-
88
- template <> void UniformValueT<int, 1>::apply (GLint location) const {glUniform1iv(location, 1, array);}
89
-
90
- template <> void UniformValueT<int, 2>::apply (GLint location) const {glUniform2iv(location, 1, array);}
91
-
92
- template <> void UniformValueT<int, 3>::apply (GLint location) const {glUniform3iv(location, 1, array);}
93
-
94
- template <> void UniformValueT<int, 4>::apply (GLint location) const {glUniform4iv(location, 1, array);}
95
-
96
- template <> void UniformValueT<float, 1>::apply (GLint location) const {glUniform1fv(location, 1, array);}
97
-
98
- template <> void UniformValueT<float, 2>::apply (GLint location) const {glUniform2fv(location, 1, array);}
99
-
100
- template <> void UniformValueT<float, 3>::apply (GLint location) const {glUniform3fv(location, 1, array);}
101
-
102
- template <> void UniformValueT<float, 4>::apply (GLint location) const {glUniform4fv(location, 1, array);}
103
-
104
-
105
- struct Uniform
106
- {
107
-
108
- struct Data
109
- {
110
-
111
- String name;
112
-
113
- boost::scoped_ptr<const UniformValue> value;
114
-
115
- };// Data
116
-
117
- Xot::PImpl<Data, true> self;
118
-
119
- Uniform (const char* name, const UniformValue* value)
120
- {
121
- if (!name || !value || name[0] == '\0')
122
- argument_error(__FILE__, __LINE__);
123
-
124
- self->name = name;
125
- self->value.reset(value);
126
- }
127
-
128
- void apply (GLuint program) const
129
- {
130
- GLint location = glGetUniformLocation(program, self->name);
131
- if (location < 0) return;
132
-
133
- self->value->apply(location);
134
- check_error(__FILE__, __LINE__);
135
- }
136
-
137
- bool operator == (const Uniform& rhs) const
138
- {
139
- return self.get() == rhs.self.get();
140
- }
141
-
142
- bool operator != (const Uniform& rhs) const
143
- {
144
- return !operator==(rhs);
145
- }
146
-
147
- };// Uniform
148
-
149
-
150
- typedef std::vector<Shader> ShaderList;
151
-
152
- typedef std::vector<ShaderList> ShaderStack;
153
-
154
- typedef std::vector<Uniform> UniformList;
155
-
156
- typedef std::vector<UniformList> UniformStack;
157
-
158
-
159
- struct Program::Data
160
- {
161
-
162
- int id;
163
-
164
- ShaderStack shader_stack;
165
-
166
- UniformStack uniform_stack;
167
-
168
- bool need_link, using_;
169
-
170
- Data ()
171
- : id(-1), need_link(false), using_(false)
172
- {
173
- shader_stack.push_back(ShaderList());
174
- uniform_stack.push_back(UniformList());
175
- }
176
-
177
- ~Data ()
178
- {
179
- clear();
180
- }
181
-
182
- void create ()
183
- {
184
- if (is_valid())
185
- invalid_state_error(__FILE__, __LINE__, "program is already created.");
186
-
187
- id = glCreateProgram();
188
- }
189
-
190
- void clear ()
191
- {
192
- if (shader_stack.size() != 1 || uniform_stack.size() != 1)
193
- invalid_state_error(__FILE__, __LINE__, "stack state is invalid.");
194
-
195
- ShaderList& list = shader_stack.back();
196
- for (ShaderList::iterator it = list.begin(); it != list.end(); ++it)
197
- detach(*it);
198
-
199
- if (id >= 0) glDeleteProgram((GLuint) id);
200
-
201
- id = -1;
202
- }
203
-
204
- void attach (const Shader& shader)
205
- {
206
- if (!is_valid()) create();
207
-
208
- ShaderList& list = shader_stack.back();
209
- ShaderList::iterator it = std::find(list.begin(), list.end(), shader);
210
- if (it != list.end()) return;
211
-
212
- glAttachShader(id, shader.id());
213
- check_error(__FILE__, __LINE__);
214
-
215
- list.push_back(shader);
216
- need_link = true;
217
- }
218
-
219
- void detach (const Shader& shader)
220
- {
221
- ShaderList& list = shader_stack.back();
222
- ShaderList::iterator it = std::find(list.begin(), list.end(), shader);
223
- if (it == list.end())
224
- rays_error(__FILE__, __LINE__, "shader is not attached.");
225
-
226
- glDetachShader(id, shader.id());
227
- check_error(__FILE__, __LINE__);
228
-
229
- list.erase(it);
230
- need_link = true;
231
- }
232
-
233
- void set_uniform (const char* name, const UniformValue* value)
234
- {
235
- if (uniform_stack.empty())
236
- invalid_state_error(__FILE__, __LINE__);
237
-
238
- UniformList& list = uniform_stack.back();
239
- std::remove_if(list.begin(), list.end(), UniformNameEqualFun(name));
240
- list.push_back(Uniform(name, value));
241
-
242
- if (using_) list.back().apply(id);
243
- }
244
-
245
- void update (bool uniforms = true)
246
- {
247
- link();
248
-
249
- use(!shader_stack.back().empty());
250
- if (using_ && uniforms) apply_all_uniforms();
251
- }
252
-
253
- bool is_valid () const
254
- {
255
- return id >= 0;
256
- }
257
-
258
- private:
259
-
260
- void link ()
261
- {
262
- if (!need_link) return;
263
-
264
- glLinkProgram(id);
265
-
266
- GLint status = GL_FALSE;
267
- glGetProgramiv(id, GL_LINK_STATUS, &status);
268
- if (status == GL_FALSE)
269
- {
270
- int len = 0;
271
- glGetProgramiv(id, GL_INFO_LOG_LENGTH, &len);
272
-
273
- boost::scoped_array<char> buffer(new char[len]);
274
- int written = 0;
275
- glGetProgramInfoLog(id, len, &written, &buffer[0]);
276
-
277
- opengl_error(__FILE__, __LINE__, &buffer[0]);
278
- }
279
-
280
- check_error(__FILE__, __LINE__);
281
-
282
- need_link = false;
283
- }
284
-
285
- void use (bool state)
286
- {
287
- if (state == using_) return;
288
-
289
- glUseProgram(state ? id : 0);
290
- using_ = state;
291
- }
292
-
293
- void apply_all_uniforms ()
294
- {
295
- if (uniform_stack.empty())
296
- invalid_state_error(__FILE__, __LINE__);
297
-
298
- UniformList& list = uniform_stack.back();
299
- for (UniformList::iterator it = list.begin(); it != list.end(); ++it)
300
- it->apply(id);
301
- }
302
-
303
- struct UniformNameEqualFun
304
- {
305
-
306
- String name;
307
-
308
- UniformNameEqualFun (const char* name)
309
- : name(name)
310
- {
311
- }
312
-
313
- bool operator () (const Uniform& uniform) const
314
- {
315
- return uniform.self->name == name;
316
- }
317
-
318
- };// UniformNameEqualFun
319
-
320
- };// Program::Data
321
-
322
-
323
- Program::Program ()
324
- {
325
- }
326
-
327
- Program::~Program ()
328
- {
329
- }
330
-
331
- void
332
- Program::attach (const Shader& shader)
333
- {
334
- self->attach(shader);
335
- self->update();
336
- }
337
-
338
- void
339
- Program::detach (const Shader& shader)
340
- {
341
- if (!*this)
342
- invalid_state_error(__FILE__, __LINE__);
343
-
344
- self->detach(shader);
345
- self->update(false);
346
- }
347
-
348
- void
349
- Program::set_uniform (const char* name, int arg1)
350
- {
351
- self->set_uniform(name, new UniformValueT<int, 1>(arg1));
352
- }
353
-
354
- void
355
- Program::set_uniform (const char* name, int arg1, int arg2)
356
- {
357
- self->set_uniform(name, new UniformValueT<int, 2>(arg1, arg2));
358
- }
359
-
360
- void
361
- Program::set_uniform (const char* name, int arg1, int arg2, int arg3)
362
- {
363
- self->set_uniform(name, new UniformValueT<int, 3>(arg1, arg2, arg3));
364
- }
365
-
366
- void
367
- Program::set_uniform (const char* name, int arg1, int arg2, int arg3, int arg4)
368
- {
369
- self->set_uniform(name, new UniformValueT<int, 4>(arg1, arg2, arg3, arg4));
370
- }
371
-
372
- void
373
- Program::set_uniform (const char* name, const int* args, size_t size)
374
- {
375
- UniformValue* value = NULL;
376
- switch (size)
377
- {
378
- case 1: value = new UniformValueT<int, 1>(args, 1); break;
379
- case 2: value = new UniformValueT<int, 2>(args, 2); break;
380
- case 3: value = new UniformValueT<int, 3>(args, 3); break;
381
- case 4: value = new UniformValueT<int, 4>(args, 4); break;
382
- }
383
-
384
- if (value == NULL)
385
- argument_error(__FILE__, __LINE__, "invalid 'size' value.");
386
-
387
- self->set_uniform(name, value);
388
- }
389
-
390
- void
391
- Program::set_uniform (const char* name, float arg1)
392
- {
393
- self->set_uniform(name, new UniformValueT<float, 1>(arg1));
394
- }
395
-
396
- void
397
- Program::set_uniform (const char* name, float arg1, float arg2)
398
- {
399
- self->set_uniform(name, new UniformValueT<float, 2>(arg1, arg2));
400
- }
401
-
402
- void
403
- Program::set_uniform (const char* name, float arg1, float arg2, float arg3)
404
- {
405
- self->set_uniform(name, new UniformValueT<float, 3>(arg1, arg2, arg3));
406
- }
407
-
408
- void
409
- Program::set_uniform (const char* name, float arg1, float arg2, float arg3, float arg4)
410
- {
411
- self->set_uniform(name, new UniformValueT<float, 4>(arg1, arg2, arg3, arg4));
412
- }
413
-
414
- void
415
- Program::set_uniform (const char* name, const float* args, size_t size)
416
- {
417
- UniformValue* value = NULL;
418
- switch (size)
419
- {
420
- case 1: value = new UniformValueT<float, 1>(args, 1); break;
421
- case 2: value = new UniformValueT<float, 2>(args, 2); break;
422
- case 3: value = new UniformValueT<float, 3>(args, 3); break;
423
- case 4: value = new UniformValueT<float, 4>(args, 4); break;
424
- }
425
-
426
- if (value == NULL)
427
- argument_error(__FILE__, __LINE__, "invalid 'size' value.");
428
-
429
- self->set_uniform(name, value);
430
- }
431
-
432
- void
433
- Program::push ()
434
- {
435
- self->shader_stack.push_back(self->shader_stack.back());
436
- self->uniform_stack.push_back(self->uniform_stack.back());
437
- }
438
-
439
- static void
440
- pop_shader_stack (Program::Data* self)
441
- {
442
- if (!self)
443
- argument_error(__FILE__, __LINE__);
444
-
445
- if (self->shader_stack.size() < 2)
446
- invalid_state_error(__FILE__, __LINE__, "shader stack underflow.");
447
-
448
- size_t size = self->shader_stack.size();
449
- ShaderList& prev = self->shader_stack[size - 1];
450
- ShaderList& next = self->shader_stack[size - 2];
451
-
452
- ShaderList::iterator prev_end = prev.end();
453
- ShaderList::iterator next_end = next.end();
454
-
455
- for (ShaderList::iterator it = prev.begin(); it != prev_end; ++it)
456
- {
457
- ShaderList::iterator found = std::find(next.begin(), next_end, *it);
458
- if (found == next_end) self->detach(*it);
459
- }
460
-
461
- for (ShaderList::iterator it = next.begin(); it != next_end; ++it)
462
- {
463
- ShaderList::iterator found = std::find(prev.begin(), prev_end, *it);
464
- if (found == prev_end) self->attach(*it);
465
- }
466
-
467
- self->shader_stack.pop_back();
468
- }
469
-
470
- static void
471
- pop_uniform_stack (Program::Data* self)
472
- {
473
- if (!self)
474
- argument_error(__FILE__, __LINE__);
475
-
476
- if (self->uniform_stack.size() < 2)
477
- invalid_state_error(__FILE__, __LINE__, "uniform stack underflow.");
478
-
479
- size_t size = self->uniform_stack.size();
480
- UniformList& prev = self->uniform_stack[size - 1];
481
- UniformList& next = self->uniform_stack[size - 2];
482
-
483
- UniformList::iterator prev_end = prev.end();
484
- UniformList::iterator next_end = next.end();
485
-
486
- for (UniformList::iterator it = next.begin(); it != next_end; ++it)
487
- {
488
- UniformList::iterator found = std::find(prev.begin(), prev_end, *it);
489
- if (found == prev_end) it->apply(self->id);
490
- }
491
-
492
- self->uniform_stack.pop_back();
493
- }
494
-
495
- void
496
- Program::pop ()
497
- {
498
- pop_shader_stack(self.get());
499
- pop_uniform_stack(self.get());
500
- self->update();
501
- }
502
-
503
- GLuint
504
- Program::id () const
505
- {
506
- return self->id;
507
- }
508
-
509
- Program::operator bool () const
510
- {
511
- return self->is_valid();
512
- }
513
-
514
- bool
515
- Program::operator ! () const
516
- {
517
- return !operator bool();
518
- }
519
-
520
-
521
- }// Rays
522
-
523
-
524
- #else// USE_SHADER
525
-
526
-
527
- #include "rays/exception.h"
528
-
529
-
530
- namespace Rays
531
- {
532
-
533
-
534
- struct Program::Data {};
535
-
536
-
537
- Program::Program ()
538
- {
539
- }
540
-
541
- Program::~Program ()
542
- {
543
- }
544
-
545
- void
546
- Program::attach (const Shader& shader)
547
- {
548
- not_implemented_error(__FILE__, __LINE__);
549
- }
550
-
551
- void
552
- Program::detach (const Shader& shader)
553
- {
554
- not_implemented_error(__FILE__, __LINE__);
555
- }
556
-
557
- void
558
- Program::set_uniform (const char* name, int arg1)
559
- {
560
- not_implemented_error(__FILE__, __LINE__);
561
- }
562
-
563
- void
564
- Program::set_uniform (const char* name, int arg1, int arg2)
565
- {
566
- not_implemented_error(__FILE__, __LINE__);
567
- }
568
-
569
- void
570
- Program::set_uniform (const char* name, int arg1, int arg2, int arg3)
571
- {
572
- not_implemented_error(__FILE__, __LINE__);
573
- }
574
-
575
- void
576
- Program::set_uniform (const char* name, int arg1, int arg2, int arg3, int arg4)
577
- {
578
- not_implemented_error(__FILE__, __LINE__);
579
- }
580
-
581
- void
582
- Program::set_uniform (const char* name, const int* args, size_t size)
583
- {
584
- not_implemented_error(__FILE__, __LINE__);
585
- }
586
-
587
- void
588
- Program::set_uniform (const char* name, float arg1)
589
- {
590
- not_implemented_error(__FILE__, __LINE__);
591
- }
592
-
593
- void
594
- Program::set_uniform (const char* name, float arg1, float arg2)
595
- {
596
- not_implemented_error(__FILE__, __LINE__);
597
- }
598
-
599
- void
600
- Program::set_uniform (const char* name, float arg1, float arg2, float arg3)
601
- {
602
- not_implemented_error(__FILE__, __LINE__);
603
- }
604
-
605
- void
606
- Program::set_uniform (const char* name, float arg1, float arg2, float arg3, float arg4)
607
- {
608
- not_implemented_error(__FILE__, __LINE__);
609
- }
610
-
611
- void
612
- Program::set_uniform (const char* name, const float* args, size_t size)
613
- {
614
- not_implemented_error(__FILE__, __LINE__);
615
- }
616
-
617
- void
618
- Program::push ()
619
- {
620
- }
621
-
622
- void
623
- Program::pop ()
624
- {
625
- }
626
-
627
- GLuint
628
- Program::id () const
629
- {
630
- return 0;
631
- }
632
-
633
- Program::operator bool () const
634
- {
635
- return false;
636
- }
637
-
638
- bool
639
- Program::operator ! () const
640
- {
641
- return !operator bool();
642
- }
643
-
644
-
645
- }// Rays
646
-
647
-
648
- #endif// USE_SHADER