| 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 #pragma once
6
7 #include <cstddef>
8 #include <map>
9
10 #include "flutter/fml/macros.h"
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 FML_DISALLOW_COPY_AND_ASSIGN(VerticesBuilder);
95 };
96
97 //------------------------------------------------------------------------------
98 /// UnskinnedVerticesBuilder
99 ///
100
101 class UnskinnedVerticesBuilder final : public VerticesBuilder {
102 public:
103 struct Vertex {
104 Vector3 position;
105 Vector3 normal;
106 Vector4 tangent;
107 Vector2 texture_coords;
108 Color color = Color::White();
109 };
110
111 UnskinnedVerticesBuilder();
112
113 virtual ~UnskinnedVerticesBuilder() override;
114
115 // |VerticesBuilder|
116 void WriteFBVertices(fb::MeshPrimitiveT& primitive) const override;
117
118 // |VerticesBuilder|
119 void SetAttributeFromBuffer(AttributeType attribute,
120 ComponentType component_type,
121 const void* buffer_start,
122 size_t attribute_stride_bytes,
123 size_t attribute_count) override;
124
125 private:
126 std::vector<Vertex> vertices_;
127
128 FML_DISALLOW_COPY_AND_ASSIGN(UnskinnedVerticesBuilder);
129 };
130
131 //------------------------------------------------------------------------------
132 /// SkinnedVerticesBuilder
133 ///
134
135 class SkinnedVerticesBuilder final : public VerticesBuilder {
136 public:
137 struct Vertex {
138 UnskinnedVerticesBuilder::Vertex vertex;
139 Vector4 joints;
140 Vector4 weights;
141 };
142
143 SkinnedVerticesBuilder();
144
145 virtual ~SkinnedVerticesBuilder() override;
146
147 // |VerticesBuilder|
148 void WriteFBVertices(fb::MeshPrimitiveT& primitive) const override;
149
150 // |VerticesBuilder|
151 void SetAttributeFromBuffer(AttributeType attribute,
152 ComponentType component_type,
153 const void* buffer_start,
154 size_t attribute_stride_bytes,
155 size_t attribute_count) override;
156
157 private:
158 std::vector<Vertex> vertices_;
159
160 FML_DISALLOW_COPY_AND_ASSIGN(SkinnedVerticesBuilder);
161 };
162
163 } // namespace importer
164 } // namespace scene
165 } // namespace impeller
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:15
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:122
impeller::scene::importer::VerticesBuilder::ComponentType::kUnsignedByte
@ kUnsignedByte
impeller::scene::importer::VerticesBuilder
Definition: vertices_builder.h:22
impeller::Vector4
Definition: vector.h:229
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:220
impeller::scene::importer::VerticesBuilder::ComponentType::kUnsignedShort
@ kUnsignedShort
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::tangent
Vector4 tangent
Definition: vertices_builder.h:106
impeller::scene::importer::SkinnedVerticesBuilder::SkinnedVerticesBuilder
SkinnedVerticesBuilder()
impeller::scene::importer::SkinnedVerticesBuilder::Vertex::joints
Vector4 joints
Definition: vertices_builder.h:139
impeller::scene::importer::VerticesBuilder::ComponentType::kSignedInt
@ kSignedInt
impeller::scene::importer::SkinnedVerticesBuilder::Vertex
Definition: vertices_builder.h:137
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:167
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:108
impeller::scene::importer::SkinnedVerticesBuilder::Vertex::vertex
UnskinnedVerticesBuilder::Vertex vertex
Definition: vertices_builder.h:138
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:28
impeller::scene::importer::SkinnedVerticesBuilder::Vertex::weights
Vector4 weights
Definition: vertices_builder.h:140
impeller::scene::importer::VerticesBuilder::ComponentType::kUnsignedInt
@ kUnsignedInt
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::texture_coords
Vector2 texture_coords
Definition: vertices_builder.h:107
impeller::Color::White
static constexpr Color White()
Definition: color.h:254
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex::position
Vector3 position
Definition: vertices_builder.h:104
impeller::scene::importer::UnskinnedVerticesBuilder::Vertex
Definition: vertices_builder.h:103
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:179
impeller::scene::importer::VerticesBuilder::ComponentType::kSignedShort
@ kSignedShort
impeller::scene::importer::SkinnedVerticesBuilder::WriteFBVertices
void WriteFBVertices(fb::MeshPrimitiveT &primitive) const override
Definition: vertices_builder.cc:205
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:139
impeller::TPoint< Scalar >
impeller::scene::importer::SkinnedVerticesBuilder
Definition: vertices_builder.h:135
impeller::scene::importer::VerticesBuilder::MakeUnskinned
static std::unique_ptr< VerticesBuilder > MakeUnskinned()
Definition: vertices_builder.cc:24
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:105
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_context.cc:10
impeller::scene::importer::UnskinnedVerticesBuilder
Definition: vertices_builder.h:101
impeller::Vector3
Definition: vector.h:17
impeller::scene::importer::VerticesBuilder::AttributeType::kTextureCoords
@ kTextureCoords