rays 0.1.47 → 0.1.48

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.doc/ext/rays/bitmap.cpp +287 -46
  3. data/.doc/ext/rays/camera.cpp +2 -2
  4. data/.doc/ext/rays/defs.cpp +32 -8
  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 +73 -3
  8. data/.doc/ext/rays/polygon.cpp +131 -97
  9. data/.doc/ext/rays/polyline.cpp +89 -10
  10. data/.doc/ext/rays/rays.cpp +80 -0
  11. data/.doc/ext/rays/{noise.cpp → util.cpp} +2 -2
  12. data/ChangeLog.md +23 -0
  13. data/VERSION +1 -1
  14. data/ext/rays/bitmap.cpp +288 -46
  15. data/ext/rays/camera.cpp +2 -2
  16. data/ext/rays/defs.cpp +32 -8
  17. data/ext/rays/defs.h +56 -3
  18. data/ext/rays/font.cpp +56 -4
  19. data/ext/rays/native.cpp +2 -4
  20. data/ext/rays/painter.cpp +80 -3
  21. data/ext/rays/polygon.cpp +134 -99
  22. data/ext/rays/polyline.cpp +95 -9
  23. data/ext/rays/rays.cpp +80 -0
  24. data/ext/rays/{noise.cpp → util.cpp} +2 -2
  25. data/include/rays/defs.h +24 -26
  26. data/include/rays/font.h +17 -3
  27. data/include/rays/painter.h +14 -0
  28. data/include/rays/polygon.h +56 -37
  29. data/include/rays/polyline.h +17 -2
  30. data/include/rays/ruby/polygon.h +0 -11
  31. data/include/rays/ruby/rays.h +4 -0
  32. data/include/rays/{noise.h → util.h} +2 -2
  33. data/lib/rays/color.rb +1 -1
  34. data/lib/rays/font.rb +1 -1
  35. data/lib/rays/image.rb +1 -1
  36. data/lib/rays/painter.rb +12 -1
  37. data/lib/rays/point.rb +1 -1
  38. data/lib/rays/polygon.rb +44 -35
  39. data/lib/rays/polyline.rb +54 -8
  40. data/lib/rays.rb +0 -1
  41. data/rays.gemspec +1 -1
  42. data/src/font.cpp +24 -2
  43. data/src/font.h +8 -1
  44. data/src/ios/font.mm +88 -27
  45. data/src/osx/font.mm +90 -28
  46. data/src/osx/helper.h +2 -2
  47. data/src/osx/helper.mm +2 -2
  48. data/src/painter.cpp +155 -85
  49. data/src/painter.h +11 -3
  50. data/src/polygon.cpp +404 -315
  51. data/src/polyline.cpp +138 -27
  52. data/src/polyline.h +3 -5
  53. data/src/shader.cpp +36 -4
  54. data/src/shader.h +1 -1
  55. data/src/texture.cpp +2 -2
  56. data/src/{noise.cpp → util.cpp} +1 -1
  57. data/src/win32/font.cpp +1 -1
  58. data/test/test_bitmap.rb +12 -5
  59. data/test/test_color.rb +4 -0
  60. data/test/test_font.rb +20 -2
  61. data/test/test_image.rb +18 -18
  62. data/test/test_point.rb +1 -1
  63. data/test/test_polygon.rb +52 -45
  64. data/test/test_polyline.rb +191 -72
  65. metadata +9 -15
  66. data/.doc/ext/rays/polygon_line.cpp +0 -97
  67. data/ext/rays/polygon_line.cpp +0 -100
  68. data/lib/rays/polygon_line.rb +0 -33
  69. data/test/test_polygon_line.rb +0 -164
data/src/painter.cpp CHANGED
@@ -65,6 +65,12 @@ namespace Rays
65
65
 
66
66
  Font font;
67
67
 
68
+ Image texture;
69
+
70
+ TexCoordMode texcoord_mode;
71
+
72
+ TexCoordWrap texcoord_wrap;
73
+
68
74
  Shader shader;
69
75
 
70
76
  void init ()
