tiny_gltf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +7 -0
- data/.yardopts +2 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +31 -0
- data/LICENSE.txt +21 -0
- data/README.md +63 -0
- data/Rakefile +24 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/tiny_gltf/extconf.rb +5 -0
- data/ext/tiny_gltf/json.hpp +14722 -0
- data/ext/tiny_gltf/rb_tiny_gltf.cpp +55 -0
- data/ext/tiny_gltf/rb_tiny_gltf.h +118 -0
- data/ext/tiny_gltf/rb_tiny_gltf_accessor.cpp +42 -0
- data/ext/tiny_gltf/rb_tiny_gltf_animation.cpp +21 -0
- data/ext/tiny_gltf/rb_tiny_gltf_animation_channel.cpp +13 -0
- data/ext/tiny_gltf/rb_tiny_gltf_animation_sampler.cpp +16 -0
- data/ext/tiny_gltf/rb_tiny_gltf_asset.cpp +15 -0
- data/ext/tiny_gltf/rb_tiny_gltf_buffer.cpp +16 -0
- data/ext/tiny_gltf/rb_tiny_gltf_buffer_view.cpp +16 -0
- data/ext/tiny_gltf/rb_tiny_gltf_camera.cpp +29 -0
- data/ext/tiny_gltf/rb_tiny_gltf_extension_map.cpp +12 -0
- data/ext/tiny_gltf/rb_tiny_gltf_image.cpp +25 -0
- data/ext/tiny_gltf/rb_tiny_gltf_init.c +81 -0
- data/ext/tiny_gltf/rb_tiny_gltf_light.cpp +16 -0
- data/ext/tiny_gltf/rb_tiny_gltf_material.cpp +14 -0
- data/ext/tiny_gltf/rb_tiny_gltf_mesh.cpp +37 -0
- data/ext/tiny_gltf/rb_tiny_gltf_model.cpp +52 -0
- data/ext/tiny_gltf/rb_tiny_gltf_node.cpp +67 -0
- data/ext/tiny_gltf/rb_tiny_gltf_parameter_map.cpp +39 -0
- data/ext/tiny_gltf/rb_tiny_gltf_primitive.cpp +35 -0
- data/ext/tiny_gltf/rb_tiny_gltf_sampler.cpp +16 -0
- data/ext/tiny_gltf/rb_tiny_gltf_scene.cpp +17 -0
- data/ext/tiny_gltf/rb_tiny_gltf_skin.cpp +17 -0
- data/ext/tiny_gltf/rb_tiny_gltf_texture.cpp +14 -0
- data/ext/tiny_gltf/rb_tiny_gltf_types.cpp +229 -0
- data/ext/tiny_gltf/rb_tiny_gltf_value.cpp +32 -0
- data/ext/tiny_gltf/stb_image.h +6509 -0
- data/ext/tiny_gltf/stb_image_write.h +1831 -0
- data/ext/tiny_gltf/tiny_gltf.h +4830 -0
- data/lib/tiny_gltf.rb +260 -0
- data/lib/tiny_gltf/version.rb +3 -0
- data/tiny_gltf.gemspec +43 -0
- metadata +189 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
#define TINYGLTF_IMPLEMENTATION
|
2
|
+
#define STB_IMAGE_IMPLEMENTATION
|
3
|
+
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
4
|
+
#include "rb_tiny_gltf.h"
|
5
|
+
|
6
|
+
/* call-seq: GLTF.load(filename_or_data[, base_dir]) => new Model
|
7
|
+
*
|
8
|
+
* Loads a model from the given file or data string. The data string can be
|
9
|
+
* ASCII or binary GLTF. `base_dir` is used to look up external dependencies
|
10
|
+
* such as images. If `base_dir` is omitted, it defaults to either the
|
11
|
+
* directory containing the file (if loading a file), or `'.'` (if loading a
|
12
|
+
* data string).
|
13
|
+
*
|
14
|
+
* Returns the new model, or raises an error if the model could not be loaded.
|
15
|
+
*/
|
16
|
+
VALUE rb_tgltf_load(int argc, VALUE *argv, VALUE self) {
|
17
|
+
TinyGLTF gltf;
|
18
|
+
VALUE rstr, rbase_dir;
|
19
|
+
rb_scan_args(argc, argv, "11", &rstr, &rbase_dir);
|
20
|
+
VALUE is_file = rb_funcall(rb_cFile, rb_intern("exist?"), 1, rstr);
|
21
|
+
|
22
|
+
if (NIL_P(rbase_dir)) {
|
23
|
+
if (is_file == Qtrue) {
|
24
|
+
rbase_dir = rb_funcall(rb_cFile, rb_intern("dirname"), 1, rstr);
|
25
|
+
} else {
|
26
|
+
rbase_dir = rb_str_new2(".");
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
if (is_file) {
|
31
|
+
VALUE rfile = rb_funcall(rb_cFile, rb_intern("open"), 2, rstr, rb_str_new2("rb"));
|
32
|
+
rstr = rb_funcall(rfile, rb_intern("read"), 0);
|
33
|
+
rb_funcall(rfile, rb_intern("close"), 0);
|
34
|
+
}
|
35
|
+
|
36
|
+
try {
|
37
|
+
Model model;
|
38
|
+
std::string err, warn, base_dir(StringValueCStr(rbase_dir));
|
39
|
+
bool res;
|
40
|
+
if (RSTRING_LEN(rstr) > 0 && *RSTRING_PTR(rstr) == 'g') { // 'glTF'
|
41
|
+
res = gltf.LoadBinaryFromMemory(&model, &err, &warn,
|
42
|
+
(unsigned char *) RSTRING_PTR(rstr), (unsigned int) RSTRING_LEN(rstr), base_dir);
|
43
|
+
} else {
|
44
|
+
res = gltf.LoadASCIIFromString( &model, &err, &warn,
|
45
|
+
RSTRING_PTR(rstr), (unsigned int) RSTRING_LEN(rstr), base_dir);
|
46
|
+
}
|
47
|
+
if (err.size() > 0) rb_raise(rb_eTinyGLTFError, "%s", err.c_str());
|
48
|
+
if (warn.size() > 0) rb_raise(rb_eTinyGLTFError, "%s", warn.c_str());
|
49
|
+
if (!res) rb_raise(rb_eTinyGLTFError, "unknown failure to load model");
|
50
|
+
return rModel_new(&model);
|
51
|
+
} catch(const std::exception &ex) {
|
52
|
+
rb_raise(rb_eTinyGLTFError, "%s", ex.what());
|
53
|
+
}
|
54
|
+
return Qnil;
|
55
|
+
}
|
@@ -0,0 +1,118 @@
|
|
1
|
+
#ifndef TINY_GLTF_H
|
2
|
+
#define TINY_GLTF_H 1
|
3
|
+
|
4
|
+
#if __cplusplus
|
5
|
+
#include "tiny_gltf.h"
|
6
|
+
using namespace tinygltf;
|
7
|
+
extern "C" {
|
8
|
+
#endif
|
9
|
+
|
10
|
+
#include "ruby.h"
|
11
|
+
|
12
|
+
void Init_tiny_gltf(void);
|
13
|
+
VALUE rb_tgltf_load(int argc, VALUE *argv, VALUE self);
|
14
|
+
|
15
|
+
extern VALUE rb_mTinyGLTF,
|
16
|
+
rb_eTinyGLTFError;
|
17
|
+
|
18
|
+
/*
|
19
|
+
extern VALUE rb_cModel;
|
20
|
+
extern const rb_data_type_t T_Model;
|
21
|
+
VALUE Model_is_equal(VALUE self, VALUE other);
|
22
|
+
VALUE Model_alloc(VALUE klass);
|
23
|
+
*/
|
24
|
+
#define GLTF_TYPE(name) \
|
25
|
+
extern VALUE rb_c ## name; \
|
26
|
+
extern const rb_data_type_t T_ ## name; \
|
27
|
+
VALUE name ## _is_equal(VALUE self, VALUE other); \
|
28
|
+
VALUE name ## _alloc(VALUE klass);
|
29
|
+
|
30
|
+
GLTF_TYPE(Model);
|
31
|
+
GLTF_TYPE(Accessor);
|
32
|
+
GLTF_TYPE(Asset);
|
33
|
+
GLTF_TYPE(Animation);
|
34
|
+
GLTF_TYPE(AnimationSampler);
|
35
|
+
GLTF_TYPE(AnimationChannel);
|
36
|
+
GLTF_TYPE(Buffer);
|
37
|
+
GLTF_TYPE(BufferView);
|
38
|
+
GLTF_TYPE(Material);
|
39
|
+
GLTF_TYPE(Mesh);
|
40
|
+
GLTF_TYPE(Node);
|
41
|
+
GLTF_TYPE(Primitive);
|
42
|
+
GLTF_TYPE(Texture);
|
43
|
+
GLTF_TYPE(Image);
|
44
|
+
GLTF_TYPE(Skin);
|
45
|
+
GLTF_TYPE(Sampler);
|
46
|
+
GLTF_TYPE(Camera);
|
47
|
+
GLTF_TYPE(Scene);
|
48
|
+
GLTF_TYPE(Light);
|
49
|
+
|
50
|
+
VALUE mode_to_sym(int mode);
|
51
|
+
VALUE component_type_to_sym(int type);
|
52
|
+
VALUE texture_filter_to_sym(int filter);
|
53
|
+
VALUE texture_wrap_to_sym(int wrap);
|
54
|
+
VALUE parameter_type_to_sym(int type);
|
55
|
+
VALUE type_to_sym(int type);
|
56
|
+
VALUE image_format_to_sym(int fmt);
|
57
|
+
VALUE texture_format_to_sym(int fmt);
|
58
|
+
VALUE texture_target_to_sym(int tgt);
|
59
|
+
VALUE texture_type_to_sym(int type);
|
60
|
+
VALUE target_to_sym(int tgt);
|
61
|
+
VALUE shader_type_to_sym(int type);
|
62
|
+
|
63
|
+
VALUE Accessor_byte_stride(VALUE self, VALUE buffer_view);
|
64
|
+
|
65
|
+
#if __cplusplus
|
66
|
+
/*
|
67
|
+
VALUE rModel_new(const Model *);
|
68
|
+
*/
|
69
|
+
#define GLTF_RUBY_WRAP(klass) VALUE r ## klass ## _new(const klass *);
|
70
|
+
|
71
|
+
/*
|
72
|
+
static inline Model *Model_unwrap(VALUE val) {
|
73
|
+
Model *a = NULL;
|
74
|
+
TypedData_Get_Struct(val, Model, &T_Model, a);
|
75
|
+
if (a == NULL)
|
76
|
+
rb_raise(rb_eArgError, "BUG: " #name " is NULL");
|
77
|
+
return a;
|
78
|
+
}
|
79
|
+
*/
|
80
|
+
#define GLTF_RUBY_UNWRAP(klass) \
|
81
|
+
static inline klass *klass ## _unwrap(VALUE val) { \
|
82
|
+
klass *a = NULL; \
|
83
|
+
TypedData_Get_Struct(val, klass, &T_ ## klass, a); \
|
84
|
+
if (a == NULL) \
|
85
|
+
rb_raise(rb_eArgError, "BUG: " #klass " is NULL"); \
|
86
|
+
return a; \
|
87
|
+
}
|
88
|
+
|
89
|
+
#define GLTF_RUBY_WRAPPERS(klass) GLTF_RUBY_WRAP(klass); \
|
90
|
+
GLTF_RUBY_UNWRAP(klass);
|
91
|
+
|
92
|
+
GLTF_RUBY_WRAP(ExtensionMap);
|
93
|
+
GLTF_RUBY_WRAP(ParameterMap);
|
94
|
+
GLTF_RUBY_WRAP(Value);
|
95
|
+
|
96
|
+
GLTF_RUBY_WRAPPERS(Asset);
|
97
|
+
GLTF_RUBY_WRAPPERS(Primitive);
|
98
|
+
GLTF_RUBY_WRAPPERS(Model);
|
99
|
+
GLTF_RUBY_WRAPPERS(Accessor);
|
100
|
+
GLTF_RUBY_WRAPPERS(Animation);
|
101
|
+
GLTF_RUBY_WRAPPERS(AnimationSampler);
|
102
|
+
GLTF_RUBY_WRAPPERS(AnimationChannel);
|
103
|
+
GLTF_RUBY_WRAPPERS(Buffer);
|
104
|
+
GLTF_RUBY_WRAPPERS(BufferView);
|
105
|
+
GLTF_RUBY_WRAPPERS(Material);
|
106
|
+
GLTF_RUBY_WRAPPERS(Mesh);
|
107
|
+
GLTF_RUBY_WRAPPERS(Node);
|
108
|
+
GLTF_RUBY_WRAPPERS(Texture);
|
109
|
+
GLTF_RUBY_WRAPPERS(Image);
|
110
|
+
GLTF_RUBY_WRAPPERS(Skin);
|
111
|
+
GLTF_RUBY_WRAPPERS(Sampler);
|
112
|
+
GLTF_RUBY_WRAPPERS(Camera);
|
113
|
+
GLTF_RUBY_WRAPPERS(Scene);
|
114
|
+
GLTF_RUBY_WRAPPERS(Light);
|
115
|
+
}
|
116
|
+
#endif
|
117
|
+
|
118
|
+
#endif /* TINY_GLTF_H */
|
@@ -0,0 +1,42 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rAccessor_new(const Accessor *accessor) {
|
4
|
+
VALUE raccessor = rb_funcall(rb_cAccessor, rb_intern("new"), 0);
|
5
|
+
*Accessor_unwrap(raccessor) = *accessor;
|
6
|
+
|
7
|
+
rb_ivar_set(raccessor, rb_intern("@buffer_view_index"), INT2NUM(accessor->bufferView));
|
8
|
+
rb_ivar_set(raccessor, rb_intern("@component_type"), component_type_to_sym(accessor->componentType));
|
9
|
+
rb_ivar_set(raccessor, rb_intern("@count"), SIZET2NUM(accessor->count));
|
10
|
+
rb_ivar_set(raccessor, rb_intern("@name"), rb_str_new2(accessor->name.c_str()));
|
11
|
+
rb_ivar_set(raccessor, rb_intern("@byte_offset"), SIZET2NUM(accessor->byteOffset));
|
12
|
+
rb_ivar_set(raccessor, rb_intern("@normalized"), accessor->normalized ? Qtrue : Qfalse);
|
13
|
+
rb_ivar_set(raccessor, rb_intern("@type"), type_to_sym(accessor->type));
|
14
|
+
rb_ivar_set(raccessor, rb_intern("@extras"), rValue_new(&accessor->extras));
|
15
|
+
rb_ivar_set(raccessor, rb_intern("@min"), Qnil);
|
16
|
+
rb_ivar_set(raccessor, rb_intern("@max"), Qnil);
|
17
|
+
|
18
|
+
if (accessor->minValues.size() > 0) {
|
19
|
+
VALUE ary = rb_ary_new();
|
20
|
+
for (size_t i = 0; i < accessor->minValues.size(); i++)
|
21
|
+
rb_ary_push(ary, DBL2NUM(accessor->minValues[i]));
|
22
|
+
rb_ivar_set(raccessor, rb_intern("@min"), ary);
|
23
|
+
}
|
24
|
+
|
25
|
+
if (accessor->maxValues.size() > 0) {
|
26
|
+
VALUE ary = rb_ary_new();
|
27
|
+
for (size_t i = 0; i < accessor->maxValues.size(); i++)
|
28
|
+
rb_ary_push(ary, DBL2NUM(accessor->maxValues[i]));
|
29
|
+
rb_ivar_set(raccessor, rb_intern("@max"), ary);
|
30
|
+
}
|
31
|
+
|
32
|
+
return raccessor;
|
33
|
+
}
|
34
|
+
|
35
|
+
/* call-seq: byte_stride(buffer_view) => Numeric
|
36
|
+
*
|
37
|
+
* Returns the calculated byte stride for this accessor and the given buffer
|
38
|
+
* view.
|
39
|
+
*/
|
40
|
+
VALUE Accessor_byte_stride(VALUE self, VALUE buffer_view) {
|
41
|
+
return INT2NUM(Accessor_unwrap(self)->ByteStride(*BufferView_unwrap(buffer_view)));
|
42
|
+
}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rAnimation_new(const Animation *animation) {
|
4
|
+
VALUE ranimation = rb_funcall(rb_cAnimation, rb_intern("new"), 0);
|
5
|
+
// *Animation_unwrap(ranimation) = *animation;
|
6
|
+
|
7
|
+
VALUE rchannels = rb_ary_new();
|
8
|
+
for (size_t i = 0; i < animation->channels.size(); i++)
|
9
|
+
rb_ary_push(rchannels, rAnimationChannel_new(&animation->channels[i]));
|
10
|
+
|
11
|
+
VALUE rsamplers = rb_ary_new();
|
12
|
+
for (size_t i = 0; i < animation->samplers.size(); i++)
|
13
|
+
rb_ary_push(rsamplers, rAnimationSampler_new(&animation->samplers[i]));
|
14
|
+
|
15
|
+
rb_ivar_set(ranimation, rb_intern("@name"), rb_str_new2(animation->name.c_str()));
|
16
|
+
rb_ivar_set(ranimation, rb_intern("@channels"), rchannels);
|
17
|
+
rb_ivar_set(ranimation, rb_intern("@rsamplers"), rsamplers);
|
18
|
+
rb_ivar_set(ranimation, rb_intern("@extras"), rValue_new(&animation->extras));
|
19
|
+
|
20
|
+
return ranimation;
|
21
|
+
}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rAnimationChannel_new(const AnimationChannel *animation_channel) {
|
4
|
+
VALUE ranimation_channel = rb_funcall(rb_cAnimationChannel, rb_intern("new"), 0);
|
5
|
+
// *AnimationChannel_unwrap(ranimation_channel) = *animation_channel;
|
6
|
+
|
7
|
+
rb_ivar_set(ranimation_channel, rb_intern("@sampler_index"), INT2NUM(animation_channel->sampler));
|
8
|
+
rb_ivar_set(ranimation_channel, rb_intern("@target_node_index"), INT2NUM(animation_channel->target_node));
|
9
|
+
rb_ivar_set(ranimation_channel, rb_intern("@target_path"), rb_intern(animation_channel->target_path.c_str()));
|
10
|
+
rb_ivar_set(ranimation_channel, rb_intern("@extras"), rValue_new(&animation_channel->extras));
|
11
|
+
|
12
|
+
return ranimation_channel;
|
13
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rAnimationSampler_new(const AnimationSampler *animation_sampler) {
|
4
|
+
VALUE ranimation_sampler = rb_funcall(rb_cAnimationSampler, rb_intern("new"), 0);
|
5
|
+
// *AnimationSampler_unwrap(ranimation_sampler) = *animation_sampler;
|
6
|
+
|
7
|
+
std::string interp = animation_sampler->interpolation;
|
8
|
+
std::transform(interp.begin(), interp.end(), interp.begin(), ::tolower);
|
9
|
+
|
10
|
+
rb_ivar_set(ranimation_sampler, rb_intern("@input"), INT2NUM(animation_sampler->input));
|
11
|
+
rb_ivar_set(ranimation_sampler, rb_intern("@output"), INT2NUM(animation_sampler->output));
|
12
|
+
rb_ivar_set(ranimation_sampler, rb_intern("@interpolation"), rb_intern(interp.c_str()));
|
13
|
+
rb_ivar_set(ranimation_sampler, rb_intern("@extras"), rValue_new(&animation_sampler->extras));
|
14
|
+
|
15
|
+
return ranimation_sampler;
|
16
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rAsset_new(const Asset *asset) {
|
4
|
+
VALUE rasset = rb_funcall(rb_cAsset, rb_intern("new"), 0);
|
5
|
+
// *Asset_unwrap(rasset) = *asset;
|
6
|
+
|
7
|
+
rb_ivar_set(rasset, rb_intern("@version"), rb_str_new2(asset->version.c_str()));
|
8
|
+
rb_ivar_set(rasset, rb_intern("@generator"), rb_str_new2(asset->generator.c_str()));
|
9
|
+
rb_ivar_set(rasset, rb_intern("@min_version"), rb_str_new2(asset->minVersion.c_str()));
|
10
|
+
rb_ivar_set(rasset, rb_intern("@copyright"), rb_str_new2(asset->copyright.c_str()));
|
11
|
+
rb_ivar_set(rasset, rb_intern("@extensions"), rExtensionMap_new(&asset->extensions));
|
12
|
+
rb_ivar_set(rasset, rb_intern("@extras"), rValue_new(&asset->extras));
|
13
|
+
|
14
|
+
return rasset;
|
15
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rBuffer_new(const Buffer *buf) {
|
4
|
+
VALUE rbuf = rb_funcall(rb_cBuffer, rb_intern("new"), 0);
|
5
|
+
// *Buffer_unwrap(rbuf) = *buf;
|
6
|
+
|
7
|
+
rb_ivar_set(rbuf, rb_intern("@name"), rb_str_new2(buf->name.c_str()));
|
8
|
+
rb_ivar_set(rbuf, rb_intern("@data"), rb_str_new((char *) buf->data.data(), buf->data.size()));
|
9
|
+
rb_ivar_set(rbuf, rb_intern("@uri"), Qnil);
|
10
|
+
rb_ivar_set(rbuf, rb_intern("@extras"), rValue_new(&buf->extras));
|
11
|
+
|
12
|
+
if (buf->uri.size() > 0)
|
13
|
+
rb_ivar_set(rbuf, rb_intern("@uri"), rb_str_new2(buf->uri.c_str()));
|
14
|
+
|
15
|
+
return rbuf;
|
16
|
+
}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rBufferView_new(const BufferView *view) {
|
4
|
+
VALUE rbuf = rb_funcall(rb_cBufferView, rb_intern("new"), 0);
|
5
|
+
*BufferView_unwrap(rbuf) = *view;
|
6
|
+
|
7
|
+
rb_ivar_set(rbuf, rb_intern("@name"), rb_str_new2(view->name.c_str()));
|
8
|
+
rb_ivar_set(rbuf, rb_intern("@buffer_index"), INT2NUM(view->buffer));
|
9
|
+
rb_ivar_set(rbuf, rb_intern("@byte_offset"), SIZET2NUM(view->byteOffset));
|
10
|
+
rb_ivar_set(rbuf, rb_intern("@byte_length"), SIZET2NUM(view->byteLength));
|
11
|
+
rb_ivar_set(rbuf, rb_intern("@byte_stride"), SIZET2NUM(view->byteStride));
|
12
|
+
rb_ivar_set(rbuf, rb_intern("@target"), target_to_sym(view->target));
|
13
|
+
rb_ivar_set(rbuf, rb_intern("@extras"), rValue_new(&view->extras));
|
14
|
+
|
15
|
+
return rbuf;
|
16
|
+
}
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rCamera_new(const Camera *camera) {
|
4
|
+
VALUE rcamera = rb_funcall(rb_cCamera, rb_intern("new"), 0);
|
5
|
+
// *Camera_unwrap(rcamera) = *camera;
|
6
|
+
|
7
|
+
rb_ivar_set(rcamera, rb_intern("@name"), rb_str_new2(camera->name.c_str()));
|
8
|
+
rb_ivar_set(rcamera, rb_intern("@type"), rb_intern(camera->type.c_str()));
|
9
|
+
rb_ivar_set(rcamera, rb_intern("@extensions"), rExtensionMap_new(&camera->extensions));
|
10
|
+
rb_ivar_set(rcamera, rb_intern("@extras"), rValue_new(&camera->extras));
|
11
|
+
|
12
|
+
if (!strcmp(camera->type.c_str(), "perspective")) {
|
13
|
+
rb_ivar_set(rcamera, rb_intern("@aspect_ratio"), DBL2NUM(camera->perspective.aspectRatio));
|
14
|
+
rb_ivar_set(rcamera, rb_intern("@yfov"), DBL2NUM(camera->perspective.yfov));
|
15
|
+
rb_ivar_set(rcamera, rb_intern("@znear"), DBL2NUM(camera->perspective.znear));
|
16
|
+
if (camera->perspective.zfar == 0.0)
|
17
|
+
rb_ivar_set(rcamera, rb_intern("@zfar"), rb_const_get(rb_cFloat, rb_intern("INFINITY")));
|
18
|
+
else
|
19
|
+
rb_ivar_set(rcamera, rb_intern("@zfar"), DBL2NUM(camera->perspective.zfar));
|
20
|
+
} else {
|
21
|
+
rb_ivar_set(rcamera, rb_intern("@xmag"), DBL2NUM(camera->orthographic.xmag));
|
22
|
+
rb_ivar_set(rcamera, rb_intern("@ymag"), DBL2NUM(camera->orthographic.ymag));
|
23
|
+
rb_ivar_set(rcamera, rb_intern("@znear"), DBL2NUM(camera->orthographic.znear));
|
24
|
+
rb_ivar_set(rcamera, rb_intern("@zfar"), DBL2NUM(camera->orthographic.zfar));
|
25
|
+
}
|
26
|
+
|
27
|
+
return rcamera;
|
28
|
+
}
|
29
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rExtensionMap_new(const ExtensionMap *value) {
|
4
|
+
VALUE res = Qnil;
|
5
|
+
|
6
|
+
for (ExtensionMap::const_iterator iterator = value->begin(); iterator != value->end(); iterator++) {
|
7
|
+
if (NIL_P(res)) res = rb_hash_new();
|
8
|
+
rb_hash_aset(res, rb_str_new2(iterator->first.c_str()), rValue_new(&iterator->second));
|
9
|
+
}
|
10
|
+
|
11
|
+
return res;
|
12
|
+
}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rImage_new(const Image *img) {
|
4
|
+
VALUE rimg = rb_funcall(rb_cImage, rb_intern("new"), 0);
|
5
|
+
// *Image_unwrap(rimg) = *img;
|
6
|
+
|
7
|
+
rb_ivar_set(rimg, rb_intern("@name"), rb_str_new2(img->name.c_str()));
|
8
|
+
rb_ivar_set(rimg, rb_intern("@data"), rb_str_new((char *) img->image.data(), img->image.size()));
|
9
|
+
rb_ivar_set(rimg, rb_intern("@uri"), Qnil);
|
10
|
+
rb_ivar_set(rimg, rb_intern("@mime_type"), Qnil);
|
11
|
+
rb_ivar_set(rimg, rb_intern("@extras"), rValue_new(&img->extras));
|
12
|
+
rb_ivar_set(rimg, rb_intern("@width"), INT2NUM(img->width));
|
13
|
+
rb_ivar_set(rimg, rb_intern("@height"), INT2NUM(img->height));
|
14
|
+
rb_ivar_set(rimg, rb_intern("@components"), INT2NUM(img->component));
|
15
|
+
rb_ivar_set(rimg, rb_intern("@buffer_view_index"), INT2NUM(img->bufferView));
|
16
|
+
rb_ivar_set(rimg, rb_intern("@extensions"), rExtensionMap_new(&img->extensions));
|
17
|
+
|
18
|
+
if (img->uri.size() > 0)
|
19
|
+
rb_ivar_set(rimg, rb_intern("@uri"), rb_str_new2(img->uri.c_str()));
|
20
|
+
|
21
|
+
if (img->mimeType.size() > 0)
|
22
|
+
rb_ivar_set(rimg, rb_intern("@mime_type"), rb_str_new2(img->mimeType.c_str()));
|
23
|
+
|
24
|
+
return rimg;
|
25
|
+
}
|
@@ -0,0 +1,81 @@
|
|
1
|
+
#include "rb_tiny_gltf.h"
|
2
|
+
|
3
|
+
VALUE rb_mTinyGLTF,
|
4
|
+
rb_eTinyGLTFError,
|
5
|
+
rb_cModel,
|
6
|
+
rb_cAccessor,
|
7
|
+
rb_cAsset,
|
8
|
+
rb_cAnimation,
|
9
|
+
rb_cAnimationChannel,
|
10
|
+
rb_cAnimationSampler,
|
11
|
+
rb_cBuffer,
|
12
|
+
rb_cBufferView,
|
13
|
+
rb_cMaterial,
|
14
|
+
rb_cMesh,
|
15
|
+
rb_cNode,
|
16
|
+
rb_cPrimitive,
|
17
|
+
rb_cTexture,
|
18
|
+
rb_cImage,
|
19
|
+
rb_cSkin,
|
20
|
+
rb_cSampler,
|
21
|
+
rb_cCamera,
|
22
|
+
rb_cScene,
|
23
|
+
rb_cLight;
|
24
|
+
|
25
|
+
void Init_tiny_gltf(void) {
|
26
|
+
rb_mTinyGLTF = rb_define_module("TinyGLTF");
|
27
|
+
|
28
|
+
// not part of the macro below just to help the yardoc parser out
|
29
|
+
rb_cModel = rb_define_class_under(rb_mTinyGLTF, "Model", rb_cObject);
|
30
|
+
rb_cAccessor = rb_define_class_under(rb_mTinyGLTF, "Accessor", rb_cObject);
|
31
|
+
rb_cAsset = rb_define_class_under(rb_mTinyGLTF, "Asset", rb_cObject);
|
32
|
+
rb_cAnimation = rb_define_class_under(rb_mTinyGLTF, "Animation", rb_cObject);
|
33
|
+
rb_cAnimationChannel = rb_define_class_under(rb_mTinyGLTF, "AnimationChannel", rb_cObject);
|
34
|
+
rb_cAnimationSampler = rb_define_class_under(rb_mTinyGLTF, "AnimationSampler", rb_cObject);
|
35
|
+
rb_cBuffer = rb_define_class_under(rb_mTinyGLTF, "Buffer", rb_cObject);
|
36
|
+
rb_cBufferView = rb_define_class_under(rb_mTinyGLTF, "BufferView", rb_cObject);
|
37
|
+
rb_cMaterial = rb_define_class_under(rb_mTinyGLTF, "Material", rb_cObject);
|
38
|
+
rb_cMesh = rb_define_class_under(rb_mTinyGLTF, "Mesh", rb_cObject);
|
39
|
+
rb_cNode = rb_define_class_under(rb_mTinyGLTF, "Node", rb_cObject);
|
40
|
+
rb_cTexture = rb_define_class_under(rb_mTinyGLTF, "Texture", rb_cObject);
|
41
|
+
rb_cImage = rb_define_class_under(rb_mTinyGLTF, "Image", rb_cObject);
|
42
|
+
rb_cPrimitive = rb_define_class_under(rb_mTinyGLTF, "Primitive", rb_cObject);
|
43
|
+
rb_cSkin = rb_define_class_under(rb_mTinyGLTF, "Skin", rb_cObject);
|
44
|
+
rb_cSampler = rb_define_class_under(rb_mTinyGLTF, "Sampler", rb_cObject);
|
45
|
+
rb_cCamera = rb_define_class_under(rb_mTinyGLTF, "Camera", rb_cObject);
|
46
|
+
rb_cScene = rb_define_class_under(rb_mTinyGLTF, "Scene", rb_cObject);
|
47
|
+
rb_cLight = rb_define_class_under(rb_mTinyGLTF, "Light", rb_cObject);
|
48
|
+
|
49
|
+
/*
|
50
|
+
rb_define_alloc_func(rb_cModel, Model_alloc);
|
51
|
+
rb_define_method(rb_cModel, "==", Model_is_equal, 1);
|
52
|
+
*/
|
53
|
+
#define DEFINE_RB_GLTF_CLASS(name) \
|
54
|
+
rb_define_alloc_func(rb_c ## name, name ## _alloc); \
|
55
|
+
rb_define_method(rb_c ## name, "==", name ## _is_equal, 1);
|
56
|
+
|
57
|
+
DEFINE_RB_GLTF_CLASS(Model);
|
58
|
+
DEFINE_RB_GLTF_CLASS(Accessor);
|
59
|
+
DEFINE_RB_GLTF_CLASS(Asset);
|
60
|
+
DEFINE_RB_GLTF_CLASS(Animation);
|
61
|
+
DEFINE_RB_GLTF_CLASS(AnimationChannel);
|
62
|
+
DEFINE_RB_GLTF_CLASS(AnimationSampler);
|
63
|
+
DEFINE_RB_GLTF_CLASS(Buffer);
|
64
|
+
DEFINE_RB_GLTF_CLASS(BufferView);
|
65
|
+
DEFINE_RB_GLTF_CLASS(Material);
|
66
|
+
DEFINE_RB_GLTF_CLASS(Mesh);
|
67
|
+
DEFINE_RB_GLTF_CLASS(Node);
|
68
|
+
DEFINE_RB_GLTF_CLASS(Texture);
|
69
|
+
DEFINE_RB_GLTF_CLASS(Image);
|
70
|
+
DEFINE_RB_GLTF_CLASS(Primitive);
|
71
|
+
DEFINE_RB_GLTF_CLASS(Skin);
|
72
|
+
DEFINE_RB_GLTF_CLASS(Sampler);
|
73
|
+
DEFINE_RB_GLTF_CLASS(Camera);
|
74
|
+
DEFINE_RB_GLTF_CLASS(Scene);
|
75
|
+
DEFINE_RB_GLTF_CLASS(Light);
|
76
|
+
|
77
|
+
rb_define_method(rb_cAccessor, "byte_stride", Accessor_byte_stride, 1);
|
78
|
+
|
79
|
+
rb_eTinyGLTFError = rb_define_class_under(rb_mTinyGLTF, "Error", rb_eStandardError);
|
80
|
+
rb_define_singleton_method(rb_mTinyGLTF, "load", rb_tgltf_load, -1);
|
81
|
+
}
|