Skip to content

Commit

Permalink
initiali triangle visualization (triangles outside of the polygon are…
Browse files Browse the repository at this point in the history
… rendered as well)
  • Loading branch information
denizdiktas committed Aug 10, 2023
1 parent f80b980 commit dbd9511
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 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 @@ -76,6 +76,7 @@ source_group( "Graphics" FILES ${source_files_graphics} )
file(GLOB source_files_graphics_geometry
Line_strips.h Line_strips.cpp
Sphere.h Sphere.cpp
Triangles.h Triangles.cpp
Vertices.h Vertices.cpp
World_coordinate_axes.h World_coordinate_axes.cpp
)
Expand Down
19 changes: 19 additions & 0 deletions Arrangement_on_surface_2/demo/earth/Main_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ void Main_widget::init_problematic_nodes()

std::unique_ptr<Line_strips> new_faces;

#include "Triangles.h"
std::unique_ptr<Triangles> g_face_triangles;


void Main_widget::initializeGL()
{
// verify that the node (180.0, -84.71338) in Antarctica is redundant
Expand Down Expand Up @@ -198,6 +202,7 @@ void Main_widget::initializeGL()
auto arrh = Aos::construct(m_countries);
auto triangle_points = Aos::get_triangles(arrh);
qDebug() << "num triangles = " << triangle_points.size() / 3;
g_face_triangles = std::make_unique<Triangles>(triangle_points);
}


Expand Down Expand Up @@ -474,8 +479,22 @@ void Main_widget::paintGL()
sp.set_uniform("u_mvp", mvp);
sp.set_uniform("u_color", m_sphere->get_color());

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
m_sphere->draw();

// DRAW SOLID FACES
{
glDisable(GL_DEPTH_TEST);
QVector4D face_color(1, .5, 0, 1);
sp.set_uniform("u_color", face_color);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
g_face_triangles->draw();

sp.set_uniform("u_color", QVector4D(0,0,0,1));
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
g_face_triangles->draw();
}

sp.unuse();
}

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

#include "Triangles.h"

#include <iostream>


Triangles::Triangles(std::vector<QVector3D>& vertices)
{
initializeOpenGLFunctions();

// computer the normals of each vertex
std::vector<QVector3D> normals;
for(const auto& p : vertices)
{
auto n = p;
n.normalize();
normals.push_back(n);
}

int num_triangles = vertices.size() / 3;

// strided vertex-data
std::vector<QVector3D> vertex_data;
for (int i = 0; i < vertices.size(); ++i)
{
vertex_data.push_back(vertices[i]);
vertex_data.push_back(normals[i]);
}


// DEFINE OPENGL BUFFERS
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
m_num_vertices = vertices.size();

// 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 = 6 * sizeof(float);
glVertexAttribPointer(position_attrib_index,
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
glEnableVertexAttribArray(position_attrib_index);

// Normal Vertex-Attribute
GLint normal_attrib_index = 1;
auto* normal_offset = reinterpret_cast<const void*>(3 * sizeof(float));
glVertexAttribPointer(normal_attrib_index,
3,
GL_FLOAT,
GL_FALSE,
stride,
normal_offset);
glEnableVertexAttribArray(normal_attrib_index);

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

void Triangles::draw()
{
// DRAW TRIANGLES
glBindVertexArray(m_vao);
glDrawArrays(GL_TRIANGLES, 0, m_num_vertices);
glBindVertexArray(0);
}

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

#ifndef TRIANGLES_H
#define TRIANGLES_H

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


class Triangles : protected OpenGLFunctionsBase
{
public:
// IMPORTANT: we assume that the triangles are on the sphere!
// this means that all vertex-normals are actually the normal vector on the
// sphere at the point of projection of the current vertex on the sphere.
Triangles(std::vector<QVector3D>& vertices);

int get_num_triangles() const;

void draw();

private:
GLuint m_vao, m_vbo;
int m_num_vertices;
};


#endif

0 comments on commit dbd9511

Please sign in to comment.