-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initiali triangle visualization (triangles outside of the polygon are…
… rendered as well)
- Loading branch information
1 parent
f80b980
commit dbd9511
Showing
4 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |