Skip to content

Commit

Permalink
Refactor: separated Geodesic arcs and their representation into diffe…
Browse files Browse the repository at this point in the history
…rent classes
  • Loading branch information
denizdiktas committed Jun 17, 2023
1 parent d68e430 commit f9c98fe
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 70 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 @@ -43,6 +43,7 @@ qt_add_executable(earth
Camera.h Camera.cpp
Common_defs.h
Geodesic_arcs.h Geodesic_arcs.cpp
Line_strips.h Line_strips.cpp
Shader_program.h Shader_program.cpp
Sphere.h Sphere.cpp
Tools.h Tools.cpp
Expand Down
10 changes: 10 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Common_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,21 @@
#ifndef COMMON_DEFS_H
#define COMMON_DEFS_H

#include <vector>

#include <qopenglfunctions_3_3_core.h>
//#include <qopenglfunctions_4_5_core.h>
#include <qvector3d.h>


using OpenGLFunctionsBase = QOpenGLFunctions_3_3_Core;


struct Line_strip_approx
{
std::vector<QVector3D> points;
std::vector<GLuint> offsets;
};


#endif
66 changes: 9 additions & 57 deletions Arrangement_on_surface_2/demo/earth/Geodesic_arcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,16 @@ std::ostream& operator << (std::ostream& os, const Approximate_Vector_3& v)
}


Geodesic_arcs::Geodesic_arcs()
Line_strip_approx Geodesic_arcs::get_approximate_arcs(double error)
{
initializeOpenGLFunctions();

// Construct the arrangement from 12 geodesic arcs.
Geom_traits traits;
Arrangement arr(&traits);


auto ctr_p = traits.construct_point_2_object();
auto ctr_cv = traits.construct_curve_2_object();


std::vector<Curve> xcvs;
xcvs.push_back(ctr_cv(ctr_p(1, 0, 0), ctr_p(0, 1, 0)));
xcvs.push_back(ctr_cv(ctr_p(1, 0, 0), ctr_p(0, 0, 1)));
Expand All @@ -75,12 +72,9 @@ Geodesic_arcs::Geodesic_arcs()

auto approx = traits.approximate_2_object();

const double error = 0.001;

std::vector<QVector3D> vertex_data;

m_arc_offsets.clear();
m_arc_offsets.push_back(0);
Line_strip_approx lsa;
lsa.offsets.push_back(0);
for (const auto& xcv : xcvs)
{
std::vector<Approximate_point_2> v;
Expand All @@ -89,54 +83,12 @@ Geodesic_arcs::Geodesic_arcs()
for (const auto& p : v)
{
const QVector3D arc_point(p.dx(), p.dy(), p.dz());
vertex_data.push_back(arc_point);
lsa.points.push_back(arc_point);
}
const auto current_vertex_data_size = vertex_data.size();
m_arc_offsets.push_back(current_vertex_data_size);
const auto current_vertex_data_size = lsa.points.size();
lsa.offsets.push_back(current_vertex_data_size);
}
//std::cout << "offset count = " << m_arc_offsets.size() << std::endl;


// DEFINE OPENGL BUFFERS
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);


// Vertex Buffer
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
auto vertex_buffer_size = sizeof(QVector3D) * vertex_data.size();
auto vertex_buffer_data = reinterpret_cast<const void*>(vertex_data.data());
glBufferData(GL_ARRAY_BUFFER,
vertex_buffer_size,
vertex_buffer_data,
GL_STATIC_DRAW);

// Position Vertex-Attribute
GLint position_attrib_index = 0;
const void* position_offset = 0;
GLsizei stride = 0;
glVertexAttribPointer(position_attrib_index,
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
glEnableVertexAttribArray(position_attrib_index);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}

void Geodesic_arcs::draw()
{
glBindVertexArray(m_vao);
{
for (int i = 1; i < m_arc_offsets.size(); i++)
{
const auto first = m_arc_offsets[i - 1];
const auto count = m_arc_offsets[i] - first;
glDrawArrays(GL_LINE_STRIP, first, count);
}
}
glBindVertexArray(0);

return lsa;
}
13 changes: 4 additions & 9 deletions Arrangement_on_surface_2/demo/earth/Geodesic_arcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
#define GEODESIC_ARCS_H

