Motive Animation System
An open source project by FPL.
 All Classes Functions Variables Typedefs Friends Pages
vector_converter.h
1 // Copyright 2014 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 MOTIVE_MATH_VECTOR_CONVERTER_H
16 #define MOTIVE_MATH_VECTOR_CONVERTER_H
17 
18 #include "mathfu/glsl_mappings.h"
19 
20 namespace motive {
21 
22 /// @class MathFuVectorConverter
23 /// @brief Convert mathfu types to float pointers.
24 ///
25 /// Create your own converter if you'd like to use your own vector types in
26 /// your Motivators' external API.
28  public:
29  typedef mathfu::vec2 Vector2;
30  typedef mathfu::vec3 Vector3;
31  typedef mathfu::vec4 Vector4;
32  typedef mathfu::mat4 Matrix4;
33 
34  static float* ToPtr(float& f) { return &f; }
35  static float* ToPtr(Vector2& v) { return &v[0]; }
36  static float* ToPtr(Vector3& v) { return &v[0]; }
37  static float* ToPtr(Vector4& v) { return &v[0]; }
38  static float* ToPtr(Matrix4& m) { return &m(0); }
39 
40  static const float* ToPtr(const float& f) { return &f; }
41  static const float* ToPtr(const Vector2& v) { return &v[0]; }
42  static const float* ToPtr(const Vector3& v) { return &v[0]; }
43  static const float* ToPtr(const Vector4& v) { return &v[0]; }
44  static const float* ToPtr(const Matrix4& m) { return &m(0); }
45 
46  static float FromPtr(const float* f, float) { return *f; }
47  static Vector2 FromPtr(const float* f, const Vector2&) { return Vector2(f); }
48  static Vector3 FromPtr(const float* f, const Vector3&) { return Vector3(f); }
49  static Vector4 FromPtr(const float* f, const Vector4&) { return Vector4(f); }
50  static Matrix4 FromPtr(const float* f, const Matrix4&) { return Matrix4(f); }
51 };
52 
53 // Map a dimension onto the external vector type.
54 // External types are specified by the VectorConverter, which is customizable.
55 template <class VectorConverter, int>
56 struct VectorT {
57  typedef void type;
58 };
59 template <class VectorConverter>
60 struct VectorT<VectorConverter, 1> {
61  typedef float type;
62 };
63 template <class VectorConverter>
64 struct VectorT<VectorConverter, 2> {
65  typedef typename VectorConverter::Vector2 type;
66 };
67 template <class VectorConverter>
68 struct VectorT<VectorConverter, 3> {
69  typedef typename VectorConverter::Vector3 type;
70 };
71 template <class VectorConverter>
72 struct VectorT<VectorConverter, 4> {
73  typedef typename VectorConverter::Vector4 type;
74 };
75 
76 } // namespace motive
77 
78 #endif // MOTIVE_MATH_VECTOR_CONVERTER_H
Definition: vector_converter.h:56
Convert mathfu types to float pointers.
Definition: vector_converter.h:27