Commit 78c715fe by Philipp Adolf

Add Tables struct

parent ce81fdb6
...@@ -52,16 +52,14 @@ Mesh *Subdivision::subdivide(Mesh *mesh) { ...@@ -52,16 +52,14 @@ Mesh *Subdivision::subdivide(Mesh *mesh) {
input.vertex_buffer = current_mesh->getVertexBuffer(); input.vertex_buffer = current_mesh->getVertexBuffer();
input.index_buffer = current_mesh->getIndexBuffer(); input.index_buffer = current_mesh->getIndexBuffer();
QVector<GLuint> edgeIndices; Subdivision::Tables tables = precomputeTables(input);
QVector<GLuint> vertexIndices; runShader(input, tables);
QVector<GLuint> vertexIndicesOffsets;
precomputeTables(input, edgeIndices, vertexIndices, vertexIndicesOffsets);
runShader(input, edgeIndices);
return NULL; return NULL;
} }
void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndices_base, Subdivision::Tables Subdivision::precomputeTables(Input input) {
QVector<unsigned int> &vertexIndices, QVector<unsigned int> &vertexIndicesOffsets) { Tables tables;
QVector<unsigned int> ib = input.index_buffer; QVector<unsigned int> ib = input.index_buffer;
QVector<Vertex> vb = input.vertex_buffer; QVector<Vertex> vb = input.vertex_buffer;
...@@ -83,7 +81,7 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic ...@@ -83,7 +81,7 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
/* /*
* Find edge xy in all other triangles, add to edgeIndices_base * Find edge xy in all other triangles, add to edge_indices
* Find edge yz * Find edge yz
* Find edge zx * Find edge zx
* - We assume that we have a closed surface, i.e. each triangle has exactly one adjacent triangle at each edge * - We assume that we have a closed surface, i.e. each triangle has exactly one adjacent triangle at each edge
...@@ -110,22 +108,22 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic ...@@ -110,22 +108,22 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
//zx = ba, cb, ac //zx = ba, cb, ac
//TODO if oder else if? //TODO if oder else if?
if (x.samePos(a) && y.samePos(c)){ if (x.samePos(a) && y.samePos(c)){
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(b_i); tables.edge_indices.push_back(b_i);
} }
if (x.samePos(b) && y.samePos(a)){ if (x.samePos(b) && y.samePos(a)){
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(c_i); tables.edge_indices.push_back(c_i);
} }
if (x.samePos(c) && y.samePos(b)){ if (x.samePos(c) && y.samePos(b)){
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(a_i); tables.edge_indices.push_back(a_i);
} }
} }
} }
...@@ -146,22 +144,22 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic ...@@ -146,22 +144,22 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
//yz = ba, cb, ac //yz = ba, cb, ac
if (y.samePos(a) && z.samePos(c)){ if (y.samePos(a) && z.samePos(c)){
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(b_i); tables.edge_indices.push_back(b_i);
} }
if (y.samePos(b) && z.samePos(a)){ if (y.samePos(b) && z.samePos(a)){
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(c_i); tables.edge_indices.push_back(c_i);
} }
if (y.samePos(c) && z.samePos(b)){ if (y.samePos(c) && z.samePos(b)){
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(a_i); tables.edge_indices.push_back(a_i);
} }
} }
} }
...@@ -183,33 +181,33 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic ...@@ -183,33 +181,33 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
if (x.samePos(a) && z.samePos(b)){ if (x.samePos(a) && z.samePos(b)){
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(c_i); tables.edge_indices.push_back(c_i);
} }
if (x.samePos(b) && z.samePos(c)){ if (x.samePos(b) && z.samePos(c)){
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(a_i); tables.edge_indices.push_back(a_i);
} }
if (x.samePos(c) && z.samePos(a)){ if (x.samePos(c) && z.samePos(a)){
edgeIndices_base.push_back(x_i); tables.edge_indices.push_back(x_i);
edgeIndices_base.push_back(z_i); tables.edge_indices.push_back(z_i);
edgeIndices_base.push_back(y_i); tables.edge_indices.push_back(y_i);
edgeIndices_base.push_back(b_i); tables.edge_indices.push_back(b_i);
} }
} }
}//quadratische Laufzeit ftw }//quadratische Laufzeit ftw
//Wichtig: Wir gehen davon aus, dass wir geschlossene Oberflächen haben, dh für jede Kante von einem Dreieck wird eine passende Kante bei einem anderen Dreieck gefunden. //Wichtig: Wir gehen davon aus, dass wir geschlossene Oberflächen haben, dh für jede Kante von einem Dreieck wird eine passende Kante bei einem anderen Dreieck gefunden.
} }
qDebug()<<"Done with edge table. "<<edgeIndices_base.length()<<" edges found. Table: "<<edgeIndices_base; qDebug()<<"Done with edge table. "<<tables.edge_indices.length()<<" edges found. Table: "<<tables.edge_indices;
if (true){ if (true){
qDebug()<<"Table (long version):"; qDebug()<<"Table (long version):";
for (int i = 0; i < edgeIndices_base.length(); i++){ for (int i = 0; i < tables.edge_indices.length(); i++){
qDebug()<<"blub:"<<edgeIndices_base[i]; qDebug()<<"blub:"<<tables.edge_indices[i];
} }
} }
...@@ -221,8 +219,8 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic ...@@ -221,8 +219,8 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
//hat evtl viel redundanz //hat evtl viel redundanz
QVector<Vertex> adj_v;//helfer QVector<Vertex> adj_v;//helfer
vertexIndicesOffsets.push_back(offset); tables.vertex_offsets.push_back(offset);
vertexIndices.push_back(i); tables.vertex_indices.push_back(i);
offset++; offset++;
Vertex originalVertex = vb[i]; Vertex originalVertex = vb[i];
...@@ -259,12 +257,12 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic ...@@ -259,12 +257,12 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
} }
if (!found1){ if (!found1){
adj_v.push_back(v1); adj_v.push_back(v1);
vertexIndices.push_back(i1); tables.vertex_indices.push_back(i1);
offset++; offset++;
} }
if (!found2){ if (!found2){
adj_v.push_back(v2); adj_v.push_back(v2);
vertexIndices.push_back(i2); tables.vertex_indices.push_back(i2);
offset++; offset++;
} }
//if one adjacent vertex is referenced by multiple different indices, only its first index will appear in the adjacent_indices list.1 //if one adjacent vertex is referenced by multiple different indices, only its first index will appear in the adjacent_indices list.1
...@@ -280,9 +278,10 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic ...@@ -280,9 +278,10 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
qDebug()<<duplicates[i]; qDebug()<<duplicates[i];
} }
return tables;
} }
void Subdivision::runShader(Input input, QVector<unsigned int> &edgeIndices) { void Subdivision::runShader(Input input, Tables &tables) {
qDebug()<<"Running compute shader"; qDebug()<<"Running compute shader";
edgeShader->bind(); edgeShader->bind();
...@@ -292,16 +291,16 @@ void Subdivision::runShader(Input input, QVector<unsigned int> &edgeIndices) { ...@@ -292,16 +291,16 @@ void Subdivision::runShader(Input input, QVector<unsigned int> &edgeIndices) {
GLuint indicesID; GLuint indicesID;
f->glGenBuffers(1, &indicesID); f->glGenBuffers(1, &indicesID);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, indicesID); f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, indicesID);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, edgeIndices.size() * sizeof(GLuint), edgeIndices.data(), GL_DYNAMIC_DRAW); f->glBufferData(GL_SHADER_STORAGE_BUFFER, tables.edge_indices.size() * sizeof(GLuint), tables.edge_indices.data(), GL_DYNAMIC_DRAW);
// Create an output buffer // Create an output buffer
GLuint bufferID; GLuint bufferID;
f->glGenBuffers(1, &bufferID); f->glGenBuffers(1, &bufferID);
f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bufferID); f->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bufferID);
f->glBufferData(GL_SHADER_STORAGE_BUFFER, edgeIndices.size() * sizeof(Vertex) / 4, NULL, GL_DYNAMIC_DRAW); f->glBufferData(GL_SHADER_STORAGE_BUFFER, tables.edge_indices.size() * sizeof(Vertex) / 4, NULL, GL_DYNAMIC_DRAW);
// Run the shader // Run the shader
f->glDispatchCompute(edgeIndices.size() / 4, 1, 1); f->glDispatchCompute(tables.edge_indices.size() / 4, 1, 1);
// Wait for the shader to complete and the data to be written back to the global memory // Wait for the shader to complete and the data to be written back to the global memory
f->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT); f->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
...@@ -315,7 +314,7 @@ void Subdivision::runShader(Input input, QVector<unsigned int> &edgeIndices) { ...@@ -315,7 +314,7 @@ void Subdivision::runShader(Input input, QVector<unsigned int> &edgeIndices) {
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID); f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID);
Vertex *ptr; Vertex *ptr;
ptr = (Vertex *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY); ptr = (Vertex *) f->glMapBuffer(GL_SHADER_STORAGE_BUFFER, GL_WRITE_ONLY);
for (int i = 0; i < edgeIndices.size() / 4; i++) { for (int i = 0; i < tables.edge_indices.size() / 4; i++) {
qDebug() << ptr[i].pos; qDebug() << ptr[i].pos;
} }
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER); f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
......
...@@ -23,13 +23,19 @@ private: ...@@ -23,13 +23,19 @@ private:
QVector<Vertex> vertex_buffer; QVector<Vertex> vertex_buffer;
}; };
struct Tables
{
QVector<GLuint> edge_indices;
QVector<GLuint> vertex_indices;
QVector<GLuint> vertex_offsets;
};
QOpenGLFunctions_4_3_Core *f; QOpenGLFunctions_4_3_Core *f;
QOpenGLShaderProgram *edgeShader; QOpenGLShaderProgram *edgeShader;
QOpenGLShaderProgram *initComputeShaderProgram(QString &source); QOpenGLShaderProgram *initComputeShaderProgram(QString &source);
void precomputeTables(Input input, QVector<GLuint> &edgeIndices_base, Tables precomputeTables(Input input);
QVector<GLuint> &vertexIndices, QVector<GLuint> &vertexIndicesOffsets); void runShader(Input input, Tables &tables);
void runShader(Input input, QVector<GLuint> &edgeIndices);
}; };
#endif #endif
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