rays 0.1.12 → 0.1.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) 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/color.cpp +224 -45
  5. data/.doc/ext/rays/color_space.cpp +137 -45
  6. data/.doc/ext/rays/defs.cpp +183 -0
  7. data/.doc/ext/rays/font.cpp +39 -21
  8. data/.doc/ext/rays/image.cpp +26 -37
  9. data/.doc/ext/rays/matrix.cpp +186 -29
  10. data/.doc/ext/rays/native.cpp +12 -6
  11. data/.doc/ext/rays/noise.cpp +53 -0
  12. data/.doc/ext/rays/painter.cpp +120 -308
  13. data/.doc/ext/rays/point.cpp +82 -77
  14. data/.doc/ext/rays/polygon.cpp +287 -0
  15. data/.doc/ext/rays/polygon_line.cpp +96 -0
  16. data/.doc/ext/rays/polyline.cpp +161 -0
  17. data/.doc/ext/rays/rays.cpp +0 -13
  18. data/.doc/ext/rays/shader.cpp +83 -9
  19. data/README.md +1 -1
  20. data/Rakefile +21 -9
  21. data/VERSION +1 -1
  22. data/ext/rays/bitmap.cpp +22 -80
  23. data/ext/rays/bounds.cpp +100 -128
  24. data/ext/rays/color.cpp +232 -51
  25. data/ext/rays/color_space.cpp +140 -46
  26. data/ext/rays/defs.cpp +183 -0
  27. data/ext/rays/defs.h +26 -2
  28. data/ext/rays/extconf.rb +1 -2
  29. data/ext/rays/font.cpp +39 -22
  30. data/ext/rays/image.cpp +27 -39
  31. data/ext/rays/matrix.cpp +198 -30
  32. data/ext/rays/native.cpp +12 -6
  33. data/ext/rays/noise.cpp +55 -0
  34. data/ext/rays/painter.cpp +129 -315
  35. data/ext/rays/point.cpp +89 -81
  36. data/ext/rays/polygon.cpp +301 -0
  37. data/ext/rays/polygon_line.cpp +99 -0
  38. data/ext/rays/polyline.cpp +170 -0
  39. data/ext/rays/rays.cpp +0 -14
  40. data/ext/rays/shader.cpp +84 -9
  41. data/include/rays.h +10 -2
  42. data/include/rays/bitmap.h +14 -26
  43. data/include/rays/bounds.h +21 -4
  44. data/include/rays/color.h +25 -14
  45. data/include/rays/color_space.h +11 -8
  46. data/include/rays/coord.h +114 -0
  47. data/include/rays/debug.h +22 -0
  48. data/include/rays/defs.h +3 -0
  49. data/include/rays/font.h +4 -4
  50. data/include/rays/image.h +11 -17
  51. data/include/rays/matrix.h +50 -24
  52. data/include/rays/noise.h +42 -0
  53. data/include/rays/opengl.h +2 -50
  54. data/include/rays/painter.h +57 -99
  55. data/include/rays/point.h +44 -51
  56. data/include/rays/polygon.h +164 -0
  57. data/include/rays/polyline.h +65 -0
  58. data/include/rays/rays.h +3 -0
  59. data/include/rays/ruby.h +7 -1
  60. data/include/rays/ruby/bounds.h +1 -1
  61. data/include/rays/ruby/color.h +1 -1
  62. data/include/rays/ruby/color_space.h +1 -1
  63. data/include/rays/ruby/font.h +1 -1
  64. data/include/rays/ruby/matrix.h +1 -1
  65. data/include/rays/ruby/point.h +1 -1
  66. data/include/rays/ruby/polygon.h +52 -0
  67. data/include/rays/ruby/polyline.h +41 -0
  68. data/include/rays/ruby/shader.h +1 -1
  69. data/include/rays/shader.h +36 -8
  70. data/lib/rays.rb +6 -1
  71. data/lib/rays/bitmap.rb +0 -15
  72. data/lib/rays/bounds.rb +17 -23
  73. data/lib/rays/color.rb +20 -47
  74. data/lib/rays/color_space.rb +13 -13
  75. data/lib/rays/image.rb +2 -6
  76. data/lib/rays/matrix.rb +28 -0
  77. data/lib/rays/module.rb +4 -19
  78. data/lib/rays/painter.rb +60 -97
  79. data/lib/rays/point.rb +13 -21
  80. data/lib/rays/polygon.rb +50 -0
  81. data/lib/rays/polygon_line.rb +36 -0
  82. data/lib/rays/polyline.rb +32 -0
  83. data/lib/rays/shader.rb +20 -1
  84. data/rays.gemspec +5 -7
  85. data/src/bitmap.h +36 -0
  86. data/src/bounds.cpp +74 -11
  87. data/src/color.cpp +58 -23
  88. data/src/color_space.cpp +50 -32
  89. data/src/color_space.h +22 -0
  90. data/src/coord.cpp +170 -0
  91. data/src/coord.h +35 -0
  92. data/src/font.cpp +118 -0
  93. data/src/font.h +64 -0
  94. data/src/frame_buffer.cpp +37 -71
  95. data/src/frame_buffer.h +4 -4
  96. data/src/image.cpp +171 -97
  97. data/src/image.h +25 -0
  98. data/src/ios/bitmap.mm +107 -105
  99. data/src/ios/font.mm +48 -60
  100. data/src/ios/helper.h +2 -2
  101. data/src/ios/opengl.mm +19 -4
  102. data/src/ios/rays.mm +3 -0
  103. data/src/matrix.cpp +111 -26
  104. data/src/matrix.h +30 -0
  105. data/src/noise.cpp +74 -0
  106. data/src/opengl.cpp +9 -27
  107. data/src/opengl.h +37 -0
  108. data/src/osx/bitmap.mm +111 -106
  109. data/src/osx/font.mm +48 -61
  110. data/src/osx/helper.h +2 -2
  111. data/src/osx/opengl.mm +19 -83
  112. data/src/osx/rays.mm +3 -0
  113. data/src/painter.cpp +780 -696
  114. data/src/painter.h +24 -0
  115. data/src/point.cpp +140 -119
  116. data/src/polygon.cpp +1100 -0
  117. data/src/polygon.h +32 -0
  118. data/src/polyline.cpp +158 -0
  119. data/src/polyline.h +67 -0
  120. data/src/render_buffer.cpp +11 -4
  121. data/src/render_buffer.h +2 -2
  122. data/src/shader.cpp +163 -106
  123. data/src/shader.h +38 -0
  124. data/src/shader_program.cpp +533 -0
  125. data/src/{program.h → shader_program.h} +28 -16
  126. data/src/shader_source.cpp +140 -0
  127. data/src/shader_source.h +52 -0
  128. data/src/texture.cpp +136 -160
  129. data/src/texture.h +65 -0
  130. data/src/win32/bitmap.cpp +62 -52
  131. data/src/win32/font.cpp +11 -13
  132. data/src/win32/font.h +24 -0
  133. data/src/win32/gdi.h +6 -6
  134. data/test/helper.rb +0 -3
  135. data/test/test_bitmap.rb +31 -7
  136. data/test/test_bounds.rb +36 -0
  137. data/test/test_color.rb +59 -19
  138. data/test/test_color_space.rb +95 -0
  139. data/test/test_image.rb +24 -20
  140. data/test/test_matrix.rb +106 -0
  141. data/test/test_painter.rb +92 -46
  142. data/test/test_painter_shape.rb +57 -0
  143. data/test/test_point.rb +21 -0
  144. data/test/test_polygon.rb +234 -0
  145. data/test/test_polygon_line.rb +167 -0
  146. data/test/test_polyline.rb +145 -0
  147. data/test/test_shader.rb +9 -9
  148. metadata +88 -67
  149. data/.doc/ext/rays/texture.cpp +0 -138
  150. data/ext/rays/texture.cpp +0 -149
  151. data/include/rays/ruby/texture.h +0 -41
  152. data/include/rays/texture.h +0 -71
  153. data/lib/rays/texture.rb +0 -24
  154. data/src/program.cpp +0 -648
  155. data/test/test_texture.rb +0 -27
