Pie Noon
An open source project by FPL.
 All Classes Pages
game_camera.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 GAME_CAMERA_H_
16 #define GAME_CAMERA_H_
17 
18 #include <queue>
19 #include "mathfu/glsl_mappings.h"
20 #include "motive/init.h"
21 #include "motive/motivator.h"
22 
23 namespace fpl {
24 namespace pie_noon {
25 
27  mathfu::vec3 position;
28  mathfu::vec3 target;
29 
30  GameCameraState() : position(mathfu::kZeros3f), target(mathfu::kZeros3f) {}
31  GameCameraState(const mathfu::vec3& position, const mathfu::vec3& target)
32  : position(position), target(target) {}
33 
34  bool operator==(const GameCameraState& rhs) const {
35  return position[0] == rhs.position[0] && position[1] == rhs.position[1] &&
36  position[2] == rhs.position[2] && target[0] == rhs.target[0] &&
37  target[1] == rhs.target[1] && target[2] == rhs.target[2];
38  }
39  bool operator!=(const GameCameraState& rhs) const { return !operator==(rhs); }
40 };
41 
42 // Defines a camera movement. These can be queued up in the GameCamera.
44  GameCameraState end;
45  float start_velocity;
46  float time;
47  motive::SplineInit init;
48 };
49 
50 // Class that encapsilates camera motion.
51 class GameCamera {
52  public:
53  // Reset the camera to have initial position and target of 'state'.
54  void Initialize(const GameCameraState& state, motive::MotiveEngine* engine);
55 
56  // Update the camera's motion. Must be called every frame.
57  void AdvanceFrame(WorldTime delta_time);
58 
59  // Enqueue a motion for the camera. When a motion is complete, the next
60  // motion will be executed.
61  void QueueMovement(const GameCameraMovement& movement) {
62  movements_.push(movement);
63  }
64 
65  // Empty the motion queue.
66  void TerminateMovements();
67 
68  // Terminate all movements and force the camera to a specific position.
69  // Maintain the current facing direction. Useful for implementing a
70  // free-cam for debugging.
71  void OverridePosition(const mathfu::vec3& position);
72 
73  // Terminate all movements and force the camera to face a certain direction.
74  void OverrideTarget(const mathfu::vec3& target);
75 
76  // Current position and target of the camera.
77  GameCameraState CurrentState() const {
78  return GameCameraState(Position(), Target());
79  }
80 
81  // Current camera position.
82  mathfu::vec3 Position() const;
83 
84  // Current camera target. That is, what the camera is facing.
85  mathfu::vec3 Target() const;
86 
87  // Unit vector from Position() to Target().
88  mathfu::vec3 Forward() const { return forward_; }
89 
90  // Unit vector to the right of the Forward().
91  mathfu::vec3 Side() const { return side_; }
92 
93  // Unit vector from the top of the camera.
94  // Forward(), Side(), and Up() make an orthonormal basis.
95  mathfu::vec3 Up() const {
96  return mathfu::vec3::CrossProduct(side_, forward_);
97  }
98 
99  // Distance of the camera from its target.
100  float Dist() const { return (Target() - Position()).Length(); }
101 
102  private:
103  void ExecuteMovement(const GameCameraMovement& movement);
104 
105  // MotiveEngine that runs the percent_ Motivator.
106  motive::MotiveEngine* engine_;
107 
108  // Percent we have moved from start_ to end_. Animated with a spline
109  // Motivator.
110  motive::Motivator1f percent_;
111 
112  // The start of the current camera movement. We animate from start_ to end_.
113  GameCameraState start_;
114 
115  // The end of the current camera movement. We animate from start_ to end_.
116  GameCameraState end_;
117 
118  // The direction the camera is facing.
119  mathfu::vec3 forward_;
120 
121  // The direction to the right of the camera.
122  mathfu::vec3 side_;
123 
124  std::queue<GameCameraMovement> movements_;
125 };
126 
127 } // pie_noon
128 } // fpl
129 
130 #endif // GAME_CAMERA_H_
Definition: game_camera.h:26
Definition: game_camera.h:43
Definition: game_camera.h:51