Commit 2d5d8f0f by Philipp Adolf

Merge branch '7-regulare-dreiecke-werden-weiter-unterteilt' into 'patchRender'

Unterteile reguläre Dreiecke nicht weiter Closes #7 See merge request !9
parents a487d284 8624300a
......@@ -89,15 +89,18 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
Tables tables = precomputeTables(input);
Result result = runShader(input, tables);
QVector<Triangle> triangles;
ibToTriangles(&result.vertex_buffer, tables.index_buffer, 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;
buildNeighborsMap(result.vertex_buffer, all_triangles, neighbors);
QVector<Triangle> regular;
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) << "subdivide: regular" << regular.length();
......@@ -158,7 +161,7 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) {
ibToTriangles(&vb, ib, triangles);
QVector<Triangle> triangles_regular;
ibToTriangles(&vb, ib_regular, triangles);
ibToTriangles(&vb, ib_regular, triangles_regular);
qCDebug(log_timing) << "building Triangles:" << formatTimeMeasurement(subTimer.elapsed());
......@@ -177,6 +180,7 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) {
subTimer.restart();
updateIndexBuffer(tables.index_buffer, modified_vertices);
updateIndexBuffer(tables.extra_triangles, modified_vertices);
qCDebug(log_timing) << "updateIndexBuffer:" << formatTimeMeasurement(subTimer.elapsed());
qCDebug(log_subdiv) << "Precompute Tables Done";
......@@ -271,18 +275,14 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
subTimer.restart();
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, output_handle);
GLint *out_ptr = (GLint *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_READ_ONLY);
bool valid = true;
for (int i = 0; i < 3 * triangles.length(); i++) {
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;
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]);
if (out_ptr[3 * other + 0] == i) {
ns.uv.edge.name = Triangle::Edge::Name::uv;
......@@ -300,8 +300,13 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
ns.uv.edge.b = ns.uv.triangle->u_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]);
if (out_ptr[3 * other + 0] == i) {
ns.vw.edge.name = Triangle::Edge::Name::uv;
......@@ -319,8 +324,13 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
ns.vw.edge.b = ns.vw.triangle->u_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]);
if (out_ptr[3 * other + 0] == i) {
ns.wu.edge.name = Triangle::Edge::Name::uv;
......@@ -338,9 +348,9 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
ns.wu.edge.b = ns.wu.triangle->u_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);
......@@ -379,6 +389,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.
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++){
//schaue alle dreiecke an
Triangle *triangle = &triangles[i];
......@@ -394,6 +414,17 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
*/
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
unsigned int uv, vw, wu;
......@@ -466,6 +497,99 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
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();
if (log_subdiv_trace().isDebugEnabled()) {
qCDebug(log_subdiv) << "Eedges found. Table: " << tables.edge_indices;
......
......@@ -34,6 +34,7 @@ private:
QVector<GLuint> vertex_offsets;
QVector<GLuint> index_buffer;
QVector<GLuint> patch_index_regular;
QVector<GLuint> extra_triangles;
};
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