@@ -80,7 +86,10 @@ namespace Rays
80
86
  nsegment = 0;
81
87
  blend_mode = BLEND_NORMAL;
82
88
  clip .reset(-1);
83
- font = default_font();
89
+ font = get_default_font();
90
+ texture = Image();
91
+ texcoord_mode = TEXCOORD_IMAGE;
92
+ texcoord_wrap = TEXCOORD_CLAMP;
84
93
  shader = Shader();
85
94
  }
86
95
 
@@ -97,7 +106,7 @@ namespace Rays
97
106
 
98
107
  const Texture& texture;
99
108
 
100
- Point texcoord_min, texcoord_max;
109
+ Point min, max;
101
110
 
102
111
  TextureInfo (
103
112
  const Texture& texture,
@@ -105,16 +114,16 @@ namespace Rays
105
114
  coord x_max, coord y_max)
106
115
  : texture(texture)
107
116
  {
108
- texcoord_min.reset(x_min, y_min);
109
- texcoord_max.reset(x_max, y_max);
117
+ min.reset(x_min, y_min);
118
+ max.reset(x_max, y_max);
110
119
  }
111
120
 
112
121
  operator bool () const
113
122
  {
114
123
  return
115
124
  texture &&
116
- texcoord_min.x < texcoord_max.x &&
117
- texcoord_min.y < texcoord_max.y;
125
+ min.x < max.x &&
126
+ min.y < max.y;
118
127
  }
119
128
 
120
129
  bool operator ! () const
@@ -310,13 +319,14 @@ namespace Rays
310
319
  return true;
311
320
  }
312
321
 
313
- void draw_polygon (
314
- GLenum mode, const Color& color,
322
+ void draw (
323
+ GLenum mode, const Color* color,
315
324
  const Coord3* points, size_t npoints,
316
325
  const uint* indices = NULL, size_t nindices = 0,
326
+ const Color* colors = NULL,
317
327
  const Coord3* texcoords = NULL,
318
- const Shader& default_shader = Shader_get_default_shader_for_shape(),
319
- const TextureInfo* texinfo = NULL)
328
+ const TextureInfo* texinfo = NULL,
329
+ const Shader* shader = NULL)
320
330
  {
321
331
  if (!points || npoints <= 0)
322
332
  argument_error(__FILE__, __LINE__);
@@ -324,15 +334,18 @@ namespace Rays
324
334
  if (!painting)
325
335
  invalid_state_error(__FILE__, __LINE__, "'painting' should be true.");
326
336
 
327
- const Shader& shader = state.shader ? state.shader : default_shader;
328
- 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);
329
342
  if (!program || !*program) return;
330
343
 
331
344
  ShaderProgram_activate(*program);
332
345
 
333
- const auto& names = Shader_get_builtin_variable_names(shader);
346
+ const auto& names = Shader_get_builtin_variable_names(*shader);
334
347
  apply_builtin_uniforms(*program, names, texinfo);
335
- apply_attributes(*program, names, points, npoints, texcoords, color);
348
+ apply_attributes(*program, names, points, npoints, texcoords, color, colors);
336
349
  draw_indices(mode, indices, nindices, npoints);
337
350
  cleanup();
338
351
 
@@ -345,6 +358,27 @@ namespace Rays
345
358
 
346
359
  std::vector<GLuint> buffers;
347
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
+
348
382
  void apply_builtin_uniforms (
349
383
  const ShaderProgram& program, const ShaderBuiltinVariableNames& names,
350
384
  const TextureInfo* texinfo)
@@ -354,9 +388,10 @@ namespace Rays
354
388
  Matrix texcoord_matrix(1);
355
389
  if (texture && *texture)
356
390
  {
391
+ bool normal = state.texcoord_mode == TEXCOORD_NORMAL;
357
392
  texcoord_matrix.scale(
358
- 1.0 / texture->reserved_width(),
359
- 1.0 / texture->reserved_height());
393
+ (normal ? texture->width() : 1.0) / texture->reserved_width(),
394
+ (normal ? texture->height() : 1.0) / texture->reserved_height());
360
395
  }
