Commit 78c715fe by Philipp Adolf

Add Tables struct

parent ce81fdb6
......@@ -52,16 +52,14 @@ Mesh *Subdivision::subdivide(Mesh *mesh) {
input.vertex_buffer = current_mesh->getVertexBuffer();
input.index_buffer = current_mesh->getIndexBuffer();
QVector<GLuint> edgeIndices;
QVector<GLuint> vertexIndices;
QVector<GLuint> vertexIndicesOffsets;
precomputeTables(input, edgeIndices, vertexIndices, vertexIndicesOffsets);
runShader(input, edgeIndices);
Subdivision::Tables tables = precomputeTables(input);
runShader(input, tables);
return NULL;
}
void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndices_base,
QVector<unsigned int> &vertexIndices, QVector<unsigned int> &vertexIndicesOffsets) {
Subdivision::Tables Subdivision::precomputeTables(Input input) {
Tables tables;
QVector<unsigned int> ib = input.index_buffer;
QVector<Vertex> vb = input.vertex_buffer;
......@@ -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 zx
* - 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
//zx = ba, cb, ac
//TODO if oder else if?
if (x.samePos(a) && y.samePos(c)){
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(b_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(b_i);
}
if (x.samePos(b) && y.samePos(a)){
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(c_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(c_i);
}
if (x.samePos(c) && y.samePos(b)){
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(a_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(a_i);
}
}
}
......@@ -146,22 +144,22 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
//yz = ba, cb, ac
if (y.samePos(a) && z.samePos(c)){
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(b_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(b_i);
}
if (y.samePos(b) && z.samePos(a)){
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(c_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(c_i);
}
if (y.samePos(c) && z.samePos(b)){
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(a_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(a_i);
}
}
}
......@@ -183,33 +181,33 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
if (x.samePos(a) && z.samePos(b)){
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(c_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(c_i);
}
if (x.samePos(b) && z.samePos(c)){
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(a_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(a_i);
}
if (x.samePos(c) && z.samePos(a)){
edgeIndices_base.push_back(x_i);
edgeIndices_base.push_back(z_i);
edgeIndices_base.push_back(y_i);
edgeIndices_base.push_back(b_i);
tables.edge_indices.push_back(x_i);
tables.edge_indices.push_back(z_i);
tables.edge_indices.push_back(y_i);
tables.edge_indices.push_back(b_i);
}
}
}//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.
}
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){
qDebug()<<"Table (long version):";
for (int i = 0; i < edgeIndices_base.length(); i++){
qDebug()<<"blub:"<<edgeIndices_base[i];
for (int i = 0; i < tables.edge_indices.length(); i++){
qDebug()<<"blub:"<<tables.edge_indices[i];
}
}
......@@ -221,8 +219,8 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
//hat evtl viel redundanz
QVector<Vertex> adj_v;//helfer
vertexIndicesOffsets.push_back(offset);
vertexIndices.push_back(i);
tables.vertex_offsets.push_back(offset);
tables.vertex_indices.push_back(i);
offset++;
Vertex originalVertex = vb[i];
......@@ -259,12 +257,12 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
}
if (!found1){
adj_v.push_back(v1);
vertexIndices.push_back(i1);
tables.vertex_indices.push_back(i1);
offset++;
}
if (!found2){
adj_v.push_back(v2);
vertexIndices.push_back(i2);
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
......@@ -280,9 +278,10 @@ void Subdivision::precomputeTables(Input input, QVector<unsigned int> &edgeIndic
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";
edgeShader->bind();
......@@ -292,16 +291,16 @@ void Subdivision::runShader(Input input, QVector<unsigned int> &edgeIndices) {
GLuint indicesID;
f->glGenBuffers(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
GLuint bufferID;
f->glGenBuffers(1, &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
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
f->glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
......@@ -315,7 +314,7 @@ void Subdivision::runShader(Input input, QVector<unsigned int> &edgeIndices) {
f->glBindBuffer(GL_SHADER_STORAGE_BUFFER, bufferID);
Vertex *ptr;
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;
}
f->glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
......
......@@ -23,13 +23,19 @@ private:
QVector<Vertex> vertex_buffer;
};
struct Tables
{
QVector<GLuint> edge_indices;
QVector<GLuint> vertex_indices;
QVector<GLuint> vertex_offsets;
};
QOpenGLFunctions_4_3_Core *f;
QOpenGLShaderProgram *edgeShader;
QOpenGLShaderProgram *initComputeShaderProgram(QString &source);
void precomputeTables(Input input, QVector<GLuint> &edgeIndices_base,
QVector<GLuint> &vertexIndices, QVector<GLuint> &vertexIndicesOffsets);
void runShader(Input input, QVector<GLuint> &edgeIndices);
Tables precomputeTables(Input input);
void runShader(Input input, Tables &tables);
};
#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