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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| // Kanin Engine - Camera class
// Author : Gallego Michaël
// Date : 2007-01-12
// Description : Implementation file
#include "Camera.hpp"
using namespace Kanin;
// Default constructor, do nothing
Camera::Camera ()
: position (0.0, 0.0, 0.0), // The position is at the origin
right (1.0, 0.0, 0.0), // The positive X-axis to the right
up (0.0, 1.0, 0.0), // The positive Y-axis up
forward (0.0, 0.0, -1.0), // The camera is facing down the negative Z-axis
rightMove (0.0, 0.0, 0.0), // The quantity that will be added when moving on one side
forwardMove (0.0, 0.0, 0.0), // The quantity that will be added when moving forward or backward
viewMatrix (), // The view matrix is set to the identity
angleAboutRight (0.0), // Nor around the right vector
angleAboutUp (0.0), // And nor around the up vector ^_^
mouseSensibility (10.0), // Set mouse sensibility to 10.0
movementsSpeed (1.0), // Set movementsSpeed to 1.0
hasMoved (false) // The camera hasn't moved because it's just beeing created :D
{
// Initialisation of the key configuration with default data
keyConfig.insert (std::make_pair ("Left", sf::Key::Left));
keyConfig.insert (std::make_pair ("Right", sf::Key::Right));
keyConfig.insert (std::make_pair ("Forward", sf::Key::Up));
keyConfig.insert (std::make_pair ("Backward", sf::Key::Down));
// Initialisation of the state of each keys
keyStates.insert (std::make_pair (keyConfig["Left"], false));
keyStates.insert (std::make_pair (keyConfig["Right"], false));
keyStates.insert (std::make_pair (keyConfig["Forward"], false));
keyStates.insert (std::make_pair (keyConfig["Backward"], false));
// The default camera type is a FPS camera
updatePositionFunction = boost::bind(&Camera::UpdatePositionFPS, this);
}
// Set the position of the camera
void Camera::SetPosition (const Vector3d & position_)
{
position = position_;
hasMoved = true;
}
// This function gets input from mouse and keyboard
void Camera::UpdateFromInput (sf::RenderWindow & app_)
{
// Get a reference to the input manager associated to our window, and store it for later use
const sf::Input & Input = app_.GetInput();
// ----------------------------------- ROTATION -----------------------------------
// This function gets the position of the mouse
int mouseX = Input.GetMouseX();
int mouseY = Input.GetMouseY();
int halfWidth = app_.GetWidth() / 2;
int halfHeight = app_.GetHeight() / 2;
// Call the function that updates the orientation
UpdateOrientation (mouseX, mouseY, halfWidth, halfHeight);
// Set the mouse position to the center of the screen, so that the mouse cursor will
// never be outside the screen
app_.SetCursorPosition (halfWidth, halfHeight);
// ------------------------------------- MOVE -------------------------------------
// Update the state of each keys
KeyStates::iterator itEnd = keyStates.end();
for (KeyStates::iterator it = keyStates.begin() ; it != itEnd ; ++it)
it->second = Input.IsKeyDown (it->first);
// Call the function that updates the position of the camera
UpdatePosition ();
}
// This function must be called by UpdateFromInput. It updates the camera
// orientation given the positions of the mouse
void Camera::UpdateOrientation (const int mouseX_, const int mouseY_,
const int halfWidth_, const int halfHeight_)
{
// If the mouse has moved on the x-axis, so we make a rotation around the
// up axis (y-axis)
if ((mouseX_ - halfWidth_) != 0)
{
angleAboutUp += (mouseX_ - halfWidth_) / mouseSensibility;
// Clamp the angle so that it doesn't be superior to 360.0 degrees or
// inferior to 0.0 degrees
if (angleAboutUp < 0.0)
angleAboutUp += 360.0;
else if (angleAboutUp >= 360.0)
angleAboutUp -= 360.0;
hasMoved = true;
}
// If the mouse has moved on the y-axis, so we make a rotation around the
// right axis (x-axis)
if ((mouseY_ - halfHeight_) != 0)
{
angleAboutRight += (mouseY_ - halfHeight_) / mouseSensibility;
// Clamp the angle so that the camera doesn't make an all turn around
// the x-axis :p
if (angleAboutRight < -90.0)
angleAboutRight = -90.0;
else if (angleAboutRight > 90.0)
angleAboutRight = 90.0;
hasMoved = true;
}
// Some trig computations
double sp = std::sin(DegToRad(-angleAboutRight));
double cp = std::cos(DegToRad(-angleAboutRight));
double sy = std::sin(DegToRad(-angleAboutUp));
double cy = std::cos(DegToRad(-angleAboutUp));
// Set the rotations around the right and forward vector...
right.Set (cy, 0.0, -sy);
forward.Set (sy * cp, -sp, cy * cp);
// And the moving vectors
forwardMove.Set (sy, sy, cy);
rightMove = right;
// The up vector is just the vector perpendicular to the forward vector
// and the right vector
up = Cross (forward, right);
}
// This function must be called by UpdateFromInput. It calls the function that
// will update the position given the user choice (FPS, FreeFly or 6DOF)
void Camera::UpdatePosition ()
{
updatePositionFunction();
}
// This function must be called by UpdateFromInput. It updates the position of
// the camera given a FPS camera
void Camera::UpdatePositionFPS ()
{
if (keyStates[keyConfig["Left"]])
{
position -= rightMove * movementsSpeed;
hasMoved = true;
}
if (keyStates[keyConfig["Right"]])
{
position += rightMove * movementsSpeed;
hasMoved = true;
}
if (keyStates[keyConfig["Forward"]])
{
position -= forwardMove * movementsSpeed;
position.y = 0.0;
hasMoved = true;
}
if (keyStates[keyConfig["Backward"]])
{
position += forwardMove * movementsSpeed;
position.y = 0.0;
hasMoved = true;
}
}
// This function must be called by UpdateFromInput. It updates the position of
// the camera given a Free-Fly camera
void Camera::UpdatePositionFreeFly ()
{
if (keyStates[keyConfig["Left"]])
{
position -= rightMove * movementsSpeed;
hasMoved = true;
}
if (keyStates[keyConfig["Right"]])
{
position += rightMove * movementsSpeed;
hasMoved = true;
}
if (keyStates[keyConfig["Forward"]])
{
position -= forwardMove * movementsSpeed;
hasMoved = true;
}
if (keyStates[keyConfig["Backward"]])
{
position += forwardMove * movementsSpeed;
hasMoved = true;
}
}
// This function must be called by UpdateFromInput. It updates the position of
// the camera given a six degrees of freedom camera
void Camera::UpdatePosition6DOF ()
{
// A faire, parceque un peu long
}
// Construct the view matrix with the orientation and the position
void Camera::ConstructViewMatrix ()
{
// First set the orientation given the quaternion
viewMatrix.Set (right.x, right.y, right.z, -Dot (right, position),
up.x, up.y, up.z, -Dot (up, position),
forward.x, forward.y, forward.z, -Dot (forward, position),
0.0, 0.0, 0.0, 1.0);
} |
Partager