summaryrefslogtreecommitdiffstats
path: root/dev/MinGfx/src/craft_cam.h
blob: bdfcd7e173c87cc1b7d4d79bc1b0dbf20413cd55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 This file is part of the MinGfx Project.
 
 Copyright (c) 2017,2018 Regents of the University of Minnesota.
 All Rights Reserved.
 
 Original Author(s) of this File:
	Dan Keefe, 2018, University of Minnesota
	
 Author(s) of Significant Updates/Modifications to the File:
	...
 */

#ifndef SRC_CRAFT_CAM_H_
#define SRC_CRAFT_CAM_H_

#include "point2.h"
#include "matrix4.h"
#include "vector2.h"


namespace mingfx {
    

/** This implements a user interface for controlling the camera with the mouse.
 This interface is appropriate for "first person" camera control, as in games
 like Minecraft.
 
 Use the arrow keys or A,S,W,Z keys to move around in the virtual world.
 UP/DOWN and W/Z move forward and back.  LEFT/RIGHT or A/S rotate your body
 to face left or right.  You can rotate and tilt your head to look 
 left/right/up/down by moving the mouse.  In Minecraft, that movement happens
 whenever you move the mouse, regardless of whether you are holding down a
 mouse button, but in some games you want to hold the camera still while you
 use the mouse to draw on the screen or do something else.  It's possible to
 use this interface both ways by calling the MouseMove() function either every
 time the mouse moves, or only when the mouse is in a dragging mode.
 
 Example usage:
 ~~~
 // Create a global or member variable in your MyGraphicsApp class:
 CraftCam cam_;
 
 
 // If you want to always rotate the view with the mouse, use this:
 void MyGraphicsApp::OnMouseMove(const Point2 &pos, const Vector2 &delta) {
    Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
    cam_.OnMouseMove(delta_ndc);
 }

 // Alternatively, if you want to only rotate the view when the mouse button is
 // held down, use this instead.  Call cam_.OnMouseMove() in either one function
 // or the other, but not both!
 // void MyGraphicsApp::OnLeftMouseDrag(const Point2 &pos, const Vector2 &delta) {
 //     Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
 //     cam_.OnMouseMove(delta_ndc);
 // }

 // This tells the camera to simulate walking based on the keyboard keys currently
 // pressed.  You need to pass a pointer to the underlying GLFW window created by
 // GraphicsApp.
 void MyGraphicsApp::UpdateSimulation(double dt)  {
     cam_.UpdateSimulation(dt, window());
 }
 
 void MyGraphicsApp::InitOpenGL() {
     cam_.set_view_matrix(Matrix4::lookAt(Point3(0,2,2), Point3(0,2,0), Vector3(0,1,0)););
 }
 
 void MyGraphicsApp::DrawOpenGL() {
     // draw your scene using the view matrix from the camera
     Matrix4 proj_matrix = Matrix4::perspective(60, aspect_ratio(), 1, 200);
     Matrix4 view_matrix = cam_.view_matrix();
     Matrix4 model_matrix = Matrix4::RotateY(to_radians(45.0));
     quick_shapes.DrawCube(model_matrix, view_matirx, proj_matrix, Color(1,1,1));
 }
 ~~~
 */
class CraftCam {
public:
    
    /// Creates a CraftCam object with an initial view matrix = identity.
    CraftCam();
    
    /// Creates a CraftCam object with the supplied initial view matrix.
    CraftCam(const Matrix4 &initial_view_matrix);
    
    virtual ~CraftCam();
    
    
    // To make the interaction work, the following set of functions need to be
    // called from your GraphicsApp or whatever main application class you use
    // to receive user input events and a draw callback.
    
    /// Call this from your app's UpdateSimulation() method.  This tells the
    /// camera to simulate walking based on the keyboard keys currently pressed.
    /// You need to pass a pointer to the underlying GLFW window created by
    /// GraphicsApp.  Example:
    /// ~~~
    /// void MyGraphicsApp::UpdateSimulation(double dt)  {
    ///     cam_.UpdateSimulation(dt, window());
    /// }
    /// ~~~
    void UpdateSimulation(double dt, GLFWwindow *window_ptr);
    

    /// Call this from your app's OnMouseMove() or On*MouseDrag() method.  Use
    /// OnMouseMove() if you want to always rotate the view with the mouse.
    /// Remember to convert the mouse coordinates (usually reported by window
    /// managers in pixels) into normalized device coordinates:
    /// ~~~
    /// void MyGraphicsApp::OnMouseMove(const Point2 &pos, const Vector2 &delta) {
    ///     Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
    ///     cam_.OnMouseMove(delta_ndc);
    /// }
    /// ~~~
    /// Alternatively, if you want to only rotate the view when the mouse button is
    /// held down, use On*MouseDrag() instead:
    /// ~~~
    /// void MyGraphicsApp::OnLeftMouseDrag(const Point2 &pos, const Vector2 &delta) {
    ///     Vector2 delta_ndc = PixelsToNormalizedDeviceCoords(pos);
    ///     cam_.OnMouseMove(delta_ndc);
    /// }
    /// ~~~
    void OnMouseMove(const Vector2 &normalized_mouse_delta);

    
    /// Access the camera view matrix created by the CraftCam interactions via
    /// this method and use it to draw the geometry in your scence.
    /// For example, within GraphicsApp::DrawUsingOpenGL(), you might have:
    /// ~~~
    /// Matrix4 P = Matrix4::Perspective(30, aspect_ratio(), 1, 20);
    /// Matrix4 V = cam.view_matrix();
    /// Matrix4 M = Matrix4::RotateY(GfxMath::ToRadians(45.0));
    /// quick_shapes.DrawCube(M, V, P, Color(1,1,1));
    /// ~~~
    Matrix4 view_matrix();
    
    
    /// Returns the "eye" point (i.e., focal point) of the camera in world
    /// space coordinates.
    Point3 eye();
    
    /// Returns the look direction (i.e., -Z axis of the camera matrix) in world
    /// space coordinates.
    Vector3 look();
    

    /// Sets the y value of the camera (i.e., the height).  If you want to set
    /// the entire view matrix, then use set_view_matrix(), but if you just want
    /// to update the height, e.g., while walking around a bumpy terrain, then
    /// use this.
    void UpdateHeight(float new_y_value);

    
    // -------------
    
    /// This is not required, but you may use this if you wish to set an initial
    /// view matrix or reset the view matrix
    void set_view_matrix(Matrix4 view_matrix);

 
    /// This is the scale factor used to speed up / slow down forward/backward
    /// translation when walking for the UP / DOWN keys.  It defaults to 1.0,
    /// smaller values will make the camera walk slower, larger values will
    /// speed it up.
    float translation_scale();

    /// This is the scale factor used to speed up / slow down forward/backward
    /// translation when walking for the UP / DOWN keys.  It defaults to 1.0,
    /// smaller values will make the camera walk slower, larger values will
    /// speed it up.
    void set_translation_scale(float s);

    /// This is the scale factor used to speed up / slow down left/right
    /// rotation when walking for the LEFT / RIGHT keys.  It defaults to 1.0,
    /// smaller values will make the camera turn slower, larger values will
    /// speed it up.
    float rotation_scale();

    /// This is the scale factor used to speed up / slow down left/right
    /// rotation when walking for the LEFT / RIGHT keys.  It defaults to 1.0,
    /// smaller values will make the camera turn slower, larger values will
    /// speed it up.
    void set_rotation_scale(float s);
    
    /// This is the scale factor used to speed up / slow down looking around
    /// when moving the head with the mouse.  It defaults to 1.0, smaller values
    /// will make the camera turn slower, larger values will speed it up.
    float look_scale();

    /// This is the scale factor used to speed up / slow down looking around
    /// when moving the head with the mouse.  It defaults to 1.0, smaller values
    /// will make the camera turn slower, larger values will speed it up.
    void set_look_scale(float s);
    
private:
    
    void WalkForward(double dt);
    void WalkBackward(double dt);
    void RotateLeft(double dt);
    void RotateRight(double dt);
    void LookWithMouse(const Vector2 &mouse_delta);
    
    float t_scale_;
    float r_scale_;
    float l_scale_;
    float yaw_;
    float pitch_;
    Matrix4 base_head_;
    Matrix4 added_rot_;
};

    
} // end namespace

#endif