361
396
 
362
397
  for (const auto& name : names.uniform_position_matrix_names)
@@ -374,24 +409,27 @@ namespace Rays
374
409
 
375
410
  if (!texinfo || !texture || !*texture) return;
376
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
+
377
418
  for (const auto& name : names.uniform_texcoord_min_names)
378
419
  {
379
420
  apply_uniform(program, name, [&](GLint loc) {
380
- Point min = texcoord_matrix * texinfo->texcoord_min;
381
421
  glUniform3fv(loc, 1, min.array);
382
422
  });
383
423
  }
384
424
  for (const auto& name : names.uniform_texcoord_max_names)
385
425
  {
386
426
  apply_uniform(program, name, [&](GLint loc) {
387
- Point max = texcoord_matrix * texinfo->texcoord_max;
388
427
  glUniform3fv(loc, 1, max.array);
389
428
  });
390
429
  }
391
430
  for (const auto& name : names.uniform_texcoord_offset_names)
392
431
  {
393
432
  apply_uniform(program, name, [&](GLint loc) {
394
- Point offset(1.0 / texture->width(), 1.0 / texture->height());
395
433
  glUniform3fv(loc, 1, offset.array);
396
434
  });
397
435
  }
@@ -412,9 +450,10 @@ namespace Rays
412
450
  void apply_attributes (
413
451
  const ShaderProgram& program, const ShaderBuiltinVariableNames& names,
414
452
  const Coord3* points, size_t npoints, const Coord3* texcoords,
415
- const Color& color)
453
+ const Color* color, const Color* colors)
416
454
  {
417
- assert(points && npoints > 0);
455
+ assert(npoints > 0);
456
+ assert(!!color != !!colors);
418
457
 
419
458
  apply_attribute(
420
459
  program, names.attribute_position_names,
@@ -424,21 +463,30 @@ namespace Rays
424
463
  program, names.attribute_texcoord_names,
425
464
  texcoords ? texcoords : points, npoints);
426
465
 
427
- #if defined(GL_VERSION_2_1) && !defined(GL_VERSION_3_0)
428
- // to fix that GL 2.1 with glVertexAttrib4fv() draws nothing
429
- // with specific glsl 'attribute' name.
430
- std::vector<Color> colors(npoints, color);
431
- apply_attribute(
432
- program, names.attribute_color_names,
433
- (const Coord4*) &colors[0], npoints);
434
- #else
435
- for (const auto& name : names.attribute_color_names)
466
+ if (colors)
436
467
  {
437
- apply_attribute(program, name, [&](GLint loc) {
438
- glVertexAttrib4fv(loc, color.array);
439
- });
468
+ apply_attribute(
469
+ program, names.attribute_color_names,
470
+ colors, npoints);
440
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
+ }
441
488
  #endif
489
+ }
442
490
  }
443
491
 
444
492
  template <typename CoordN>
@@ -557,52 +605,27 @@ namespace Rays
557
605
  };// Painter::Data
558
606
 
559
607
 
560
- static void
561
- draw_polygon (
562
- Painter* painter,
563
- const GLenum* modes,
564
- coord offset_x, coord offset_y,
565
- bool nofill, bool nostroke,
566
- const Coord3* points, size_t npoints,
567
- const uint* indices = NULL, size_t nindices = 0,
568
- const Coord3* texcoords = NULL,
569
- const Shader& default_shader = Shader_get_default_shader_for_shape(),
570
- 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)
571
614
  {
572
- assert(painter);
573
-
574
- bool offset = offset_x != 0 || offset_y != 0;
575
- if (offset)
576
- {
577
- painter->push_matrix();
578
- painter->translate(offset_x, offset_y);
579
- }
580
-
581
- Color color;
582
- for (int type = COLOR_TYPE_BEGIN; type < COLOR_TYPE_END; ++type)
583
- {
584
- if ((nofill && type == FILL) || (nostroke && type == STROKE))
585
- continue;
586
-
587
- if (!painter->self->get_color(&color, (ColorType) type))
588
- continue;
589
-
590
- painter->self->draw_polygon(
591
- modes[type], color, points, npoints, indices, nindices, texcoords,
592
- default_shader, texinfo);
593
- }
594
-
595
- if (offset)
596
- painter->pop_matrix();
615
+ painter->self->draw(
616
+ mode, &color, points, npoints, indices, nindices, NULL, texcoords);
597
617
  }
