forked from rlk/OVR_SDL2
/
OVR_SDL2_nav.cpp
136 lines (112 loc) · 4.11 KB
/
OVR_SDL2_nav.cpp
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
// Copyright (c) 2014 Robert Kooima
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include "OVR_SDL2_nav.hpp"
//------------------------------------------------------------------------------
OVR_SDL2_nav::OVR_SDL2_nav() :
rotation(0), drotation(0),
move_L(false),
move_R(false),
move_D(false),
move_U(false),
move_F(false),
move_B(false)
{
// FPS-style mouselook requires relative mouse motion.
SDL_SetRelativeMouseMode(SDL_TRUE);
}
OVR_SDL2_nav::~OVR_SDL2_nav()
{
}
/// Animate the camera position.
void OVR_SDL2_nav::step()
{
mat3 N = normal(inverse(yrotation(to_radians(rotation))));
vec3 v = dposition;
if (move_L) v = v + vec3(-1, 0, 0);
if (move_R) v = v + vec3(+1, 0, 0);
if (move_D) v = v + vec3(0, -1, 0);
if (move_U) v = v + vec3(0, +1, 0);
if (move_F) v = v + vec3(0, 0, -1);
if (move_B) v = v + vec3(0, 0, +1);
if (length(v) > 1.0)
v = normalize(v);
position = position + N * v / 30.0;
rotation = rotation + drotation;
}
/// Handle a key press or release.
void OVR_SDL2_nav::keyboard(int key, bool down, bool repeat)
{
OVR_SDL2_app::keyboard(key, down, repeat);
if (!repeat)
{
switch (key)
{
case SDL_SCANCODE_A: move_L = down; return;
case SDL_SCANCODE_D: move_R = down; return;
case SDL_SCANCODE_C: move_D = down; return;
case SDL_SCANCODE_SPACE: move_U = down; return;
case SDL_SCANCODE_W: move_F = down; return;
case SDL_SCANCODE_S: move_B = down; return;
}
}
}
/// Handle mouse pointer motion.
void OVR_SDL2_nav::mouse_motion(int dx, int dy)
{
rotation += 0.5f * dx;
}
/// Handle gamepad button press or release.
void OVR_SDL2_nav::game_button(int device, int button, bool down)
{
switch (button)
{
case SDL_CONTROLLER_BUTTON_DPAD_LEFT: move_L = down; break;
case SDL_CONTROLLER_BUTTON_DPAD_RIGHT: move_R = down; break;
case SDL_CONTROLLER_BUTTON_B: move_D = down; break;
case SDL_CONTROLLER_BUTTON_A: move_U = down; break;
case SDL_CONTROLLER_BUTTON_DPAD_UP: move_F = down; break;
case SDL_CONTROLLER_BUTTON_DPAD_DOWN: move_B = down; break;
}
}
/// Handle gamepad axis motion.
void OVR_SDL2_nav::game_axis(int device, int axis, float value)
{
float k = copysignf(value * value, value);
switch (axis)
{
case SDL_CONTROLLER_AXIS_LEFTX:
dposition[0] = (fabsf(k) > 0.1f) ? k : 0.0f;
break;
case SDL_CONTROLLER_AXIS_LEFTY:
dposition[2] = (fabsf(k) > 0.1f) ? k : 0.0f;
break;
case SDL_CONTROLLER_AXIS_RIGHTX:
drotation = (fabsf(k) > 0.1f) ? k : 0.0f;
break;
}
}
//------------------------------------------------------------------------------
/// Return the current view matrix.
mat4 OVR_SDL2_nav::view() const
{
return OVR_SDL2_app::view() * yrotation(to_radians(rotation))
* translation(-position);
}
//------------------------------------------------------------------------------