Commit 4d9d60a3 by Kai Westerkamp

Merge remote-tracking branch 'origin/patchRender' into patchRender

parents 97c17290 2d5d8f0f
...@@ -89,15 +89,18 @@ void Subdivision::subdivide(Mesh *mesh, int level) { ...@@ -89,15 +89,18 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
Tables tables = precomputeTables(input); Tables tables = precomputeTables(input);
Result result = runShader(input, tables); Result result = runShader(input, tables);
QVector<Triangle> triangles;
ibToTriangles(&result.vertex_buffer, tables.index_buffer, triangles);
QVector<Triangle> all_triangles; QVector<Triangle> all_triangles;
ibToTriangles(&result.vertex_buffer, tables.index_buffer, all_triangles); ibToTriangles(&result.vertex_buffer, tables.extra_triangles, all_triangles);
all_triangles += triangles;
QMap<Triangle, Triangle::Neighbors> neighbors; QMap<Triangle, Triangle::Neighbors> neighbors;
buildNeighborsMap(result.vertex_buffer, all_triangles, neighbors); buildNeighborsMap(result.vertex_buffer, all_triangles, neighbors);
QVector<Triangle> regular; QVector<Triangle> regular;
QVector<Triangle> irregular; QVector<Triangle> irregular;
findRegular(all_triangles, neighbors, regular, irregular); findRegular(triangles, neighbors, regular, irregular);
qCDebug(log_subdiv) << "Indices" << tables.index_buffer.length(); qCDebug(log_subdiv) << "Indices" << tables.index_buffer.length();
qCDebug(log_subdiv) << "subdivide: regular" << regular.length(); qCDebug(log_subdiv) << "subdivide: regular" << regular.length();
...@@ -160,7 +163,7 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) { ...@@ -160,7 +163,7 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) {
ibToTriangles(&vb, ib, triangles); ibToTriangles(&vb, ib, triangles);
QVector<Triangle> triangles_regular; QVector<Triangle> triangles_regular;
ibToTriangles(&vb, ib_regular, triangles); ibToTriangles(&vb, ib_regular, triangles_regular);
qCDebug(log_timing) << "building Triangles:" << formatTimeMeasurement(subTimer.elapsed()); qCDebug(log_timing) << "building Triangles:" << formatTimeMeasurement(subTimer.elapsed());
...@@ -179,6 +182,7 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) { ...@@ -179,6 +182,7 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) {
subTimer.restart(); subTimer.restart();
updateIndexBuffer(tables.index_buffer, modified_vertices); updateIndexBuffer(tables.index_buffer, modified_vertices);
updateIndexBuffer(tables.extra_triangles, modified_vertices);
qCDebug(log_timing) << "updateIndexBuffer:" << formatTimeMeasurement(subTimer.elapsed()); qCDebug(log_timing) << "updateIndexBuffer:" << formatTimeMeasurement(subTimer.elapsed());
qCDebug(log_subdiv) << "Precompute Tables Done"; qCDebug(log_subdiv) << "Precompute Tables Done";
...@@ -273,18 +277,14 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria ...@@ -273,18 +277,14 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
subTimer.restart(); subTimer.restart();
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_handle); f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_handle);
GLint *out_ptr = (GLint *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY); GLint *out_ptr = (GLint *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
bool valid = true; for (int i = 0; i < triangles.length(); i++) {
for (int i = 0; i < 3 * triangles.length(); i++) { Triangle::Neighbors ns;
if (out_ptr[i] < 0 || out_ptr[i] >= triangles.length()) {
qCWarning(log_subdiv) << "Invalid neighbor:" << i << out_ptr[i];
valid = false;
}
}
if (valid) {
for (int i = 0; i < triangles.length(); i++) {
Triangle::Neighbors ns;
GLint other = out_ptr[3 * i + 0]; GLint other = out_ptr[3 * i + 0];
assert(other < triangles.length());
if (other < 0) {
ns.uv.triangle = NULL;
} else {
ns.uv.triangle = &(triangles[other]); ns.uv.triangle = &(triangles[other]);
if (out_ptr[3 * other + 0] == i) { if (out_ptr[3 * other + 0] == i) {
ns.uv.edge.name = Triangle::Edge::Name::uv; ns.uv.edge.name = Triangle::Edge::Name::uv;
...@@ -302,8 +302,13 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria ...@@ -302,8 +302,13 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
ns.uv.edge.b = ns.uv.triangle->u_idx(); ns.uv.edge.b = ns.uv.triangle->u_idx();
ns.uv.edge.c = ns.uv.triangle->v_idx(); ns.uv.edge.c = ns.uv.triangle->v_idx();
} }
}
other = out_ptr[3 * i + 1]; other = out_ptr[3 * i + 1];
assert(other < triangles.length());
if (other < 0) {
ns.vw.triangle = NULL;
} else {
ns.vw.triangle = &(triangles[other]); ns.vw.triangle = &(triangles[other]);
if (out_ptr[3 * other + 0] == i) { if (out_ptr[3 * other + 0] == i) {
ns.vw.edge.name = Triangle::Edge::Name::uv; ns.vw.edge.name = Triangle::Edge::Name::uv;
...@@ -321,8 +326,13 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria ...@@ -321,8 +326,13 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
ns.vw.edge.b = ns.vw.triangle->u_idx(); ns.vw.edge.b = ns.vw.triangle->u_idx();
ns.vw.edge.c = ns.vw.triangle->v_idx(); ns.vw.edge.c = ns.vw.triangle->v_idx();
} }
}
other = out_ptr[3 * i + 2]; other = out_ptr[3 * i + 2];
assert(other < triangles.length());
if (other < 0) {
ns.wu.triangle = NULL;
} else {
ns.wu.triangle = &(triangles[other]); ns.wu.triangle = &(triangles[other]);
if (out_ptr[3 * other + 0] == i) { if (out_ptr[3 * other + 0] == i) {
ns.wu.edge.name = Triangle::Edge::Name::uv; ns.wu.edge.name = Triangle::Edge::Name::uv;
...@@ -340,9 +350,9 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria ...@@ -340,9 +350,9 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
ns.wu.edge.b = ns.wu.triangle->u_idx(); ns.wu.edge.b = ns.wu.triangle->u_idx();
ns.wu.edge.c = ns.wu.triangle->v_idx(); ns.wu.edge.c = ns.wu.triangle->v_idx();
} }
neighbors.insert(triangles[i], ns);
} }
neighbors.insert(triangles[i], ns);
} }
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
...@@ -381,6 +391,16 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian ...@@ -381,6 +391,16 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
// Maps edges to the index of the new vertex added on that edge. The keys are the indices of the two vertices defining the edge and must be in the order uv, vw or uw. // Maps edges to the index of the new vertex added on that edge. The keys are the indices of the two vertices defining the edge and must be in the order uv, vw or uw.
QMap<Edge, unsigned int> edge_indices; QMap<Edge, unsigned int> edge_indices;
QMap<Triangle, bool> irregular_triangles;
QVectorIterator<Triangle> it(triangles);
while (it.hasNext()) {
irregular_triangles.insert(it.next(), true);
}
// Regular neighbors of irregular triangles
// We have to generate edge vertices without putting them in the index buffer for rendering patches.
QMap<Triangle, bool> extra_triangles;
for (int i = 0; i < triangles.length(); i++){ for (int i = 0; i < triangles.length(); i++){
//schaue alle dreiecke an //schaue alle dreiecke an
Triangle *triangle = &triangles[i]; Triangle *triangle = &triangles[i];
...@@ -396,6 +416,17 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian ...@@ -396,6 +416,17 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
*/ */
Triangle::Neighbors ns = neighbors.value(*triangle); Triangle::Neighbors ns = neighbors.value(*triangle);
QVector<Triangle> neighboring_triangles;
neighboring_triangles += vertexNeighbors(*triangle, ns.uv, neighbors);
neighboring_triangles += vertexNeighbors(*triangle, ns.vw, neighbors);
neighboring_triangles += vertexNeighbors(*triangle, ns.wu, neighbors);
QVectorIterator<Triangle> extra(neighboring_triangles);
while (extra.hasNext()) {
Triangle extra_triangle = extra.next();
if (!irregular_triangles.contains(extra_triangle)) {
extra_triangles.insert(extra_triangle, true);
}
}
// indices of the three vertices added to the edges of this triangle // indices of the three vertices added to the edges of this triangle
unsigned int uv, vw, wu; unsigned int uv, vw, wu;
...@@ -468,6 +499,99 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian ...@@ -468,6 +499,99 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
tables.index_buffer.push_back(wu); tables.index_buffer.push_back(wu);
} }
qDebug() << "extra triangles" << extra_triangles.size();
QMapIterator<Triangle, bool> extra_it(extra_triangles);
while (extra_it.hasNext()) {
extra_it.next();
Triangle triangle = extra_it.key();
qDebug() << triangle;
int uv = -1;
int vw = -1;
int wu = -1;
Edge edge;
Triangle::Neighbors ns = neighbors.value(triangle);
if (ns.uv.triangle != NULL) {
edge = { triangle.u_idx(), triangle.v_idx() };
if (edge_indices.contains(edge)) {
uv = edge_indices.value(edge);
} else {
uv = offset++;
edge_indices.insert(edge, uv);
edge = { ns.uv.edge.a, ns.uv.edge.b };
edge_indices.insert(edge, uv);
tables.edge_indices.push_back(triangle.u_idx());
tables.edge_indices.push_back(triangle.v_idx());
tables.edge_indices.push_back(triangle.w_idx());
tables.edge_indices.push_back(ns.uv.edge.c);
}
}
if (ns.vw.triangle != NULL) {
edge = { triangle.v_idx(), triangle.w_idx() };
if (edge_indices.contains(edge)) {
vw = edge_indices.value(edge);
} else {
vw = offset++;
edge_indices.insert(edge, vw);
edge = { ns.vw.edge.a, ns.vw.edge.b };
edge_indices.insert(edge, vw);
tables.edge_indices.push_back(triangle.v_idx());
tables.edge_indices.push_back(triangle.w_idx());
tables.edge_indices.push_back(triangle.u_idx());
tables.edge_indices.push_back(ns.vw.edge.c);
}
}
if (ns.wu.triangle != NULL) {
edge = { triangle.w_idx(), triangle.u_idx() };
if (edge_indices.contains(edge)) {
wu = edge_indices.value(edge);
} else {
wu = offset++;
edge_indices.insert(edge, wu);
edge = { ns.wu.edge.a, ns.wu.edge.b };
edge_indices.insert(edge, wu);
tables.edge_indices.push_back(triangle.w_idx());
tables.edge_indices.push_back(triangle.u_idx());
tables.edge_indices.push_back(triangle.v_idx());
tables.edge_indices.push_back(ns.wu.edge.c);
}
}
if (uv >= 0 && wu >= 0) {
tables.extra_triangles.push_back(triangle.u_idx());
tables.extra_triangles.push_back(uv);
tables.extra_triangles.push_back(wu);
}
if (vw >= 0 && uv >= 0) {
tables.extra_triangles.push_back(triangle.v_idx());
tables.extra_triangles.push_back(vw);
tables.extra_triangles.push_back(uv);
}
if (wu >= 0 && vw >= 0) {
tables.extra_triangles.push_back(triangle.w_idx());
tables.extra_triangles.push_back(wu);
tables.extra_triangles.push_back(vw);
}
if (uv >= 0 && vw >= 0 && wu >= 0) {
tables.extra_triangles.push_back(uv);
tables.extra_triangles.push_back(vw);
tables.extra_triangles.push_back(wu);
}
}
qCDebug(log_subdiv) << "Done with edge table. " << tables.edge_indices.length(); qCDebug(log_subdiv) << "Done with edge table. " << tables.edge_indices.length();
if (log_subdiv_trace().isDebugEnabled()) { if (log_subdiv_trace().isDebugEnabled()) {
qCDebug(log_subdiv) << "Eedges found. Table: " << tables.edge_indices; qCDebug(log_subdiv) << "Eedges found. Table: " << tables.edge_indices;
...@@ -724,9 +848,11 @@ QVector<unsigned int> Subdivision::patchIBToTriangleIB(QVector<unsigned int> ib) ...@@ -724,9 +848,11 @@ QVector<unsigned int> Subdivision::patchIBToTriangleIB(QVector<unsigned int> ib)
bool isVertexRegular(Triangle &triangle, Triangle::Neighbor current_neighbor, QMap<Triangle, Triangle::Neighbors> neighbors) { bool isVertexRegular(Triangle &triangle, Triangle::Neighbor current_neighbor, QMap<Triangle, Triangle::Neighbors> neighbors) {
unsigned int count = 1; unsigned int count = 1;
assert(current_neighbor.triangle != NULL);
while (count < 6 && *current_neighbor.triangle != triangle) { while (count < 6 && *current_neighbor.triangle != triangle) {
count++; count++;
current_neighbor = neighbors.value(*current_neighbor.triangle).get_neighbor(rotate_edge_name(current_neighbor.edge.name)); current_neighbor = neighbors.value(*current_neighbor.triangle).get_neighbor(rotate_edge_name(current_neighbor.edge.name));
assert(current_neighbor.triangle != NULL);
} }
return count == 6 && *current_neighbor.triangle == triangle; return count == 6 && *current_neighbor.triangle == triangle;
} }
......
...@@ -34,6 +34,7 @@ private: ...@@ -34,6 +34,7 @@ private:
QVector<GLuint> vertex_offsets; QVector<GLuint> vertex_offsets;
QVector<GLuint> index_buffer; QVector<GLuint> index_buffer;
QVector<GLuint> patch_index_regular; QVector<GLuint> patch_index_regular;
QVector<GLuint> extra_triangles;
}; };
struct Result struct Result
......
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