Pie Noon
An open source project by FPL.
 All Classes Pages
scene_object.h
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 COMPONENTS_SCENEOBJECT_H_
16 #define COMPONENTS_SCENEOBJECT_H_
17 
18 #include "common.h"
19 #include "components_generated.h"
20 #include "corgi/component.h"
21 #include "mathfu/constants.h"
22 #include "motive/motivator.h"
23 #include "scene_description.h"
24 
25 namespace motive {
26 class MatrixInit;
27 }
28 
29 namespace fpl {
30 namespace pie_noon {
31 
32 // Data for scene object components.
34  public:
36  : global_matrix_(mathfu::mat4::Identity()),
37  tint_(mathfu::kOnes4f),
38  renderable_id_(0),
39  variant_(0),
40  visible_(true) {}
41  void Initialize(motive::MotiveEngine* engine);
42 
43  // Set components of the transformation from object-to-local space.
44  // We apply a fixed transformation to objects:
45  // 1. scale
46  // 2. translate to the object's origin
47  // 3. rotate about z, then y, then x
48  // 4. rotate again about z, then y, then x
49  // 5. translate to final location
50  // We have two rotation steps because some objects need to rotate about x
51  // before y, say. For maximum flexibility we could have three rotation
52  // steps, but that seems excessive.
53  //
54  // See comment on TransformMatrixOperations for more information.
55  // TODO: Allow callers to set up their own transformation pipeline, instead
56  // of using this fixed one.
57  void SetRotation(const mathfu::vec3& rotation) {
58  transform_.SetChildValue3f(kRotateAboutX, rotation);
59  }
60  void SetRotationAboutX(float angle) {
61  transform_.SetChildValue1f(kRotateAboutX, angle);
62  }
63  void SetRotationAboutY(float angle) {
64  transform_.SetChildValue1f(kRotateAboutY, angle);
65  }
66  void SetRotationAboutZ(float angle) {
67  transform_.SetChildValue1f(kRotateAboutZ, angle);
68  }
69  void SetRotationAboutAxis(float angle, fplbase::Axis axis) {
70  transform_.SetChildValue1f(kRotateAboutX + axis, angle);
71  }
72  void SetPreRotation(const mathfu::vec3& rotation) {
73  transform_.SetChildValue3f(kPreRotateAboutX, rotation);
74  }
75  void SetPreRotationAboutX(float angle) {
76  transform_.SetChildValue1f(kPreRotateAboutX, angle);
77  }
78  void SetPreRotationAboutY(float angle) {
79  transform_.SetChildValue1f(kPreRotateAboutY, angle);
80  }
81  void SetPreRotationAboutZ(float angle) {
82  transform_.SetChildValue1f(kPreRotateAboutZ, angle);
83  }
84  void SetPreRotationAboutAxis(float angle, fplbase::Axis axis) {
85  transform_.SetChildValue1f(kPreRotateAboutX + axis, angle);
86  }
87  void SetTranslation(const mathfu::vec3& translation) {
88  transform_.SetChildValue3f(kTranslateX, translation);
89  }
90  void SetScale(const mathfu::vec3& scale) {
91  transform_.SetChildValue3f(kScaleX, scale);
92  }
93  void SetScaleX(float scale) { transform_.SetChildValue1f(kScaleX, scale); }
94  void SetScaleY(float scale) { transform_.SetChildValue1f(kScaleY, scale); }
95  void SetScaleZ(float scale) { transform_.SetChildValue1f(kScaleZ, scale); }
96  void SetOriginPoint(const mathfu::vec3& origin) {
97  transform_.SetChildValue3f(kTranslateToOriginX, -origin);
98  }
99 
100  // Get components of the transformation from object-to-local space.
101  mathfu::vec3 Translation() const {
102  return transform_.ChildValue3f(kTranslateX);
103  }
104  mathfu::vec3 Rotation() const {
105  return transform_.ChildValue3f(kRotateAboutX);
106  }
107  mathfu::vec3 Scale() const { return transform_.ChildValue3f(kScaleX); }
108  mathfu::vec3 OriginPoint() const {
109  return transform_.ChildValue3f(kTranslateToOriginX);
110  }
111 
112  const mathfu::mat4& LocalMatrix() const { return transform_.Value(); }
113  const mathfu::vec3 GlobalPosition() const {
114  return global_matrix_.TranslationVector3D();
115  }
116  void set_global_matrix(const mathfu::mat4& m) { global_matrix_ = m; }
117  const mathfu::mat4& global_matrix() const { return global_matrix_; }
118 
119  bool HasParent() const { return parent_.IsValid(); }
120  corgi::EntityRef& parent() { return parent_; }
121  const corgi::EntityRef& parent() const { return parent_; }
122  void set_parent(corgi::EntityRef& parent) { parent_ = parent; }
123 
124  mathfu::vec4 tint() const { return mathfu::vec4(tint_); }
125  void set_tint(const mathfu::vec4& tint) { tint_ = tint; }
126 
127  uint16_t renderable_id() const { return renderable_id_; }
128  void set_renderable_id(uint16_t id) { renderable_id_ = id; }
129 
130  uint16_t variant() const { return variant_; }
131  void set_variant(uint16_t variant) { variant_ = variant; }
132 
133  bool visible() const { return visible_; }
134  void set_visible(bool visible) { visible_ = visible; }
135 
136  private:
137  // Basic matrix operations from with 'transform_.Value()' is calculated.
138  // These operations are applied last-to-first to convert the object from
139  // object space (i.e. the space in which it was authored) to local space
140  // (i.e. the space relative to 'parent_').
141  enum TransformMatrixOperations {
142  kTranslateX,
143  kTranslateY,
144  kTranslateZ,
145  kRotateAboutX,
146  kRotateAboutY,
147  kRotateAboutZ,
148  kPreRotateAboutX,
149  kPreRotateAboutY,
150  kPreRotateAboutZ,
151  kTranslateToOriginX,
152  kTranslateToOriginY,
153  kTranslateToOriginZ,
154  kScaleX,
155  kScaleY,
156  kScaleZ,
157  kNumTransformMatrixOperations
158  };
159 
160  // Position, orientation, and scale (in world-space) of the object.
161  mathfu::mat4 global_matrix_;
162 
163  // Position, orientation, and scale (in local space) of the object.
164  // Composed of the basic matrix operations in TransformMatrixOperations.
165  motive::MatrixMotivator4f transform_;
166 
167  // The parent defines the scene heirarchy. This scene object is positioned
168  // relative to its parent. That is,
169  // global_matrix_ = parent_->global_matrix * transform_.Value()
170  // If no parent is specified, the 'transform_' is assumed to be in global
171  // space already.
172  corgi::EntityRef parent_;
173 
174  // Color of object.
175  mathfu::vec4_packed tint_;
176 
177  // Id of object model to render.
178  uint16_t renderable_id_;
179 
180  // Id of object model to render.
181  uint16_t variant_;
182 
183  // Whether object is currently on-screen or not.
184  bool visible_;
185 };
186 
187 // A sceneobject is "a thing I want to place in the scene and move around."
188 // So it contains basic drawing info.
189 class SceneObjectComponent : public corgi::Component<SceneObjectData> {
190  public:
191  explicit SceneObjectComponent(motive::MotiveEngine* engine)
192  : engine_(engine) {}
193  virtual void AddFromRawData(corgi::EntityRef& entity, const void* data);
194  virtual void InitEntity(corgi::EntityRef& entity);
195  void PopulateScene(SceneDescription* scene);
196 
197  private:
198  void UpdateGlobalMatrix(corgi::EntityRef& entity,
199  std::vector<bool>& matrix_calculated);
200  void UpdateGlobalMatrices();
201  bool VisibleInHierarchy(const corgi::EntityRef& entity) const;
202 
203  motive::MotiveEngine* engine_;
204 };
205 
206 } // pie_noon
207 } // fpl
208 
209 CORGI_REGISTER_COMPONENT(fpl::pie_noon::SceneObjectComponent,
211 
212 #endif // COMPONENTS_SCENEOBJECT_H_
Definition: scene_object.h:189
Definition: scene_description.h:60
Definition: scene_object.h:33