@@ -1,34 +1,33 @@
1
1
  // -*- c++ -*-
2
2
  #pragma once
3
- #ifndef __RAYS_SRC_PROGRAM_H__
4
- #define __RAYS_SRC_PROGRAM_H__
3
+ #ifndef __RAYS_SRC_SHADER_PROGRAM_H__
4
+ #define __RAYS_SRC_SHADER_PROGRAM_H__
5
5
 
6
6
 
7
7
  #include <xot/pimpl.h>
8
- #include <rays/opengl.h>
8
+ #include <rays/defs.h>
9
+ #include <rays/coord.h>
10
+ #include "opengl.h"
9
11
 
10
12
 
11
13
  namespace Rays
12
14
  {
13
15
 
14
16
 
15
- class Shader;
17
+ class ShaderSource;
18
+ class Texture;
16
19
 
17
20
 
18
- class Program
21
+ class ShaderProgram
19
22
  {
20
23
 
21
- typedef Program This;
24
+ typedef ShaderProgram This;
22
25
 
23
26
  public:
24
27
 
25
- Program ();
28
+ ShaderProgram (const ShaderSource& vertex, const ShaderSource& fragment);
26
29
 
27
- ~Program ();
28
-
29
- void attach (const Shader& shader);
30
-
31
- void detach (const Shader& shader);
30
+ ~ShaderProgram ();
32
31
 
33
32
  void set_uniform (const char* name, int arg1);
34
33
 
@@ -50,9 +49,13 @@ namespace Rays
50
49
 
51
50
  void set_uniform (const char* name, const float* args, size_t size);
52
51
 
53
- void push ();
52
+ void set_uniform (const char* name, const Coord2& vec2);
53
+
54
+ void set_uniform (const char* name, const Coord3& vec3);
54
55
 
55
- void pop ();
56
+ void set_uniform (const char* name, const Coord4& vec4);
57
+
58
+ void set_uniform (const char* name, const Texture& texture);
56
59
 
57
60
  GLuint id () const;
58
61
 
@@ -60,11 +63,20 @@ namespace Rays
60
63
 
61
64
  bool operator ! () const;
62
65
 
66
+ friend bool operator == (const This& lhs, const This& rhs);
67
+
68
+ friend bool operator != (const This& lhs, const This& rhs);
69
+
63
70
  struct Data;
64
71
 
65
- Xot::PImpl<Data, true> self;
72
+ Xot::PSharedImpl<Data> self;
73
+
74
+ };// ShaderProgram
75
+
76
+
77
+ void ShaderProgram_activate (const ShaderProgram& program);
66
78
 
67
- };// Program
79
+ void ShaderProgram_deactivate ();
68
80
 
69
81
 
70
82
  }// Rays
@@ -0,0 +1,140 @@
1
+ #include "shader_source.h"
2
+
3
+
4
+ #include "rays/exception.h"
5
+ #include "rays/debug.h"
6
+
7
+
8
+ namespace Rays
9
+ {
10
+
11
+
12
+ struct ShaderSource::Data
13
+ {
14
+
15
+ GLuint id = 0;
16
+
17
+ GLenum type = 0;
18
+
19
+ String source;
20
+
21
+ Data ()
22
+ {
23
+ }
24
+
25
+ ~Data ()
26
+ {
27
+ clear();
28
+ }
29
+
30
+ void compile (GLenum type_, const char* source_)
31
+ {
32
+ if (!is_valid_type(type_) || !source_ || !*source_)
33
+ argument_error(__FILE__, __LINE__);
34
+
35
+ if (is_valid())
36
+ invalid_state_error(__FILE__, __LINE__);
37
+
38
+ id = glCreateShader(type_);
39
+ glShaderSource(id, 1, &source_, NULL);
40
+ glCompileShader(id);
41
+
42
+ GLint status = GL_FALSE;
43
+ glGetShaderiv(id, GL_COMPILE_STATUS, &status);
44
+ if (status == GL_FALSE)
45
+ opengl_error(__FILE__, __LINE__, get_compile_log().c_str());
46
+
47
+ type = type_;
48
+ source = source_;
49
+ }
50
+
51
+ String get_compile_log () const
52
+ {
53
+ GLsizei len = 0;
54
+ glGetShaderiv(id, GL_INFO_LOG_LENGTH, &len);
55
+ if (len <= 0) return "";
56
+
57
+ std::unique_ptr<char[]> buffer(new char[len]);
58
+ int written = 0;
59
+ glGetShaderInfoLog(id, len, &written, &buffer[0]);
60
+ return buffer.get();
61
+ }
62
+
63
+ void clear ()
64
+ {
65
+ if (id > 0) glDeleteShader(id);
66
+
67
+ id = 0;
68
+ type = 0;
69
+ source.clear();
70
+ }
71
+
72
+ bool is_valid () const
73
+ {
74
+ return is_valid_type(type) && id > 0;
75
+ }
76
+
77
+ static bool is_valid_type (GLenum type)
78
+ {
79
+ return type == GL_VERTEX_SHADER || type == GL_FRAGMENT_SHADER;
80
+ }
81
+
82
+ };// ShaderSource::Data
83
+
84
+
85
+ ShaderSource::ShaderSource ()
86
+ {
87
+ }
88
+
89
+ ShaderSource::ShaderSource (GLenum type, const char* source)
90
+ {
91
+ self->compile(type, source);
92
+ }
93
+
94
+ ShaderSource::~ShaderSource ()
95
+ {
96
+ }
97
+
98
+ const char*
99
+ ShaderSource::source () const
100
+ {
101
+ return self->source;
102
+ }
103
+
104
+ GLenum
105
+ ShaderSource::type () const
106
+ {
107
+ return self->type;
108
+ }
109
+
110
+ GLuint
111
+ ShaderSource::id () const
112
+ {
113
+ return self->id;
114
+ }
115
+
116
+ ShaderSource::operator bool () const
117
+ {
118
+ return self->is_valid();
119
+ }
120
+
121
+ bool
122
+ ShaderSource::operator ! () const
123
+ {
124
+ return !operator bool();
125
+ }
126
+
127
+ bool
128
+ operator == (const ShaderSource& lhs, const ShaderSource& rhs)
129
+ {
130
+ return (!lhs && !rhs) || lhs.self->id == rhs.self->id;
131
+ }
132
+
133
+ bool
134
+ operator != (const ShaderSource& lhs, const ShaderSource& rhs)
135
+ {
136
+ return !operator==(lhs, rhs);
137
+ }
138
+
139
+
140
+ }// Rays
@@ -0,0 +1,52 @@
1
+ // -*- c++ -*-
2
+ #pragma once
3
+ #ifndef __RAYS_SRC_SHADER_SOURCE_H__
4
+ #define __RAYS_SRC_SHADER_SOURCE_H__
5
+
6
+
7
+ #include <xot/pimpl.h>
8
+ #include "opengl.h"
9
+
10
+
11
+ namespace Rays
12
+ {
13
+
14
+
15
+ class ShaderSource
16
+ {
17
+
18
+ typedef ShaderSource This;
19
+
20
+ public:
21
+
22
+ ShaderSource ();
23
+
24
+ ShaderSource (GLenum type, const char* source);
25
+
26
+ ~ShaderSource ();
27
+
28
+ const char* source () const;
29
+
30
+ GLenum type () const;
31
+
32
+ GLuint id () const;
33
+
34
+ operator bool () const;
35
+
36
+ bool operator ! () const;
37
+
38
+ friend bool operator == (const This& lhs, const This& rhs);
39
+
40
+ friend bool operator != (const This& lhs, const This& rhs);
41
+
42
+ struct Data;
43
+
44
+ Xot::PSharedImpl<Data> self;
45
+
46
+ };// ShaderSource
47
+
48
+
49
+ }// Rays
50
+
51
+
52
+ #endif//EOH
@@ -1,9 +1,12 @@
1
- #include "rays/texture.h"
1
+ #include "texture.h"
2
2
 
3
3
 
4
+ #include <assert.h>
4
5
  #include "rays/exception.h"
5
6
  #include "rays/bitmap.h"
6
- #include "rays/opengl.h"
7
+ #include "rays/debug.h"
8
+ #include "opengl.h"
9
+ #include "color_space.h"
7
10
  #include "frame_buffer.h"
8
11
 
9
12
 
@@ -14,14 +17,17 @@ namespace Rays
14
17
  struct Texture::Data
15
18
  {
16
19
 
17
- int id, width, height, width_pow2, height_pow2;
20
+ Context context = NULL;
21
+
22
+ GLuint id = 0;
23
+
24
+ int width, height, width_pow2, height_pow2;
18
25
 
19
26
  ColorSpace color_space;
20
27
 
21
- bool alpha_only, dirty;
28
+ bool modified;
22
29
 
23
30
  Data ()
24
- : id(-1)
25
31
  {
26
32
  clear();
27
33
  }
@@ -33,47 +39,40 @@ namespace Rays
33
39
 
34
40
  void clear ()
35
41
  {
36
- if (id >= 0)
37
- {
38
- GLuint id_ = id;
39
- glDeleteTextures(1, &id_);
40
- }
41
-
42
- id = -1;
43
- width = height = width_pow2 = height_pow2 = 0;
42
+ delete_texture();
43
+
44
+ width =
45
+ height =
46
+ width_pow2 =
47
+ height_pow2 = 0;
44
48
  color_space = COLORSPACE_UNKNOWN;
45
- alpha_only = dirty = false;
49
+ modified = false;
46
50
  }
47
51
 
48
- };// Texture::Data
52
+ void delete_texture ()
53
+ {
54
+ if (!has_id()) return;
49
55
 
56
+ Context current_context = OpenGL_get_context();
50
57
 
51
- static void
52
- create_texture (
53
- Texture::Data* self, const Bitmap* bitmap, GLenum format, GLenum type)
54
- {
55
- if (!self || (bitmap && !*bitmap))
56
- argument_error(__FILE__, __LINE__);
58
+ assert(context);
59
+ OpenGL_set_context(context);
57
60
 
58
- GLuint id = 0;
59
- glGenTextures(1, &id);
60
- glBindTexture(GL_TEXTURE_2D, id);
61
- if (glIsTexture(id) == GL_FALSE)
62
- opengl_error(__FILE__, __LINE__, "failed to create texture.");
61
+ glDeleteTextures(1, &id);
63
62
 
64
- self->id = id;
63
+ OpenGL_set_context(current_context);
65
64
 
66
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
67
- //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
68
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
69
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);
65
+ context = NULL;
66
+ id = 0;
67
+ }
70
68
 
71
- glTexImage2D(
72
- GL_TEXTURE_2D, 0, format, self->width_pow2, self->height_pow2, 0,
73
- format, type, bitmap ? bitmap->pixels() : NULL);
69
+ bool has_id () const
70
+ {
71
+ return context && id > 0;
72
+ }
73
+
74
+ };// Texture::Data
74
75
 
75
- check_error(__FILE__, __LINE__);
76
- }
77
76
 
78
77
  static int
79
78
  min_pow2 (int num)
@@ -84,26 +83,35 @@ namespace Rays
84
83
  }
85
84
 
86
85
  static void
87
- colorspace_for_alphabitmap (ColorSpace* result, ColorSpace cs)
86
+ setup_texture (Texture::Data* self, const void* pixels = NULL)
88
87
  {
89
- if (!result || !cs)
90
- argument_error(__FILE__, __LINE__);
88
+ assert(self && !self->has_id());
91
89
 
92
- *result = COLORSPACE_UNKNOWN;
90
+ if (self->context)
91
+ invalid_state_error(__FILE__, __LINE__);
93
92
 
94
- if (cs.is_float())
95
- *result = GRAY_float;
96
- else
97
- {
98
- switch (cs.bpc())
99
- {
100
- case 8: *result = GRAY_8; break;
101
- case 16: *result = GRAY_16; break;
102
- case 24: *result = GRAY_24; break;
103
- case 32: *result = GRAY_32; break;
104
- default: rays_error(__FILE__, __LINE__, "invalid bpc.");
105
- }
106
- }
93
+ self->context = OpenGL_get_context();
94
+ if (!self->context)
95
+ opengl_error(__FILE__, __LINE__);
96
+
97
+ glGenTextures(1, &self->id);
98
+ glBindTexture(GL_TEXTURE_2D, self->id);
99
+ if (glIsTexture(self->id) == GL_FALSE)
100
+ opengl_error(__FILE__, __LINE__, "failed to create texture.");
101
+
102
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
103
+ //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
104
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
105
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);
106
+
107
+ GLenum format, type;
108
+ ColorSpace_get_gl_format_and_type(&format, &type, self->color_space);
109
+
110
+ glTexImage2D(
111
+ GL_TEXTURE_2D, 0, format, self->width_pow2, self->height_pow2, 0,
112
+ format, type, pixels);
113
+
114
+ OpenGL_check_error(__FILE__, __LINE__);
107
115
  }
