Commit 9349f665 by Philipp Adolf

Merge branch '14-benutze-neighbors-map-in-precomputevertextable' into 'patchRender'

Resolve "Benutze neighbors map in precomputeVertexTable" Closes #14 See merge request !11
parents 25fccdc3 6883649b
......@@ -40,22 +40,24 @@ 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);
QVector<unsigned int> patches;
this->init(f, VB_handle, Vertices, Vertices, Indices_irregular, patches);
this->init(f, VB_handle, Vertices, Vertices, Indices_irregular, patches,patches);
}
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,QVector<unsigned int>& Indices_extra){
this->f = f;
this->VB_handle = VB_handle;
vertices = Vertices;
indices_irregular = Indices_irregular;
indices_regular = patches;
indices_extra = Indices_extra;
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 patches" << patches.length();
qCDebug(log_mesh) << "Indices extra" << Indices_extra.length();
f->glGenBuffers(1, &VB_handle_irregular);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VB_handle_irregular);
......@@ -71,6 +73,11 @@ void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f, GLuint VB_handle, QVe
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB_regular_handle);
if(patches.size() != 0)
f->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * patches.size(), &patches[0], GL_STATIC_DRAW);
f->glGenBuffers(1, &IB_extra_handle);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB_extra_handle);
if(Indices_extra.size() != 0)
f->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * Indices_extra.size(), &Indices_extra[0], GL_STATIC_DRAW);
}
......@@ -103,10 +110,10 @@ void Mesh::MeshEntry::init(QOpenGLFunctions_4_3_Core *f,QVector<Vertex>& Vertice
}
}
void Mesh::MeshEntry::update(GLuint VB_handle, QVector<Vertex>& Vertices,QVector<Vertex>& Vertices_Irregular, 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,QVector<unsigned int>& Indices_extra) {
SubdivEntry *entry = new SubdivEntry;
buffers.push_back(std::shared_ptr<SubdivEntry>(entry));
entry->init(f, VB_handle, Vertices, Vertices_Irregular,Indices_irregular, patches);
entry->init(f, VB_handle, Vertices, Vertices_Irregular,Indices_irregular,patches, Indices_extra);
}
Mesh::MeshEntry::~MeshEntry()
......@@ -495,6 +502,16 @@ void Mesh::renderMesh(QOpenGLShaderProgram *shader, int index, int subdivision,
f->glPatchParameteri(GL_PATCH_VERTICES, 3);
f->glDrawElements(GL_PATCHES, entry->indices_irregular.size(), GL_UNSIGNED_INT, 0);
}
if (!entry->indices_extra.isEmpty() && false) {
shader->setUniformValue("materialInfo.Diffuse", QVector3D(0.0, 156.0, 18.0)/255.0);
f->glBindBuffer(GL_ARRAY_BUFFER, entry->VB_handle);
f->glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, entry->IB_extra_handle);
f->glPatchParameteri(GL_PATCH_VERTICES, 3);
f->glDrawElements(GL_PATCHES, entry->indices_extra.size(), GL_UNSIGNED_INT, 0);
}
}
}
......
......@@ -34,20 +34,23 @@ public:
GLuint VB_handle;
GLuint VB_handle_irregular;
GLuint IB_irregular_handle;
GLuint IB_regular_handle;
GLuint IB_extra_handle;
QVector<Vertex> vertices;
QVector<Vertex> vertices_irregular;
QVector<unsigned int> indices_irregular;
QVector<unsigned int> indices_regular;
QVector<unsigned int> indices_extra;
QOpenGLFunctions_4_3_Core *f;
void init(QOpenGLFunctions_4_3_Core *f, QVector<Vertex>& Vertices,
QVector<unsigned int>& Indices_irregular);
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,QVector<unsigned int>& Indices_extra);
void updateIndices();
};
......@@ -56,7 +59,7 @@ public:
~MeshEntry();
void init(QOpenGLFunctions_4_3_Core *f, QVector<Vertex>& Vertices, QVector<unsigned int>& Indices);
void update(GLuint VB_handle, QVector<Vertex>& Vertices, QVector<Vertex>& Vertices_Irregular, 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,QVector<unsigned int>& Indices_extra);
QString name;
......
......@@ -17,9 +17,9 @@ Subdivision::~Subdivision()
}
QString Subdivision::formatTimeMeasurement(int time){
return QString("%1m %2s %3ms").arg( time / 60000 , 2, 10, QChar('0'))
.arg((time % 60000) / 1000, 2, 10, QChar('0'))
.arg((time % 1000) , 3, 10, QChar('0'));
return QString("%1m %2s %3ms").arg( time / 60000 , 2, 10, QChar('0'))
.arg((time % 60000) / 1000, 2, 10, QChar('0'))
.arg((time % 1000) , 3, 10, QChar('0'));
}
void Subdivision::init() {
......@@ -72,9 +72,9 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
Input input;
int currentMax = current_mesh->buffers.size()-1;
//qCDebug(log_subdiv) << "Subdiv Level" << level << "Requested. Current Max:" << currentMax;
if(level <= currentMax)
return;
qCDebug(log_subdiv) << "========== Caluculating Subdiv Level" << level << "==========";
std::shared_ptr<Mesh::SubdivEntry> entry = current_mesh->buffers[currentMax];
......@@ -119,7 +119,7 @@ void Subdivision::subdivide(Mesh *mesh, int level) {
getPatchIndexBuffer(regular, neighbors, patches);
qCDebug(log_subdiv) << "patches" << patches.length();
current_mesh->update(result.vb_handle, result.vertex_buffer, result.vertex_buffer_irregular, irregular_ib, patches);
current_mesh->update(result.vb_handle, result.vertex_buffer, result.vertex_buffer_irregular, irregular_ib, patches,tables.extra_triangles);
qCDebug(log_timing) << "subdivide done:" << formatTimeMeasurement(timer.elapsed());
}
......@@ -177,13 +177,15 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) {
QVector<Triangle> all_triangles = triangles + triangles_regular;
buildNeighborsMap(vb, all_triangles, neighbors);
QVector<Triangle> extra_old_triangles;
subTimer.restart();
precomputeEdgeTable(tables, triangles, neighbors, (unsigned int) vb.length());
precomputeEdgeTable(tables, triangles, neighbors, (unsigned int) vb.length(), extra_old_triangles);
qCDebug(log_timing) << "precomputeEdgeTable:" << formatTimeMeasurement(subTimer.elapsed());
QVector<Triangle> irregular_and_extra_triangles = triangles+extra_old_triangles;
QMap<unsigned int, unsigned int> modified_vertices;
subTimer.restart();
precomputeVertexTable(tables, input, modified_vertices);
precomputeVertexTable(tables, vb, irregular_and_extra_triangles, neighbors, modified_vertices);
qCDebug(log_timing) << "precomputeVertexTable:" << formatTimeMeasurement(subTimer.elapsed());
subTimer.restart();
......@@ -380,15 +382,22 @@ void Subdivision::buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &tria
QVector<Triangle> Subdivision::vertexNeighbors(Triangle &triangle, Triangle::Neighbor current_neighbor, QMap<Triangle, Triangle::Neighbors> neighbors) {
QVector<Triangle> ns;
do {
assert(current_neighbor.triangle != NULL);
if(current_neighbor.triangle == NULL){
ns.clear();
return ns;
}
ns.push_back(*current_neighbor.triangle);
current_neighbor = neighbors.value(*current_neighbor.triangle).get_neighbor(rotate_edge_name(current_neighbor.edge.name));
if(current_neighbor.triangle == NULL){
ns.clear();
return ns;
}
} while (*current_neighbor.triangle != triangle);
assert(current_neighbor.triangle != NULL);
return ns;
}
void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors, unsigned int offset) {
void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors, unsigned int offset, QVector<Triangle> extra_old_triangles) {
//compute edge table
//Format: first two entries: edge vertices. last two entries: distant vertices.
......@@ -507,6 +516,7 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
while (extra_it.hasNext()) {
extra_it.next();
Triangle triangle = extra_it.key();
extra_old_triangles.push_back(triangle);
int uv = -1;
int vw = -1;
int wu = -1;
......@@ -603,81 +613,101 @@ void Subdivision::precomputeEdgeTable(Subdivision::Tables &tables, QVector<Trian
}
}
void Subdivision::precomputeVertexTable(Subdivision::Tables &tables, Input &input, QMap<unsigned int, unsigned int> &modified_vertices) {
//compute vertex table
//Format: First entry: original vertex. Other entries: adjacent vertices.
QVector<QVector<unsigned int> > duplicates;//for debugging
QVector<unsigned int> ib_combined = input.index_irregular_buffer + input.index_regular_buffer;
unsigned int offset = 0;
unsigned int output_offset = input.vertex_buffer.size() + tables.edge_indices.size() / 4;
for (int i = 0; i < input.vertex_buffer.length(); i++){
//hat evtl viel redundanz
QVector<Vertex> adj_v;//helfer
tables.vertex_offsets.push_back(offset);
tables.vertex_indices.push_back(i);
modified_vertices.insert(i, output_offset + i);
offset++;
Vertex originalVertex = input.vertex_buffer[i];
QVector<unsigned int> d;
for (int j = 0; j < ib_combined.length(); j++){
//suche verweise auf vertex im indexbuffer
if (input.vertex_buffer[ib_combined[j]].samePos(originalVertex)){
d.push_back(j);
unsigned int i1, i2; //indices for neighbour vertices
switch (j % 3) {
case 0:
i1 = ib_combined[j+1];
i2 = ib_combined[j+2];
break;
case 1:
i1 = ib_combined[j-1];
i2 = ib_combined[j+1];
break;
case 2:
i1 = ib_combined[j-2];
i2 = ib_combined[j-1];
break;
}
Vertex v1,v2; //neighbour vertices;
v1 = input.vertex_buffer[i1];
v2 = input.vertex_buffer[i2];
//check if vertices where already used to influence this vertex
//Note: Can't use contain. Contain uses equal (would have to implement equal, but we're only interested in the position here.
bool found1 = false;
bool found2 = false;
for (int k = 0; k < adj_v.length(); k++){
if (adj_v[k].samePos(v1)) found1 = true;
if (adj_v[k].samePos(v2)) found2 = true;
}
if (!found1){
adj_v.push_back(v1);
tables.vertex_indices.push_back(i1);
offset++;
}
if (!found2){
adj_v.push_back(v2);
tables.vertex_indices.push_back(i2);
offset++;
}
//if one adjacent vertex is referenced by multiple different indices, only its first index will appear in the adjacent_indices list.1
void Subdivision::precomputeVertexTable(Tables &tables, QVector<Vertex> &vb, QVector<Triangle> &irregular_triangles, QMap<Triangle, Triangle::Neighbors> neighbors, QMap<unsigned int, unsigned int> &modified_vertices) {
QTime timer;
timer.start();
qCDebug(log_timing) << "precomputeVertexTable started";
unsigned int output_offset = vb.size() + tables.edge_indices.size() / 4;
tables.vertex_offsets.push_back(0);
for (int i = 0; i < irregular_triangles.length(); i++) {
Triangle triangle = irregular_triangles[i];
Triangle::Neighbors ns = neighbors.value(triangle);
unsigned int idx;
QVector<unsigned int> neighbor_indices;
bool removed;
idx = triangle.u_idx();
if (!modified_vertices.contains(idx)) {
QVector<Triangle> neighborTriangles = vertexNeighbors(triangle, ns.uv, neighbors);
if(!neighborTriangles.empty()){
tables.vertex_indices.push_back(idx);
modified_vertices.insert(idx, output_offset++);
neighbor_indices = getAllVertexIndices(vb, neighborTriangles);
removed = removeVertexIndex(vb, neighbor_indices, triangle.u());
assert(removed);
tables.vertex_indices += neighbor_indices;
tables.vertex_offsets.push_back(tables.vertex_indices.length());
}
}
idx = triangle.v_idx();
if (!modified_vertices.contains(idx)) {
QVector<Triangle> neighborTriangles = vertexNeighbors(triangle, ns.vw, neighbors);
if(!neighborTriangles.empty()){
tables.vertex_indices.push_back(idx);
modified_vertices.insert(idx, output_offset++);
neighbor_indices = getAllVertexIndices(vb, neighborTriangles);
removed = removeVertexIndex(vb, neighbor_indices, triangle.v());
assert(removed);
tables.vertex_indices += neighbor_indices;
tables.vertex_offsets.push_back(tables.vertex_indices.length());
}
}
idx = triangle.w_idx();
if (!modified_vertices.contains(idx)) {
QVector<Triangle> neighborTriangles = vertexNeighbors(triangle, ns.wu, neighbors);
if(!neighborTriangles.empty()){
tables.vertex_indices.push_back(idx);
modified_vertices.insert(idx, output_offset++);
neighbor_indices = getAllVertexIndices(vb, neighborTriangles);
removed = removeVertexIndex(vb, neighbor_indices, triangle.w());
assert(removed);
tables.vertex_indices += neighbor_indices;
tables.vertex_offsets.push_back(tables.vertex_indices.length());
}
}
if (!duplicates.contains(d)) duplicates.push_back(d);
}
qCDebug(log_timing) << "precomputeVertexTable done:" << formatTimeMeasurement(timer.elapsed());
}
tables.vertex_offsets.push_back(offset);
bool Subdivision::containsVertexIndex(const QVector<Vertex> &vb, const QVector<unsigned int> &vertices, const Vertex vertex) const {
for (int i = 0; i < vertices.length(); i++) {
if (vertex.samePos(vb[vertices[i]])) {
return true;
}
}
return false;
}
qCDebug(log_subdiv) << "Done with vertex index table. ";
qCDebug(log_subdiv) << "Duplicates: "<<duplicates.length();
for (int i = 0; i < duplicates.length(); i++){
qCDebug(log_subdiv_trace) << duplicates[i];
QVector<unsigned int> Subdivision::getAllVertexIndices(const QVector<Vertex> &vb, const QVector<Triangle> &triangles) {
QVector<unsigned int> result;
QVectorIterator<Triangle> i(triangles);
while (i.hasNext()) {
Triangle extra_triangle = i.next();
if (!containsVertexIndex(vb, result, extra_triangle.u()))
result.push_back(extra_triangle.u_idx());
if (!containsVertexIndex(vb, result, extra_triangle.v()))
result.push_back(extra_triangle.v_idx());
if (!containsVertexIndex(vb, result, extra_triangle.w()))
result.push_back(extra_triangle.w_idx());
}
return result;
}
bool Subdivision::removeVertexIndex(const QVector<Vertex> &vb, QVector<unsigned int> &vertices, const Vertex &vertex) {
QMutableVectorIterator<unsigned int> i(vertices);
while (i.hasNext()) {
if (vertex.samePos(vb[i.next()])) {
i.remove();
return true;
}
}
return false;
}
void Subdivision::updateIndexBuffer(QVector<unsigned int> &index_buffer, QMap<unsigned int, unsigned int> map) {
......@@ -852,6 +882,7 @@ void Subdivision::findRegular(QVector<Triangle> triangles, QMap<Triangle, Triang
Triangle::Neighbors ns = neighbors.value(triangle);
bool is_regular = true;
if (!isVertexRegular(triangle, ns.uv, neighbors)) {
is_regular = false;
}
......@@ -862,6 +893,7 @@ void Subdivision::findRegular(QVector<Triangle> triangles, QMap<Triangle, Triang
is_regular = false;
}
if (is_regular) {
regular.push_back(triangle);
} else {
......@@ -1057,20 +1089,39 @@ bool Subdivision::containsVertex(QVector<Vertex> &vertices, Vertex vertex) {
return false;
}
QVector<Vertex> Subdivision::getAllVertices(const QVector<Triangle> &triangles) {
QVector<Vertex> result;
QVectorIterator<Triangle> i(triangles);
while (i.hasNext()) {
Triangle extra_triangle = i.next();
if (!containsVertex(result, extra_triangle.u()))
result.push_back(extra_triangle.u());
if (!containsVertex(result, extra_triangle.v()))
result.push_back(extra_triangle.v());
if (!containsVertex(result, extra_triangle.w()))
result.push_back(extra_triangle.w());
}
return result;
}
bool removeVertex(QVector<Vertex> &vertices, const Vertex &vertex) {
QMutableVectorIterator<Vertex> i(vertices);
while (i.hasNext()) {
if (vertex.samePos(i.next())) {
i.remove();
return true;
}
}
return false;
}
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());
}
surroundingVertex = getAllVertices(neighboring_triangles);
bool removed = removeVertex(surroundingVertex, currentCorner);
assert(removed);
QVector3D newPos(0.0, 0.0, 0.0);
......
......@@ -62,8 +62,11 @@ private:
Tables precomputeTables(Input input);
void buildNeighborsMap(QVector<Vertex> &vb, QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors);
QVector<Triangle> vertexNeighbors(Triangle &triangle, Triangle::Neighbor current_neighbor, QMap<Triangle, Triangle::Neighbors> neighbors);
void precomputeEdgeTable(Subdivision::Tables &tables, QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors, unsigned int offset);
void precomputeVertexTable(Subdivision::Tables &tables, Input &input, QMap<unsigned int, unsigned int> &modified_vertices);
void precomputeEdgeTable(Subdivision::Tables &tables, QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors, unsigned int offset, QVector<Triangle> extra_old_triangles);
void precomputeVertexTable(Tables &tables, QVector<Vertex> &vb, QVector<Triangle> &irregular_triangles, QMap<Triangle, Triangle::Neighbors> neighbors, QMap<unsigned int, unsigned int> &modified_vertices);
bool containsVertexIndex(const QVector<Vertex> &vb, const QVector<unsigned int> &vertices, const Vertex vertex) const;
QVector<unsigned int> getAllVertexIndices(const QVector<Vertex> &vb, const QVector<Triangle> &triangles);
bool removeVertexIndex(const QVector<Vertex> &vb, QVector<unsigned int> &vertices, const Vertex &vertex);
void updateIndexBuffer(QVector<unsigned int> &index_buffer, QMap<unsigned int, unsigned int> map);
void findRegular(QVector<Triangle> triangles, QMap<Triangle, Triangle::Neighbors> neighbors, QVector<Triangle> &regular, QVector<Triangle> &irregular);
Result runShader(Input input, Tables &tables);
......@@ -72,6 +75,7 @@ private:
void runEdgeShader(GLuint size, GLuint vb_handle, GLuint edge_indices_handle, GLuint output_handle, GLuint offset_handle);
void updateIrregularForDraw(const QVector<Triangle> &triangles, QMap<Triangle, Triangle::Neighbors> &neighbors, Result &result);
bool containsVertex(QVector<Vertex> &vertices, Vertex vertex);
QVector<Vertex> getAllVertices(const QVector<Triangle> &triangles);
QVector3D updateIrregularVertexForDraw(Vertex currentCorner, QVector<Triangle> neighboring_triangles);
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