Skip to content

Commit

Permalink
Refactor: camera manipulator for zoom moved to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jul 13, 2023
1 parent 96c81a0 commit 8bbb43c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
26 changes: 18 additions & 8 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ file(GLOB source_files_aos
source_group( "Aos" FILES ${source_files_aos} )


# GIS
file(GLOB source_files_gis
Kml_reader.h Kml_reader.cpp
Shapefile.h Shapefile.cpp
)
source_group( "GIS" FILES ${source_files_gis} )


# GRAPHICS
file(GLOB source_files_graphics
Camera.h Camera.cpp
Camera_manip.h Camera_manip.cpp
Camera_manip_rot.h Camera_manip_rot.cpp
Camera_manip_rot_bpa.h Camera_manip_rot_bpa.cpp
Shader_program.h Shader_program.cpp
)
source_group( "Graphics" FILES ${source_files_graphics} )
Expand All @@ -70,12 +75,16 @@ file(GLOB source_files_graphics_geometry
)
source_group( "Graphics_Geometry" FILES ${source_files_graphics_geometry} )

# GIS
file(GLOB source_files_gis
Kml_reader.h Kml_reader.cpp
Shapefile.h Shapefile.cpp

# GUI
file(GLOB source_files_gui
Camera_manip.h Camera_manip.cpp
Camera_manip_rot.h Camera_manip_rot.cpp
Camera_manip_rot_bpa.h Camera_manip_rot_bpa.cpp
Camera_manip_zoom.h Camera_manip_zoom.cpp
)
source_group( "GIS" FILES ${source_files_gis} )
source_group( "GUI" FILES ${source_files_gui} )


#SOURCE FILES (NOT CATEGORIZED YET)
file(GLOB source_files
Expand All @@ -93,6 +102,7 @@ qt_add_executable(earth
${source_files_gis}
${source_files_graphics}
${source_files_graphics_geometry}
${source_files_gui}
${source_files}
)

Expand Down
22 changes: 22 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Camera_manip_zoom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#include "Camera_manip_zoom.h"


Camera_manip_zoom::Camera_manip_zoom(Camera& camera) :
Camera_manip(camera)
{
}

void Camera_manip_zoom::mouse_move_event(QMouseEvent* e)
{
if (m_middle_mouse_button_down)
{
auto current_mouse_pos = QVector2D(e->position());
const auto diff = current_mouse_pos - m_last_mouse_pos;

const float zoom_scale_factor = 0.01f;
const auto distance = zoom_scale_factor * diff.y();
m_camera.move_forward(distance);
}
}

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

#ifndef CAMERA_MANIP_ZOOM_H
#define CAMERA_MANIP_ZOOM_H

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

#include "Camera_manip.h"


class Camera_manip_zoom : public Camera_manip
{
public:
Camera_manip_zoom(Camera& camera);

protected:
virtual void mouse_move_event(QMouseEvent* e) override;

private:
float m_theta = 0, m_phi = 0;
};

#endif
31 changes: 18 additions & 13 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Aos.h"
#include "Camera_manip_rot.h"
#include "Camera_manip_rot_bpa.h"
#include "Camera_manip_zoom.h"
#include "Kml_reader.h"
#include "Shapefile.h"
#include "Tools.h"
Expand Down Expand Up @@ -37,33 +38,36 @@ 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->mousePressEvent(e);
// forward the event to the camera manipulators
m_camera_manip_rot->mousePressEvent(e);
m_camera_manip_zoom->mousePressEvent(e);

set_mouse_button_pressed_flag(e, true);
m_mouse_press_pos = m_last_mouse_pos = QVector2D(e->position());
}
void Main_widget::mouseMoveEvent(QMouseEvent* e)
{
// forward the event to the camera manipulator
m_camera_manip->mouseMoveEvent(e);
m_camera_manip_rot->mouseMoveEvent(e);
m_camera_manip_zoom->mouseMoveEvent(e);

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

if(m_middle_mouse_button_down)
{
const float zoom_scale_factor = 0.01f;
const auto distance = zoom_scale_factor * diff.y();
m_camera.move_forward(distance);
}
//if(m_middle_mouse_button_down)
//{
// 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 Main_widget::mouseReleaseEvent(QMouseEvent* e)
{
// forward the event to the camera manipulator
m_camera_manip->mouseReleaseEvent(e);
m_camera_manip_rot->mouseReleaseEvent(e);
m_camera_manip_zoom->mouseReleaseEvent(e);

set_mouse_button_pressed_flag(e, false);
}
Expand Down Expand Up @@ -246,8 +250,9 @@ void Main_widget::initializeGL()
void Main_widget::init_camera()
{
m_camera.set_pos(0, 0, 3);
//m_camera_manip = std::make_unique<Camera_manip_rot>(m_camera);
m_camera_manip = std::make_unique<Camera_manip_rot_bpa>(m_camera);
m_camera_manip_rot = std::make_unique<Camera_manip_rot>(m_camera);
//m_camera_manip_rot = std::make_unique<Camera_manip_rot_bpa>(m_camera);
m_camera_manip_zoom = std::make_unique<Camera_manip_zoom>(m_camera);
}
void Main_widget::init_geometry()
{
Expand Down Expand Up @@ -384,7 +389,7 @@ void Main_widget::find_minimum_projected_error_on_sphere(float we)

void Main_widget::resizeGL(int w, int h)
{
m_camera_manip->resizeGL(w, h);
m_camera_manip_rot->resizeGL(w, h);

m_vp_width = w;
m_vp_height = h;
Expand Down
3 changes: 2 additions & 1 deletion Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase

// Camera & controls
Camera m_camera;
std::unique_ptr<Camera_manip> m_camera_manip;
std::unique_ptr<Camera_manip> m_camera_manip_rot;
std::unique_ptr<Camera_manip> m_camera_manip_zoom;

bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
Expand Down

0 comments on commit 8bbb43c

Please sign in to comment.