rays 0.3.10 → 0.3.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/polygon.cpp +1 -1
  3. data/.github/workflows/release-gem.yml +1 -1
  4. data/.github/workflows/test.yml +4 -4
  5. data/ChangeLog.md +7 -0
  6. data/Gemfile.lock +6 -5
  7. data/VERSION +1 -1
  8. data/ext/rays/polygon.cpp +1 -1
  9. data/rays.gemspec +2 -2
  10. data/src/bitmap.h +4 -0
  11. data/src/color_space.cpp +1 -41
  12. data/src/image.cpp +0 -1
  13. data/src/ios/bitmap.mm +5 -32
  14. data/src/ios/rays.mm +3 -3
  15. data/src/opengl/bitmap.cpp +41 -0
  16. data/src/opengl/color_space.cpp +51 -0
  17. data/src/{color_space.h → opengl/color_space.h} +2 -2
  18. data/src/{frame_buffer.cpp → opengl/frame_buffer.cpp} +1 -1
  19. data/src/{frame_buffer.h → opengl/frame_buffer.h} +2 -2
  20. data/src/{ios → opengl/ios}/opengl.mm +3 -3
  21. data/src/{opengl.h → opengl/opengl.h} +2 -6
  22. data/src/{osx → opengl/osx}/opengl.mm +3 -3
  23. data/src/opengl/painter.cpp +756 -0
  24. data/src/{render_buffer.h → opengl/render_buffer.h} +2 -2
  25. data/src/{sdl → opengl/sdl}/opengl.cpp +4 -3
  26. data/src/{shader.cpp → opengl/shader.cpp} +1 -2
  27. data/src/{shader.h → opengl/shader.h} +2 -2
  28. data/src/{shader_program.cpp → opengl/shader_program.cpp} +3 -3
  29. data/src/{shader_program.h → opengl/shader_program.h} +2 -2
  30. data/src/{shader_source.h → opengl/shader_source.h} +2 -2
  31. data/src/{texture.cpp → opengl/texture.cpp} +2 -3
  32. data/src/opengl/texture.h +21 -0
  33. data/src/{win32 → opengl/win32}/opengl.cpp +4 -3
  34. data/src/osx/bitmap.mm +5 -33
  35. data/src/osx/rays.mm +3 -3
  36. data/src/painter.cpp +21 -905
  37. data/src/painter.h +210 -11
  38. data/src/polygon.cpp +38 -13
  39. data/src/renderer.h +22 -0
  40. data/src/sdl/bitmap.cpp +5 -33
  41. data/src/sdl/rays.cpp +3 -3
  42. data/src/texture.h +0 -3
  43. data/src/win32/bitmap.cpp +6 -34
  44. data/src/win32/rays.cpp +3 -3
  45. metadata +29 -24
  46. /data/src/{opengl.cpp → opengl/opengl.cpp} +0 -0
  47. /data/src/{render_buffer.cpp → opengl/render_buffer.cpp} +0 -0
  48. /data/src/{shader_source.cpp → opengl/shader_source.cpp} +0 -0
data/src/painter.h CHANGED
@@ -4,26 +4,225 @@
4
4
  #define __RAYS_SRC_PAINTER_H__
5
5
 
6
6
 
7
+ #include <vector>
7
8
  #include "rays/painter.h"
8
- #include "opengl.h"
9
+ #include "rays/bounds.h"
10
+ #include "rays/color.h"
11
+ #include "rays/font.h"
12
+ #include "rays/image.h"
13
+ #include "rays/shader.h"
14
+ #include "matrix.h"
15
+ #include "texture.h"
9
16
 
10
17
 
11
18
  namespace Rays
12
19
  {
13
20
 
14
21
 
15
- void Painter_draw (
16
- Painter* painter, GLenum mode, const Color& color,
17
- const Coord3* points, size_t npoints,
18
- const uint* indices = NULL, size_t nindices = 0,
19
- const Coord3* texcoords = NULL);
22
+ enum ColorType
23
+ {
24
+
25
+ FILL = 0,
26
+
27
+ STROKE,
28
+
29
+ COLOR_TYPE_MAX
30
+
31
+ };// ColorType
32
+
33
+
34
+ enum PrimitiveMode
35
+ {
36
+
37
+ MODE_NONE = -1,
38
+
39
+ MODE_POINTS = 0x0,
40
+
41
+ MODE_LINES = 0x1,
42
+
43
+ MODE_LINE_LOOP = 0x2,
44
+
45
+ MODE_LINE_STRIP = 0x3,
46
+
47
+ MODE_TRIANGLES = 0x4,
48
+
49
+ MODE_TRIANGLE_STRIP = 0x5,
50
+
51
+ MODE_TRIANGLE_FAN = 0x6,
52
+
53
+ MODE_QUADS = 0x7,
54
+
55
+ MODE_QUAD_STRIP = 0x8,
56
+
57
+ MODE_POLYGON = 0x9,
58
+
59
+ };// PrimitiveMode
60
+
61
+
62
+ struct State
63
+ {
64
+
65
+ Color background, colors[COLOR_TYPE_MAX];
66
+
67
+ bool nocolors[COLOR_TYPE_MAX];
68
+
69
+ coord stroke_width;
70
+
71
+ float stroke_outset;
72
+
73
+ CapType stroke_cap;
74
+
75
+ JoinType stroke_join;
76
+
77
+ coord miter_limit;
78
+
79
+ uint nsegment;
80
+
81
+ coord line_height;
82
+
83
+ BlendMode blend_mode;
84
+
85
+ Bounds clip;
86
+
87
+ Font font;
88
+
89
+ Image texture;
90
+
91
+ TexCoordMode texcoord_mode;
92
+
93
+ TexCoordWrap texcoord_wrap;
94
+
95
+ Shader shader;
96
+
97
+ void init ()
98
+ {
99
+ background .reset(0, 0);
100
+ colors[FILL] .reset(1, 1);
101
+ colors[STROKE] .reset(1, 0);
102
+ nocolors[FILL] = false;
103
+ nocolors[STROKE] = true;
104
+ stroke_width = 0;
105
+ stroke_outset = 0;
106
+ stroke_cap = CAP_DEFAULT;
107
+ stroke_join = JOIN_DEFAULT;
108
+ miter_limit = JOIN_DEFAULT_MITER_LIMIT;
109
+ nsegment = 0;
110
+ line_height = -1;
111
+ blend_mode = BLEND_NORMAL;
112
+ clip .reset(-1);
113
+ font = get_default_font();
114
+ texture = Image();
115
+ texcoord_mode = TEXCOORD_IMAGE;
116
+ texcoord_wrap = TEXCOORD_CLAMP;
117
+ shader = Shader();
118
+ }
119
+
120
+ bool get_color (Color* color, ColorType type) const
121
+ {
122
+ const Color& c = colors[type];
123
+ if (blend_mode == BLEND_REPLACE ? nocolors[type] : !c)
124
+ return false;
125
+
126
+ *color = c;
127
+ return true;
128
+ }
129
+
130
+ bool has_color () const
131
+ {
132
+ if (blend_mode == BLEND_REPLACE)
133
+ return !nocolors[FILL] || !nocolors[STROKE];
134
+ else
135
+ return colors[FILL] || colors[STROKE];
136
+ }
137
+
138
+ };// State
139
+
140
+
141
+ struct TextureInfo
142
+ {
143
+
144
+ const Texture& texture;
145
+
146
+ Point min, max;
147
+
148
+ TextureInfo (
149
+ const Texture& texture,
150
+ coord x_min, coord y_min,
151
+ coord x_max, coord y_max)
152
+ : texture(texture)
153
+ {
154
+ min.reset(x_min, y_min);
155
+ max.reset(x_max, y_max);
156
+ }
157
+
158
+ operator bool () const
159
+ {
160
+ return
161
+ texture &&
162
+ min.x < max.x &&
163
+ min.y < max.y;
164
+ }
165
+
166
+ bool operator ! () const
167
+ {
168
+ return !operator bool();
169
+ }
170
+
171
+ };// TextureInfo
172
+
173
+
174
+ struct Painter::Data
175
+ {
176
+
177
+ bool painting = false;
178
+
179
+ float pixel_density = 1;
180
+
181
+ Bounds viewport;
182
+
183
+ State state;
184
+
185
+ std::vector<State> state_stack;
186
+
187
+ Matrix position_matrix;
188
+
189
+ std::vector<Matrix> position_matrix_stack;
190
+
191
+ Image text_image;
192
+
193
+ Data ()
194
+ {
195
+ state.init();
196
+ }
197
+
198
+ virtual ~Data () = default;
199
+
200
+ void set_pixel_density (float density);
201
+
202
+ };// Painter::Data
203
+
204
+
205
+ void Painter_update_clip (Painter* painter);
20
206
 
21
207
  void Painter_draw (
22
- Painter* painter, GLenum mode,
23
- const Coord3* points, size_t npoints,
24
- const uint* indices = NULL, size_t nindices = 0,
25
- const Color* colors = NULL,
26
- const Coord3* texcoords = NULL);
208
+ Painter* painter, PrimitiveMode mode, const Color* color,
209
+ const Coord3* points, size_t npoints,
210
+ const uint* indices = NULL, size_t nindices = 0,
211
+ const Color* colors = NULL,
212
+ const Coord3* texcoords = NULL,
213
+ const TextureInfo* texinfo = NULL,
214
+ const Shader* shader = NULL);
215
+
216
+ void Painter_draw_image (
217
+ Painter* painter, const Image& image,
218
+ coord src_x, coord src_y, coord src_w, coord src_h,
219
+ coord dst_x, coord dst_y, coord dst_w, coord dst_h,
220
+ bool nofill = false, bool nostroke = false,
221
+ const Shader* shader = NULL);
222
+
223
+ void Painter_draw_text_line (
224
+ Painter* painter, const Font& font, const char* line, coord x, coord y,
225
+ coord width = 0, coord height = 0);
27
226
 
28
227
 
29
228
  }// Rays
data/src/polygon.cpp CHANGED
@@ -50,6 +50,32 @@ namespace Rays
50
50
  {
51
51
 
52
52
 
53
+ static void
54
+ draw_polygon (
55
+ Painter* painter, PrimitiveMode mode, const Color& color,
56
+ const Coord3* points, size_t npoints,
57
+ const uint* indices = NULL, size_t nindices = 0,
58
+ const Coord3* texcoords = NULL)
59
+ {
60
+ Painter_draw(
61
+ painter, mode, &color, points, npoints, indices, nindices,
62
+ NULL, texcoords);
63
+ }
64
+
65
+ static void
66
+ draw_polygon (
67
+ Painter* painter, PrimitiveMode mode,
68
+ const Coord3* points, size_t npoints,
69
+ const uint* indices = NULL, size_t nindices = 0,
70
+ const Color* colors = NULL,
71
+ const Coord3* texcoords = NULL)
72
+ {
73
+ Painter_draw(
74
+ painter, mode, NULL, points, npoints, indices, nindices,
75
+ colors, texcoords);
76
+ }
77
+
78
+
53
79
  class Triangles
54
80
  {
55
81
 
@@ -119,8 +145,8 @@ namespace Rays
119
145
 
120
146
  if (pcolors)
121
147
  {
122
- Painter_draw(
123
- painter, GL_TRIANGLES,
148
+ draw_polygon(
149
+ painter, MODE_TRIANGLES,
124
150
  &points[0], points.size(),
125
151
  &indices[0], indices.size(),
126
152
  &(*pcolors)[0],
@@ -128,8 +154,8 @@ namespace Rays
128
154
  }
129
155
  else
130
156
  {
131
- Painter_draw(
132
- painter, GL_TRIANGLES, color,
157
+ draw_polygon(
158
+ painter, MODE_TRIANGLES, color,
133
159
  &points[0], points.size(),
134
160
  &indices[0], indices.size(),
135
161
  ptexcoords ? &(*ptexcoords)[0] : NULL);
@@ -383,8 +409,8 @@ namespace Rays
383
409
 
384
410
  for (const auto& polyline : polylines)
385
411
  {
386
- Painter_draw(
387
- painter, polyline.loop() ? GL_LINE_LOOP : GL_LINE_STRIP, color,
412
+ draw_polygon(
413
+ painter, polyline.loop() ? MODE_LINE_LOOP : MODE_LINE_STRIP, color,
388
414
  &polyline[0], polyline.size());
389
415
  }
390
416
  }
@@ -666,8 +692,7 @@ namespace Rays
666
692
  invalid_state_error(__FILE__, __LINE__);
667
693
 
668
694
  const auto& outline = polylines[0];
669
- Painter_draw(
670
- painter, GL_TRIANGLE_FAN, color, &outline[0], outline.size());
695
+ draw_polygon(painter, MODE_TRIANGLE_FAN, color, &outline[0], outline.size());
671
696
  }
672
697
 
673
698
  private:
@@ -802,7 +827,7 @@ namespace Rays
802
827
 
803
828
  typedef Polygon::Data Super;
804
829
 
805
- GLenum mode = 0;
830
+ PrimitiveMode mode = MODE_NONE;
806
831
 
807
832
  EllipseData (
808
833
  coord x, coord y, coord width, coord height,
@@ -826,7 +851,7 @@ namespace Rays
826
851
  if (polylines.size() <= 0)
827
852
  invalid_state_error(__FILE__, __LINE__);
828
853
 
829
- if (mode == 0)
854
+ if (mode == MODE_NONE)
830
855
  {
831
856
  Super::fill(painter, color);
832
857
  return;
@@ -836,7 +861,7 @@ namespace Rays
836
861
  invalid_state_error(__FILE__, __LINE__);
837
862
 
838
863
  const auto& outline = polylines[0];
839
- Painter_draw(painter, mode, color, &outline[0], outline.size());
864
+ draw_polygon(painter, mode, color, &outline[0], outline.size());
840
865
  }
841
866
 
842
867
  private:
@@ -867,7 +892,7 @@ namespace Rays
867
892
  float radian_from = Xot::deg2rad(0);
868
893
  float radian_to = Xot::deg2rad(360);
869
894
 
870
- if (!has_hole) mode = GL_TRIANGLE_FAN;
895
+ if (!has_hole) mode = MODE_TRIANGLE_FAN;
871
896
 
872
897
  std::vector<Point> points;
873
898
  points.reserve(nsegment);
@@ -915,7 +940,7 @@ namespace Rays
915
940
  if (!has_hole)
916
941
  {
917
942
  points.emplace_back(x + width / 2, y + height / 2);
918
- mode = GL_TRIANGLE_FAN;
943
+ mode = MODE_TRIANGLE_FAN;
919
944
  }
920
945
 
921
946
  for (uint seg = 0; seg <= nsegment; ++seg)
data/src/renderer.h ADDED
@@ -0,0 +1,22 @@
1
+ // -*- c++ -*-
2
+ #pragma once
3
+ #ifndef __RAYS_SRC_RENDERER_H__
4
+ #define __RAYS_SRC_RENDERER_H__
5
+
6
+
7
+ #include "rays/defs.h"
8
+
9
+
10
+ namespace Rays
11
+ {
12
+
13
+
14
+ void Renderer_init ();
15
+
16
+ void Renderer_fin ();
17
+
18
+
19
+ }// Rays
20
+
21
+
22
+ #endif//EOH
data/src/sdl/bitmap.cpp CHANGED
@@ -9,10 +9,8 @@
9
9
  #include <SDL.h>
10
10
  #include <xot/util.h>
11
11
  #include "rays/exception.h"
12
- #include "../color_space.h"
13
12
  #include "../font.h"
14
13
  #include "../texture.h"
15
- #include "../frame_buffer.h"
16
14
 
17
15
 
18
16
  namespace Rays
@@ -50,11 +48,10 @@ namespace Rays
50
48
  };// Bitmap::Data
51
49
 
52
50
 
53
- static void
54
- setup_bitmap (
55
- Bitmap* bitmap,
56
- int w, int h, const ColorSpace& cs,
57
- const void* pixels = NULL, bool clear_pixels = true)
51
+ void
52
+ Bitmap_setup (
53
+ Bitmap* bitmap, int w, int h, const ColorSpace& cs,
54
+ const void* pixels, bool clear_pixels)
58
55
  {
59
56
  if (w <= 0)
60
57
  argument_error(__FILE__, __LINE__);
@@ -104,31 +101,6 @@ namespace Rays
104
101
  SDL_FillRect(self->surface, NULL, 0);
105
102
  }
106
103
 
107
- Bitmap
108
- Bitmap_from (const Texture& tex)
109
- {
110
- if (!tex)
111
- argument_error(__FILE__, __LINE__);
112
-
113
- Bitmap bmp;
114
- setup_bitmap(
115
- &bmp, tex.width(), tex.height(), tex.color_space(), NULL, false);
116
-
117
- GLenum format, type;
118
- ColorSpace_get_gl_format_and_type(&format, &type, tex.color_space());
119
-
120
- FrameBuffer fb(tex);
121
- FrameBufferBinder binder(fb.id());
122
-
123
- for (int y = 0; y < bmp.height(); ++y)
124
- {
125
- GLvoid* ptr = (GLvoid*) bmp.at<uchar>(0, y);
126
- glReadPixels(0, y, bmp.width(), 1, format, type, ptr);
127
- }
128
-
129
- return bmp;
130
- }
131
-
132
104
  void
133
105
  Bitmap_draw_string (
134
106
  Bitmap* bitmap, const RawFont& font,
@@ -248,7 +220,7 @@ namespace Rays
248
220
  Bitmap::Bitmap (
249
221
  int width, int height, const ColorSpace& color_space, const void* pixels)
250
222
  {
251
- setup_bitmap(this, width, height, color_space, pixels);
223
+ Bitmap_setup(this, width, height, color_space, pixels);
252
224
  }
253
225
 
254
226
  Bitmap::~Bitmap ()
data/src/sdl/rays.cpp CHANGED
@@ -4,7 +4,7 @@
4
4
  #include <SDL.h>
5
5
  #include "rays/exception.h"
6
6
  #include "rays/debug.h"
7
- #include "../opengl.h"
7
+ #include "../renderer.h"
8
8
 
9
9
 
10
10
  namespace Rays
@@ -28,7 +28,7 @@ namespace Rays
28
28
  if (SDL_Init(SDL_INIT_VIDEO) < 0)
29
29
  rays_error(__FILE__, __LINE__, SDL_GetError());
30
30
 
31
- OpenGL_init();
31
+ Renderer_init();
32
32
 
33
33
  global::initialized = true;
34
34
  }
@@ -39,7 +39,7 @@ namespace Rays
39
39
  if (!global::initialized)
40
40
  rays_error(__FILE__, __LINE__, "not initialized");
41
41
 
42
- OpenGL_fin();
42
+ Renderer_fin();
43
43
 
44
44
  SDL_Quit();
45
45
 
data/src/texture.h CHANGED
@@ -7,7 +7,6 @@
7
7
  #include <xot/pimpl.h>
8
8
  #include "rays/defs.h"
9
9
  #include "rays/color_space.h"
10
- #include "opengl.h"
11
10
 
12
11
 
13
12
  namespace Rays
@@ -46,8 +45,6 @@ namespace Rays
46
45
 
47
46
  bool smooth () const;
48
47
 
49
- GLuint id () const;
50
-
51
48
  void set_modified (bool modified = true);
52
49
 
53
50
  bool modified () const;
data/src/win32/bitmap.cpp CHANGED
@@ -7,10 +7,8 @@
7
7
  #include <stb_image_write.h>
8
8
 
9
9
  #include "rays/exception.h"
10
- #include "../color_space.h"
11
10
  #include "../font.h"
12
11
  #include "../texture.h"
13
- #include "../frame_buffer.h"
14
12
  #include "gdi.h"
15
13
 
16
14
 
@@ -54,11 +52,10 @@ namespace Rays
54
52
  };// Bitmap::Data
55
53
 
56
54
 
57
- static void
58
- setup_bitmap (
59
- Bitmap* bitmap,
60
- int w, int h, const ColorSpace& cs,
61
- const void* pixels = NULL, bool clear_pixels = true, HDC hdc = NULL)
55
+ void
56
+ Bitmap_setup (
57
+ Bitmap* bitmap, int w, int h, const ColorSpace& cs,
58
+ const void* pixels, bool clear_pixels)
62
59
  {
63
60
  if (w <= 0)
64
61
  argument_error(__FILE__, __LINE__);
@@ -91,7 +88,7 @@ namespace Rays
91
88
  header.biBitCount = self->color_space.bpp();
92
89
  header.biCompression = BI_RGB;
93
90
 
94
- Win32::DC dc = hdc ? Win32::DC(hdc) : Win32::screen_dc();
91
+ Win32::DC dc = Win32::screen_dc();
95
92
 
96
93
  HBITMAP hbmp = CreateDIBSection(
97
94
  dc.handle(), &bmpinfo, DIB_RGB_COLORS, (void**) &self->pixels, NULL, 0);
@@ -109,31 +106,6 @@ namespace Rays
109
106
  memset(self->pixels, 0, size);
110
107
  }
111
108
 
112
- Bitmap
113
- Bitmap_from (const Texture& tex)
114
- {
115
- if (!tex)
116
- argument_error(__FILE__, __LINE__);
117
-
118
- Bitmap bmp;
119
- setup_bitmap(
120
- &bmp, tex.width(), tex.height(), tex.color_space(), NULL, false);
121
-
122
- GLenum format, type;
123
- ColorSpace_get_gl_format_and_type(&format, &type, tex.color_space());
124
-
125
- FrameBuffer fb(tex);
126
- FrameBufferBinder binder(fb.id());
127
-
128
- for (int y = 0; y < bmp.height(); ++y)
129
- {
130
- GLvoid* ptr = (GLvoid*) bmp.at<uchar>(0, y);
131
- glReadPixels(0, y, bmp.width(), 1, format, type, ptr);
132
- }
133
-
134
- return bmp;
135
- }
136
-
137
109
  void
138
110
  Bitmap_draw_string (
139
111
  Bitmap* bitmap, const RawFont& font,
@@ -253,7 +225,7 @@ namespace Rays
253
225
  Bitmap::Bitmap (
254
226
  int width, int height, const ColorSpace& color_space, const void* pixels)
255
227
  {
256
- setup_bitmap(this, width, height, color_space, pixels);
228
+ Bitmap_setup(this, width, height, color_space, pixels);
257
229
  }
258
230
 
259
231
  Bitmap::~Bitmap ()
data/src/win32/rays.cpp CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
  #include "rays/exception.h"
5
- #include "../opengl.h"
5
+ #include "../renderer.h"
6
6
 
7
7
 
8
8
  namespace Rays
@@ -25,7 +25,7 @@ namespace Rays
25
25
 
26
26
  global::initialized = true;
27
27
 
28
- OpenGL_init();
28
+ Renderer_init();
29
29
  }
30
30
 
31
31
  void
@@ -34,7 +34,7 @@ namespace Rays
34
34
  if (!global::initialized)
35
35
  rays_error(__FILE__, __LINE__, "not initialized.");
36
36
 
37
- OpenGL_fin();
37
+ Renderer_fin();
38
38
 
39
39
  global::initialized = false;
40
40
  }