rays 0.1.46 → 0.1.48

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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/bitmap.cpp +499 -0
  3. data/.doc/ext/rays/camera.cpp +2 -2
  4. data/.doc/ext/rays/defs.cpp +35 -11
  5. data/.doc/ext/rays/font.cpp +50 -2
  6. data/.doc/ext/rays/native.cpp +2 -4
  7. data/.doc/ext/rays/painter.cpp +111 -6
  8. data/.doc/ext/rays/polygon.cpp +152 -41
  9. data/.doc/ext/rays/polyline.cpp +89 -10
  10. data/.doc/ext/rays/rays.cpp +91 -11
  11. data/.doc/ext/rays/{noise.cpp → util.cpp} +2 -2
  12. data/.github/workflows/test.yml +0 -1
  13. data/ChangeLog.md +38 -0
  14. data/Rakefile +4 -4
  15. data/VERSION +1 -1
  16. data/ext/rays/bitmap.cpp +501 -0
  17. data/ext/rays/camera.cpp +2 -2
  18. data/ext/rays/defs.cpp +35 -11
  19. data/ext/rays/defs.h +56 -3
  20. data/ext/rays/font.cpp +56 -4
  21. data/ext/rays/native.cpp +2 -4
  22. data/ext/rays/painter.cpp +125 -11
  23. data/ext/rays/polygon.cpp +161 -41
  24. data/ext/rays/polyline.cpp +95 -9
  25. data/ext/rays/rays.cpp +91 -11
  26. data/ext/rays/{noise.cpp → util.cpp} +2 -2
  27. data/include/rays/defs.h +24 -0
  28. data/include/rays/font.h +17 -3
  29. data/include/rays/matrix.h +2 -0
  30. data/include/rays/painter.h +29 -1
  31. data/include/rays/polygon.h +57 -33
  32. data/include/rays/polyline.h +20 -1
  33. data/include/rays/ruby/polygon.h +0 -11
  34. data/include/rays/ruby/rays.h +4 -0
  35. data/include/rays/{noise.h → util.h} +2 -2
  36. data/lib/rays/color.rb +1 -1
  37. data/lib/rays/font.rb +1 -1
  38. data/lib/rays/image.rb +1 -1
  39. data/lib/rays/painter.rb +13 -2
  40. data/lib/rays/point.rb +1 -1
  41. data/lib/rays/polygon.rb +54 -16
  42. data/lib/rays/polyline.rb +54 -8
  43. data/lib/rays.rb +0 -1
  44. data/rays.gemspec +2 -2
  45. data/src/color_space.cpp +2 -2
  46. data/src/font.cpp +24 -2
  47. data/src/font.h +8 -1
  48. data/src/ios/font.mm +88 -27
  49. data/src/matrix.cpp +8 -0
  50. data/src/osx/font.mm +90 -28
  51. data/src/osx/helper.h +2 -2
  52. data/src/osx/helper.mm +2 -2
  53. data/src/painter.cpp +227 -90
  54. data/src/painter.h +11 -3
  55. data/src/polygon.cpp +588 -205
  56. data/src/polyline.cpp +154 -28
  57. data/src/polyline.h +3 -5
  58. data/src/shader.cpp +36 -4
  59. data/src/shader.h +1 -1
  60. data/src/texture.cpp +2 -2
  61. data/src/{noise.cpp → util.cpp} +1 -1
  62. data/src/win32/font.cpp +1 -1
  63. data/test/test_bitmap.rb +16 -2
  64. data/test/test_color.rb +4 -0
  65. data/test/test_font.rb +20 -2
  66. data/test/test_image.rb +18 -18
  67. data/test/test_point.rb +1 -1
  68. data/test/test_polygon.rb +52 -45
  69. data/test/test_polyline.rb +191 -72
  70. metadata +11 -17
  71. data/.doc/ext/rays/polygon_line.cpp +0 -97
  72. data/ext/rays/polygon_line.cpp +0 -100
  73. data/lib/rays/polygon_line.rb +0 -33
  74. data/test/test_polygon_line.rb +0 -164
data/src/painter.cpp CHANGED
@@ -49,6 +49,8 @@ namespace Rays
49
49
 
50
50
  coord stroke_width;
