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