From 820aa90406e5b030f9a8c1f248b17217fe765ddb Mon Sep 17 00:00:00 2001 From: Jeremy Hu Date: Thu, 4 Jul 2019 19:06:50 +0930 Subject: [PATCH] Rename Vertex to ShaderVertex Fix crash on hiding nodes after removing node --- dust3d.pro | 5 +++++ src/document.cpp | 1 + src/meshloader.cpp | 36 +++++++++++++++++++++++------------ src/meshloader.h | 39 ++++++++++---------------------------- src/modelmeshbinder.cpp | 32 +++++++++++++++---------------- src/riggenerator.cpp | 4 ++-- src/shadervertex.h | 28 +++++++++++++++++++++++++++ src/skinnedmeshcreator.cpp | 4 ++-- src/toolmesh.cpp | 17 +++++++++++++++++ src/toolmesh.h | 27 ++++++++++++++++++++++++++ 10 files changed, 132 insertions(+), 61 deletions(-) create mode 100644 src/shadervertex.h create mode 100644 src/toolmesh.cpp create mode 100644 src/toolmesh.h diff --git a/dust3d.pro b/dust3d.pro index 367703c2..b1d38c38 100644 --- a/dust3d.pro +++ b/dust3d.pro @@ -367,6 +367,11 @@ HEADERS += src/preferences.h SOURCES += src/bonenameconverter.cpp HEADERS += src/bonenameconverter.h +SOURCES += src/toolmesh.cpp +HEADERS += src/toolmesh.h + +HEADERS += src/shadervertex.h + SOURCES += src/main.cpp HEADERS += src/version.h diff --git a/src/document.cpp b/src/document.cpp index c9b8003d..5e9eb8ec 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -843,6 +843,7 @@ void Document::splitPartByNode(std::vector> *groups, QUuid no { const SkeletonNode *node = findNode(nodeId); std::set visitMap; + visitMap.insert(nodeId); for (auto edgeIt = node->edgeIds.begin(); edgeIt != node->edgeIds.end(); edgeIt++) { std::vector group; const SkeletonEdge *edge = findEdge(*edgeIt); diff --git a/src/meshloader.cpp b/src/meshloader.cpp index 487424a6..d28561d6 100644 --- a/src/meshloader.cpp +++ b/src/meshloader.cpp @@ -19,14 +19,14 @@ MeshLoader::MeshLoader(const MeshLoader &mesh) : { if (nullptr != mesh.m_triangleVertices && mesh.m_triangleVertexCount > 0) { - this->m_triangleVertices = new Vertex[mesh.m_triangleVertexCount]; + this->m_triangleVertices = new ShaderVertex[mesh.m_triangleVertexCount]; this->m_triangleVertexCount = mesh.m_triangleVertexCount; for (int i = 0; i < mesh.m_triangleVertexCount; i++) this->m_triangleVertices[i] = mesh.m_triangleVertices[i]; } if (nullptr != mesh.m_edgeVertices && mesh.m_edgeVertexCount > 0) { - this->m_edgeVertices = new Vertex[mesh.m_edgeVertexCount]; + this->m_edgeVertices = new ShaderVertex[mesh.m_edgeVertexCount]; this->m_edgeVertexCount = mesh.m_edgeVertexCount; for (int i = 0; i < mesh.m_edgeVertexCount; i++) this->m_edgeVertices[i] = mesh.m_edgeVertices[i]; @@ -49,7 +49,7 @@ MeshLoader::MeshLoader(const MeshLoader &mesh) : this->m_triangulatedFaces = mesh.m_triangulatedFaces; } -MeshLoader::MeshLoader(Vertex *triangleVertices, int vertexNum) : +MeshLoader::MeshLoader(ShaderVertex *triangleVertices, int vertexNum) : m_triangleVertices(triangleVertices), m_triangleVertexCount(vertexNum), m_edgeVertices(nullptr), @@ -63,14 +63,14 @@ MeshLoader::MeshLoader(const std::vector &vertices, const std::vector const QColor &color) { m_triangleVertexCount = triangles.size() * 3; - m_triangleVertices = new Vertex[m_triangleVertexCount]; + m_triangleVertices = new ShaderVertex[m_triangleVertexCount]; int destIndex = 0; for (size_t i = 0; i < triangles.size(); ++i) { for (auto j = 0; j < 3; j++) { int vertexIndex = triangles[i][j]; const QVector3D *srcVert = &vertices[vertexIndex]; const QVector3D *srcNormal = &(triangleVertexNormals)[i][j]; - Vertex *dest = &m_triangleVertices[destIndex]; + ShaderVertex *dest = &m_triangleVertices[destIndex]; dest->colorR = color.redF(); dest->colorG = color.greenF(); dest->colorB = color.blueF(); @@ -103,7 +103,7 @@ MeshLoader::MeshLoader(Outcome &outcome) : m_faces = outcome.triangleAndQuads; m_triangleVertexCount = outcome.triangles.size() * 3; - m_triangleVertices = new Vertex[m_triangleVertexCount]; + m_triangleVertices = new ShaderVertex[m_triangleVertexCount]; int destIndex = 0; const auto triangleVertexNormals = outcome.triangleVertexNormals(); const auto triangleVertexUvs = outcome.triangleVertexUvs(); @@ -125,7 +125,7 @@ MeshLoader::MeshLoader(Outcome &outcome) : const QVector3D *srcTangent = &defaultTangent; if (triangleTangents) srcTangent = &(*triangleTangents)[i]; - Vertex *dest = &m_triangleVertices[destIndex]; + ShaderVertex *dest = &m_triangleVertices[destIndex]; dest->colorR = triangleColor->redF(); dest->colorG = triangleColor->greenF(); dest->colorB = triangleColor->blueF(); @@ -152,7 +152,7 @@ MeshLoader::MeshLoader(Outcome &outcome) : edgeCount += face.size(); } m_edgeVertexCount = edgeCount * 2; - m_edgeVertices = new Vertex[m_edgeVertexCount]; + m_edgeVertices = new ShaderVertex[m_edgeVertexCount]; size_t edgeVertexIndex = 0; for (size_t faceIndex = 0; faceIndex < outcome.triangleAndQuads.size(); ++faceIndex) { const auto &face = outcome.triangleAndQuads[faceIndex]; @@ -160,8 +160,8 @@ MeshLoader::MeshLoader(Outcome &outcome) : for (size_t x = 0; x < 2; ++x) { size_t sourceIndex = face[(i + x) % face.size()]; const QVector3D *srcVert = &outcome.vertices[sourceIndex]; - Vertex *dest = &m_edgeVertices[edgeVertexIndex]; - memset(dest, 0, sizeof(Vertex)); + ShaderVertex *dest = &m_edgeVertices[edgeVertexIndex]; + memset(dest, 0, sizeof(ShaderVertex)); dest->colorR = 0.0; dest->colorG = 0.0; dest->colorB = 0.0; @@ -191,6 +191,8 @@ MeshLoader::~MeshLoader() m_triangleVertexCount = 0; delete[] m_edgeVertices; m_edgeVertexCount = 0; + delete[] m_toolVertices; + m_toolVertexCount = 0; delete m_textureImage; delete m_normalMapImage; delete m_metalnessRoughnessAmbientOcclusionImage; @@ -216,7 +218,7 @@ const std::vector &MeshLoader::triangulatedFaces() return m_triangulatedFaces; } -Vertex *MeshLoader::triangleVertices() +ShaderVertex *MeshLoader::triangleVertices() { return m_triangleVertices; } @@ -226,7 +228,7 @@ int MeshLoader::triangleVertexCount() return m_triangleVertexCount; } -Vertex *MeshLoader::edgeVertices() +ShaderVertex *MeshLoader::edgeVertices() { return m_edgeVertices; } @@ -236,6 +238,16 @@ int MeshLoader::edgeVertexCount() return m_edgeVertexCount; } +ShaderVertex *MeshLoader::toolVertices() +{ + return m_toolVertices; +} + +int MeshLoader::toolVertexCount() +{ + return m_toolVertexCount; +} + void MeshLoader::setTextureImage(QImage *textureImage) { m_textureImage = textureImage; diff --git a/src/meshloader.h b/src/meshloader.h index a5111475..2ab9fee5 100644 --- a/src/meshloader.h +++ b/src/meshloader.h @@ -1,36 +1,13 @@ #ifndef DUST3D_MESH_LOADER_H #define DUST3D_MESH_LOADER_H #include -#include #include #include #include #include #include #include "outcome.h" - -#pragma pack(push) -#pragma pack(1) -typedef struct -{ - GLfloat posX; - GLfloat posY; - GLfloat posZ; - GLfloat normX; - GLfloat normY; - GLfloat normZ; - GLfloat colorR; - GLfloat colorG; - GLfloat colorB; - GLfloat texU; - GLfloat texV; - GLfloat metalness; - GLfloat roughness; - GLfloat tangentX; - GLfloat tangentY; - GLfloat tangentZ; -} Vertex; -#pragma pack(pop) +#include "shadervertex.h" struct TriangulatedFace { @@ -45,14 +22,16 @@ public: const std::vector> &triangleVertexNormals, const QColor &color=Qt::white); MeshLoader(Outcome &outcome); - MeshLoader(Vertex *triangleVertices, int vertexNum); + MeshLoader(ShaderVertex *triangleVertices, int vertexNum); MeshLoader(const MeshLoader &mesh); MeshLoader(); ~MeshLoader(); - Vertex *triangleVertices(); + ShaderVertex *triangleVertices(); int triangleVertexCount(); - Vertex *edgeVertices(); + ShaderVertex *edgeVertices(); int edgeVertexCount(); + ShaderVertex *toolVertices(); + int toolVertexCount(); const std::vector &vertices(); const std::vector> &faces(); const std::vector &triangulatedVertices(); @@ -74,10 +53,12 @@ public: void exportAsObj(const QString &filename); void exportAsObj(QTextStream *textStream); private: - Vertex *m_triangleVertices = nullptr; + ShaderVertex *m_triangleVertices = nullptr; int m_triangleVertexCount = 0; - Vertex *m_edgeVertices = nullptr; + ShaderVertex *m_edgeVertices = nullptr; int m_edgeVertexCount = 0; + ShaderVertex *m_toolVertices = nullptr; + int m_toolVertexCount = 0; std::vector m_vertices; std::vector> m_faces; std::vector m_triangulatedVertices; diff --git a/src/modelmeshbinder.cpp b/src/modelmeshbinder.cpp index 15578a7d..44644da0 100644 --- a/src/modelmeshbinder.cpp +++ b/src/modelmeshbinder.cpp @@ -98,7 +98,7 @@ void ModelMeshBinder::paint(ModelShaderProgram *program) m_vboTriangle.destroy(); m_vboTriangle.create(); m_vboTriangle.bind(); - m_vboTriangle.allocate(m_mesh->triangleVertices(), m_mesh->triangleVertexCount() * sizeof(Vertex)); + m_vboTriangle.allocate(m_mesh->triangleVertices(), m_mesh->triangleVertexCount() * sizeof(ShaderVertex)); m_renderTriangleVertexCount = m_mesh->triangleVertexCount(); QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glEnableVertexAttribArray(0); @@ -108,13 +108,13 @@ void ModelMeshBinder::paint(ModelShaderProgram *program) f->glEnableVertexAttribArray(4); f->glEnableVertexAttribArray(5); f->glEnableVertexAttribArray(6); - f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); - f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(3 * sizeof(GLfloat))); - f->glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(6 * sizeof(GLfloat))); - f->glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(9 * sizeof(GLfloat))); - f->glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(11 * sizeof(GLfloat))); - f->glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(12 * sizeof(GLfloat))); - f->glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(13 * sizeof(GLfloat))); + f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), 0); + f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(3 * sizeof(GLfloat))); + f->glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(6 * sizeof(GLfloat))); + f->glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(9 * sizeof(GLfloat))); + f->glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(11 * sizeof(GLfloat))); + f->glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(12 * sizeof(GLfloat))); + f->glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(13 * sizeof(GLfloat))); m_vboTriangle.release(); } { @@ -123,7 +123,7 @@ void ModelMeshBinder::paint(ModelShaderProgram *program) m_vboEdge.destroy(); m_vboEdge.create(); m_vboEdge.bind(); - m_vboEdge.allocate(m_mesh->edgeVertices(), m_mesh->edgeVertexCount() * sizeof(Vertex)); + m_vboEdge.allocate(m_mesh->edgeVertices(), m_mesh->edgeVertexCount() * sizeof(ShaderVertex)); m_renderEdgeVertexCount = m_mesh->edgeVertexCount(); QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); f->glEnableVertexAttribArray(0); @@ -133,13 +133,13 @@ void ModelMeshBinder::paint(ModelShaderProgram *program) f->glEnableVertexAttribArray(4); f->glEnableVertexAttribArray(5); f->glEnableVertexAttribArray(6); - f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); - f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(3 * sizeof(GLfloat))); - f->glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(6 * sizeof(GLfloat))); - f->glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(9 * sizeof(GLfloat))); - f->glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(11 * sizeof(GLfloat))); - f->glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(12 * sizeof(GLfloat))); - f->glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), reinterpret_cast(13 * sizeof(GLfloat))); + f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), 0); + f->glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(3 * sizeof(GLfloat))); + f->glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(6 * sizeof(GLfloat))); + f->glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(9 * sizeof(GLfloat))); + f->glVertexAttribPointer(4, 1, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(11 * sizeof(GLfloat))); + f->glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(12 * sizeof(GLfloat))); + f->glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, sizeof(ShaderVertex), reinterpret_cast(13 * sizeof(GLfloat))); m_vboEdge.release(); } } else { diff --git a/src/riggenerator.cpp b/src/riggenerator.cpp index abc5ec13..758da1c2 100644 --- a/src/riggenerator.cpp +++ b/src/riggenerator.cpp @@ -200,7 +200,7 @@ void RigGenerator::generate() // Create mesh for demo - Vertex *triangleVertices = new Vertex[m_outcome->triangles.size() * 3]; + ShaderVertex *triangleVertices = new ShaderVertex[m_outcome->triangles.size() * 3]; int triangleVerticesNum = 0; const QVector3D defaultUv = QVector3D(0, 0, 0); const QVector3D defaultTangents = QVector3D(0, 0, 0); @@ -210,7 +210,7 @@ void RigGenerator::generate() if (nullptr != triangleTangents) sourceTangent = &(*triangleTangents)[triangleIndex]; for (int i = 0; i < 3; i++) { - Vertex ¤tVertex = triangleVertices[triangleVerticesNum++]; + ShaderVertex ¤tVertex = triangleVertices[triangleVerticesNum++]; const auto &sourcePosition = inputVerticesPositions[sourceTriangle[i]]; const auto &sourceColor = inputVerticesColors[sourceTriangle[i]]; const auto *sourceNormal = &defaultUv; diff --git a/src/shadervertex.h b/src/shadervertex.h new file mode 100644 index 00000000..e3cbd7b4 --- /dev/null +++ b/src/shadervertex.h @@ -0,0 +1,28 @@ +#ifndef DUST3D_SHADER_VERTEX_H +#define DUST3D_SHADER_VERTEX_H +#include + +#pragma pack(push) +#pragma pack(1) +typedef struct +{ + GLfloat posX; + GLfloat posY; + GLfloat posZ; + GLfloat normX; + GLfloat normY; + GLfloat normZ; + GLfloat colorR; + GLfloat colorG; + GLfloat colorB; + GLfloat texU; + GLfloat texV; + GLfloat metalness; + GLfloat roughness; + GLfloat tangentX; + GLfloat tangentY; + GLfloat tangentZ; +} ShaderVertex; +#pragma pack(pop) + +#endif diff --git a/src/skinnedmeshcreator.cpp b/src/skinnedmeshcreator.cpp index 4be2d8fb..3a3d8d57 100644 --- a/src/skinnedmeshcreator.cpp +++ b/src/skinnedmeshcreator.cpp @@ -59,11 +59,11 @@ MeshLoader *SkinnedMeshCreator::createMeshFromTransform(const std::vector +#include +#include +#include "shadervertex.h" + +class ToolMesh +{ +public: + void addNode(const QVector3D &position, float radius, const QMatrix4x4 &transform); + void generate(); + ShaderVertex *takeShaderVertices(int *shaderVertexCount); + +private: + struct Node + { + QVector3D position; + float radius; + QMatrix4x4 transform; + }; + + std::vector m_nodes; + ShaderVertex *m_shaderVertices = nullptr; +}; + +#endif