framework2

Форк
0
176 строк · 5.4 Кб
1
#include "ofxFirstPersonCamera.h"
2

3
ofxFirstPersonCamera::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
{
22
  auto &events = ofEvents();
23
  ofAddListener(events.update      , this, &ofxFirstPersonCamera::update      , OF_EVENT_ORDER_APP);
24
  ofAddListener(events.keyPressed  , this, &ofxFirstPersonCamera::keyPressed  , OF_EVENT_ORDER_APP);
25
  ofAddListener(events.keyReleased , this, &ofxFirstPersonCamera::keyReleased , OF_EVENT_ORDER_APP);
26
  ofAddListener(events.mouseMoved  , this, &ofxFirstPersonCamera::mouseMoved  , OF_EVENT_ORDER_APP);
27
  ofAddListener(events.mouseDragged, this, &ofxFirstPersonCamera::mouseDragged, OF_EVENT_ORDER_APP);
28
}
29

30
ofxFirstPersonCamera::~ofxFirstPersonCamera()
31
{
32
  auto &events = ofEvents();
33
  ofRemoveListener(events.update      , this, &ofxFirstPersonCamera::update      , OF_EVENT_ORDER_APP);
34
  ofRemoveListener(events.keyPressed  , this, &ofxFirstPersonCamera::keyPressed  , OF_EVENT_ORDER_APP);
35
  ofRemoveListener(events.keyReleased , this, &ofxFirstPersonCamera::keyReleased , OF_EVENT_ORDER_APP);
36
  ofRemoveListener(events.mouseMoved  , this, &ofxFirstPersonCamera::mouseMoved  , OF_EVENT_ORDER_APP);
37
  ofRemoveListener(events.mouseDragged, this, &ofxFirstPersonCamera::mouseDragged, OF_EVENT_ORDER_APP);
38
}
39

40
bool ofxFirstPersonCamera::isControlled()
41
{
42
  return m_isControlled;
43
}
44

45
void ofxFirstPersonCamera::toggleControl()
46
{
47
  m_isControlled ? disableControl() : enableControl();
48
}
49

50
void ofxFirstPersonCamera::enableControl()
51
{
52
  GLFWwindow * win = static_cast<ofAppGLFWWindow*>(ofGetWindowPtr())->getGLFWWindow();
53

54
  glfwSetInputMode(win, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
55

56
  m_isControlled = true;
57
  m_glfwWindow   = win;
58
}
59

60
void ofxFirstPersonCamera::disableControl()
61
{
62
  glfwSetInputMode(m_glfwWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
63
  m_previousMouseX = 0;
64
  m_previousMouseY = 0;
65

66
  m_isControlled = false;
67
}
68

69
void ofxFirstPersonCamera::update(ofEventArgs& args)
70
{
71
  if (m_isControlled == false) {
72
    return;
73
  }
74

75
  Actions key = m_keyState;
76

77
  if (m_previousMouseX != 0 || m_previousMouseY != 0) { // Rotation
78
    float sensit = sensitivity;
79
    float xdiff = (m_previousMouseX - ofGetMouseX()) * sensit;
80
    float ydiff = (m_previousMouseY - ofGetMouseY()) * sensit;
81

82
    ofVec3f upvec = upvector;
83
    ofVec3f sidev = this->getSideDir();
84
    this->rotateDeg(ydiff, sidev);
85
    this->rotateDeg(xdiff, upvec);
86
  }
87
  { // Roll
88
    bool unroll = key.RollReset;
89
    int rolldir = key.RollLeft - key.RollRight;
90

91
    if (rolldir) {
92
      float roll = rollspeed;
93
      float rate = ofGetFrameRate();
94
      float angle = roll * (60.0f / rate);
95
      this->rollDeg(rolldir * angle);
96
      upvector = this->getUpDir();
97
    }
98

99
    if (unroll) {
100
      if (upvector != ofVec3f(0, 1, 0) && upvector != ofVec3f(0, -1, 0)) {
101
        ofVec3f lookAtDir = this->getGlobalPosition() + this->getLookAtDir();
102
        glm::quat init;
103
        this->setGlobalOrientation(init);
104
        this->lookAt(lookAtDir, ofVec3f(0, 1, 0));
105
        upvector = ofVec3f(0, 1, 0);
106
      }
107
    }
108
  }
109
  { // Position
110
    float look = key.Forward - key.Backward;
111
    float side = key.Right - key.Left;
112
    float up   = key.Up - key.Down;
113

114
    if (look != 0 || side != 0 || up != 0)
115
    {
116
      ofVec3f lookdir = this->getLookAtDir();
117
      ofVec3f sidedir = this->getSideDir();
118
      ofVec3f updir   = this->getUpDir();
119
      float move  = movespeed;
120
      float rate  = ofGetFrameRate();
121
      float speed = move * (60.0f / rate);
122
      this->move(lookdir * speed * look +
123
                 sidedir * speed * side +
124
                   updir * speed * up);
125
    }
126
  }
127

128
  m_previousMouseX = ofGetMouseX();
129
  m_previousMouseY = ofGetMouseY();
130
}
131

132
void ofxFirstPersonCamera::mouseMoved(ofMouseEventArgs& mouse)
133
{
134
}
135

136
void ofxFirstPersonCamera::mouseDragged(ofMouseEventArgs& mouse)
137
{
138
}
139

140
void ofxFirstPersonCamera::keyPressed(ofKeyEventArgs& keys)
141
{
142
  Actions key = m_keyState;
143
  int keycode = keys.keycode;
144

145
  if      (keycode == keyUp       ) key.Up        = 1;
146
  else if (keycode == keyDown     ) key.Down      = 1;
147
  else if (keycode == keyLeft     ) key.Left      = 1;
148
  else if (keycode == keyRight    ) key.Right     = 1;
149
  else if (keycode == keyForward  ) key.Forward   = 1;
150
  else if (keycode == keyBackward ) key.Backward  = 1;
151

152
  else if (keycode == keyRollLeft ) key.RollLeft  = 1;
153
  else if (keycode == keyRollRight) key.RollRight = 1;
154
  else if (keycode == keyRollReset) key.RollReset = 1;
155

156
  m_keyState = key;
157
}
158

159
void ofxFirstPersonCamera::keyReleased(ofKeyEventArgs& keys)
160
{
161
  Actions key = m_keyState;
162
  int keycode = keys.keycode;
163

164
  if      (keycode == keyUp       ) key.Up        = 0;
165
  else if (keycode == keyDown     ) key.Down      = 0;
166
  else if (keycode == keyLeft     ) key.Left      = 0;
167
  else if (keycode == keyRight    ) key.Right     = 0;
168
  else if (keycode == keyForward  ) key.Forward   = 0;
169
  else if (keycode == keyBackward ) key.Backward  = 0;
170

171
  else if (keycode == keyRollLeft ) key.RollLeft  = 0;
172
  else if (keycode == keyRollRight) key.RollRight = 0;
173
  else if (keycode == keyRollReset) key.RollReset = 0;
174

175
  m_keyState = key;
176
}
177

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.