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