598
618
 
599
619
  void
600
- Painter_draw_polygon (
601
- Painter* painter, GLenum mode, const Color& color,
620
+ Painter_draw (
621
+ Painter* painter, GLenum mode,
602
622
  const Coord3* points, size_t npoints,
603
- const uint* indices, size_t nindices)
623
+ const uint* indices, size_t nindices,
624
+ const Color* colors,
625
+ const Coord3* texcoords)
604
626
  {
605
- painter->self->draw_polygon(mode, color, points, npoints, indices, nindices);
627
+ painter->self->draw(
628
+ mode, NULL, points, npoints, indices, nindices, colors, texcoords);
606
629
  }
607
630
 
608
631
 
@@ -792,10 +815,7 @@ namespace Rays
792
815
  if (Polygon_triangulate(&triangles, polygon))
793
816
  {
794
817
  for (size_t i = 0; i < triangles.size(); i += 3)
795
- {
796
- painter->self->draw_polygon(
797
- GL_LINE_LOOP, invert_color, &triangles[i], 3);
798
- }
818
+ painter->self->draw(GL_LINE_LOOP, &invert_color, &triangles[i], 3);
799
819
  }
800
820
  #endif
801
821
  }
@@ -1015,7 +1035,8 @@ namespace Rays
1015
1035
  Painter* painter, const Image& image,
1016
1036
  coord src_x, coord src_y, coord src_w, coord src_h,
1017
1037
  coord dst_x, coord dst_y, coord dst_w, coord dst_h,
1018
- bool nostroke = false, const Shader* shader = NULL)
1038
+ bool nofill = false, bool nostroke = false,
1039
+ const Shader* shader = NULL)
1019
1040
  {
1020
1041
  static const GLenum MODES[] = {GL_TRIANGLE_FAN, GL_LINE_LOOP};
1021
1042
 
@@ -1051,12 +1072,19 @@ namespace Rays
1051
1072
 
1052
1073
  TextureInfo texinfo(texture, src_x, src_y, src_x + src_w, src_y + src_h);
1053
1074
 
1054
- if (!shader)
1055
- 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;
1080
+
1081
+ if (!painter->self->get_color(&color, (ColorType) type))
1082
+ continue;
1056
1083
 
1057
- draw_polygon(
1058
- painter, MODES, 0, 0, false, nostroke, points, 4, NULL, 0, texcoords,
1059
- *shader, &texinfo);
1084
+ painter->self->draw(
1085
+ MODES[type], &color, points, 4, NULL, 0, NULL, texcoords,
1086
+ &texinfo, shader);
1087
+ }
1060
1088
  }
1061
1089
 
1062
1090
  void
@@ -1229,7 +1257,7 @@ namespace Rays
1229
1257
  painter, self->text_image,
1230
1258
  0, 0, str_w, str_h,
1231
1259
  x, y, str_w, str_h,
1232
- true, &Shader_get_shader_for_text());
1260
+ false, true, &Shader_get_shader_for_text());
1233
1261
 
1234
1262
  debug_draw_text(painter, font, x, y, str_w / density, str_h / density);
1235
1263
  }
@@ -1522,7 +1550,7 @@ namespace Rays
1522
1550
  {
1523
1551
  return
1524
1552
  font.size() == size &&
1525
- font.name() == (name ? name : default_font().name().c_str());
1553
+ font.name() == (name ? name : get_default_font().name().c_str());
1526
1554
  }
1527
1555
 
1528
1556
  void
@@ -1545,6 +1573,48 @@ namespace Rays
1545
1573
  return self->state.font;
1546
1574
  }
1547
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
+
1548
1618
  void
1549
1619
  Painter::set_shader (const Shader& shader)
1550
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