Loading...
Searching...
No Matches
TinyObj.hpp
1#pragma once
2#include <boost/container/vector.hpp>
3#include <halp/controls.hpp>
4#include <halp/geometry.hpp>
5#include <halp/meta.hpp>
6#include <ossia/detail/pod_vector.hpp>
7
8#include <QMatrix4x4>
9#include <QQuaternion>
10
11#include <cstring>
12#include <vector>
13
14#include <string_view>
15namespace Threedim
16{
17using float_vec = boost::container::vector<float, ossia::pod_allocator<float>>;
18
19struct mesh {
20 int64_t vertices{};
21 // offset are in "elements", not bytes
22 int64_t pos_offset{}, texcoord_offset{}, normal_offset{}, color_offset{};
23 bool texcoord{};
24 bool normals{};
25 bool colors{};
26 bool points{};
27};
28
29std::vector<mesh> ObjFromString(
30 std::string_view obj_data
31 , std::string_view mtl_data
32 , float_vec& data);
33
34std::vector<mesh> ObjFromString(
35 std::string_view obj_data
36 , float_vec& data);
37
38template <std::size_t N>
39static void fromGL(float (&from)[N], auto& to)
40{
41 memcpy(to.data(), from, sizeof(float[N]));
42}
43template <std::size_t N>
44static void toGL(auto& from, float (&to)[N])
45{
46 memcpy(to, from.data(), sizeof(float[N]));
47}
48
49inline void rebuild_transform(auto& inputs, auto& outputs)
50{
51 QMatrix4x4 model{};
52 auto& pos = inputs.position;
53 auto& rot = inputs.rotation;
54 auto& sc = inputs.scale;
55
56 model.translate(pos.value.x, pos.value.y, pos.value.z);
57 model.rotate(QQuaternion::fromEulerAngles(rot.value.x, rot.value.y, rot.value.z));
58 model.scale(sc.value.x, sc.value.y, sc.value.z);
59
60 toGL(model, outputs.geometry.transform);
61 outputs.geometry.dirty_transform = true;
62}
63struct PositionControl : halp::xyz_spinboxes_f32<"Position", halp::free_range_min<>>
64{
65 void update(auto& o) { rebuild_transform(o.inputs, o.outputs); }
66};
68 : halp::xyz_spinboxes_f32<"Rotation", halp::range{0., 359.9999999, 0.}>
69{
70 void update(auto& o) { rebuild_transform(o.inputs, o.outputs); }
71};
72struct ScaleControl : halp::xyz_spinboxes_f32<"Scale", halp::range{0.00001, 1000., 1.}>
73{
74 void update(auto& o) { rebuild_transform(o.inputs, o.outputs); }
75};
76
77struct Update
78{
79 void update(auto& obj) { obj.update(); }
80};
81
83{
84 struct
85 {
86 halp_meta(name, "Geometry");
87 halp::position_normals_texcoords_geometry_volume mesh;
88 float transform[16]{};
89 bool dirty_mesh = false;
90 bool dirty_transform = false;
91 } geometry;
92};
93}
Definition TinyObj.hpp:64
Definition TinyObj.hpp:83
Definition TinyObj.hpp:69
Definition TinyObj.hpp:73
Definition TinyObj.hpp:78
Definition TinyObj.hpp:19