Skip to content

Commit

Permalink
Refactor: name changes and member variable fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 16, 2023
1 parent 782d722 commit e87f5a5
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 60 deletions.
15 changes: 1 addition & 14 deletions Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ qt_standard_project_setup()

qt_add_executable(earth
main.cpp
mainwidget.h mainwidget.cpp
Main_widget.h Main_widget.cpp
Camera.h Camera.cpp
Common_defs.h
Geodesic_arcs.h Geodesic_arcs.cpp
Expand Down Expand Up @@ -68,19 +68,6 @@ target_link_libraries(earth PRIVATE
CGAL::CGAL
)


# Resources:
#set(shaders_resource_files
# "vertex_shader.glsl"
#)
#
#qt6_add_resources(earth "shaders"
# PREFIX
# "/"
# FILES
# ${shaders_resource_files}
#)

file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/shaders
DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

Expand Down
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/Geodesic_arcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@ void Geodesic_arcs::draw()
}
}
glBindVertexArray(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include "mainwidget.h"
#include "Main_widget.h"

#include <cmath>
#include <iostream>
Expand All @@ -8,17 +8,14 @@
#include <QMouseEvent>


MainWidget::~MainWidget()
Main_widget::~Main_widget()
{
// Make sure the context is current when deleting the texture and the buffers.
makeCurrent();
doneCurrent();
}


float theta = 0, phi = 0;
int vp_width=0, vp_height=0;

std::ostream& operator << (std::ostream& os, const QVector2D& v)
{
os << v.x() << ", " << v.y();
Expand All @@ -31,7 +28,7 @@ std::ostream& operator << (std::ostream& os, const QVector3D& v)
}


void MainWidget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
void Main_widget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
{
switch (e->button())
{
Expand All @@ -44,12 +41,12 @@ void MainWidget::set_mouse_button_pressed_flag(QMouseEvent* e, bool flag)
break;
}
}
void MainWidget::mousePressEvent(QMouseEvent* e)
void Main_widget::mousePressEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, true);
m_last_mouse_pos = QVector2D(e->position());
}
void MainWidget::mouseMoveEvent(QMouseEvent* e)
void Main_widget::mouseMoveEvent(QMouseEvent* e)
{
auto current_mouse_pos = QVector2D(e->position());
const auto diff = current_mouse_pos - m_last_mouse_pos;
Expand All @@ -58,21 +55,18 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e)
{
const float rotation_scale_factor = 0.1f;

if(0)
if(1)
{
// OUR CUSTOM AD-HOC CAMERA ROTATION
//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);
m_theta += rotation_scale_factor * diff.x();
m_phi += rotation_scale_factor * diff.y();
m_camera.rotate(-m_theta, -m_phi);
}
else
{
// ROTATION AROUND AN AXIS ORTHOGONAL TO THE BACKPROJECTED DIF-VECTOR!
QVector3D p0(m_last_mouse_pos.x(), vp_height - m_last_mouse_pos.y(), 0);
QVector3D p1(current_mouse_pos.x(), vp_height - current_mouse_pos.y(), 0);
// ROTATION AROUND AN AXIS ORTHOGONAL TO THE BACKPROJECTED DIF-VECTOR
QVector3D p0(m_last_mouse_pos.x(), m_vp_height - m_last_mouse_pos.y(), 0);
QVector3D p1(current_mouse_pos.x(), m_vp_height - current_mouse_pos.y(), 0);
auto dp = p1 - p0; // difference vector in OpenGL window coords.
QVector3D rdp(-dp.y(), dp.x(), 0); // rotate diff-vector CCW by 90-deg
QVector3D rp = p0 + rdp; // r1 rotated CCW by 90 deg
Expand All @@ -81,7 +75,7 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e)
auto proj = m_camera.get_projection_matrix();
auto view = m_camera.get_view_matrix();
auto model_view = view * model;
QRect viewport(0, 0, vp_width, vp_height);
QRect viewport(0, 0, m_vp_width, m_vp_height);
auto wp0 = p0.unproject(model_view, proj, viewport);
auto wrp = rp.unproject(model_view, proj, viewport);

Expand All @@ -106,17 +100,17 @@ void MainWidget::mouseMoveEvent(QMouseEvent* e)

m_last_mouse_pos = current_mouse_pos;
}
void MainWidget::mouseReleaseEvent(QMouseEvent* e)
void Main_widget::mouseReleaseEvent(QMouseEvent* e)
{
set_mouse_button_pressed_flag(e, false);
}
void MainWidget::timerEvent(QTimerEvent*)
void Main_widget::timerEvent(QTimerEvent*)
{
update();
}


