Skip to content

Commit

Permalink
Bug fixed: camera now rotates from the last zoom position
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 19, 2023
1 parent 1e760f4 commit a1af86f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 45 deletions.
58 changes: 20 additions & 38 deletions Arrangement_on_surface_2/demo/earth/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Camera::Camera() :

void Camera::perspective(float fov, float aspect, float z_near, float z_far)
{
m_z_near = z_near;
m_z_far = z_far;

m_projection.setToIdentity();
m_projection.perspective(fov, aspect, z_near, z_far);
}
Expand All @@ -25,45 +28,23 @@ QMatrix4x4 Camera::get_view_matrix() const
}


void Camera::rotate_around_x(float theta)
{
QMatrix4x4 rot;
rot.rotate(theta, m_ux);
auto pos = m_pos.toVector4D(); pos.setW(1);
auto uy = m_uy.toVector4D(); uy.setW(0);
auto uz = m_uz.toVector4D(); uz.setW(0);

pos = pos * rot;
uy = uy * rot;
uz = uz * rot;

m_pos = pos.toVector3D();
m_uy = uy.toVector3D();
m_uz = uz.toVector3D();
}
void Camera::rotate_around_y(float theta)
{
QMatrix4x4 rot;
rot.rotate(theta, m_uy);
auto pos = m_pos.toVector4D(); pos.setW(1);
auto ux = m_ux.toVector4D(); ux.setW(0);
auto uz = m_uz.toVector4D(); uz.setW(0);

pos = pos * rot;
ux = ux * rot;
uz = uz * rot;

m_pos = pos.toVector3D();
m_ux = ux.toVector3D();
m_uz = uz.toVector3D();
}
//void Camera::rotate(float theta_around_x, float theta_around_y)
//{
// rotate_around_x(theta_around_x);
// rotate_around_y(theta_around_y);
//}
void Camera::rotate(float theta, float phi)
{
// TO-DO: use the following logic to eliminate the QT-deprecation warnings!
//QMatrix4x4 rot;
//rot.rotate(theta, m_ux);
//auto pos = m_pos.toVector4D(); pos.setW(1);
//auto uy = m_uy.toVector4D(); uy.setW(0);
//auto uz = m_uz.toVector4D(); uz.setW(0);

//pos = pos * rot;
//uy = uy * rot;
//uz = uz * rot;

//m_pos = pos.toVector3D();
//m_uy = uy.toVector3D();
//m_uz = uz.toVector3D();

QMatrix4x4 r1;
QVector3D ey(0, 1, 0);
r1.rotate(theta, ey);
Expand All @@ -76,7 +57,8 @@ void Camera::rotate(float theta, float phi)
// total rotation:
auto r = r2 * r1;

m_pos = r * QVector3D(0, 0, 3);
const auto dist_cam_to_origin = m_pos.length();
m_pos = r * QVector3D(0, 0, dist_cam_to_origin);
m_ux = r * QVector3D(1, 0, 0); // should be the same as rx (sanity check?)
m_uy = r * QVector3D(0, 1, 0);
m_uz = r * QVector3D(0, 0, 1);
Expand Down
8 changes: 3 additions & 5 deletions Arrangement_on_surface_2/demo/earth/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ class Camera

void perspective(float fov, float aspect_ratio, float z_near, float z_far);

float get_z_near() const { return m_z_near; }
QMatrix4x4 get_view_matrix() const;
QMatrix4x4 get_projection_matrix() const { return m_projection; }

// rotate the camera around its own axes
void rotate_around_x(float theta);
void rotate_around_y(float theta);
//void rotate(float theta_around_x, float theta_around_y);

// theta: angle around y-axis
// phi: angle from the xz-plane (= rotated x-axis after the above rotation)
Expand All @@ -41,6 +37,8 @@ class Camera
QVector3D m_uy;
QVector3D m_uz;

float m_z_near, m_z_far;

QMatrix4x4 m_projection;
};

Expand Down
4 changes: 2 additions & 2 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void Main_widget::resizeGL(int w, int h)
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 z_near = m_camera.get_z_near();
const float r = 1.f; // sphere radius
const QVector3D origin(0, 0, 0);
const float dist_to_cam = m_camera.get_pos().distanceToPoint(origin);
Expand Down Expand Up @@ -314,7 +314,7 @@ void Main_widget::paintGL()
sp.unuse();
}

// WORLD COORDINATE AXES & GEODESIC ARCS
// WORLD COORDINATE AXES
{
auto& sp = m_sp_per_vertex_color;
sp.use();
Expand Down

0 comments on commit a1af86f

Please sign in to comment.