Commit 55d42e89 by Philipp Adolf

Render regular patches without subdivision

Splits original mesh into regular and irregular meshes and renders them separately.
parent 0e9ef934
......@@ -149,7 +149,7 @@ void MainWidget::loadNewMesh(QString path){
qDebug()<<"Opening File:"<<path;
mesh = new Mesh(this,path);
subdivision->splitRegular(mesh);
}
void MainWidget::subdivide(int level){
......@@ -222,19 +222,15 @@ void MainWidget::paintGL(){
rot.rotate(time/100.0*36/5,QVector3D(0,1,0));
}
if(subdivLevel>0){
regularShader->bind();
regularShader->setUniformValue("wireframe",wireframe);
regularShader->setUniformValue("colorTexture",0);
regularShader->setUniformValue("LightPos",QVector3D(0,100,100));
regularShader->setUniformValue("subdiv",subdivLevel);
mesh->render(regularShader,cam->getMatrix()*rot, m_projection,subdivLevel,true);
regularShader->bind();
regularShader->setUniformValue("wireframe",wireframe);
regularShader->setUniformValue("colorTexture",0);
regularShader->setUniformValue("LightPos",QVector3D(0,100,100));
regularShader->setUniformValue("subdiv",subdivLevel);
regularShader->release();
}
mesh->render(regularShader,cam->getMatrix()*rot, m_projection,subdivLevel,true);
regularShader->release();
subdivisionShader->bind();
subdivisionShader->setUniformValue("wireframe",wireframe);
......
......@@ -37,6 +37,18 @@ void Mesh::SubdivEntry::addRegular(QVector<unsigned int>& Indices){
}
void Mesh::SubdivEntry::updateIndices() {
f->glDeleteBuffers(1, &IB_handle);
f->glGenBuffers(1, &IB_handle);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB_handle);
f->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indices.size(), &indices[0], GL_STATIC_DRAW);
f->glDeleteBuffers(1, &IB_Regular);
f->glGenBuffers(1, &IB_Regular);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IB_Regular);
f->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indicesRegular.size(), &indicesRegular[0], GL_STATIC_DRAW);
}
void Mesh::SubdivEntry::init(QOpenGLFunctions_4_3_Core *f,QVector<Vertex>& Vertices,
QVector<unsigned int>& Indices){
......@@ -462,26 +474,24 @@ void Mesh::renderMesh(QOpenGLShaderProgram *shader, int index, int subdivision,
// qDebug()<<"Render"<<subdivision<<entry->indices;
if(regular){
f->glBindBuffer(GL_ARRAY_BUFFER, entries[index].buffers[subdivision-1]->VB_handle);
f->glVertexAttribPointer(positionIndex, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
if (!entry->indicesRegular.isEmpty()) {
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_Regular);
f->glPatchParameteri(GL_PATCH_VERTICES, 12);
f->glDrawElements(GL_PATCHES, entry->indicesRegular.size(), GL_UNSIGNED_INT, 0);
} else{
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_handle);
f->glPatchParameteri(GL_PATCH_VERTICES, 3);
f->glDrawElements(GL_PATCHES, entry->indices.size(), GL_UNSIGNED_INT, 0);
f->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, entry->IB_Regular);
f->glPatchParameteri(GL_PATCH_VERTICES, 12);
f->glDrawElements(GL_PATCHES, entry->indicesRegular.size(), GL_UNSIGNED_INT, 0);
}
} else {
if (!entry->indices.isEmpty()) {
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_handle);
f->glPatchParameteri(GL_PATCH_VERTICES, 3);
f->glDrawElements(GL_PATCHES, entry->indices.size(), GL_UNSIGNED_INT, 0);
}
}
}
void Mesh::findObjectDimension(Node node, QMatrix4x4 transform, QVector3D &min, QVector3D &max){
......
......@@ -74,7 +74,7 @@ public:
void init(QOpenGLFunctions_4_3_Core *f,GLuint VB_handle,QVector<Vertex>& Vertices,
QVector<unsigned int>& Indices);
void addRegular(QVector<unsigned int>& Indices);
void updateIndices();
};
struct MeshEntry{
......
......@@ -12,7 +12,7 @@ void main()
tcPosition[gl_InvocationID] = vPosition[gl_InvocationID];
float tesselation = subdiv+15;
float tesselation = subdiv+1;
gl_TessLevelOuter[0] = tesselation;
gl_TessLevelOuter[1] = tesselation;
......
......@@ -363,6 +363,31 @@ Subdivision::Tables Subdivision::precomputeTables(Input input) {
return tables;
}
void Subdivision::splitRegular(Mesh *mesh) {
Mesh::Node root = mesh->getRootNode();
int first_mesh_index = -1;
if (!root.getFirstMeshIndex(first_mesh_index)) {
qCritical()<<"No mesh found, aborting subdivision";
return;
}
Mesh::MeshEntry *current_mesh = mesh->getMeshEntry(first_mesh_index);
QVector<unsigned int> regular;
QVector<unsigned int> irregular;
findRegular(current_mesh->buffers[0]->indices, current_mesh->buffers[0]->vertices, regular, irregular);
findRegular(current_mesh->buffers[0]->indicesRegular, current_mesh->buffers[0]->vertices, regular, irregular);
QVector<unsigned int> patches = getPatchIndexBuffer(regular);
current_mesh->buffers[0]->indicesRegular = patches;
current_mesh->buffers[0]->indices = irregular;
current_mesh->buffers[0]->updateIndices();
qDebug() << "regular: " << regular.length();
qDebug() << "irregular: " << irregular.length();
}
/**
* @brief Subdivision::getPatchIndexBuffer
* Computes an index buffer for the patches. patches are in order from figure 6(a) from the paper:
......
......@@ -13,6 +13,7 @@ public:
~Subdivision();
void init();
void splitRegular(Mesh *mesh);
void subdivide(Mesh *mesh, int level);
void setDebugOutbut(bool debug);
......
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