-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoamingCamera.cpp
67 lines (56 loc) · 1.98 KB
/
RoamingCamera.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
#include "pch.h"
#include "RoamingCamera.h"
RoamingCamera::RoamingCamera(SceneManager* scene_manager, RenderWindow* render_window, Vector3 position, Vector3 lookat_position )
{
// Creating and attaching the camera
camera_node_ = scene_manager->getRootSceneNode()->createChildSceneNode();
camera_ = scene_manager->createCamera("RoamingCamera");
camera_node_->attachObject(camera_);
camera_node_->setPosition(position);
camera_node_->lookAt(lookat_position, Ogre::Node::TransformSpace::TS_WORLD);
// Configuring the camera
camera_->setNearClipDistance(5);
Ogre::Viewport* vp = render_window->addViewport(camera_);
vp->setBackgroundColour(Ogre::ColourValue(0, 0, 0));
camera_->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
// Setting motion parameters
movement_speed_ = 20.0f;
rotation_speed_ = 3.0f;
}
RoamingCamera::~RoamingCamera()
{
}
void RoamingCamera::update(Ogre::Real delta_time, const Uint8 * keyboard_state)
{
int x = 0, y = 0;
// Leave if right mouse button is not being pressed
// ...but also retrieve and store mouse movement
if (!(SDL_GetRelativeMouseState(&x, &y) & SDL_BUTTON_LMASK)) return;
// Construct displacement vector
Ogre::Vector3 vec = Ogre::Vector3(0, 0, 0);
if (keyboard_state[SDL_SCANCODE_W]) {
vec = Ogre::Vector3(0, 0, -1);
}
if (keyboard_state[SDL_SCANCODE_S]) {
vec += Ogre::Vector3(0, 0, 1);
}
if (keyboard_state[SDL_SCANCODE_A]) {
vec += Ogre::Vector3(-1, 0, 0);
}
if (keyboard_state[SDL_SCANCODE_D]) {
vec += Ogre::Vector3(1, 0, 0);
}
if (keyboard_state[SDL_SCANCODE_Q]) {
vec += Ogre::Vector3(0, 1, 0);
}
if (keyboard_state[SDL_SCANCODE_E]) {
vec += Ogre::Vector3(0, -1, 0);
}
// Construct view rotation
float rotX = x * delta_time * -1 * rotation_speed_;
float rotY = y * delta_time * -1 * rotation_speed_;
// Update camera with new displacement and rotation
camera_->yaw(Ogre::Radian(rotX));
camera_->pitch(Ogre::Radian(rotY));
camera_->moveRelative(delta_time * movement_speed_ * vec);
}