void MainWidget::initializeGL()
void Main_widget::initializeGL()
{
initializeOpenGLFunctions();

Expand All @@ -140,12 +134,12 @@ void MainWidget::initializeGL()



void MainWidget::init_camera()
void Main_widget::init_camera()
{
m_camera.set_pos(0, 0, 3);
//m_camera.rotate_around_x(-90);
}
void MainWidget::init_geometry()
void Main_widget::init_geometry()
{
int num_slices, num_stacks;
num_slices = num_stacks = 64;
Expand All @@ -158,25 +152,25 @@ void MainWidget::init_geometry()
const float axes_length = 2;
m_world_coord_axes = std::make_unique<World_coord_axes>(axes_length);
}
void MainWidget::init_shader_programs()
void Main_widget::init_shader_programs()
{
init_sp_smooth();
init_sp_per_vertex_color();
init_sp_arc();
}
void MainWidget::init_sp_smooth()
void Main_widget::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()
void Main_widget::init_sp_per_vertex_color()
{
const char* vs = "shaders/per_vertex_color_vs.glsl";
const char* fs = "shaders/per_vertex_color_fs.glsl";
m_sp_per_vertex_color.init(vs, "", fs);
}
void MainWidget::init_sp_arc()
void Main_widget::init_sp_arc()
{
const char* vs = "shaders/arc_vs.glsl";
const char* fs = "shaders/arc_fs.glsl";
Expand All @@ -190,17 +184,17 @@ std::ostream& operator << (std::ostream& os, const QVector4D& v)
return os;
}

void MainWidget::resizeGL(int w, int h)
void Main_widget::resizeGL(int w, int h)
{
vp_width = w;
vp_height = h;
m_vp_width = w;
m_vp_height = h;

// Reset projection
qreal aspect = qreal(w) / qreal(h ? h : 1);
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;
m_camera.perspective(fov, aspect, z_near, z_far);
}
void MainWidget::paintGL()
void Main_widget::paintGL()
{
QMatrix4x4 model;
model.rotate(-90, 1,0,0); // this makes z-axes point upwards!
Expand Down Expand Up @@ -263,4 +257,4 @@ void MainWidget::paintGL()

sp.unuse();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#ifndef MAIN_WIDGET_H
#define MAIN_WIDGET_H

#include <QOpenGLWidget>
#include <QMatrix4x4>
Expand All @@ -22,13 +22,13 @@
#include "World_coordinate_axes.h"


class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
{
Q_OBJECT

public:
using QOpenGLWidget::QOpenGLWidget;
~MainWidget();
~Main_widget();

protected:
void set_mouse_button_pressed_flag(QMouseEvent* e, bool flag);
Expand Down Expand Up @@ -66,9 +66,11 @@ class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
bool m_left_mouse_button_down = false;
bool m_middle_mouse_button_down = false;
QVector2D m_last_mouse_pos;
float m_theta = 0, m_phi = 0;
int m_vp_width = 0, m_vp_height = 0;

// Timer for continuous screen-updates
QBasicTimer m_timer;
};

#endif // MAINWIDGET_H
#endif
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/Shader_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ void Shader_program::set_uniform(const GLchar* name, const QVector4D& v)
{
const auto uniform_loc = get_uniform_location(name);
set_uniform(uniform_loc, v);
}
}
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/Sphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ void Sphere::draw()
glDrawElements(GL_TRIANGLES, m_num_indices, GL_UNSIGNED_INT, 0);
}
glBindVertexArray(0);
}
}
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ std::string read_file(const std::string& file_name)
ifs.read(&bytes[0], file_size);

return std::string(&bytes[0], file_size);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ void World_coord_axes::draw()
glDrawArrays(GL_LINES, 0, count);
}
glBindVertexArray(0);
}
}
6 changes: 3 additions & 3 deletions Arrangement_on_surface_2/demo/earth/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s): Deniz Diktas <[email protected]>
// Author(s): Engin Deniz Diktas <[email protected]>

#include <QApplication>
#include <QLabel>
#include <QSurfaceFormat>

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


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

0 comments on commit e87f5a5

Please sign in to comment.