51
51
 
52
+ float stroke_outset;
53
+
52
54
  CapType stroke_cap;
53
55
 
54
56
  JoinType stroke_join;
@@ -63,6 +65,12 @@ namespace Rays
63
65
 
64
66
  Font font;
65
67
 
68
+ Image texture;
69
+
70
+ TexCoordMode texcoord_mode;
71
+
72
+ TexCoordWrap texcoord_wrap;
73
+
66
74
  Shader shader;
67
75
 
68
76
  void init ()
@@ -71,13 +79,17 @@ namespace Rays
71
79
  colors[FILL] .reset(1, 1);
72
80
  colors[STROKE] .reset(1, 0);
73
81
  stroke_width = 0;
82
+ stroke_outset = 0;
74
83
  stroke_cap = CAP_DEFAULT;
75
84
  stroke_join = JOIN_DEFAULT;
76
85
  miter_limit = JOIN_DEFAULT_MITER_LIMIT;
77
86
  nsegment = 0;
78
87
  blend_mode = BLEND_NORMAL;
79
88
  clip .reset(-1);
80
- font = default_font();
89
+ font = get_default_font();
90
+ texture = Image();
91
+ texcoord_mode = TEXCOORD_IMAGE;
92
+ texcoord_wrap = TEXCOORD_CLAMP;
81
93
  shader = Shader();
82
94
  }
83
95
 
@@ -94,7 +106,7 @@ namespace Rays
94
106
 
95
107
  const Texture& texture;
96
108
 
97
- Point texcoord_min, texcoord_max;
109
+ Point min, max;
98
110
 
99
111
  TextureInfo (
100
112
  const Texture& texture,
@@ -102,16 +114,16 @@ namespace Rays
102
114
  coord x_max, coord y_max)
103
115
  : texture(texture)
104
116
  {
105
- texcoord_min.reset(x_min, y_min);
106
- texcoord_max.reset(x_max, y_max);
117
+ min.reset(x_min, y_min);
118
+ max.reset(x_max, y_max);
107
119
  }
108
120
 
109
121
  operator bool () const
110
122
  {
111
123
  return
112
124
  texture &&
113
- texcoord_min.x < texcoord_max.x &&
114
- texcoord_min.y < texcoord_max.y;
125
+ min.x < max.x &&
126
+ min.y < max.y;
115
127
  }
116
128
 
117
129
  bool operator ! () const
@@ -307,13 +319,14 @@ namespace Rays
307
319
  return true;
308
320
  }
309
321
 
310
- void draw_polygon (
311
- GLenum mode, const Color& color,
322
+ void draw (
323
+ GLenum mode, const Color* color,
312
324
  const Coord3* points, size_t npoints,
313
325
  const uint* indices = NULL, size_t nindices = 0,
326
+ const Color* colors = NULL,
314
327
  const Coord3* texcoords = NULL,
315
- const Shader& default_shader = Shader_get_default_shader_for_shape(),
316
- const TextureInfo* texinfo = NULL)
328
+ const TextureInfo* texinfo = NULL,
329
+ const Shader* shader = NULL)
317
330
  {
318
331
  if (!points || npoints <= 0)
319
332
  argument_error(__FILE__, __LINE__);
@@ -321,15 +334,18 @@ namespace Rays
321
334
  if (!painting)
322
335
  invalid_state_error(__FILE__, __LINE__, "'painting' should be true.");
323
336
 
324
- const Shader& shader = state.shader ? state.shader : default_shader;
325
- const ShaderProgram* program = Shader_get_program(shader);
337
+ std::unique_ptr<TextureInfo> ptexinfo;
338
+ texinfo = setup_texinfo(texinfo, ptexinfo);
339
+ shader = setup_shader(shader, texinfo);
340
+
341
+ const ShaderProgram* program = Shader_get_program(*shader);
326
342
  if (!program || !*program) return;
327
343
 
328
344
  ShaderProgram_activate(*program);
329
345
 
330
- const auto& names = Shader_get_builtin_variable_names(shader);
346
+ const auto& names = Shader_get_builtin_variable_names(*shader);
331
347
  apply_builtin_uniforms(*program, names, texinfo);
332
- apply_attributes(*program, names, points, npoints, texcoords, color);
348
+ apply_attributes(*program, names, points, npoints, texcoords, color, colors);
333
349
  draw_indices(mode, indices, nindices, npoints);
334
350
  cleanup();
335
351
 
@@ -342,6 +358,27 @@ namespace Rays
342
358
 
343
359
  std::vector<GLuint> buffers;
344
360
 
361
+ const TextureInfo* setup_texinfo (const TextureInfo* texinfo, auto& ptr)
362
+ {
363
+ if (texinfo) return texinfo;
364
+
365
+ const Texture* tex =
366
+ state.texture ? &Image_get_texture(state.texture) : NULL;
367
+ if (!tex) return NULL;
368
+
369
+ ptr.reset(new TextureInfo(*tex, 0, 0, tex->width(), tex->height()));
370
+ return ptr.get();
371
+ }
372
+
373
+ const Shader* setup_shader (const Shader* shader, bool for_texture)
374
+ {
375
+ if (state.shader) return &state.shader;
376
+ if (shader) return shader;
377
+ return for_texture
378
+ ? &Shader_get_default_shader_for_texture(state.texcoord_wrap)
379
+ : &Shader_get_default_shader_for_shape();
380
+ }
381
+
345
382
  void apply_builtin_uniforms (
346
383
  const ShaderProgram& program, const ShaderBuiltinVariableNames& names,
347
384
  const TextureInfo* texinfo)
@@ -351,9 +388,10 @@ namespace Rays
351
388
  Matrix texcoord_matrix(1);
352
389
  if (texture && *texture)
353
390
  {
391
+ bool normal = state.texcoord_mode == TEXCOORD_NORMAL;
354
392
  texcoord_matrix.scale(
355
- 1.0 / texture->reserved_width(),
356
- 1.0 / texture->reserved_height());
393
+ (normal ? texture->width() : 1.0) / texture->reserved_width(),
394
+ (normal ? texture->height() : 1.0) / texture->reserved_height());
357
395
  }
358
396
 
359
397
  for (const auto& name : names.uniform_position_matrix_names)
@@ -371,24 +409,27 @@ namespace Rays
371
409
 
372
410
  if (!texinfo || !texture || !*texture) return;
373
411
 
412
+ coord tw = texture->reserved_width();
413
+ coord th = texture->reserved_height();
414
+ Point min(texinfo->min.x / tw, texinfo->min.y / th);
415
+ Point max(texinfo->max.x / tw, texinfo->max.y / th);
416
+ Point offset( 1 / tw, 1 / th);
417
+
374
418
  for (const auto& name : names.uniform_texcoord_min_names)
375
419
  {
376
420
  apply_uniform(program, name, [&](GLint loc) {
377
- Point min = texcoord_matrix * texinfo->texcoord_min;
378
421
  glUniform3fv(loc, 1, min.array);
379
422
  });
380
423
  }
381
424
  for (const auto& name : names.uniform_texcoord_max_names)
382
425
  {
383
426
  apply_uniform(program, name, [&](GLint loc) {
384
- Point max = texcoord_matrix * texinfo->texcoord_max;
385
427
  glUniform3fv(loc, 1, max.array);
386
428
  });
387
429
  }
388
430
  for (const auto& name : names.uniform_texcoord_offset_names)
389
431
  {
390
432
  apply_uniform(program, name, [&](GLint loc) {
391
- Point offset(1.0 / texture->width(), 1.0 / texture->height());
392
433
  glUniform3fv(loc, 1, offset.array);
393
434
  });
394
435
  }