#include "Common_defs.h"
#include <vector>

class Geodesic_arcs : protected OpenGLFunctionsBase

class Geodesic_arcs
{
public:
Geodesic_arcs();

void draw();

private:
GLuint m_vao, m_vbo, m_num_arc_points;
std::vector<GLuint> m_arc_offsets;

Line_strip_approx get_approximate_arcs(double error);
};


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

#include "Line_strips.h"


Line_strips::Line_strips(Line_strip_approx& lsa)
{
initializeOpenGLFunctions();

const auto& vertex_data = lsa.points;
m_offsets = lsa.offsets;

// DEFINE OPENGL BUFFERS
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);


// Vertex Buffer
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
auto vertex_buffer_size = sizeof(QVector3D) * vertex_data.size();
auto vertex_buffer_data = reinterpret_cast<const void*>(vertex_data.data());
glBufferData(GL_ARRAY_BUFFER,
vertex_buffer_size,
vertex_buffer_data,
GL_STATIC_DRAW);

// Position Vertex-Attribute
GLint position_attrib_index = 0;
const void* position_offset = 0;
GLsizei stride = 0;
glVertexAttribPointer(position_attrib_index,
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
glEnableVertexAttribArray(position_attrib_index);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}

void Line_strips::draw()
{
glBindVertexArray(m_vao);
{
for (int i = 1; i < m_offsets.size(); i++)
{
const auto first = m_offsets[i - 1];
const auto count = m_offsets[i] - first;
glDrawArrays(GL_LINE_STRIP, first, count);
}
}
glBindVertexArray(0);
}
22 changes: 22 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Line_strips.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

#ifndef LINE_STRIPS_H
#define LINE_STRIPS_H

#include <vector>
#include "Common_defs.h"


class Line_strips : protected OpenGLFunctionsBase
{
public:
Line_strips(Line_strip_approx& lsa);

void draw();

private:
GLuint m_vao, m_vbo;
std::vector<GLuint> m_offsets;
};


#endif
8 changes: 6 additions & 2 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <QMouseEvent>

#include "Geodesic_arcs.h"
#include "Tools.h"


Expand Down Expand Up @@ -109,7 +110,10 @@ void Main_widget::initializeGL()
{
// has to be defined after camera has been defined:
// because we want to compute the error based on camera parameters!
m_geodesic_arcs = std::make_unique<Geodesic_arcs>();
Geodesic_arcs ga;
const double error = 0.001; // calculate this from cam parameters!
auto lsa = ga.get_approximate_arcs(error);
m_geodesic_arcs = std::make_unique<Line_strips>(lsa);
}

glClearColor(0, 0, 0, 1);
Expand Down Expand Up @@ -195,7 +199,7 @@ void Main_widget::paintGL()

// GEODESIC ARCS
{
glDisable(GL_DEPTH_TEST);
//glDisable(GL_DEPTH_TEST);

auto& sp = m_sp_arc;
sp.use();
Expand Down
4 changes: 2 additions & 2 deletions Arrangement_on_surface_2/demo/earth/Main_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include "Camera.h"
#include "Common_defs.h"
#include "Geodesic_arcs.h"
#include "Line_strips.h"
#include "Shader_program.h"
#include "Sphere.h"
#include "World_coordinate_axes.h"
Expand Down Expand Up @@ -51,7 +51,7 @@ class Main_widget : public QOpenGLWidget, protected OpenGLFunctionsBase
// Objects in the scene
std::unique_ptr<Sphere> m_sphere;
std::unique_ptr<World_coord_axes> m_world_coord_axes;
std::unique_ptr<Geodesic_arcs> m_geodesic_arcs;
std::unique_ptr<Line_strips> m_geodesic_arcs;

// Shaders
Shader_program m_sp_smooth;
Expand Down

0 comments on commit f9c98fe

Please sign in to comment.