Commit b93db9db authored by Loïc Lachiver's avatar Loïc Lachiver
Browse files

quick save 2, camera locking when vec3 up aligned

parent 26365d86
set(GUI_SOURCES
${GUI_SOURCES}
${CMAKE_CURRENT_SOURCE_DIR}/Arcball.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Trackball.cpp
${CMAKE_CURRENT_SOURCE_DIR}/CameraController.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mrf_baseUI.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mrf_interactiveUI.cpp
${CMAKE_CURRENT_SOURCE_DIR}/glBuffer.cpp
......@@ -12,7 +12,7 @@ set(GUI_SOURCES
set(GUI_HEADERS
${GUI_SOURCES}
${CMAKE_CURRENT_SOURCE_DIR}/Arcball.h
${CMAKE_CURRENT_SOURCE_DIR}/Trackball.h
${CMAKE_CURRENT_SOURCE_DIR}/CameraController.h
${CMAKE_CURRENT_SOURCE_DIR}/mrf_baseUI.h
${CMAKE_CURRENT_SOURCE_DIR}/mrf_interactiveUI.h
${CMAKE_CURRENT_SOURCE_DIR}/glBuffer.h
......
......@@ -26,7 +26,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Trackball.h"
#include "CameraController.h"
//------------------------------------------------------------------------------
......@@ -39,69 +39,68 @@ namespace mrf
namespace rendering
{
void Trackball::setCamera(Camera* c){
m_p_camera = c;
void CameraController::setCamera(Camera* c)
{
m_p_camera = c;
updatePanSpeed();
}
void Trackball::zoomCamera(float zoom)
void CameraController::zoomCamera(float zoom)
{
math::Vec3f new_position = m_p_camera->position() + (m_p_camera->lookAt() - m_p_camera->position()) * m_zoom_speed * zoom;
m_p_camera->setPosition(new_position);
updatePanSpeed();
}
void Trackball::panCamera(float dx, float dy)
void CameraController::panCamera(float dx, float dy)
{
math::Vec3f pan = m_p_camera->rightAxis() * dx * m_pan_speed + m_p_camera->up() * dy * m_pan_speed;
math::Vec3f pan = m_p_camera->rightAxis() * (-dx) * m_pan_speed + ((m_p_camera->lookAt() - m_p_camera->position()).cross(m_p_camera->rightAxis())).normalize() * (-dy) * m_pan_speed;
m_p_camera->setPosition(m_p_camera->position() + pan);
m_p_camera->lookAt(m_p_camera->lookAt() + pan);
}
void Trackball::rotateCamera(float dx, float dy)
{
const math::Vec2f start = {0.5f, 0.5f};
const math::Vec2f end = {0.5f + dx, 0.5f + dy};
math::Vec3f start_vec = getMouseProjectionOnBall(start);
math::Vec3f end_vec = getMouseProjectionOnBall(end);
std::cout << end_vec << std::endl;
math::Quatf q;
q.fromTwoVecs(end_vec, start_vec);
m_p_camera->setPosition(q * m_p_camera->position());
m_p_camera->setUp(q * m_p_camera->up());
/* THE ANGLE WAY :
// float angle = (float)acos(start_vec.dot(end_vec) / start_vec.norm() / end_vec.norm());
float deltaAngleX = (2*M_PI/ 500.f); // a movement from left to right = 2*PI = 360 deg
float deltaAngleY = (M_PI/ 500.f); // a movement from top to bottom = PI = 180 deg
// if (angle != 0.0f)
// {
// math::Vec3f axis = start_vec.cross(end_vec).normalize();
float angleX = dx * deltaAngleX;
float angleY = dy * deltaAngleY;
// axis[2] = axis[0];
// axis[0] = 0.f;
math::Vec3f position = m_p_camera->position();
math::Vec3f pivot = m_p_camera->lookAt();
// std::cout << axis << std::endl;
// //angle *= m_rotation_speed;
// math::Quatf q1, q2;
// q1.fromAxisAngle(math::Vec3f(0.f,0.f,1.f), dy);
// q2.fromAxisAngle(math::Vec3f(0.f,1.f,0.f), dx);
math::Mat4f m = m_p_camera->rotationMatrix();
math::Mat4f rot_mat_x;
rot_mat_x = rot_mat_x.rotationY(angleX);
position = rot_mat_x.upperLeft() * (position - pivot) + pivot;
math::Mat4f rot_mat_y;
rot_mat_y = rot_mat_y.rotationZ(angleY);
position = rot_mat_y * (position - pivot) + pivot;
// //m_p_camera->lookAt(q * m_p_camera->lookAt());
// m_p_camera->setPosition(q1 * m_p_camera->position());
// m_p_camera->setPosition(q2 * m_p_camera->position());
// m_p_camera->setUp(q2 * m_p_camera->up());
m_p_camera->setPosition(position);
*/
// }
void CameraController::rotateCamera(float dx, float dy)
{
const math::Vec2f start = {0.5f, 0.5f};
const math::Vec2f end = {0.5f + dx * m_rotation_speed, 0.5f + dy * m_rotation_speed};
math::Vec3f start_vec = getMouseProjectionOnBall(start);
math::Vec3f end_vec = getMouseProjectionOnBall(end);
//m_p_camera->setUp(q * m_p_camera->up());
math::Quatf q;
q.fromTwoVecs(start_vec, end_vec);
m_p_camera->setRotationMatrix(q.getMatrix().upperLeft());
m_p_camera->setUp(mrf::math::Vec3f(0.f, 1.f, 0.f));
}
math::Vec3f Trackball::getMouseProjectionOnBall(math::Vec2f coor)
math::Vec3f CameraController::getMouseProjectionOnBall(math::Vec2f coor)
{
float x = (coor.x() - 0.5f);
float y = (1.0f - coor.y() - 0.5f);
......@@ -119,10 +118,14 @@ math::Vec3f Trackball::getMouseProjectionOnBall(math::Vec2f coor)
{
z = sqrtf(1.0f - len2);
}
return math::Vec3f(x, y, z);
return math::Vec3f(x, y, z).normalize();
}
void CameraController::updatePanSpeed(){
m_pan_speed = 0.5f*(m_p_camera->lookAt() - m_p_camera->position()).norm();
}
} // namespace rendering
} // namespace mrf
\ No newline at end of file
......@@ -47,45 +47,35 @@ enum class TRACKBALL_STATE : uint8_t{
ZOOM
};
class Trackball
class CameraController
{
public:
Trackball(): m_p_camera(nullptr),
m_rotation_speed(1.0f),
CameraController(): m_p_camera(nullptr),
m_rotation_speed(1.f),
m_zoom_speed(0.1f),
m_pan_speed(1.0f),
m_rot_start(0.f, 0.f, 0.f),
m_rot_end(0.f, 0.f, 0.f),
m_zoom_start(0.f, 0.f),
m_zoom_end(0.f, 0.f),
m_pan_start(0.f, 0.f),
m_pan_end(0.f, 0.f),
m_pivot(math::Vec3f(0.f)) {}
m_pan_speed(100.0f),
m_pivot(math::Vec3f(0.5f)) {}
~Trackball() {}
void setCamera(Camera* c);
void setPivot(math::Vec3f p);
void mouseReleased(int button);
void mouseMove(float mouse_x, float mouse_y);
void keyPressed(int key);
void keyReleased(int key);
void mouseWheel(double xoffset, double yoffset);
void updateCamera();
~CameraController() {}
void rotateCamera(float dx, float dy);
void zoomCamera(float zoom);
void panCamera(float dx, float dy);
private:
float rotationSpeed() {return m_rotation_speed;}
// CAMERA COORDINATES [-1; 1]
math::Vec3f getMouseProjectionOnBall(math::Vec2f coor);
void setCamera(Camera* c);
void setPivot(math::Vec3f p) {m_pivot = p;}
void setRotationSpeed(float s) {m_rotation_speed = s;}
void setZoomSpeed(float s) {m_zoom_speed = s;}
void setPanSpeed(float s) {m_pan_speed = s;}
private:
void updatePanSpeed();
/* Project the mouse coordinates in camera space onto the unit sphere. */
math::Vec3f getMouseProjectionOnBall(math::Vec2f coor);
bool m_enabled;
......@@ -94,13 +84,6 @@ private:
float m_zoom_speed;
float m_pan_speed;
math::Vec3f m_rot_start;
math::Vec3f m_rot_end;
math::Vec2f m_zoom_start;
math::Vec2f m_zoom_end;
math::Vec2f m_pan_start;
math::Vec2f m_pan_end;
math::Vec3f m_pivot;
TRACKBALL_STATE m_state;
......
......@@ -19,7 +19,7 @@ BaseUI::BaseUI()
, _current_sensitivity(color::CIE_1931_2DEG)
, _current_space(SPACE::SRGB)
, _arcball(math::Vec2f(0.5f), 1.0f)
, _trackball()
, _camera_controller()
, _frame_width(512)
, _max_screen_width(0)
, _frame_height(512)
......@@ -126,9 +126,6 @@ void BaseUI::initializeUIRenderingResources(bool v_sync)
#endif
_shader_prog[PROCESS]->createShaderProgram();
_update_parameters[PROCESS] = true;
_trackball.setCamera(_renderer->getMainCamera());
}
void BaseUI::run()
......@@ -273,6 +270,8 @@ void BaseUI::mainLoop()
glClearColor(_clearColor.x(), _clearColor.y(), _clearColor.z(), _clearColor.w());
_camera_controller.setCamera(_renderer->getMainCamera());
// Start loop
while (!glfwWindowShouldClose(_window))
{
......@@ -662,27 +661,29 @@ void BaseUI::mouseMotion(double x, double y)
{
const float dx = static_cast<float>(x - _mouse_prev_pos.x()) / static_cast<float>(_frame_width);
const float dy = static_cast<float>(y - _mouse_prev_pos.y()) / static_cast<float>(_frame_height);
if (_mouse_button == GLFW_MOUSE_BUTTON_MIDDLE)
{
const float dmax = fabsf(dx) > fabs(dy) ? dx : dy;
const float scale = std::min<float>(dmax, 2.f);
_trackball.zoomCamera(scale);
_camera_controller.zoomCamera(scale);
_camera_changed = true;
}
else if (_mouse_button == GLFW_MOUSE_BUTTON_LEFT)
{
_trackball.rotateCamera(dx,dy);
const math::Vec2f a = {0.5f, 0.5f};
const math::Vec2f b = {0.5f + 0.01f, 0.5f + 0.01f};
// math::Vec2f a = {0.5f, 0.5f};
// math::Vec2f b = {0.5f + dx, 0.5f + dy};
// _renderer->getMainCamera()->setRotationMatrix(_arcball.rotate(a,b).upperLeft());
_camera_controller.rotateCamera(dx,dy);
_camera_changed = true;
}
else if (_mouse_button == GLFW_MOUSE_BUTTON_RIGHT) //TODO: left clic + shift = pan
{
_trackball.panCamera(dx,dy);
_camera_controller.panCamera(dx,dy);
_camera_changed = true;
}
......@@ -692,7 +693,7 @@ void BaseUI::mouseMotion(double x, double y)
void BaseUI::mouseScroll(double /*xoffset*/, double yoffset)
{
_trackball.zoomCamera(yoffset);
_camera_controller.zoomCamera(yoffset);
_camera_changed = true;
}
......@@ -709,38 +710,26 @@ void BaseUI::charPress(unsigned int codepoint)
}
case ('d'):
{
const math::Vec2f a = {0.5f, 0.5f};
const math::Vec2f b = {0.5f + 0.005f, 0.5f};
//main_camera->setRotationMatrix(_arcball.rotate(b, a).upperLeft());
_camera_controller.rotateCamera(0.005f,0.f);
_camera_changed = true;
break;
}
case ('q'):
{
const math::Vec2f a = {0.5f, 0.5f};
const math::Vec2f b = {0.5f - 0.005f, 0.5f};
// main_camera->setRotationMatrix(_arcball.rotate(b, a).upperLeft());
_camera_controller.rotateCamera(-0.005f,0.f);
_camera_changed = true;
break;
}
case ('s'):
{
const math::Vec2f a = {0.5f, 0.5f};
const math::Vec2f b = {0.5f, 0.5f + 0.005f};
// main_camera->setRotationMatrix(_arcball.rotate(b, a).upperLeft());
_camera_controller.rotateCamera(0.f, 0.005f);
_camera_changed = true;
break;
}
case ('z'):
{
const math::Vec2f a = {0.5f, 0.5f};
const math::Vec2f b = {0.5f, 0.5f - 0.005f};
// main_camera->setRotationMatrix(_arcball.rotate(b, a).upperLeft());
_camera_controller.rotateCamera(0.f, -0.005f);
_camera_changed = true;
break;
}
......@@ -921,6 +910,7 @@ void BaseUI::makeHeadStraight()
void BaseUI::previousCamera()
{
_renderer->previousCamera();
_camera_controller.setCamera(_renderer->getMainCamera());
_frame_width = _renderer->getWidth();
_frame_height = _renderer->getHeight();
_camera_changed = true;
......@@ -929,6 +919,7 @@ void BaseUI::previousCamera()
void BaseUI::nextCamera()
{
_renderer->nextCamera();
_camera_controller.setCamera(_renderer->getMainCamera());
_frame_width = _renderer->getWidth();
_frame_height = _renderer->getHeight();
_camera_changed = true;
......
......@@ -11,7 +11,7 @@
#include <mrf_core/radiometry/d65.hpp>
#include "Arcball.h"
#include "Trackball.h"
#include "CameraController.h"
#include "shader.h"
#include "texture.h"
......@@ -333,7 +333,7 @@ protected:
std::map<int, Shader *> _shader_prog;
std::map<int, bool> _update_parameters;
rendering::Trackball _trackball;
rendering::CameraController _camera_controller;
rendering::Arcball _arcball;
size_t _frame_width;
......@@ -353,6 +353,7 @@ protected:
int _mouse_button;
bool _mouse_pressed;
bool _render_on_pause;
float _frame_time;
float _rendering_time;
float _total_rendering_time;
float _samples_per_second;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment