diff --git a/QtMeshViewer/Header/GeometryEngine.h b/QtMeshViewer/Header/GeometryEngine.h index b1d4174..538818d 100644 --- a/QtMeshViewer/Header/GeometryEngine.h +++ b/QtMeshViewer/Header/GeometryEngine.h @@ -6,6 +6,13 @@ #include #include +struct DrawInformation { + unsigned int offset; + unsigned int size; + unsigned int textureIndex; + QMatrix4x4 modelMatrix; +}; + class GeometryEngine : protected QOpenGLFunctions { public: @@ -16,9 +23,7 @@ private: QOpenGLBuffer m_arrayBuf; QOpenGLBuffer m_indexBuf; QVector m_textures; - - QVector* m_models = Q_NULLPTR; - QVector* m_textureNames = Q_NULLPTR; //TODO: remove, use it local and only hold the textures itself + QVector m_drawList; void initCubeGeometry(); void initTexture(); diff --git a/QtMeshViewer/Resources/Resources.qrc b/QtMeshViewer/Resources/Resources.qrc index bdb879b..b54819d 100644 --- a/QtMeshViewer/Resources/Resources.qrc +++ b/QtMeshViewer/Resources/Resources.qrc @@ -5,5 +5,6 @@ cube.png + icon.ico diff --git a/QtMeshViewer/Resources/fshader.glsl b/QtMeshViewer/Resources/fshader.glsl index 44dc245..12edc28 100644 --- a/QtMeshViewer/Resources/fshader.glsl +++ b/QtMeshViewer/Resources/fshader.glsl @@ -10,6 +10,6 @@ varying vec2 v_texcoord; void main() { - // Set fragment color from texture - gl_FragColor = texture2D(texture, v_texcoord); + // Set fragment color from texture + gl_FragColor = texture2D(texture, v_texcoord); } diff --git a/QtMeshViewer/Resources/icon.ico b/QtMeshViewer/Resources/icon.ico index 3a2186f..6f6a2f3 100644 Binary files a/QtMeshViewer/Resources/icon.ico and b/QtMeshViewer/Resources/icon.ico differ diff --git a/QtMeshViewer/Resources/vshader.glsl b/QtMeshViewer/Resources/vshader.glsl index d8b9246..f5b84d7 100644 --- a/QtMeshViewer/Resources/vshader.glsl +++ b/QtMeshViewer/Resources/vshader.glsl @@ -4,7 +4,8 @@ precision mediump int; precision mediump float; #endif -uniform mat4 mvp_matrix; +uniform mat4 vp_matrix; +uniform mat4 m_matrix; attribute vec4 a_position; attribute vec2 a_texcoord; @@ -13,10 +14,10 @@ varying vec2 v_texcoord; void main() { - // Calculate vertex position in screen space - gl_Position = mvp_matrix * a_position; + // Calculate vertex position in screen space + gl_Position = vp_matrix * m_matrix * a_position; - // Pass texture coordinate to fragment shader - // Value will be automatically interpolated to fragments inside polygon faces - v_texcoord = a_texcoord; + // Pass texture coordinate to fragment shader + // Value will be automatically interpolated to fragments inside polygon faces + v_texcoord = a_texcoord; } diff --git a/QtMeshViewer/Source/GeometryEngine.cpp b/QtMeshViewer/Source/GeometryEngine.cpp index 84fe2d6..47e2e73 100644 --- a/QtMeshViewer/Source/GeometryEngine.cpp +++ b/QtMeshViewer/Source/GeometryEngine.cpp @@ -35,11 +35,14 @@ GeometryEngine::~GeometryEngine() void GeometryEngine::initCubeGeometry() { + QVector* models; + QVector vertexData; + QVector indexData; try { - MshFile file("..\\Release\\Msh\\cubeTex.msh"); - m_models = file.getModels(); + MshFile file("..\\Release\\Msh\\sphere.msh"); + models = file.getModels(); //TODO use models local, apply MVP directly to the vertex, save size and tex index info //TODO: handle the textures @@ -50,13 +53,40 @@ void GeometryEngine::initCubeGeometry() auto msg = e.what(); } + // collect data + unsigned int offsetCount(0); + for (auto& modelIterator : *models) + { + for (auto& segmentIterator : modelIterator->segmList) + { + // get draw information + DrawInformation new_info; + new_info.offset = offsetCount; + new_info.size = segmentIterator->indices.size(); + new_info.textureIndex = segmentIterator->textureIndex; + new_info.modelMatrix = modelIterator->m4x4Translation; + + // add offset to indices + for (auto& it : segmentIterator->indices) + it += new_info.offset; + + // save data + vertexData += segmentIterator->vertices; + indexData += segmentIterator->indices; + m_drawList.push_back(new_info); + + // update offset + offsetCount += new_info.size; + } + } + // Transfer vertex data to VBO 0 m_arrayBuf.bind(); - m_arrayBuf.allocate(m_models->first()->segmList.front()->vertices.data(), m_models->first()->segmList.front()->vertices.size() * sizeof(VertexData)); + m_arrayBuf.allocate(vertexData.data(),vertexData.size() * sizeof(VertexData)); // Transfer index data to VBO 1 m_indexBuf.bind(); - m_indexBuf.allocate(m_models->first()->segmList.front()->indices.data(), m_models->first()->segmList.front()->indices.size() * sizeof(GLuint)); + m_indexBuf.allocate(indexData.data(), indexData.size() * sizeof(GLuint)); // load the texture initTexture(); @@ -86,12 +116,12 @@ void GeometryEngine::initTexture() void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program) { +// Setup // Tell OpenGL which VBOs to use m_arrayBuf.bind(); m_indexBuf.bind(); - m_textures.first()->bind(); - // Use texture unit 0 which contains cube.png + // Allways use texture unit 0 program->setUniformValue("texture", 0); // Offset for position @@ -110,6 +140,20 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program) program->enableAttributeArray(texcoordLocation); program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData)); - // Draw cube geometry using indices from VBO 1 - glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); +// Paint + + for (auto& it : m_drawList) + { + // bind the correct texture + if (it.textureIndex < m_textures.size()) + m_textures.last()->bind(); + else + m_textures.at(it.textureIndex)->bind(); + + // Set model matrix + program->setUniformValue("m_matrix", it.modelMatrix); + + // Draw cube geometry using indices from VBO 1 + glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint))); + } } diff --git a/QtMeshViewer/Source/MainWindow.cpp b/QtMeshViewer/Source/MainWindow.cpp index 57860fd..5f6e92b 100644 --- a/QtMeshViewer/Source/MainWindow.cpp +++ b/QtMeshViewer/Source/MainWindow.cpp @@ -12,9 +12,11 @@ MainWindow::MainWindow(QWidget *parent) format.setDepthBufferSize(24); QSurfaceFormat::setDefaultFormat(format); - this->setCentralWidget(new OglViewerWidget(this)); + setCentralWidget(new OglViewerWidget(this)); - ui->statusBar->showMessage("haha vbgf"); + setWindowIcon(QIcon(":/images/icon.ico")); + setWindowTitle("Mesh Viewer"); + ui->statusBar->showMessage("pre-alpha"); } diff --git a/QtMeshViewer/Source/OglViewerWidget.cpp b/QtMeshViewer/Source/OglViewerWidget.cpp index 2132c3e..aab0310 100644 --- a/QtMeshViewer/Source/OglViewerWidget.cpp +++ b/QtMeshViewer/Source/OglViewerWidget.cpp @@ -142,8 +142,8 @@ void OglViewerWidget::paintGL() view.translate(m_translation); view.rotate(m_rotation); - // Set modelview-projection matrix - m_program.setUniformValue("mvp_matrix", m_projection * view); + // Set view-projection matrix + m_program.setUniformValue("vp_matrix", m_projection * view); // Draw cube geometry m_dataEngine->drawGeometry(&m_program);