intermediate camera fix (initial axes are not correct, this needs to be fixed!)

This commit is contained in:
denizdiktas 2023-06-16 10:40:53 +03:00
parent eaece85eaa
commit b3d657d1db
3 changed files with 36 additions and 9 deletions

View File

@ -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)

View File

@ -25,7 +25,11 @@ public:
// 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);

View File

@ -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())
@ -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)
{
@ -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()
{
@ -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()
{