@@ -409,9 +450,10 @@ namespace Rays
409
450
  void apply_attributes (
410
451
  const ShaderProgram& program, const ShaderBuiltinVariableNames& names,
411
452
  const Coord3* points, size_t npoints, const Coord3* texcoords,
412
- const Color& color)
453
+ const Color* color, const Color* colors)
413
454
  {
414
- assert(points && npoints > 0);
455
+ assert(npoints > 0);
456
+ assert(!!color != !!colors);
415
457
 
416
458
  apply_attribute(
417
459
  program, names.attribute_position_names,
@@ -421,21 +463,30 @@ namespace Rays
421
463
  program, names.attribute_texcoord_names,
422
464
  texcoords ? texcoords : points, npoints);
423
465
 
424
- #if defined(GL_VERSION_2_1) && !defined(GL_VERSION_3_0)
425
- // to fix that GL 2.1 with glVertexAttrib4fv() draws nothing
426
- // with specific glsl 'attribute' name.
427
- std::vector<Color> colors(npoints, color);
428
- apply_attribute(
429
- program, names.attribute_color_names,
430
- (const Coord4*) &colors[0], npoints);
431
- #else
432
- for (const auto& name : names.attribute_color_names)
466
+ if (colors)
433
467
  {
434
- apply_attribute(program, name, [&](GLint loc) {
435
- glVertexAttrib4fv(loc, color.array);
436
- });
468
+ apply_attribute(
469
+ program, names.attribute_color_names,
470
+ colors, npoints);
437
471
  }
472
+ else if (color)
473
+ {
474
+ #if defined(GL_VERSION_2_1) && !defined(GL_VERSION_3_0)
475
+ // to fix that GL 2.1 with glVertexAttrib4fv() draws nothing
476
+ // with specific glsl 'attribute' name.
477
+ std::vector<Color> colors_(npoints, *color);
478
+ apply_attribute(
479
+ program, names.attribute_color_names,
480
+ (const Coord4*) &colors_[0], npoints);
481
+ #else
482
+ for (const auto& name : names.attribute_color_names)
483
+ {
484
+ apply_attribute(program, name, [&](GLint loc) {
485
+ glVertexAttrib4fv(loc, color->array);
486
+ });
487
+ }
438
488
  #endif
489
+ }
439
490
  }
440
491
 
441
492
  template <typename CoordN>
@@ -554,52 +605,27 @@ namespace Rays
554
605
  };// Painter::Data
555
606
 
556
607
 
557
- static void
558
- draw_polygon (
559
- Painter* painter,
560
- const GLenum* modes,
561
- coord offset_x, coord offset_y,
562
- bool nofill, bool nostroke,
563
- const Coord3* points, size_t npoints,
564
- const uint* indices = NULL, size_t nindices = 0,
565
- const Coord3* texcoords = NULL,
566
- const Shader& default_shader = Shader_get_default_shader_for_shape(),
567
- const TextureInfo* texinfo = NULL)
608
+ void
609
+ Painter_draw (
610
+ Painter* painter, GLenum mode, const Color& color,
611
+ const Coord3* points, size_t npoints,
612
+ const uint* indices, size_t nindices,
613
+ const Coord3* texcoords)
568
614
  {
569
- assert(painter);
570
-
571
- bool offset = offset_x != 0 || offset_y != 0;
572
- if (offset)
573
- {
574
- painter->push_matrix();
575
- painter->translate(offset_x, offset_y);
576
- }
577
-
578
- Color color;
579
- for (int type = COLOR_TYPE_BEGIN; type < COLOR_TYPE_END; ++type)
580
- {
581
- if ((nofill && type == FILL) || (nostroke && type == STROKE))
582
- continue;
583
-
584
- if (!painter->self->get_color(&color, (ColorType) type))
585
- continue;
586
-
587
- painter->self->draw_polygon(
588
- modes[type], color, points, npoints, indices, nindices, texcoords,
589
- default_shader, texinfo);
590
- }
591
-
592
- if (offset)
593
- painter->pop_matrix();
615
+ painter->self->draw(
616
+ mode, &color, points, npoints, indices, nindices, NULL, texcoords);
594
617
  }
595
618
 
596
619
  void
597
- Painter_draw_polygon (
598
- Painter* painter, GLenum mode, const Color& color,
620
+ Painter_draw (
621
+ Painter* painter, GLenum mode,
599
622
  const Coord3* points, size_t npoints,
600
- const uint* indices, size_t nindices)
623
+ const uint* indices, size_t nindices,
624
+ const Color* colors,
625
+ const Coord3* texcoords)
601
626
  {
602
- painter->self->draw_polygon(mode, color, points, npoints, indices, nindices);
627
+ painter->self->draw(
628
+ mode, NULL, points, npoints, indices, nindices, colors, texcoords);
603
629
  }
604
630
 
605
631
 
