FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
flatbuffer_utils.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPLBASE_FLATBUFFER_UTILS_H
16 #define FPLBASE_FLATBUFFER_UTILS_H
17 
18 #include "fplbase/config.h" // Must come first.
19 
20 #include "common_generated.h"
21 #include "mathfu/constants.h"
22 #include "mathfu/glsl_mappings.h"
23 
24 /// @file fplbase/flatbuffer_utils.h
25 /// @brief Contains helper functions for loading the structs in common.fbs from
26 /// Flatbuffer files.
27 
28 /// @brief Namespace for FPLBase library.
29 namespace fplbase {
30 
31 /// @addtogroup fplbase_flatbuffer_utils
32 /// @{
33 
34 // If defined, treat Flatbuffer types as byte-for-byte equivalents of mathfu
35 // types. This speeds up load operations significantly.
36 // Flatbuffers stores data in little endian, so if the platform is also
37 // little endian, we're byte-for-byte equivalent.
38 #if FLATBUFFERS_LITTLEENDIAN
39 #define FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT 1
40 #endif
41 
42 /// @brief Converts a Vec2.
43 ///
44 /// @param v The Flatbuffer Vec2 to convert.
45 /// @return Returns a converted mathfu vec2.
46 inline mathfu::vec2 LoadVec2(const Vec2* v) {
47 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
48  return mathfu::vec2::FromType(*v);
49 #else
50  // Eschew the constructor that loads contiguous floats because we need to
51  // swap endian for each float. Very few systems are big-endian, so this
52  // code path is more for completeness than practicality. If a big-endian
53  // system does become popular, we should implement endian-swap on load
54  // in a mathfu vec2 function.
55  return mathfu::vec2(v->x(), v->y());
56 #endif
57 }
58 
59 /// @brief Converts a Vec3.
60 ///
61 /// @param v The Flatbuffer Vec3 to convert.
62 /// @return Returns a converted mathfu vec3.
63 inline mathfu::vec3 LoadVec3(const Vec3* v) {
64 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
65  // Even if mathfu::vec3 is padded to be 16-bytes instead of 12-bytes,
66  // FromType() only reads 12-bytes (because we pun to VectorPacked<>).
67  // We do not read off the end of `v`.
68  return mathfu::vec3::FromType(*v);
69 #else
70  return mathfu::vec3(v->x(), v->y(), v->z());
71 #endif
72 }
73 
74 /// @brief Converts a Vec4.
75 ///
76 /// @param v The Flatbuffer Vec4 to convert.
77 /// @return Returns a converted mathfu vec4.
78 inline mathfu::vec4 LoadVec4(const Vec4* v) {
79 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
80  return mathfu::vec4::FromType(*v);
81 #else
82  return mathfu::vec4(v->x(), v->y(), v->z(), v->w());
83 #endif
84 }
85 
86 /// @brief Converts a Vec2i.
87 ///
88 /// @param v The Flatbuffer Vec2i to convert.
89 /// @return Returns a converted mathfu vec2i.
90 inline mathfu::vec2i LoadVec2i(const Vec2i* v) {
91 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
92  return mathfu::vec2i::FromType(*v);
93 #else
94  return mathfu::vec2i(v->x(), v->y());
95 #endif
96 }
97 
98 /// @brief Converts a Vec3i.
99 ///
100 /// @param v The Flatbuffer Vec3i to convert.
101 /// @return Returns a converted mathfu vec3i.
102 inline mathfu::vec3i LoadVec3i(const Vec3i* v) {
103 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
104  return mathfu::vec3i::FromType(*v);
105 #else
106  return mathfu::vec3i(v->x(), v->y(), v->z());
107 #endif
108 }
109 
110 /// @brief Converts a Vec4i.
111 ///
112 /// @param v The Flatbuffer Vec4i to convert.
113 /// @return Returns a converted mathfu vec4i.
114 inline mathfu::vec4i LoadVec4i(const Vec4i* v) {
115 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
116  return mathfu::vec4i::FromType(*v);
117 #else
118  return mathfu::vec4i(v->x(), v->y(), v->z(), v->w());
119 #endif
120 }
121 
122 /// @brief Converts a Axis to the corresponding vec3.
123 ///
124 /// @param axis The Flatbuffer Axis to convert.
125 /// @return Returns the corresponding unit length mathfu vec3.
126 inline mathfu::vec3 LoadAxis(Axis axis) {
127  return axis == Axis_X ? mathfu::kAxisX3f : axis == Axis_Y ? mathfu::kAxisY3f
128  : mathfu::kAxisZ3f;
129 }
130 
131 /// @brief Converts a ColorRGBA to a vec4.
132 ///
133 /// @param c The Flatbuffer ColorRGBA to convert.
134 /// @return Returns a converted mathfu vec4.
135 inline mathfu::vec4 LoadColorRGBA(const ColorRGBA* c) {
136 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
137  return mathfu::vec4::FromType(*c);
138 #else
139  return mathfu::vec4(c->r(), c->g(), c->b(), c->a());
140 #endif
141 }
142 
143 /// @brief Converts a vec4 to a ColorRGBA.
144 ///
145 /// @param v The mathfu vec4 to convert.
146 /// @return Returns a converted ColorRGBA.
147 inline ColorRGBA Vec4ToColorRGBA(const mathfu::vec4& v) {
148 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
149  return mathfu::vec4::ToType<ColorRGBA>(v);
150 #else
151  return ColorRGBA(v.x(), v.y(), v.z(), v.w());
152 #endif
153 }
154 
155 /// @brief Converts a Mat3x4 to a mat4.
156 ///
157 /// Affine transform can be serialized as a 3x4 matrix (three rows of four
158 /// elements). The fourth row is (0,0,0,1). We load the rows into the columns
159 /// of a 4x4 matrix, transpose it, and return it as a `mathfu::AffineTransform`.
160 ///
161 /// @param m The Flatbuffer Mat3x4 to convert.
162 /// @return Returns a converted mathfu mat4.
163 inline mathfu::AffineTransform LoadAffine(const Mat3x4* m) {
164 #if FPLBASE_FLATBUFFER_AND_MATHFU_PACKED_TYPES_EQUIVALENT
165  return mathfu::AffineTransform::FromType(*m);
166 #else
167  const mathfu::vec4 c0 = LoadVec4(&m->c0());
168  const mathfu::vec4 c1 = LoadVec4(&m->c1());
169  const mathfu::vec4 c2 = LoadVec4(&m->c2());
170  return mathfu::mat4::ToAffineTransform(
171  mathfu::mat4(c0, c1, c2, mathfu::kAxisW4f).Transpose());
172 #endif
173 }
174 
175 /// @}
176 } // namespace fplbase
177 
178 #endif // FPLBASE_FLATBUFFER_UTILS_H
mathfu::vec3i LoadVec3i(const Vec3i *v)
Converts a Vec3i.
Definition: flatbuffer_utils.h:102
mathfu::vec2i LoadVec2i(const Vec2i *v)
Converts a Vec2i.
Definition: flatbuffer_utils.h:90
mathfu::vec4i LoadVec4i(const Vec4i *v)
Converts a Vec4i.
Definition: flatbuffer_utils.h:114
mathfu::vec2 LoadVec2(const Vec2 *v)
Converts a Vec2.
Definition: flatbuffer_utils.h:46
mathfu::vec4 LoadColorRGBA(const ColorRGBA *c)
Converts a ColorRGBA to a vec4.
Definition: flatbuffer_utils.h:135
mathfu::AffineTransform LoadAffine(const Mat3x4 *m)
Converts a Mat3x4 to a mat4.
Definition: flatbuffer_utils.h:163
mathfu::vec3 LoadAxis(Axis axis)
Converts a Axis to the corresponding vec3.
Definition: flatbuffer_utils.h:126
mathfu::vec4 LoadVec4(const Vec4 *v)
Converts a Vec4.
Definition: flatbuffer_utils.h:78
mathfu::vec3 LoadVec3(const Vec3 *v)
Converts a Vec3.
Definition: flatbuffer_utils.h:63
ColorRGBA Vec4ToColorRGBA(const mathfu::vec4 &v)
Converts a vec4 to a ColorRGBA.
Definition: flatbuffer_utils.h:147