framework2
176 строк · 5.4 Кб
1#include "ofxFirstPersonCamera.h"
2
3ofxFirstPersonCamera::ofxFirstPersonCamera()
4:m_keyState ({0})
5,m_isControlled (false)
6,m_previousMouseX(0)
7,m_previousMouseY(0)
8,upvector (0,1,0)
9,movespeed (1.00f)
10,rollspeed (1.00f)
11,sensitivity (0.10f)
12,keyUp (GLFW_KEY_E)
13,keyDown (GLFW_KEY_Q)
14,keyLeft (GLFW_KEY_A)
15,keyRight (GLFW_KEY_D)
16,keyForward (GLFW_KEY_W)
17,keyBackward (GLFW_KEY_S)
18,keyRollLeft (GLFW_KEY_T)
19,keyRollRight (GLFW_KEY_Y)
20,keyRollReset (GLFW_KEY_R)
21{
22auto &events = ofEvents();
23ofAddListener(events.update , this, &ofxFirstPersonCamera::update , OF_EVENT_ORDER_APP);
24ofAddListener(events.keyPressed , this, &ofxFirstPersonCamera::keyPressed , OF_EVENT_ORDER_APP);
25ofAddListener(events.keyReleased , this, &ofxFirstPersonCamera::keyReleased , OF_EVENT_ORDER_APP);
26ofAddListener(events.mouseMoved , this, &ofxFirstPersonCamera::mouseMoved , OF_EVENT_ORDER_APP);
27ofAddListener(events.mouseDragged, this, &ofxFirstPersonCamera::mouseDragged, OF_EVENT_ORDER_APP);
28}
29
30ofxFirstPersonCamera::~ofxFirstPersonCamera()
31{
32auto &events = ofEvents();
33ofRemoveListener(events.update , this, &ofxFirstPersonCamera::update , OF_EVENT_ORDER_APP);
34ofRemoveListener(events.keyPressed , this, &ofxFirstPersonCamera::keyPressed , OF_EVENT_ORDER_APP);
35ofRemoveListener(events.keyReleased , this, &ofxFirstPersonCamera::keyReleased , OF_EVENT_ORDER_APP);
36ofRemoveListener(events.mouseMoved , this, &ofxFirstPersonCamera::mouseMoved , OF_EVENT_ORDER_APP);
37ofRemoveListener(events.mouseDragged, this, &ofxFirstPersonCamera::mouseDragged, OF_EVENT_ORDER_APP);
38}
39
40bool ofxFirstPersonCamera::isControlled()
41{
42return m_isControlled;
43}
44
45void ofxFirstPersonCamera::toggleControl()
46{
47m_isControlled ? disableControl() : enableControl();
48}
49
50void ofxFirstPersonCamera::enableControl()
51{
52GLFWwindow * win = static_cast<ofAppGLFWWindow*>(ofGetWindowPtr())->getGLFWWindow();
53
54glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
55
56m_isControlled = true;
57m_glfwWindow = win;
58}
59
60void ofxFirstPersonCamera::disableControl()
61{
62glfwSetInputMode(m_glfwWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
63m_previousMouseX = 0;
64m_previousMouseY = 0;
65
66m_isControlled = false;
67}
68
69void ofxFirstPersonCamera::update(ofEventArgs& args)
70{
71if (m_isControlled == false) {
72return;
73}
74
75Actions key = m_keyState;
76
77if (m_previousMouseX != 0 || m_previousMouseY != 0) { // Rotation
78float sensit = sensitivity;
79float xdiff = (m_previousMouseX - ofGetMouseX()) * sensit;
80float ydiff = (m_previousMouseY - ofGetMouseY()) * sensit;
81
82ofVec3f upvec = upvector;
83ofVec3f sidev = this->getSideDir();
84this->rotateDeg(ydiff, sidev);
85this->rotateDeg(xdiff, upvec);
86}
87{ // Roll
88bool unroll = key.RollReset;
89int rolldir = key.RollLeft - key.RollRight;
90
91if (rolldir) {
92float roll = rollspeed;
93float rate = ofGetFrameRate();
94float angle = roll * (60.0f / rate);
95this->rollDeg(rolldir * angle);
96upvector = this->getUpDir();
97}
98
99if (unroll) {
100if (upvector != ofVec3f(0, 1, 0) && upvector != ofVec3f(0, -1, 0)) {
101ofVec3f lookAtDir = this->getGlobalPosition() + this->getLookAtDir();
102glm::quat init;
103this->setGlobalOrientation(init);
104this->lookAt(lookAtDir, ofVec3f(0, 1, 0));
105upvector = ofVec3f(0, 1, 0);
106}
107}
108}
109{ // Position
110float look = key.Forward - key.Backward;
111float side = key.Right - key.Left;
112float up = key.Up - key.Down;
113
114if (look != 0 || side != 0 || up != 0)
115{
116ofVec3f lookdir = this->getLookAtDir();
117ofVec3f sidedir = this->getSideDir();
118ofVec3f updir = this->getUpDir();
119float move = movespeed;
120float rate = ofGetFrameRate();
121float speed = move * (60.0f / rate);
122this->move(lookdir * speed * look +
123sidedir * speed * side +
124updir * speed * up);
125}
126}
127
128m_previousMouseX = ofGetMouseX();
129m_previousMouseY = ofGetMouseY();
130}
131
132void ofxFirstPersonCamera::mouseMoved(ofMouseEventArgs& mouse)
133{
134}
135
136void ofxFirstPersonCamera::mouseDragged(ofMouseEventArgs& mouse)
137{
138}
139
140void ofxFirstPersonCamera::keyPressed(ofKeyEventArgs& keys)
141{
142Actions key = m_keyState;
143int keycode = keys.keycode;
144
145if (keycode == keyUp ) key.Up = 1;
146else if (keycode == keyDown ) key.Down = 1;
147else if (keycode == keyLeft ) key.Left = 1;
148else if (keycode == keyRight ) key.Right = 1;
149else if (keycode == keyForward ) key.Forward = 1;
150else if (keycode == keyBackward ) key.Backward = 1;
151
152else if (keycode == keyRollLeft ) key.RollLeft = 1;
153else if (keycode == keyRollRight) key.RollRight = 1;
154else if (keycode == keyRollReset) key.RollReset = 1;
155
156m_keyState = key;
157}
158
159void ofxFirstPersonCamera::keyReleased(ofKeyEventArgs& keys)
160{
161Actions key = m_keyState;
162int keycode = keys.keycode;
163
164if (keycode == keyUp ) key.Up = 0;
165else if (keycode == keyDown ) key.Down = 0;
166else if (keycode == keyLeft ) key.Left = 0;
167else if (keycode == keyRight ) key.Right = 0;
168else if (keycode == keyForward ) key.Forward = 0;
169else if (keycode == keyBackward ) key.Backward = 0;
170
171else if (keycode == keyRollLeft ) key.RollLeft = 0;
172else if (keycode == keyRollRight) key.RollRight = 0;
173else if (keycode == keyRollReset) key.RollReset = 0;
174
175m_keyState = key;
176}
177