Skip to content

Commit

Permalink
refactor on Single_vertex: position is held inside this class now (no…
Browse files Browse the repository at this point in the history
… need for the application to keep track of it)
  • Loading branch information
denizdiktas committed Aug 18, 2023
1 parent a323352 commit 96145bf
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Arrangement_on_surface_2/demo/earth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ source_group( "Graphics" FILES ${source_files_graphics} )
# GRAPHICS-GEOMETRY (Graphics-related)
file(GLOB source_files_graphics_geometry
Line_strips.h Line_strips.cpp
SingleVertex.h SingleVertex.cpp
Single_vertex.h Single_vertex.cpp
Sphere.h Sphere.cpp
Triangles.h Triangles.cpp
Vertices.h Vertices.cpp
Expand Down
10 changes: 7 additions & 3 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Main_widget::~Main_widget()
doneCurrent();
}

void Main_widget::set_mouse_pos(const QVector3D mouse_pos)
{
m_gr_mouse_vertex->set_pos(mouse_pos);
}
void Main_widget::hightlight_country(const std::string& country_name)
{
static std::string prev_picked_country;
Expand Down Expand Up @@ -102,8 +106,8 @@ void Main_widget::initializeGL()
{
m_pick_handler = std::make_unique<GUI_country_pick_handler>(*this);

m_mouse_pos = QVector3D(0, -1, 0);
m_gr_mouse_vertex = std::make_unique<SingleVertex>(m_mouse_pos);
QVector3D initial_mouse_pos(0, -1, 0);
m_gr_mouse_vertex = std::make_unique<Single_vertex>(initial_mouse_pos);

// triangulation
{
Expand Down Expand Up @@ -386,7 +390,7 @@ void Main_widget::paintGL()
//auto pos = m_mouse_vertex->get_pos();
//pos.setX(pos.x() + 0.01);
//m_mouse_vertex->set_pos(pos);
m_gr_mouse_vertex->set_pos(m_mouse_pos);
//m_gr_mouse_vertex->set_pos(m_mouse_pos);
draw_safe(m_gr_mouse_vertex);
}

Expand Down
7 changes: 3 additions & 4 deletions Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "Kml_reader.h"
#include "Line_strips.h"
#include "Shader_program.h"
#include "SingleVertex.h"
#include "Single_vertex.h"
#include "Sphere.h"
#include "Triangles.h"
#include "Vertices.h"
Expand All @@ -49,7 +49,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
auto& get_model_matrix() { return m_model; }
auto& get_arr_handle() { return m_arrh; }

void set_mouse_pos(const QVector3D mouse_pos) { m_mouse_pos = mouse_pos; }
void set_mouse_pos(const QVector3D mouse_pos);

void hightlight_country(const std::string& country_name);

Expand Down Expand Up @@ -92,8 +92,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
std::unique_ptr<GUI_event_handler> m_pick_handler;

// These are used to highlight the picked position by right-mouse click
QVector3D m_mouse_pos;
std::unique_ptr<SingleVertex> m_gr_mouse_vertex;
std::unique_ptr<Single_vertex> m_gr_mouse_vertex;


// TRIANGLES for rendering the countries in solid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
//
// Author(s): Engin Deniz Diktas <[email protected]>

#include "SingleVertex.h"
#include "Single_vertex.h"


SingleVertex::SingleVertex(const QVector3D& pos)
Single_vertex::Single_vertex(const QVector3D& pos)
{
initializeOpenGLFunctions();

Expand Down Expand Up @@ -47,35 +47,40 @@ SingleVertex::SingleVertex(const QVector3D& pos)
}


void SingleVertex::set_visible(bool flag)
void Single_vertex::set_visible(bool flag)
{
m_visible = flag;
}
void SingleVertex::set_pos(const QVector3D& pos)
void Single_vertex::set_pos(const QVector3D& pos)
{
m_pos = pos;
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
auto vertex_buffer_size = sizeof(m_pos);
auto vertex_buffer_data = reinterpret_cast<const void*>(&m_pos);
auto offset = 0;
glBufferSubData(GL_ARRAY_BUFFER,
offset,
vertex_buffer_size,
vertex_buffer_data);
m_update = true;
}
const QVector3D& SingleVertex::get_pos() const
const QVector3D& Single_vertex::get_pos() const
{
return m_pos;
}


void SingleVertex::draw()
void Single_vertex::draw()
{
if (m_visible)
{
if (m_update)
{
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
auto vertex_buffer_size = sizeof(m_pos);
auto vertex_buffer_data = reinterpret_cast<const void*>(&m_pos);
auto offset = 0;
glBufferSubData(GL_ARRAY_BUFFER,
offset,
vertex_buffer_size,
vertex_buffer_data);
m_update = false;
}

glBindVertexArray(m_vao);
glDrawArrays(GL_POINTS, 0, 1);
glBindVertexArray(0);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
#include "Common_defs.h"


class SingleVertex : protected OpenGLFunctionsBase
class Single_vertex : protected OpenGLFunctionsBase
{
public:
SingleVertex(const QVector3D& pos);
Single_vertex(const QVector3D& pos);

void set_visible(bool flag);
void set_pos(const QVector3D& pos);
Expand All @@ -28,6 +28,7 @@ class SingleVertex : protected OpenGLFunctionsBase

private:
bool m_visible;
bool m_update = true; // flag to update the VBO (set_pos sets this)
GLuint m_vao, m_vbo;
QVector3D m_pos;
};
Expand Down

0 comments on commit 96145bf

Please sign in to comment.