Skip to content

Commit

Permalink
Refactor: moved camera rotation-manipulator to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jul 12, 2023
1 parent 78cead6 commit e7db9ce
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 10 deletions.
1 change: 1 addition & 0 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ source_group( "Aos" FILES ${source_files_aos} )
# GRAPHICS
file(GLOB source_files_graphics
Camera.h Camera.cpp
Camera_manip_rot.h Camera_manip_rot.cpp
Shader_program.h Shader_program.cpp
)
source_group( "Graphics" FILES ${source_files_graphics} )
Expand Down
44 changes: 44 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_rot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include "Camera_manip_rot.h"


Camera_manip_rot::Camera_manip_rot(Camera& camera) :
m_camera(camera)
{
}


void Camera_manip_rot::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
{
switch (e->button())
{
case Qt::LeftButton:
m_left_mouse_button_down = flag;
break;
}
}
void Camera_manip_rot::mousePressEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, true);
m_mouse_press_pos = m_last_mouse_pos = QVector2D(e->position());
}
void Camera_manip_rot::mouseMoveEvent(QMouseEvent* e)
{
auto current_mouse_pos = QVector2D(e->position());
const auto diff = current_mouse_pos - m_last_mouse_pos;

if (m_left_mouse_button_down)
{
const float rotation_scale_factor = 0.1f;
m_theta += rotation_scale_factor * diff.x();
m_phi += rotation_scale_factor * diff.y();
m_camera.rotate_from_init_config(-m_theta, -m_phi);
}

m_last_mouse_pos = current_mouse_pos;
}
void Camera_manip_rot::mouseReleaseEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, false);
}

33 changes: 33 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_rot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#ifndef CAMERA_MANIP_ROT_H
#define CAMERA_MANIP_ROT_H

#include <qevent.h>
#include <qvector2d.h>

#include "Camera.h"


class Camera_manip_rot
{
public:
Camera_manip_rot(Camera& camera);

void mousePressEvent(QMouseEvent* e);
void mouseMoveEvent(QMouseEvent* e);
void mouseReleaseEvent(QMouseEvent* e);

protected:
void set_mouse_button_pressed_flag(QMouseEvent* e, bool flag);

private:
Camera& m_camera;
float m_theta = 0, m_phi = 0;

bool m_left_mouse_button_down = false;
QVector2D m_last_mouse_pos;
QVector2D m_mouse_press_pos;
};


#endif
19 changes: 13 additions & 6 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ void Main_widget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
}
void Main_widget::mousePressEvent(QMouseEvent* e)
{
// forward the event to the camera manipulator
m_camera_manip_rot->mousePressEvent(e);

set_mouse_button_pressed_flag(e, true);
m_mouse_press_pos = m_last_mouse_pos = QVector2D(e->position());

Expand All @@ -46,6 +49,9 @@ void Main_widget::mousePressEvent(QMouseEvent* e)
}
void Main_widget::mouseMoveEvent(QMouseEvent* e)
{
// forward the event to the camera manipulator
m_camera_manip_rot->mouseMoveEvent(e);

auto current_mouse_pos = QVector2D(e->position());
const auto diff = current_mouse_pos - m_last_mouse_pos;

Expand All @@ -56,9 +62,9 @@ void Main_widget::mouseMoveEvent(QMouseEvent* e)
if(1)
{
// OUR CUSTOM AD-HOC CAMERA ROTATION
m_theta += rotation_scale_factor * diff.x();
m_phi += rotation_scale_factor * diff.y();
m_camera.rotate_from_init_config(-m_theta, -m_phi);
//m_theta += rotation_scale_factor * diff.x();
//m_phi += rotation_scale_factor * diff.y();
//m_camera.rotate_from_init_config(-m_theta, -m_phi);
}
else
{
Expand Down Expand Up @@ -100,6 +106,9 @@ void Main_widget::mouseMoveEvent(QMouseEvent* e)
}
void Main_widget::mouseReleaseEvent(QMouseEvent* e)
{
// forward the event to the camera manipulator
m_camera_manip_rot->mouseReleaseEvent(e);

set_mouse_button_pressed_flag(e, false);
}
void Main_widget::timerEvent(QTimerEvent*)
Expand Down Expand Up @@ -166,8 +175,6 @@ void Main_widget::keyPressEvent(QKeyEvent* event)
}
}



void Main_widget::initializeGL()
{
// verify that the node (180.0, -84.71338) in Antarctica is redundant
Expand Down Expand Up @@ -284,7 +291,7 @@ void Main_widget::initializeGL()
void Main_widget::init_camera()
{
m_camera.set_pos(0, 0, 3);
//m_camera.rotate_around_x(-90);
m_camera_manip_rot = std::make_unique<Camera_manip_rot>(m_camera);
}
void Main_widget::init_geometry()
{
Expand Down
5 changes: 4 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <qopenglwidget.h>

#include "Camera.h"
#include "Camera_manip_rot.h"
#include "Common_defs.h"
#include "Kml_reader.h"
#include "Line_strips.h"
Expand Down Expand Up @@ -82,7 +83,9 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
Shader_program m_sp_arc;

// Camera & controls
Camera m_camera;
Camera m_camera;
std::unique_ptr<Camera_manip_rot> m_camera_manip_rot;

bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
QVector2D m_last_mouse_pos;
Expand Down
4 changes: 1 addition & 3 deletions Arrangement_on_surface_2/demo/earth/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#ifndef QT_NO_OPENGL
#include "Main_widget.h"
#include "Main_widget2.h"
#endif


Expand All @@ -33,8 +32,7 @@ int main(int argc, char* argv[])
app.setApplicationName("Earth");
app.setApplicationVersion("0.1");
#ifndef QT_NO_OPENGL
//Main_widget widget;
Main_widget2 widget;
Main_widget widget;
widget.show();
#else
QLabel note("OpenGL Support required");
Expand Down

0 comments on commit e7db9ce

Please sign in to comment.