diff --git a/QtMeshViewer/Header/GeometryEngine.h b/QtMeshViewer/Header/GeometryEngine.h index b1d4174dd9d4c5ae5b28fe7bc44846414e99d996..538818d7385c8a44c289164502c8307bfab5c9da 100644 --- a/QtMeshViewer/Header/GeometryEngine.h +++ b/QtMeshViewer/Header/GeometryEngine.h @@ -6,6 +6,13 @@ #include <QOpenGLTexture> #include <QVector> +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<QOpenGLTexture*> m_textures; - - QVector<Model*>* m_models = Q_NULLPTR; - QVector<std::string>* m_textureNames = Q_NULLPTR; //TODO: remove, use it local and only hold the textures itself + QVector<DrawInformation> m_drawList; void initCubeGeometry(); void initTexture(); diff --git a/QtMeshViewer/Resources/Resources.qrc b/QtMeshViewer/Resources/Resources.qrc index bdb879ba4685e2a9e03f967c1b04b78125020f21..b54819d4c2247256a6657b83013ba4d475b1656a 100644 --- a/QtMeshViewer/Resources/Resources.qrc +++ b/QtMeshViewer/Resources/Resources.qrc @@ -5,5 +5,6 @@ </qresource> <qresource prefix="/images"> <file>cube.png</file> + <file>icon.ico</file> </qresource> </RCC> diff --git a/QtMeshViewer/Resources/fshader.glsl b/QtMeshViewer/Resources/fshader.glsl index 44dc2450ca210650b0c4eb612426535d0882ba47..12edc28fd650281f644e723181437f0212d30410 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 3a2186f250235aa317c4f2138818e46d07b4a504..6f6a2f335613d7688b6f41d200eb400bc6eb4b98 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 d8b92469a8babe0359d332222e5c5dc4343616cd..f5b84d72e0bf63ec5e05ddcc026778453a5d011c 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 84fe2d6ffc74686079b9739bcdc9c6066fc7a500..47e2e732c63e492b851b3386ddeeb7ef2fa0f172 100644 --- a/QtMeshViewer/Source/GeometryEngine.cpp +++ b/QtMeshViewer/Source/GeometryEngine.cpp @@ -35,11 +35,14 @@ GeometryEngine::~GeometryEngine() void GeometryEngine::initCubeGeometry() { + QVector<Model*>* models; + QVector<VertexData> vertexData; + QVector<GLuint> 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 57860fda2d94e546204f9b03277bc38c12e9864d..5f6e92b634b6051debef70ae0dd22b954b5fa066 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 2132c3e7395c033671fa0bdb2f65d73db19cf435..aab0310d69a3124b2c7b5ededa3abfd01cfdc983 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);