108
116
 
109
117
  template <int BytesPerPixel>
@@ -141,134 +149,104 @@ namespace Rays
141
149
  static inline void
142
150
  copy_pixels (
143
151
  size_t width,
144
- uchar* dest, size_t dest_offset, const uchar* src, size_t src_offset)
152
+ uchar* dest, size_t dest_stride, const uchar* src, size_t src_stride)
145
153
  {
146
- if (!dest || !src || dest_offset <= 0 || src_offset <= 0)
154
+ if (!dest || !src || dest_stride <= 0 || src_stride <= 0)
147
155
  argument_error(__FILE__, __LINE__);
148
156
 
149
157
  while (width--)
150
158
  {
151
159
  copy_pixel<BytesPerPixel>(dest, src);
152
- dest += dest_offset;
153
- src += src_offset;
160
+ dest += dest_stride;
161
+ src += src_stride;
154
162
  }
155
163
  }
156
164
 
157
165
  static inline void
158
166
  copy_pixels (
159
167
  size_t Bpp, size_t width,
160
- uchar* dest, size_t dest_offset, const uchar* src, size_t src_offset)
168
+ uchar* dest, size_t dest_stride, const uchar* src, size_t src_stride)
161
169
  {
162
170
  switch (Bpp)
163
171
  {
164
- case 1: copy_pixels<1>(width, dest, dest_offset, src, src_offset); break;
165
- case 2: copy_pixels<2>(width, dest, dest_offset, src, src_offset); break;
166
- case 3: copy_pixels<3>(width, dest, dest_offset, src, src_offset); break;
167
- case 4: copy_pixels<4>(width, dest, dest_offset, src, src_offset); break;
172
+ case 1: copy_pixels<1>(width, dest, dest_stride, src, src_stride); break;
173
+ case 2: copy_pixels<2>(width, dest, dest_stride, src, src_stride); break;
174
+ case 3: copy_pixels<3>(width, dest, dest_stride, src, src_stride); break;
175
+ case 4: copy_pixels<4>(width, dest, dest_stride, src, src_stride); break;
168
176
  }
169
177
  }
