Skip to content

Commit

Permalink
Added: camera-zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 13, 2023
1 parent 84aa351 commit 2ee3832
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,10 @@ void Camera::rotate(float theta_around_x, float theta_around_y)
m_ux = m_ux * rot;
m_uz = m_uz * rot;
}

void Camera::move_forward(float distance)
{
// recall that in OpenGL camera model, camera's z-axis points always
// out of the screen (towards the user).
m_pos -= distance * m_uz;
}
3 changes: 3 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Camera
// rotate the camera around its own axes
void rotate(float theta_around_x, float theta_around_y);

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

private:
QVector3D m_pos;
QVector3D m_ux;
Expand Down
42 changes: 35 additions & 7 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,57 @@ MainWidget::~MainWidget()

void MainWidget::mousePressEvent(QMouseEvent *e)
{
m_mouse_pressed = true;
switch (e->button())
{
case Qt::LeftButton:
m_left_mouse_button_down = true;
break;

case Qt::MiddleButton:
m_middle_mouse_button_down = true;
break;
}
m_last_mouse_pos = QVector2D(e->position());
}
void MainWidget::mouseMoveEvent(QMouseEvent* e)
{
auto current_mouse_pos = QVector2D(e->position());
const auto diff = current_mouse_pos - m_last_mouse_pos;

if (m_mouse_pressed)
if (m_left_mouse_button_down)
{
const auto diff = current_mouse_pos - m_last_mouse_pos;
const float scale_factor = 0.1f;
const float theta_around_x = scale_factor * diff.y();
const float theta_around_y = scale_factor * diff.x();
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);
}
else
{
const float zoom_scale_factor = 0.01f;
const auto distance = zoom_scale_factor * diff.y();
m_camera.move_forward(distance);
}

m_last_mouse_pos = current_mouse_pos;
}
void MainWidget::wheelEvent(QWheelEvent* e)
{
auto distance = e->angleDelta();
m_camera.move_forward(distance.x());
}
void MainWidget::mouseReleaseEvent(QMouseEvent *e)
{
m_mouse_pressed = false;
switch (e->button())
{
case Qt::LeftButton:
m_left_mouse_button_down = false;
break;

case Qt::MiddleButton:
m_middle_mouse_button_down = false;
break;
}
}
void MainWidget::timerEvent(QTimerEvent *)
{
Expand Down
4 changes: 3 additions & 1 deletion Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
protected:
void mousePressEvent(QMouseEvent *e) override;
void mouseMoveEvent(QMouseEvent* e) override;
void wheelEvent(QWheelEvent* event) override;
void mouseReleaseEvent(QMouseEvent *e) override;
void timerEvent(QTimerEvent *e) override;

Expand All @@ -54,7 +55,8 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase

// camera & controls
Camera m_camera;
bool m_mouse_pressed = false;
bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
QVector2D m_last_mouse_pos;


Expand Down

0 comments on commit 2ee3832

Please sign in to comment.