@@ -789,33 +815,82 @@ namespace Rays
789
815
  if (Polygon_triangulate(&triangles, polygon))
790
816
  {
791
817
  for (size_t i = 0; i < triangles.size(); i += 3)
792
- {
793
- painter->self->draw_polygon(
794
- GL_LINE_LOOP, invert_color, &triangles[i], 3);
795
- }
818
+ painter->self->draw(GL_LINE_LOOP, &invert_color, &triangles[i], 3);
796
819
  }
797
820
  #endif
798
821
  }
799
822
 
800
- void
801
- Painter::polygon (const Polygon& polygon)
823
+ static void
824
+ draw_polygon (
825
+ Painter* painter, const Polygon& polygon,
826
+ coord x, coord y, coord width = 0, coord height = 0, bool resize = false)
802
827
  {
828
+ Painter::Data* self = painter->self.get();
829
+
803
830
  if (!self->painting)
804
831
  invalid_state_error(__FILE__, __LINE__, "painting flag should be true.");
805
832
 
806
833
  if (!self->state.has_color())
807
834
  return;
808
835
 
836
+ bool translate = x != 0 || y != 0;
837
+ Matrix matrix(nullptr);
838
+ bool backup = false;
839
+
840
+ if (translate || resize)
841
+ {
842
+ matrix = self->position_matrix;
843
+ backup = true;
844
+
845
+ if (translate)
846
+ self->position_matrix.translate(x, y);
847
+
848
+ if (resize)
849
+ {
850
+ const Bounds& b = polygon.bounds();
851
+ self->position_matrix.scale(width / b.width, height / b.height);
852
+ }
853
+ }
854
+
809
855
  Color color;
810
856
 
811
857
  if (self->get_color(&color, FILL))
812
858
  {
813
- Polygon_fill(polygon, this, color);
814
- debug_draw_triangulation(this, polygon, color);
859
+ Polygon_fill(polygon, painter, color);
860
+ debug_draw_triangulation(painter, polygon, color);
815
861
  }
816
862
 
817
863
  if (self->get_color(&color, STROKE))
818
- Polygon_stroke(polygon, this, color);
864
+ Polygon_stroke(polygon, painter, color);
865
+
866
+ if (backup)
867
+ self->position_matrix = matrix;
868
+ }
869
+
870
+ void
871
+ Painter::polygon (const Polygon& polygon, const coord x, coord y)
872
+ {
873
+ draw_polygon(this, polygon, x, y);
874
+ }
875
+
876
+ void
877
+ Painter::polygon (const Polygon& polygon, const Point& position)
878
+ {
879
+ draw_polygon(this, polygon, position.x, position.y);
880
+ }
881
+
882
+ void
883
+ Painter::polygon (
884
+ const Polygon& polygon, coord x, coord y, coord width, coord height)
885
+ {
886
+ draw_polygon(this, polygon, x, y, width, height, true);
887
+ }
888
+
889
+ void
890
+ Painter::polygon (const Polygon& polygon, const Bounds& bounds)
891
+ {
892
+ draw_polygon(
893
+ this, polygon, bounds.x, bounds.y, bounds.width, bounds.height, true);
819
894
  }
820
895
 
821
896
  void
@@ -960,7 +1035,8 @@ namespace Rays
960
1035
  Painter* painter, const Image& image,
961
1036
  coord src_x, coord src_y, coord src_w, coord src_h,
962
1037
  coord dst_x, coord dst_y, coord dst_w, coord dst_h,
963
- bool nostroke = false, const Shader* shader = NULL)
1038
+ bool nofill = false, bool nostroke = false,
1039
+ const Shader* shader = NULL)
964
1040
  {
965
1041
  static const GLenum MODES[] = {GL_TRIANGLE_FAN, GL_LINE_LOOP};
966
1042
 
@@ -996,12 +1072,19 @@ namespace Rays
996
1072
 
997
1073
  TextureInfo texinfo(texture, src_x, src_y, src_x + src_w, src_y + src_h);
998
1074
 
999
- if (!shader)
1000
- shader = &Shader_get_default_shader_for_texture();
1075
+ Color color;
1076
+ for (int type = COLOR_TYPE_BEGIN; type < COLOR_TYPE_END; ++type)
1077
+ {
1078
+ if ((nofill && type == FILL) || (nostroke && type == STROKE))
1079
+ continue;
1001
1080
 
1002
- draw_polygon(
1003
- painter, MODES, 0, 0, false, nostroke, points, 4, NULL, 0, texcoords,
1004
- *shader, &texinfo);
1081
+ if (!painter->self->get_color(&color, (ColorType) type))
1082
+ continue;
1083
+
1084
+ painter->self->draw(
1085
+ MODES[type], &color, points, 4, NULL, 0, NULL, texcoords,
1086
+ &texinfo, shader);
1087
+ }
1005
1088
  }
1006
1089
 
1007
1090
  void
@@ -1174,7 +1257,7 @@ namespace Rays
1174
1257
  painter, self->text_image,
1175
1258
  0, 0, str_w, str_h,
1176
1259
  x, y, str_w, str_h,
1177
- true, &Shader_get_shader_for_text());
1260
+ false, true, &Shader_get_shader_for_text());
1178
1261
 
1179
1262
  debug_draw_text(painter, font, x, y, str_w / density, str_h / density);
1180
1263
  }
@@ -1310,6 +1393,18 @@ namespace Rays
1310
1393
  return self->state.stroke_width;
1311
1394
  }
1312
1395
 
1396
+ void
1397
+ Painter::set_stroke_outset (float outset)
1398
+ {
1399
+ self->state.stroke_outset = outset;
1400
+ }
1401
+
1402
+ float
1403
+ Painter::stroke_outset () const
1404
+ {
1405
+ return self->state.stroke_outset;
1406
+ }
1407
+
1313
1408
  void
1314
1409
  Painter::set_stroke_cap (CapType cap)
1315
1410
  {
@@ -1455,7 +1550,7 @@ namespace Rays
1455
1550
  {
1456
1551
  return
1457
1552
  font.size() == size &&
1458
- font.name() == (name ? name : default_font().name().c_str());
1553
+ font.name() == (name ? name : get_default_font().name().c_str());
1459
1554
  }
1460
1555
 
1461
1556
  void
@@ -1478,6 +1573,48 @@ namespace Rays
1478
1573
  return self->state.font;
1479
1574
  }
1480
1575
 
1576
+ void
1577
+ Painter::set_texture (const Image& image)
1578
+ {
1579
+ self->state.texture = image;
1580
+ }
1581
+
1582
+ void
1583
+ Painter::no_texture ()
1584
+ {
1585
+ self->state.texture = Image();
1586
+ }
1587
+
1588
+ const Image&
1589
+ Painter::texture () const
1590
+ {
1591
+ return self->state.texture;
1592
+ }
1593
+
1594
+ void
1595
+ Painter::set_texcoord_mode (TexCoordMode mode)
1596
+ {
1597
+ self->state.texcoord_mode = mode;
1598
+ }
1599
+
1600
+ TexCoordMode
1601
+ Painter::texcoord_mode () const
1602
+ {
1603
+ return self->state.texcoord_mode;
1604
+ }
1605
+
1606
+ void
1607
+ Painter::set_texcoord_wrap (TexCoordWrap wrap)
1608
+ {
1609
+ self->state.texcoord_wrap = wrap;
1610
+ }
1611
+
1612
+ TexCoordWrap
1613
+ Painter::texcoord_wrap () const
1614
+ {
1615
+ return self->state.texcoord_wrap;
1616
+ }
1617
+
1481
1618
  void
1482
1619
  Painter::set_shader (const Shader& shader)
1483
1620
  {
data/src/painter.h CHANGED
@@ -12,10 +12,18 @@ namespace Rays
12
12
  {
13
13
 
14
14
 
15
- void Painter_draw_polygon (
15
+ void Painter_draw (
16
16
  Painter* painter, GLenum mode, const Color& color,
17
- const Coord3* points, size_t npoints,
18
- const uint* indices = NULL, size_t nindices = 0);
17
+ const Coord3* points, size_t npoints,
18
+ const uint* indices = NULL, size_t nindices = 0,
19
+ const Coord3* texcoords = NULL);
20
+
21
+ 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);
19
27
 
20
28
 
21
29
  }// Rays