170
178
 
171
179
  static void
172
- copy_bitmap (Bitmap* dest, const Bitmap& src, int src_offset = 0)
180
+ copy_bitmap (Bitmap* dest, const Bitmap& src)
173
181
  {
174
- if (!dest || !src || src_offset < 0)
182
+ if (!dest || !src)
175
183
  argument_error(__FILE__, __LINE__);
176
184
 
185
+ int width = std::min(src.width(), dest->width());
186
+ int height = std::min(src.height(), dest->height());
177
187
  int src_Bpp = src.color_space().Bpp();
178
- int src_Bpc = src.color_space().Bpc();
179
188
  int dest_Bpp = dest->color_space().Bpp();
180
189
 
181
- if (src_offset >= (src_Bpp / src_Bpc))
182
- rays_error(__FILE__, __LINE__, "invalid src_offset.");
183
-
184
- int width = std::min(src.width(), dest->width());
185
- int height = std::min(src.height(), dest->height());
186
-
187
190
  for (int y = 0; y < height; ++y)
188
191
  {
189
- const uchar* s = src. at<uchar>(0, y) + src_offset * src_Bpc;
190
- uchar* d = dest->at<uchar>(0, y);
191
- copy_pixels(src_Bpp, width, d, dest_Bpp, s, src_Bpp);
192
+ copy_pixels(
193
+ src_Bpp, width,
194
+ dest->at<uchar>(0, y), dest_Bpp,
195
+ src. at<uchar>(0, y), src_Bpp);
192
196
  }
193
197
  }
194
198
 
195
- static void
196
- setup_texture (
197
- Texture::Data* self, int width, int height, const ColorSpace& cs,
198
- const Bitmap* bitmap, bool alpha_only)
199
- {
200
- if (!self || width <= 0 || height <= 0 || !cs || (bitmap && !*bitmap))
201
- argument_error(__FILE__, __LINE__);
202
199
 
203
- GLenum format, type;
204
- cs.get_gl_enums(&format, &type, alpha_only);
200
+ Texture::Texture ()
201
+ {
202
+ }
205
203
 
206
- self->clear();
204
+ Texture::Texture (int width, int height, const ColorSpace& cs)
205
+ {
206
+ if (width <= 0 || height <= 0 || !cs)
207
+ argument_error(__FILE__, __LINE__);
207
208
 
208
209
  self->width = width;
209
210
  self->height = height;
210
211
  self->width_pow2 = min_pow2(width);
211
212
  self->height_pow2 = min_pow2(height);
212
213
  self->color_space = cs;
213
- self->alpha_only = alpha_only;
214
- self->dirty = true;
215
-
216
- if (alpha_only)
217
- {
218
- colorspace_for_alphabitmap(&self->color_space, cs);
219
- if (!self->color_space.is_gray())
220
- rays_error(__FILE__, __LINE__, "alpha_only takes only gray color-space.");
221
- }
222
-
223
- Bitmap bmp;
224
- if (bitmap)
225
- {
226
- if (
227
- self->color_space != cs ||
228
- self->width_pow2 != self->width ||
229
- self->height_pow2 != self->height)
230
- {
231
- bmp = Bitmap(self->width_pow2, self->height_pow2, self->color_space);
232
- if (!bmp)
233
- rays_error(__FILE__, __LINE__, "creating bitmap failed.");
234
-
235
- copy_bitmap(&bmp, *bitmap, alpha_only ? self->color_space.alpha_pos() : 0);
236
- bitmap = &bmp;
237
- }
238
- }
214
+ self->modified = true;
239
215
 
240
- create_texture(self, bitmap, format, type);
216
+ setup_texture(self.get());
241
217
  }
242
218
 
243
-
244
- Texture::Texture ()
219
+ Texture::Texture (const Bitmap& bitmap)
245
220
  {
246
- }
221
+ if (!bitmap)
222
+ argument_error(__FILE__, __LINE__);
247
223
 
248
- Texture::Texture (int width, int height, const ColorSpace& cs, bool alpha_only)
249
- {
250
- setup_texture(
251
- self.get(), width, height, cs,
252
- NULL, alpha_only);
253
- }
224
+ self->width = bitmap.width();
225
+ self->height = bitmap.height();
226
+ self->width_pow2 = min_pow2(self->width);
227
+ self->height_pow2 = min_pow2(self->height);
228
+ self->color_space = bitmap.color_space();
229
+ self->modified = true;
230
+
231
+ Bitmap bmp = bitmap;
232
+ if (
233
+ self->width_pow2 != self->width ||
234
+ self->height_pow2 != self->height)
235
+ {
236
+ bmp = Bitmap(self->width_pow2, self->height_pow2, self->color_space);
237
+ if (!bmp)
238
+ rays_error(__FILE__, __LINE__);
254
239
 
255
- Texture::Texture (const Bitmap& bitmap, bool alpha_only)
256
- {
257
- setup_texture(
258
- self.get(), bitmap.width(), bitmap.height(), bitmap.color_space(),
259
- &bitmap, alpha_only);
240
+ copy_bitmap(&bmp, bitmap);
241
+ }
242
+
243
+ setup_texture(self.get(), bmp.pixels());
260
244
  }
261
245
 
262
246
  Texture::~Texture ()
263
247
  {
264
248
  }
265
249
 
266
- GLuint
267
- Texture::id () const
268
- {
269
- return self->id;
270
- }
271
-
272
250
  int
273
251
  Texture::width () const
274
252
  {
@@ -276,62 +254,60 @@ namespace Rays
276
254
  }
277
255
 
278
256
  int
279
- Texture::height () const
257
+ Texture::reserved_width () const
280
258
  {
281
- return self->height;
259
+ return self->width_pow2;
282
260
  }
283
261
 
284
- const ColorSpace&
285
- Texture::color_space () const
262
+ int
263
+ Texture::height () const
286
264
  {
287
- return self->color_space;
265
+ return self->height;
288
266
  }
289
267
 
290
- bool
291
- Texture::alpha_only () const
268
+ int
269
+ Texture::reserved_height () const
292
270
  {
293
- return self->alpha_only;
271
+ return self->height_pow2;
294
272
  }
295
273
 
296
- float
297
- Texture::s (float x) const
274
+ const ColorSpace&
275
+ Texture::color_space () const
298
276
  {
299
- return x / (float) self->width_pow2;
277
+ return self->color_space;
300
278
  }
301
279
 
302
- float
303
- Texture::t (float y) const
280
+ Context
281
+ Texture::context () const
304
282
  {
305
- return y / (float) self->height_pow2;
283
+ return self->context;
306
284
  }
307
285
 
308
- float
309
- Texture::s_max () const
286
+ GLuint
287
+ Texture::id () const
310
288
  {
311
- return s(self->width);
289
+ return self->id;
312
290
  }
313
291
 
314
- float
315
- Texture::t_max () const
292
+ void
293
+ Texture::set_modified (bool modified)
316
294
  {
317
- return t(self->height);
295
+ self->modified = modified;
318
296
  }
319
297
 
320
298
  bool
321
- Texture::dirty () const
322
- {
323
- return self->dirty;
324
- }
325
-
326
- void
327
- Texture::set_dirty (bool b)
299
+ Texture::modified () const
328
300
  {
329
- self->dirty = b;
301
+ return self->modified;
330
302
  }
331
303
 
332
304
  Texture::operator bool () const
333
305
  {
334
- return self->id >= 0 && self->width > 0 && self->height > 0;
306
+ return
307
+ self->has_id() &&
308
+ self->width > 0 &&
309
+ self->height > 0 &&
310
+ self->color_space;
335
311
  }
336
312
 
337
313
  bool