Skip to content

Commit

Permalink
Corrected behavior of the camera manipulator (backprojected diff-vect…
Browse files Browse the repository at this point in the history
…or method)
  • Loading branch information
denizdiktas committed Jun 19, 2023
1 parent a1af86f commit e815709
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 23 deletions.
18 changes: 17 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ QMatrix4x4 Camera::get_view_matrix() const
}


void Camera::rotate(float theta, float phi)
void Camera::rotate_from_init_config(float theta, float phi)
{
// TO-DO: use the following logic to eliminate the QT-deprecation warnings!
//QMatrix4x4 rot;
Expand Down Expand Up @@ -72,6 +72,22 @@ void Camera::rotate(QMatrix4x4 rot)
m_uz = rot * m_uz;
}

void Camera::save_config()
{
m_saved_pos = m_pos;
m_saved_ux = m_ux;
m_saved_uy = m_uy;
m_saved_uz = m_uz;
}

void Camera::rotate_from_saved_config(QMatrix4x4 rot)
{
m_pos = rot * m_saved_pos;
m_ux = rot * m_saved_ux;
m_uy = rot * m_saved_uy;
m_uz = rot * m_saved_uz;
}

void Camera::move_forward(float distance)
{
// recall that in OpenGL camera model, camera's z-axis points always
Expand Down
11 changes: 10 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ class Camera

// theta: angle around y-axis
// phi: angle from the xz-plane (= rotated x-axis after the above rotation)
void rotate(float theta, float phi);
void rotate_from_init_config(float theta, float phi);
void rotate(QMatrix4x4 rot);

// save config & rotate from saved config (move to separate class?)
void save_config();
void rotate_from_saved_config(QMatrix4x4 rot);

// move the camera forward around its own z-axis
void move_forward(float distance);

Expand All @@ -37,6 +41,11 @@ class Camera
QVector3D m_uy;
QVector3D m_uz;

QVector3D m_saved_pos;
QVector3D m_saved_ux;
QVector3D m_saved_uy;
QVector3D m_saved_uz;

float m_z_near, m_z_far;

QMatrix4x4 m_projection;
Expand Down
33 changes: 12 additions & 21 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ void Main_widget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
void Main_widget::mousePressEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, true);
m_last_mouse_pos = QVector2D(e->position());
m_mouse_press_pos = m_last_mouse_pos = QVector2D(e->position());

// for the backprojected diff-vector method:
if (m_left_mouse_button_down)
{
m_camera.save_config();
}
}
void Main_widget::mouseMoveEvent(QMouseEvent* e)
{
Expand All @@ -50,12 +56,13 @@ void Main_widget::mouseMoveEvent(QMouseEvent* e)
// OUR CUSTOM AD-HOC CAMERA ROTATION
m_theta += rotation_scale_factor * diff.x();
m_phi += rotation_scale_factor * diff.y();
m_camera.rotate(-m_theta, -m_phi);
m_camera.rotate_from_init_config(-m_theta, -m_phi);
}
else
{
// ROTATION AROUND AN AXIS ORTHOGONAL TO THE BACKPROJECTED DIF-VECTOR
QVector3D p0(m_last_mouse_pos.x(), m_vp_height - m_last_mouse_pos.y(), 0);
//QVector3D p0(m_last_mouse_pos.x(), m_vp_height - m_last_mouse_pos.y(), 0);
QVector3D p0(m_mouse_press_pos.x(), m_vp_height - m_mouse_press_pos.y(), 0);
QVector3D p1(current_mouse_pos.x(), m_vp_height - current_mouse_pos.y(), 0);
auto dp = p1 - p0; // difference vector in OpenGL window coords.
QVector3D rdp(-dp.y(), dp.x(), 0); // rotate diff-vector CCW by 90-deg
Expand All @@ -77,7 +84,7 @@ void Main_widget::mouseMoveEvent(QMouseEvent* e)
QMatrix4x4 rot_matrix;
rot_matrix.rotate(-rot_angle, rot_axis);

m_camera.rotate(rot_matrix);
m_camera.rotate_from_saved_config(rot_matrix);
}
}
else if(m_middle_mouse_button_down)
Expand Down Expand Up @@ -108,23 +115,7 @@ void Main_widget::initializeGL()
init_shader_programs();

{
//// compute the back-projected error
//QRect vp(0, 0, m_vp_width, m_vp_height);
//auto proj = m_camera.get_projection_matrix();
//auto view = m_camera.get_view_matrix();
//QMatrix4x4 model;
//auto model_view = view * model;
//
//QVector3D p0(m_vp_width / 2, m_vp_height / 2, 0);
//QVector3D p1(p0.x() + 1, p0.y(), 0);
//auto wp0 = p0.unproject(model_view, proj, vp);
//auto wp1 = p1.unproject(model_view, proj, vp);
//const float z_near = 1.f;
//const float r = 1.f; // sphere radius
//const float d = m_camera.get_pos().distanceToPoint(QVector3D(0, 0, 0)) - r;
//float err = wp0.distanceToPoint(wp1) * (d / z_near);
//std::cout << "error = " << err << std::endl;

// TO-DO: move this code to resizeGL (when viewport is initialized)
// has to be defined after camera has been defined:
// because we want to compute the error based on camera parameters!
Geodesic_arcs ga;
Expand Down
1 change: 1 addition & 0 deletions Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
QVector2D m_last_mouse_pos;
QVector2D m_mouse_press_pos;
float m_theta = 0, m_phi = 0;
int m_vp_width = 0, m_vp_height = 0;

Expand Down

0 comments on commit e815709

Please sign in to comment.