Phyks (Lucas Verney) 2 years ago
parent
commit
1cc998ef7c
3 changed files with 82 additions and 2 deletions
  1. 44
    1
      src/core/SkiMapRenderer.cc
  2. 8
    1
      src/core/SkiMapRenderer.h
  3. 30
    0
      src/platform/emscripten/main.cc

+ 44
- 1
src/core/SkiMapRenderer.cc View File

@@ -139,7 +139,7 @@ void SkiMapRenderer::on_move(Move::Enum movement) {
139 139
     vec3_mul_cross(side_vector, forward_vector, up_);
140 140
     vec3_norm(side_vector, side_vector);
141 141
 
142
-    // Compute current side vector (normalized)
142
+    // Compute current up vector (normalized)
143 143
     vec3 up_vector = {};
144 144
     vec3_mul_cross(up_vector, side_vector, forward_vector);
145 145
     vec3_norm(up_vector, up_vector);
@@ -156,9 +156,11 @@ void SkiMapRenderer::on_move(Move::Enum movement) {
156 156
             // Compute new position of the camera
157 157
             if (Move::LEFT == movement) {
158 158
                 vec3_sub(eye_, eye_, move_vector);
159
+                vec3_sub(center_, center_, move_vector);
159 160
             }
160 161
             else if (Move::RIGHT == movement) {
161 162
                 vec3_add(eye_, eye_, move_vector);
163
+                vec3_add(center_, center_, move_vector);
162 164
             }
163 165
             else {
164 166
                 // Empty on purpose
@@ -190,9 +192,11 @@ void SkiMapRenderer::on_move(Move::Enum movement) {
190 192
             // Compute new position of the camera
191 193
             if (Move::DOWN == movement) {
192 194
                 vec3_sub(eye_, eye_, move_vector);
195
+                vec3_sub(center_, center_, move_vector);
193 196
             }
194 197
             else if (Move::UP == movement) {
195 198
                 vec3_add(eye_, eye_, move_vector);
199
+                vec3_add(center_, center_, move_vector);
196 200
             }
197 201
             else {
198 202
                 // Empty on purpose
@@ -202,7 +206,46 @@ void SkiMapRenderer::on_move(Move::Enum movement) {
202 206
 
203 207
     // Redraw the canvas
204 208
     on_surface_changed();
209
+}
210
+
205 211
 
212
+void SkiMapRenderer::on_mouse(int movement_x, int movement_y) {
213
+    // TODO: Factor the vectors computation with keypress handling
214
+    // Compute current forward vector, direction in which we are looking at
215
+    // right now (normalized)
216
+    float forward_vector[3] = {};
217
+    vec3_sub(forward_vector, center_, eye_);
218
+    vec3_norm(forward_vector, forward_vector);
219
+
220
+    // Compute current side vector (normalized)
221
+    vec3 side_vector = {};
222
+    vec3_mul_cross(side_vector, forward_vector, up_);
223
+    vec3_norm(side_vector, side_vector);
224
+
225
+    // Compute current up vector (normalized)
226
+    vec3 up_vector = {};
227
+    vec3_mul_cross(up_vector, side_vector, forward_vector);
228
+    vec3_norm(up_vector, up_vector);
229
+
230
+    vec3 forward_projection = {};
231
+    vec3 side_projection = {};
232
+    vec3 up_projection = {};
233
+    // Compute move along X axis
234
+    // TODO: This breaks the move forward
235
+    vec3_scale(forward_projection, forward_vector, cos(DEFAULT_MOUSE_MOVE * movement_x));  // Compute the projection of the center move on forward vector
236
+    vec3_scale(side_projection, side_vector, sin(DEFAULT_MOUSE_MOVE * movement_x));  // Same on side vector
237
+    vec3_add(forward_vector, forward_projection, side_projection);  // Compute the move vector for the center
238
+    vec3_add(center_, eye_, forward_vector);  // Update center
239
+
240
+    // Same on Y axis
241
+    movement_y = -movement_y;
242
+    vec3_scale(forward_projection, forward_vector, cos(DEFAULT_MOUSE_MOVE * movement_y));  // Compute the projection of the center move on forward vector
243
+    vec3_scale(up_projection, up_vector, sin(DEFAULT_MOUSE_MOVE * movement_y));  // Same on side vector
244
+    vec3_add(forward_vector, forward_projection, up_projection);  // Compute the move vector for the center
245
+    vec3_add(center_, eye_, forward_vector);  // Update center
246
+
247
+    // Redraw the canvas
248
+    on_surface_changed();
206 249
 }
207 250
 
208 251
 

+ 8
- 1
src/core/SkiMapRenderer.h View File

@@ -24,7 +24,8 @@ namespace Move {
24 24
 }
25 25
 
26 26
 
27
-const float DEFAULT_DELTA_MOVE = 100;  /**< Default move distance for a keypress. */
27
+const float DEFAULT_DELTA_MOVE = 100.0f;  /**< Default move distance for a keypress. */
28
+const float DEFAULT_MOUSE_MOVE = 0.01f;  /**< Default move angle for a mouse move. */
28 29
 
29 30
 
30 31
 /**
@@ -66,6 +67,12 @@ class SkiMapRenderer: public Renderer {
66 67
          * Event handler called when a move event is triggered.
67 68
          */
68 69
         void on_move(Move::Enum movement);
70
+
71
+
72
+        /**
73
+         * Event handler called when a mouse event is triggered.
74
+         */
75
+        void on_mouse(int movement_x, int movement_y);
69 76
     private:
70 77
         /**
71 78
          * Build the view and projection matrices, based on data members. Sets the

+ 30
- 0
src/platform/emscripten/main.cc View File

@@ -81,6 +81,21 @@ class GLContext {
81 81
 
82 82
 
83 83
         /**
84
+         * Wrapper around the handle_mouse method to be able to pass it to
85
+         * emscripten_set_mouse_callback method.
86
+         */
87
+        static inline int mouse_callback(int event_type, const EmscriptenMouseEvent* e, void* user_data) {
88
+            // Check that we are handling a keypress event
89
+            if (EMSCRIPTEN_EVENT_MOUSEMOVE == event_type) {
90
+                // Call the internal non-static method
91
+                return reinterpret_cast<GLContext*>(user_data)->handle_mouse(e->movementX, e->movementY);
92
+            }
93
+
94
+            return 0;
95
+        };
96
+
97
+
98
+        /**
84 99
          * Close gl context.
85 100
          */
86 101
         void shutdown_gl(void) {
@@ -126,6 +141,16 @@ class GLContext {
126 141
         };
127 142
 
128 143
 
144
+        /**
145
+         * Handle the actions to trigger on a mouse move.
146
+         */
147
+        int handle_mouse(int movementX, int movementY) {
148
+            surface_->on_mouse(movementX, movementY);
149
+
150
+            return 0;
151
+        };
152
+
153
+
129 154
         SkiMapRenderer* surface_;  /**< The surface rendered by our renderer. */
130 155
         GLFWwindow* window_;  /**< The GL window. */
131 156
 };
@@ -146,6 +171,11 @@ int main(void)
146 171
                 static_cast<void*>(&context),
147 172
                 true,
148 173
                 GLContext::key_callback);
174
+        emscripten_set_mousemove_callback(
175
+                NULL,
176
+                static_cast<void*>(&context),
177
+                true,
178
+                GLContext::mouse_callback);
149 179
 		emscripten_set_main_loop_arg(
150 180
                 GLContext::do_frame_handler,
151 181
                 static_cast<void*>(&context),