| Flutter Impeller |

vertices_builder.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef FLUTTER_IMPELLER_SCENE_IMPORTER_VERTICES_BUILDER_H_
6 #define FLUTTER_IMPELLER_SCENE_IMPORTER_VERTICES_BUILDER_H_
7
8 #include <cstddef>
9 #include <map>
10
11 #include "impeller/geometry/matrix.h"
12 #include "impeller/scene/importer/scene_flatbuffers.h"
13
14 namespace impeller {
15 namespace scene {
16 namespace importer {
17
18 //------------------------------------------------------------------------------
19 /// VerticesBuilder
20 ///
21
22 class VerticesBuilder {
23 public:
24 static std::unique_ptr<VerticesBuilder> MakeUnskinned();
25
26 static std::unique_ptr<VerticesBuilder> MakeSkinned();
27
28 enum class ComponentType {
29 kSignedByte = 5120,
30 kUnsignedByte,
31 kSignedShort,
32 kUnsignedShort,
33 kSignedInt,
34 kUnsignedInt,
35 kFloat,
36 };
37
38 enum class AttributeType {
39 kPosition,
40 kNormal,
41 kTangent,
42 kTextureCoords,
43 kColor,
44 kJoints,
45 kWeights,
46 };
47
48 using ComponentConverter = std::function<
49 Scalar(const void* source, size_t byte_offset, bool normalized)>;
50 struct ComponentProperties {
51 size_t size_bytes = 0;
52 ComponentConverter convert_proc;
53 };
54
55 struct AttributeProperties;
56 using AttributeWriter =
57 std::function<void(Scalar* destination,
58 const void* source,
59 const ComponentProperties& component_props,
60 const AttributeProperties& attribute_props)>;
61 struct AttributeProperties {
62 size_t offset_bytes = 0;
63 size_t size_bytes = 0;
64 size_t component_count = 0;
65 AttributeWriter write_proc;
66 };
67
68 VerticesBuilder();
69
70 virtual ~VerticesBuilder();
71
72 virtual void WriteFBVertices(fb::MeshPrimitiveT& primitive) const = 0;
73
74 virtual void SetAttributeFromBuffer(AttributeType attribute,
75 ComponentType component_type,
76 const void* buffer_start,
77 size_t attribute_stride_bytes,
78 size_t attribute_count) = 0;
79
80 protected:
81 static void WriteAttribute(void* destination,
82 size_t destination_stride_bytes,
83 AttributeType attribute,
84 ComponentType component_type,
85 const void* source,
86 size_t attribute_stride_bytes,
87 size_t attribute_count);
88
89 private:
90 static std::map<VerticesBuilder::AttributeType,
91 VerticesBuilder::AttributeProperties>
92 kAttributeTypes;
93
94 VerticesBuilder(const VerticesBuilder&) = delete;
95
96 VerticesBuilder& operator=(const VerticesBuilder&) = delete;
97 };
98
99 //------------------------------------------------------------------------------
100 /// UnskinnedVerticesBuilder
101 ///
102
103 class UnskinnedVerticesBuilder final : public VerticesBuilder {
104 public:
105 struct Vertex {
106 Vector3 position;
107 Vector3 normal;
108 Vector4 tangent;
109 Vector2 texture_coords;
110 Color color = Color::White();
111 };
112
113 UnskinnedVerticesBuilder();
114
115 virtual ~UnskinnedVerticesBuilder() override;
116
117 // |VerticesBuilder|
118 void WriteFBVertices(fb::MeshPrimitiveT& primitive) const override;
119
120 // |VerticesBuilder|
121 void SetAttributeFromBuffer(AttributeType attribute,
122 ComponentType component_type,
123 const void* buffer_start,
124 size_t attribute_stride_bytes,
125 size_t attribute_count) override;
126
127 private:
128 std::vector<Vertex> vertices_;
129
130 UnskinnedVerticesBuilder(const UnskinnedVerticesBuilder&) = delete;
131
132 UnskinnedVerticesBuilder& operator=(const UnskinnedVerticesBuilder&) = delete;
133 };
134
135 //------------------------------------------------------------------------------
136 /// SkinnedVerticesBuilder
137 ///
138
139 class SkinnedVerticesBuilder final : public VerticesBuilder {
140 public:
141 struct Vertex {
142 UnskinnedVerticesBuilder::Vertex vertex;
143 Vector4 joints;
144 Vector4 weights;
145 };
146
147 SkinnedVerticesBuilder();
148
149 virtual ~SkinnedVerticesBuilder() override;
150
151 // |VerticesBuilder|
152 void WriteFBVertices(fb::MeshPrimitiveT& primitive) const override;
153
154 // |VerticesBuilder|
155 void SetAttributeFromBuffer(AttributeType attribute,
156 ComponentType component_type,
157 const void* buffer_start,
158 size_t attribute_stride_bytes,
159 size_t attribute_count) override;
160
161 private:
162 std::vector<Vertex> vertices_;
163
164 SkinnedVerticesBuilder(const SkinnedVerticesBuilder&) = delete;
165
166 SkinnedVerticesBuilder& operator=(const SkinnedVerticesBuilder&) = delete;
167 };
168
169 } // namespace importer
170 } // namespace scene
171 } // namespace impeller
172
173 #endif // FLUTTER_IMPELLER_SCENE_IMPORTER_VERTICES_BUILDER_H_
impeller::scene::importer::VerticesBuilder::VerticesBuilder
VerticesBuilder()
impeller::scene::importer::VerticesBuilder::AttributeType::kNormal
@ kNormal
impeller::scene::importer::VerticesBuilder::ComponentProperties::convert_proc
ComponentConverter convert_proc
Definition: vertices_builder.h:52
impeller::scene::importer::UnskinnedVerticesBuilder::~UnskinnedVerticesBuilder
virtual ~UnskinnedVerticesBuilder() override
impeller::scene::importer::VerticesBuilder::AttributeType::kJoints
@ kJoints
impeller::scene::importer::VerticesBuilder::ComponentProperties::size_bytes
size_t size_bytes
Definition: vertices_builder.h:51
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::scene::importer::VerticesBuilder::AttributeProperties
Definition: vertices_builder.h:61
impeller::scene::importer::VerticesBuilder::AttributeType
AttributeType
Definition: vertices_builder.h:38
impeller::Color
Definition: color.h:124
impeller::scene::importer::VerticesBuilder::ComponentType::kUnsignedByte
@ kUnsignedByte
impeller::scene::importer::VerticesBuilder
Definition: vertices_builder.h:22
impeller::Vector4
Definition: vector.h:232
impeller::scene::importer::VerticesBuilder::AttributeProperties::component_count
size_t component_count
Definition: vertices_builder.h:64
impeller::scene::importer::VerticesBuilder::AttributeType::kWeights
@ kWeights
impeller::scene::importer::SkinnedVerticesBuilder::SetAttributeFromBuffer
void SetAttributeFromBuffer(AttributeType attribute, ComponentType component_type, const void *buffer_start, size_t attribute_stride_bytes, size_t attribute_count) override
Definition: vertices_builder.cc:221
impeller::scene::importer::VerticesBuilder::ComponentType::kUnsignedShort
@ kUnsignedShort
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::tangent
Vector4 tangent
Definition: vertices_builder.h:108
impeller::scene::importer::SkinnedVerticesBuilder::SkinnedVerticesBuilder
SkinnedVerticesBuilder()
impeller::scene::importer::SkinnedVerticesBuilder::Vertex::joints
Vector4 joints
Definition: vertices_builder.h:143
impeller::scene::importer::VerticesBuilder::ComponentType::kSignedInt
@ kSignedInt
impeller::scene::importer::SkinnedVerticesBuilder::Vertex
Definition: vertices_builder.h:141
matrix.h
impeller::scene::importer::VerticesBuilder::AttributeType::kPosition
@ kPosition
impeller::scene::importer::VerticesBuilder::WriteFBVertices
virtual void WriteFBVertices(fb::MeshPrimitiveT &primitive) const =0
impeller::scene::importer::UnskinnedVerticesBuilder::WriteFBVertices
void WriteFBVertices(fb::MeshPrimitiveT &primitive) const override
Definition: vertices_builder.cc:168
impeller::scene::importer::VerticesBuilder::~VerticesBuilder
virtual ~VerticesBuilder()
impeller::scene::importer::VerticesBuilder::AttributeType::kTangent
@ kTangent
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::color
Color color
Definition: vertices_builder.h:110
impeller::scene::importer::SkinnedVerticesBuilder::Vertex::vertex
UnskinnedVerticesBuilder::Vertex vertex
Definition: vertices_builder.h:142
impeller::scene::importer::VerticesBuilder::AttributeWriter
std::function< void(Scalar *destination, const void *source, const ComponentProperties &component_props, const AttributeProperties &attribute_props)> AttributeWriter
Definition: vertices_builder.h:60
impeller::scene::importer::VerticesBuilder::MakeSkinned
static std::unique_ptr< VerticesBuilder > MakeSkinned()
Definition: vertices_builder.cc:29
impeller::scene::importer::SkinnedVerticesBuilder::Vertex::weights
Vector4 weights
Definition: vertices_builder.h:144
impeller::scene::importer::VerticesBuilder::ComponentType::kUnsignedInt
@ kUnsignedInt
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::texture_coords
Vector2 texture_coords
Definition: vertices_builder.h:109
impeller::Color::White
static constexpr Color White()
Definition: color.h:266
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::position
Vector3 position
Definition: vertices_builder.h:106
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex
Definition: vertices_builder.h:105
impeller::scene::importer::VerticesBuilder::AttributeProperties::size_bytes
size_t size_bytes
Definition: vertices_builder.h:63
impeller::scene::importer::UnskinnedVerticesBuilder::UnskinnedVerticesBuilder
UnskinnedVerticesBuilder()
impeller::scene::importer::VerticesBuilder::SetAttributeFromBuffer
virtual void SetAttributeFromBuffer(AttributeType attribute, ComponentType component_type, const void *buffer_start, size_t attribute_stride_bytes, size_t attribute_count)=0
impeller::scene::importer::VerticesBuilder::ComponentProperties
Definition: vertices_builder.h:50
impeller::scene::importer::UnskinnedVerticesBuilder::SetAttributeFromBuffer
void SetAttributeFromBuffer(AttributeType attribute, ComponentType component_type, const void *buffer_start, size_t attribute_stride_bytes, size_t attribute_count) override
Definition: vertices_builder.cc:180
impeller::scene::importer::VerticesBuilder::ComponentType::kSignedShort
@ kSignedShort
impeller::scene::importer::SkinnedVerticesBuilder::WriteFBVertices
void WriteFBVertices(fb::MeshPrimitiveT &primitive) const override
Definition: vertices_builder.cc:206
impeller::scene::importer::VerticesBuilder::WriteAttribute
static void WriteAttribute(void *destination, size_t destination_stride_bytes, AttributeType attribute, ComponentType component_type, const void *source, size_t attribute_stride_bytes, size_t attribute_count)
Definition: vertices_builder.cc:140
impeller::TPoint< Scalar >
impeller::scene::importer::SkinnedVerticesBuilder
Definition: vertices_builder.h:139
impeller::scene::importer::VerticesBuilder::MakeUnskinned
static std::unique_ptr< VerticesBuilder > MakeUnskinned()
Definition: vertices_builder.cc:25
impeller::scene::importer::VerticesBuilder::AttributeProperties::offset_bytes
size_t offset_bytes
Definition: vertices_builder.h:62
impeller::scene::importer::SkinnedVerticesBuilder::~SkinnedVerticesBuilder
virtual ~SkinnedVerticesBuilder() override
impeller::scene::importer::VerticesBuilder::ComponentType::kSignedByte
@ kSignedByte
impeller::scene::importer::VerticesBuilder::AttributeType::kColor
@ kColor
impeller::scene::importer::VerticesBuilder::ComponentConverter
std::function< Scalar(const void *source, size_t byte_offset, bool normalized)> ComponentConverter
Definition: vertices_builder.h:49
impeller::scene::importer::VerticesBuilder::ComponentType::kFloat
@ kFloat
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::normal
Vector3 normal
Definition: vertices_builder.h:107
impeller::scene::importer::VerticesBuilder::AttributeProperties::write_proc
AttributeWriter write_proc
Definition: vertices_builder.h:65
impeller::scene::importer::VerticesBuilder::ComponentType
ComponentType
Definition: vertices_builder.h:28
impeller
Definition: aiks_blend_unittests.cc:18
impeller::scene::importer::UnskinnedVerticesBuilder
Definition: vertices_builder.h:103
impeller::Vector3
Definition: vector.h:20
impeller::scene::importer::VerticesBuilder::AttributeType::kTextureCoords
@ kTextureCoords