Skip to content

Commit

Permalink
intermediate camera fix (initial axes are not correct, this needs to …
Browse files Browse the repository at this point in the history
…be fixed!)
  • Loading branch information
denizdiktas committed Jun 16, 2023
1 parent eaece85 commit b3d657d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
25 changes: 22 additions & 3 deletions Arrangement_on_surface_2/demo/earth/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,29 @@ void Camera::rotate_around_y(float theta)
m_ux = ux.toVector3D();
m_uz = uz.toVector3D();
}
void Camera::rotate(float theta_around_x, float theta_around_y)
//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)
{
rotate_around_x(theta_around_x);
rotate_around_y(theta_around_y);
QMatrix4x4 r1;
QVector3D ey(0, 1, 0);
r1.rotate(theta, ey);

// rx = rotated x axis
auto rx = r1 * QVector3D(1,0,0);
QMatrix4x4 r2;
r2.rotate(phi, rx);

// total rotation:
auto r = r2 * r1;

m_pos = r * QVector3D(0, 0, 3);
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);
}

void Camera::move_forward(float distance)
Expand Down
6 changes: 5 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class Camera
// 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);
//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)
void rotate(float theta, float phi);

// move the camera forward around its own z-axis
void move_forward(float distance);
Expand Down
14 changes: 9 additions & 5 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ MainWidget::~MainWidget()
}


float theta = 0, phi = 0;

void MainWidget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
{
switch (e->button())
Expand All @@ -42,9 +44,12 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e)
if (m_left_mouse_button_down)
{
const float rotation_scale_factor = 0.1f;
const float theta_around_x = rotation_scale_factor * diff.y();
const float theta_around_y = rotation_scale_factor * diff.x();
m_camera.rotate(theta_around_x, theta_around_y);
//const float theta_around_x = rotation_scale_factor * diff.y();
//const float theta_around_y = rotation_scale_factor * diff.x();
//m_camera.rotate(theta_around_x, theta_around_y);
theta += rotation_scale_factor * diff.x();
phi += rotation_scale_factor * diff.y();
m_camera.rotate(-theta, -phi);
}
else if(m_middle_mouse_button_down)
{
Expand Down Expand Up @@ -92,7 +97,7 @@ void MainWidget::initializeGL()
void MainWidget::init_camera()
{
m_camera.set_pos(0, 0, 3);
m_camera.rotate_around_x(-90);
//m_camera.rotate_around_x(-90);
}
void MainWidget::init_geometry()
{
Expand All @@ -118,7 +123,6 @@ void MainWidget::init_sp_smooth()
const char* vs = "shaders/smooth_vs.glsl";
const char* fs = "shaders/smooth_fs.glsl";
m_sp_smooth.init(vs, "", fs);

}
void MainWidget::init_sp_per_vertex_color()
{
Expand Down

0 comments on commit b3d657d

Please sign in to comment.