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
20 int64_t offset{}; // in elements (floats), not bytes
21 halp::attribute_semantic semantic{};
22 halp::attribute_format format{};
23 int components{}; // 1-4 floats
24 std::string name; // For custom semantics; empty = use semantic name
25};
26
27struct mesh {
28 int64_t vertices{};
29 // offset are in "elements", not bytes
30 int64_t pos_offset{}, texcoord_offset{}, normal_offset{}, color_offset{}, tangent_offset{};
31 bool texcoord{};
32 bool normals{};
33 bool colors{};
34 bool tangents{};
35 bool points{};
36 std::vector<extra_attribute> extras;
37};
38
39std::vector<mesh> ObjFromString(
40 std::string_view obj_data
41 , std::string_view mtl_data
42 , float_vec& data);
43
44std::vector<mesh> ObjFromString(
45 std::string_view obj_data
46 , float_vec& data);
47
48template <std::size_t N>
49static void fromGL(float (&from)[N], auto& to)
50{
51 memcpy(to.data(), from, sizeof(float[N]));
52}
53template <std::size_t N>
54static void toGL(auto& from, float (&to)[N])
55{
56 memcpy(to, from.data(), sizeof(float[N]));
57}
58
59inline void rebuild_transform(auto& inputs, auto& outputs)
60{
61 QMatrix4x4 model{};
62 auto& pos = inputs.position;
63 auto& rot = inputs.rotation;
64 auto& sc = inputs.scale;
65
66 model.translate(pos.value.x, pos.value.y, pos.value.z);
67 model.rotate(QQuaternion::fromEulerAngles(rot.value.x, rot.value.y, rot.value.z));
68 model.scale(sc.value.x, sc.value.y, sc.value.z);
69
70 toGL(model, outputs.geometry.transform);
71 outputs.geometry.dirty_transform = true;
72}
73struct PositionControl : halp::xyz_spinboxes_f32<"Position", halp::free_range_min<>>
74{
75 void update(auto& o) { rebuild_transform(o.inputs, o.outputs); }
76};
78 : halp::xyz_spinboxes_f32<"Rotation", halp::range{0., 359.9999999, 0.}>
79{
80 void update(auto& o) { rebuild_transform(o.inputs, o.outputs); }
81};
82struct ScaleControl : halp::xyz_spinboxes_f32<"Scale", halp::range{0.00001, 1000., 1.}>
83{
84 void update(auto& o) { rebuild_transform(o.inputs, o.outputs); }
85};
86
87struct Update
88{
89 void update(auto& obj) { obj.update(); }
90};
91
93{
94 struct
95 {
96 halp_meta(name, "Geometry");
97 halp::position_normals_texcoords_geometry_volume mesh;
98 float transform[16]{};
99 bool dirty_mesh = false;
100 bool dirty_transform = false;
101 } geometry;
102};
103}
Definition TinyObj.hpp:74
Definition TinyObj.hpp:93
Definition TinyObj.hpp:79
Definition TinyObj.hpp:83
Definition TinyObj.hpp:88
Definition TinyObj.hpp:19
Definition TinyObj.hpp:27