diff --git a/QtMeshViewer/Header/FileInterface.h b/QtMeshViewer/Header/FileInterface.h index 0b9b792..bcd66f1 100644 --- a/QtMeshViewer/Header/FileInterface.h +++ b/QtMeshViewer/Header/FileInterface.h @@ -21,6 +21,7 @@ struct VertexData { QVector3D position; QVector2D texCoord; + QVector3D normal; }; struct Segment { diff --git a/QtMeshViewer/Resources/vshader.glsl b/QtMeshViewer/Resources/vshader.glsl index 583b344..e10730e 100644 --- a/QtMeshViewer/Resources/vshader.glsl +++ b/QtMeshViewer/Resources/vshader.glsl @@ -10,6 +10,7 @@ uniform mat4 m_matrix; attribute vec4 a_position; attribute vec2 a_texcoord; +attribute vec3 a_normal; varying vec2 v_texcoord; diff --git a/QtMeshViewer/Source/GeometryEngine.cpp b/QtMeshViewer/Source/GeometryEngine.cpp index 2c82955..c65e928 100644 --- a/QtMeshViewer/Source/GeometryEngine.cpp +++ b/QtMeshViewer/Source/GeometryEngine.cpp @@ -196,6 +196,14 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program) program->enableAttributeArray(texcoordLocation); program->setAttributeBuffer(texcoordLocation, GL_FLOAT, offset, 2, sizeof(VertexData)); + //Offset for normal + offset += sizeof(QVector2D); + + // Tell OpenGL programmable pipeline how to locate vertex normal data + int normLocation = program->attributeLocation("a_normal"); + program->enableAttributeArray(normLocation); + program->setAttributeBuffer(normLocation, GL_FLOAT, offset, 3, sizeof(VertexData)); + // Paint for (auto& it : m_drawList) diff --git a/QtMeshViewer/Source/MshFile.cpp b/QtMeshViewer/Source/MshFile.cpp index 00ed57b..03e6511 100644 --- a/QtMeshViewer/Source/MshFile.cpp +++ b/QtMeshViewer/Source/MshFile.cpp @@ -384,15 +384,31 @@ void MshFile::analyseSegmChunks(Model * dataDestination, std::list } // normals - /*else if (!strcmp("NRML", it->name)) + else if (!strcmp("NRML", it->name)) { - fsMesh.seekg(it->position); - std::uint32_t tempSize; - fsMesh.read(reinterpret_cast(&tempSize), sizeof(tempSize)); - // List of normals - // long int - 4 - number of normal vectores stored in this list - // float[3][] - 12 each - UVW vector for each vertex - }*/ + std::uint32_t tmp_size; + m_file.seekg(it->position); + m_file.read(F2V(tmp_size), sizeof(tmp_size)); + + if (tmp_size < new_segment->vertices.size()) + { + emit sendMessage("WARNING: too less normals " + QString::number(tmp_size) + " < " + QString::number(new_segment->vertices.size()), 1); + + for (unsigned int i = new_segment->vertices.size(); i != tmp_size; i--) + for (unsigned int j = 0; j < 3; j++) + new_segment->vertices[i - 1].normal[j] = 0; + } + else if (tmp_size > new_segment->vertices.size()) + { + emit sendMessage("WARNING: too many normals " + QString::number(tmp_size) + " > " + QString::number(new_segment->vertices.size()), 1); + tmp_size = new_segment->vertices.size(); + } + + for (unsigned int i = 0; i < tmp_size; i++) + for (unsigned int j = 0; j < 3; j++) + m_file.read(F2V(new_segment->vertices[i].normal[j]), sizeof(float)); + + } // uv else if (!strcmp("UV0L", it->name)) @@ -584,11 +600,8 @@ void MshFile::readUV(Segment * dataDestination, std::streampos position) } for (unsigned int i = 0; i < tmp_size; i++) - { - float tmp[2]; for (unsigned int j = 0; j < 2; j++) m_file.read(F2V(dataDestination->vertices[i].texCoord[j]), sizeof(float)); - } } QMatrix4x4 MshFile::getParentMatrix(std::string parent) const