paella/Code/src/SFMLTools/FreeFlyCamera.cpp

104 lines
3.0 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Paella
// Copyright (C) 2015 - Thomas FORGIONE, Emilie JALRAS, Marion LENFANT, Thierry MALON, Amandine PAILLOUX
// Authors :
// Thomas FORGIONE
// Emilie JALRAS
// Marion LENFANT
// Thierry MALON
// Amandine PAILLOUX
//
// This file is part of the project Paella
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cmath>
#include <GL/glu.h>
#include <boost/algorithm/clamp.hpp>
#include "SFMLTools/VectorFunctions.hpp"
#include "SFMLTools/FreeFlyCamera.hpp"
namespace sft
{
FreeFlyCamera::FreeFlyCamera() : speed(0.1), sensitivity(0.001), theta(0), phi(0)
{
vectorsFromAngles();
}
void FreeFlyCamera::nextStep(int width, int height)
{
// Gestion de la souris
sf::Vector2i mouse_position = sf::Mouse::getPosition();
theta += sensitivity * (width/2-mouse_position.x);
phi += sensitivity * (height/2-mouse_position.y);
vectorsFromAngles();
// Gestion du clavier
sf::Vector3<double> move;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
move += forward * speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
move -= forward * speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q))
{
move += left * speed;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
move -= left * speed;
}
position += move;
target += move;
}
void FreeFlyCamera::vectorsFromAngles()
{
static constexpr auto PI_2_MINUS_EPSILON = M_PI_2 - 0.1;
phi = boost::algorithm::clamp(phi,-PI_2_MINUS_EPSILON,PI_2_MINUS_EPSILON);
// Passage du sphérique au cartésien
double r_temp = std::cos(phi);
forward.z = std::sin(phi);
forward.x = r_temp * std::cos(theta);
forward.y = r_temp * std::sin(theta);
left = crossProduct(sf::Vector3<double>(0,0,1), forward);
normalize(left);
target = position + forward;
}
void FreeFlyCamera::look() const
{
gluLookAt(position.x, position.y, position.z,
target.x, target.y, target.z,
0,0,1);
}
} // namespace sft