Commit 1a3b3656 by Philipp Adolf

Merge branch '1-irregulare-dreiecke-und-patches-sind-nicht-verbunden' into 'patchRender'

Resolve "Irreguläre Dreiecke und Patches sind nicht verbunden" Closes #1 See merge request !10
parents 2d5d8f0f 8c518806
...@@ -8,7 +8,7 @@ QT += core gui opengl ...@@ -8,7 +8,7 @@ QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11 CONFIG += c++11 debug
TARGET = Subdivision TARGET = Subdivision
TEMPLATE = app TEMPLATE = app
......
...@@ -6,7 +6,7 @@ int main(int argc, char *argv[]) ...@@ -6,7 +6,7 @@ int main(int argc, char *argv[])
{ {
QApplication a(argc, argv); QApplication a(argc, argv);
QLoggingCategory::setFilterRules("timing.debug=true\n" QLoggingCategory::setFilterRules("timing.debug=true\n"
"mesh.debug=false\n" "mesh.debug=true\n"
"mesh.render.debug=false\n" "mesh.render.debug=false\n"
"subdiv.debug=true\n" "subdiv.debug=true\n"
"subdiv.trace.debug=false\n"); "subdiv.trace.debug=false\n");
......
...@@ -145,7 +145,8 @@ void MainWidget::initializeGL(){ ...@@ -145,7 +145,8 @@ void MainWidget::initializeGL(){
subdivision->init(); subdivision->init();
//loadNewMesh( "../Models/demon_head.3ds"); //loadNewMesh( "../Models/demon_head.3ds");
loadNewMesh( "../Models/torus.obj"); // loadNewMesh( "../Models/torus.obj");
loadNewMesh( "../Models/box.obj");
} }
void MainWidget::loadNewMesh(){ void MainWidget::loadNewMesh(){
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
Mesh::SubdivEntry::SubdivEntry() Mesh::SubdivEntry::SubdivEntry()
{ {
VB_handle = 0xFFFFFFFF; VB_handle = 0xFFFFFFFF;
VB_handle_irregular = 0xFFFFFFFF;
IB_irregular_handle = 0xFFFFFFFF; IB_irregular_handle = 0xFFFFFFFF;
IB_regular_handle = 0xFFFFFFFF; IB_regular_handle = 0xFFFFFFFF;
} }
...@@ -39,10 +40,10 @@ void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f,QVector<Vertex>& Verti ...@@ -39,10 +40,10 @@ void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f,QVector<Vertex>& Verti
f->glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW); f->glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * Vertices.size(), &Vertices[0], GL_STATIC_DRAW);
QVector<unsigned int> patches; QVector<unsigned int> patches;
this->init(f, VB_handle, Vertices, Indices_irregular, patches); this->init(f, VB_handle, Vertices, Vertices, Indices_irregular, patches);
} }
void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f, GLuint VB_handle, QVector<Vertex>& Vertices, void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f, GLuint VB_handle, QVector<Vertex>& Vertices, QVector<Vertex>& Vertices_irregular,
QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches){ QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches){
this->f = f; this->f = f;
this->VB_handle = VB_handle; this->VB_handle = VB_handle;
...@@ -52,9 +53,15 @@ void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f, GLuint VB_handle, QVe ...@@ -52,9 +53,15 @@ void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f, GLuint VB_handle, QVe
indices_regular = patches; indices_regular = patches;
qCDebug(log_mesh) << "Vertices" << vertices.length(); qCDebug(log_mesh) << "Vertices" << vertices.length();
qCDebug(log_mesh) << "Vertices Irregular" << Vertices_irregular.length();
qCDebug(log_mesh) << "Indices irregular" << indices_irregular.length(); qCDebug(log_mesh) << "Indices irregular" << indices_irregular.length();
qCDebug(log_mesh) << "Indices patches" << patches.length(); qCDebug(log_mesh) << "Indices patches" << patches.length();
f->glGenBuffers(1, &VB_handle_irregular);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VB_handle_irregular);
if(Vertices_irregular.size() != 0)
f->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Vertex) * Vertices_irregular.size(), &Vertices_irregular[0], GL_STATIC_DRAW);
f->glGenBuffers(1, &IB_irregular_handle); f->glGenBuffers(1, &IB_irregular_handle);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB_irregular_handle); f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB_irregular_handle);
if(indices_irregular.size() != 0) if(indices_irregular.size() != 0)
...@@ -96,10 +103,10 @@ void Mesh::MeshEntry::init(QOpenGLFunctions_4_3_Core *f,QVector<Vertex>& Vertice ...@@ -96,10 +103,10 @@ void Mesh::MeshEntry::init(QOpenGLFunctions_4_3_Core *f,QVector<Vertex>& Vertice
} }
} }
void Mesh::MeshEntry::update(GLuint VB_handle, QVector<Vertex>& Vertices, QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches) { void Mesh::MeshEntry::update(GLuint VB_handle, QVector<Vertex>& Vertices,QVector<Vertex>& Vertices_Irregular, QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches) {
SubdivEntry *entry = new SubdivEntry; SubdivEntry *entry = new SubdivEntry;
buffers.push_back(std::shared_ptr<SubdivEntry>(entry)); buffers.push_back(std::shared_ptr<SubdivEntry>(entry));
entry->init(f, VB_handle, Vertices, Indices_irregular, patches); entry->init(f, VB_handle, Vertices, Vertices_Irregular,Indices_irregular, patches);
} }
Mesh::MeshEntry::~MeshEntry() Mesh::MeshEntry::~MeshEntry()
...@@ -481,7 +488,7 @@ void Mesh::renderMesh(QOpenGLShaderProgram *shader, int index, int subdivision, ...@@ -481,7 +488,7 @@ void Mesh::renderMesh(QOpenGLShaderProgram *shader, int index, int subdivision,
} }
} else { } else {
if (!entry->indices_irregular.isEmpty()) { if (!entry->indices_irregular.isEmpty()) {
f->glBindBuffer(GL_ARRAY_BUFFER, entry->VB_handle); f->glBindBuffer(GL_ARRAY_BUFFER, entry->VB_handle_irregular);
f->glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); f->glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, entry->IB_irregular_handle); f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, entry->IB_irregular_handle);
......
...@@ -32,11 +32,13 @@ public: ...@@ -32,11 +32,13 @@ public:
SubdivEntry(); SubdivEntry();
~SubdivEntry(); ~SubdivEntry();
GLuint VB_handle; GLuint VB_handle;
GLuint VB_handle_irregular;
GLuint IB_irregular_handle; GLuint IB_irregular_handle;
GLuint IB_regular_handle; GLuint IB_regular_handle;
QVector<Vertex> vertices; QVector<Vertex> vertices;
QVector<Vertex> vertices_irregular;
QVector<unsigned int> indices_irregular; QVector<unsigned int> indices_irregular;
QVector<unsigned int> indices_regular; QVector<unsigned int> indices_regular;
...@@ -44,7 +46,7 @@ public: ...@@ -44,7 +46,7 @@ public:
void init(QOpenGLFunctions_4_3_Core *f, QVector<Vertex>& Vertices, void init(QOpenGLFunctions_4_3_Core *f, QVector<Vertex>& Vertices,
QVector<unsigned int>& Indices_irregular); QVector<unsigned int>& Indices_irregular);
void init(QOpenGLFunctions_4_3_Core *f, GLuint VB_handle, QVector<Vertex>& Vertices, void init(QOpenGLFunctions_4_3_Core *f, GLuint VB_handle, QVector<Vertex>& Vertices, QVector<Vertex>& Vertices_irregular,
QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches); QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches);
void updateIndices(); void updateIndices();
}; };
...@@ -54,7 +56,7 @@ public: ...@@ -54,7 +56,7 @@ public:
~MeshEntry(); ~MeshEntry();
void init(QOpenGLFunctions_4_3_Core *f, QVector<Vertex>& Vertices, QVector<unsigned int>& Indices); void init(QOpenGLFunctions_4_3_Core *f, QVector<Vertex>& Vertices, QVector<unsigned int>& Indices);
void update(GLuint VB_handle, QVector<Vertex>& Vertices, QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches); void update(GLuint VB_handle, QVector<Vertex>& Vertices, QVector<Vertex>& Vertices_Irregular, QVector<unsigned int>& Indices_irregular, QVector<unsigned int>& patches);
QString name; QString name;
......
...@@ -109,11 +109,13 @@ void Subdivision::subdivide(Mesh *mesh, int level) { ...@@ -109,11 +109,13 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
QVector<unsigned int> irregular_ib; QVector<unsigned int> irregular_ib;
trianglesToIB(irregular, irregular_ib); trianglesToIB(irregular, irregular_ib);
updateIrregularForDraw(irregular, neighbors, result);
QVector<unsigned int> patches; QVector<unsigned int> patches;
getPatchIndexBuffer(regular, neighbors, patches); getPatchIndexBuffer(regular, neighbors, patches);
qCDebug(log_subdiv) << "patches" << patches.length(); qCDebug(log_subdiv) << "patches" << patches.length();
current_mesh->update(result.vb_handle, result.vertex_buffer, irregular_ib, patches); current_mesh->update(result.vb_handle, result.vertex_buffer, result.vertex_buffer_irregular, irregular_ib, patches);
} }
} }
...@@ -1037,3 +1039,68 @@ void Subdivision::runEdgeShader(GLuint size, GLuint vb_handle, GLuint edge_indic ...@@ -1037,3 +1039,68 @@ void Subdivision::runEdgeShader(GLuint size, GLuint vb_handle, GLuint edge_indic
edgeShader->release(); edgeShader->release();
} }
QVector3D Subdivision::updateIrregularVertexForDraw(Vertex currentCorner, QVector<Triangle> neighboring_triangles) {
QVector<Vertex> surroundingVertex;
QVectorIterator<Triangle> extra(neighboring_triangles);
while (extra.hasNext()) {
Triangle extra_triangle = extra.next();
if (!currentCorner.samePos(extra_triangle.u()) && !containsVertex(surroundingVertex, extra_triangle.u()))
surroundingVertex.push_back(extra_triangle.u());
if (!currentCorner.samePos(extra_triangle.v()) && !containsVertex(surroundingVertex, extra_triangle.v()))
surroundingVertex.push_back(extra_triangle.v());
if (!currentCorner.samePos(extra_triangle.w()) && !containsVertex(surroundingVertex, extra_triangle.w()))
surroundingVertex.push_back(extra_triangle.w());
}
QVector3D newPos(0.0, 0.0, 0.0);
double n = surroundingVertex.length();
double a = 5.0/8.0 - pow((3.0 / 8.0 + 2.0/8.0 * cos(2.0 * M_PI / n)), 2.0);
double omega = 3.0 * n / (8.0 * a);
for (int i = 0; i < surroundingVertex.length(); i++) {
newPos += surroundingVertex[i].pos;
}
newPos *= 1.0 / (omega + n);
newPos += omega / (omega + n) * currentCorner.pos;
return newPos;
}
void Subdivision::updateIrregularForDraw(const QVector<Triangle> &triangles,QMap<Triangle, Triangle::Neighbors> &neighbors, Result &result){
result.vertex_buffer_irregular.resize(result.vertex_buffer.size());
QVectorIterator<Triangle> it(triangles);
while (it.hasNext()) {
Triangle triangle = it.next();
Triangle::Neighbors ns = neighbors.value(triangle);
// u
QVector3D new_u_pos = updateIrregularVertexForDraw(triangle.u(), vertexNeighbors(triangle, ns.uv, neighbors));
//v
QVector3D new_v_pos = updateIrregularVertexForDraw(triangle.v(), vertexNeighbors(triangle, ns.vw, neighbors));
//w
QVector3D new_w_pos = updateIrregularVertexForDraw(triangle.w(), vertexNeighbors(triangle, ns.wu, neighbors));
result.vertex_buffer_irregular[triangle.u_idx()] = result.vertex_buffer.at(triangle.u_idx());
result.vertex_buffer_irregular[triangle.u_idx()].pos = new_u_pos;
result.vertex_buffer_irregular[triangle.v_idx()] = result.vertex_buffer.at(triangle.v_idx());
result.vertex_buffer_irregular[triangle.v_idx()].pos = new_v_pos;
result.vertex_buffer_irregular[triangle.w_idx()] = result.vertex_buffer.at(triangle.w_idx());
result.vertex_buffer_irregular[triangle.w_idx()].pos = new_w_pos;
}
}
bool Subdivision::containsVertex(QVector<Vertex> &vertices, Vertex vertex) {
for (int i = 0; i < vertices.length(); i++) {
if (vertex.samePos(vertices[i])) {
return true;
}
}
return false;
}
...@@ -41,6 +41,7 @@ private: ...@@ -41,6 +41,7 @@ private:
{ {
GLuint vb_handle; GLuint vb_handle;
QVector<Vertex> vertex_buffer; QVector<Vertex> vertex_buffer;
QVector<Vertex> vertex_buffer_irregular;
}; };
struct Patch struct Patch
...@@ -69,6 +70,11 @@ private: ...@@ -69,6 +70,11 @@ private:
void runCopyShader(GLuint size, GLuint vb_in, GLuint vb_out); void runCopyShader(GLuint size, GLuint vb_in, GLuint vb_out);
void runVertexShader(GLuint size, GLuint vb_handle, GLuint vertex_indices_handle, GLuint vertex_offsets_handle, GLuint output_handle, GLuint offset_handle); void runVertexShader(GLuint size, GLuint vb_handle, GLuint vertex_indices_handle, GLuint vertex_offsets_handle, GLuint output_handle, GLuint offset_handle);
void runEdgeShader(GLuint size, GLuint vb_handle, GLuint edge_indices_handle, GLuint output_handle, GLuint offset_handle); void runEdgeShader(GLuint size, GLuint vb_handle, GLuint edge_indices_handle, GLuint output_handle, GLuint offset_handle);
QVector3D updateIrregularVertexForDraw(Vertex currentCorner, QVector<Triangle> neighboring_triangles);
void updateIrregularForDraw(const QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors, Result &result);
bool containsVertex(QVector<Vertex> &vertices, Vertex vertex);
QVector<unsigned int> patchIBToTriangleIB(QVector<unsigned int> ib); QVector<unsigned int> patchIBToTriangleIB(QVector<unsigned int> ib);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment