From 6ce16b083716705307c80490c19fa1b496f81e33 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 18 Aug 2024 17:37:57 +0300 Subject: [PATCH 1/5] deleting XML document after use --- xmlModelReader.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xmlModelReader.cpp b/xmlModelReader.cpp index 011c6ca..234c27f 100644 --- a/xmlModelReader.cpp +++ b/xmlModelReader.cpp @@ -42,6 +42,8 @@ namespace vb01{ assetRootNode->getDescendants(descendants); setupDrivers(descendants); + delete doc; + return asset; } From 19990e48f4f790a90091bf902a01dde6597a4453 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Mon, 19 Aug 2024 18:23:09 +0300 Subject: [PATCH 2/5] using only the neeeded number of vertex positions --- box.cpp | 8 ++--- mesh.cpp | 22 +++++++------ meshData.cpp | 38 ++++++++++++---------- meshData.h | 36 ++++++++++++++++++--- model.cpp | 6 ++-- quad.cpp | 25 ++++++++------- rayCaster.cpp | 4 +-- xmlModelReader.cpp | 78 ++++++++++++++++++++++++++++++++-------------- 8 files changed, 145 insertions(+), 72 deletions(-) diff --git a/box.cpp b/box.cpp index f69613a..118cf2f 100755 --- a/box.cpp +++ b/box.cpp @@ -63,14 +63,14 @@ namespace vb01{ MeshData::Vertex *vertices = new MeshData::Vertex[3 * numTris]; for(int i = 0; i < 3 * numTris; i++){ - MeshData::Vertex v; - v.pos = pos[data[3 * i]]; - v.norm = norm[data[3 * i + 1]]; + MeshData::Vertex v; + v.pos = &pos[data[3 * i]]; + v.norm = &norm[data[3 * i + 1]]; v.uv = tex[data[3 * i + 2]]; vertices[i] = v; indices[i] = i; } - meshBase = MeshData(vertices, indices, numTris); + meshBase = MeshData((Vector3*)pos, nullptr, nullptr, nullptr, 8, (Vector3*)norm, vertices, indices, numTris); } } diff --git a/mesh.cpp b/mesh.cpp index c8c8a25..1ac93b4 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -44,31 +44,35 @@ namespace vb01{ glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); - u32 size = sizeof(MeshData::Vertex); + u32 size = sizeof(MeshData::OldVertex); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, 3 * meshBase.numTris * size, meshBase.vertices, GL_DYNAMIC_DRAW); + + MeshData::OldVertex *glVertData = meshBase.toGlVerts(); + glBufferData(GL_ARRAY_BUFFER, 3 * meshBase.numTris * size, glVertData, GL_DYNAMIC_DRAW); + delete glVertData; + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * meshBase.numTris * sizeof(u32), meshBase.indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, size, (void*)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, norm))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, norm))); glEnableVertexAttribArray(1); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, uv))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, uv))); glEnableVertexAttribArray(2); - glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, tan))); + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, tan))); glEnableVertexAttribArray(3); - glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, biTan))); + glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, biTan))); glEnableVertexAttribArray(4); - glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, weights))); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, weights))); glEnableVertexAttribArray(5); - glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, boneIndices))); + glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, boneIndices))); glEnableVertexAttribArray(6); for(int i = 0; i < meshBase.numShapeKeys; i++){ - glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::Vertex, shapeKeyOffsets) + 3 * i * sizeof(float))); + glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, shapeKeyOffsets) + 3 * i * sizeof(float))); glEnableVertexAttribArray(7 + i); } } diff --git a/meshData.cpp b/meshData.cpp index b5223c9..7ae627d 100644 --- a/meshData.cpp +++ b/meshData.cpp @@ -17,22 +17,28 @@ namespace vb01{ } } - MeshData::ShapeKey::ShapeKey(string name, float value, float minValue, float maxValue) : Animatable(Animatable::SHAPE_KEY, name){ - this->name = name; - this->value = value; - this->minValue = minValue; - this->maxValue = maxValue; - } + MeshData::OldVertex* MeshData::toGlVerts(){ + int numVertices = 3 * numTris; + OldVertex *vertData = new OldVertex[numVertices]; - MeshData::MeshData(Vertex *vertices, u32 *indices, int numTris, string attachableName, string *vertexGroups, int numVertexGroups, string fullSkeletonName, ShapeKey *shapeKeys, int numShapeKeys){ - this->vertices = vertices; - this->indices = indices; - this->numTris = numTris; - this->attachableName = attachableName; - this->vertexGroups = vertexGroups; - this->numVertexGroups = numVertexGroups; - this->fullSkeletonName = fullSkeletonName; - this->shapeKeys = shapeKeys; - this->numShapeKeys = numShapeKeys; + for(int i = 0; i < numVertices; i++){ + vertData[i].pos = *vertices[i].pos; + vertData[i].norm = *vertices[i].norm; + vertData[i].tan = vertices[i].tan; + vertData[i].biTan = vertices[i].biTan; + vertData[i].uv = vertices[i].uv; + + if(weights){ + for(int j = 0; j < 4; j++){ + vertData[i].weights[j] = vertices[i].weights[j]; + vertData[i].boneIndices[j] = vertices[i].boneIndices[j]; + } + + } + + //if(shapeKeyOffsets) for(int j = 0; j < 100; j++) vertData[i].shapeKeyOffsets[j] = vertices[i].shapeKeyOffsets[j]; + } + + return vertData; } } diff --git a/meshData.h b/meshData.h index 783f60d..a8738d8 100644 --- a/meshData.h +++ b/meshData.h @@ -14,12 +14,12 @@ namespace vb01{ std::string name; float minValue, value, maxValue; - ShapeKey(std::string, float, float, float); + ShapeKey(std::string n, float v, float mnv, float mxv) : name(n), value(v), minValue(mnv), maxValue(mxv), Animatable(Animatable::SHAPE_KEY, name){} ShapeKey() : Animatable(Animatable::SHAPE_KEY){} void animate(float, KeyframeChannel); }; - struct Vertex{ + struct OldVertex{ Vector3 pos, norm, tan, biTan; Vector2 uv; float weights[4]{0, 0, 0, 0}; @@ -27,15 +27,43 @@ namespace vb01{ Vector3 shapeKeyOffsets[100]; }; + struct Vertex{ + Vector3 *pos = nullptr, *norm = nullptr, tan, biTan; + Vector2 uv; + float *weights = nullptr; + int *boneIndices = nullptr; + Vector3 *shapeKeyOffsets = nullptr; + }; + + OldVertex* toGlVerts(); + + Vector3 *positions = nullptr, *normals = nullptr, **shapeKeyOffsets = nullptr; + float **weights = nullptr; + int **boneIndices = nullptr; Vertex *vertices; std::string *vertexGroups = nullptr; ShapeKey *shapeKeys = nullptr; u32 *indices, VAO, VBO, EBO; - int numTris, numVertexGroups = 0, numShapeKeys = 0; + int numPos, numTris, numVertexGroups = 0, numShapeKeys = 0; std::string attachableName = "", fullSkeletonName = ""; MeshData(){} - MeshData(Vertex*, u32*, int, std::string = "", std::string *vg = nullptr, int = 0, std::string = "", ShapeKey *sk = nullptr, int = 0); + MeshData(Vector3 *pos, float **w, int **bi, Vector3 **sko, int np, Vector3 *norm, Vertex *vert, u32 *ind, int nt, std::string an = "", std::string *vg = nullptr, int nvg = 0, std::string fsn = "", ShapeKey *sk = nullptr, int nsk = 0) : + positions(pos), + weights(w), + boneIndices(bi), + shapeKeyOffsets(sko), + numPos(np), + normals(norm), + vertices(vert), + indices(ind), + numTris(nt), + vertexGroups(vg), + numVertexGroups(nvg), + fullSkeletonName(fsn), + shapeKeys(sk), + numShapeKeys(nsk) + {} }; } diff --git a/model.cpp b/model.cpp index a98cf93..bb5dbd0 100755 --- a/model.cpp +++ b/model.cpp @@ -28,14 +28,14 @@ namespace vb01{ } Model::~Model(){ - delete children[0]->getMesh(0)->getMaterial(); - vector descendants; getDescendants(descendants); for(Node *node : descendants) - for(Mesh *mesh : node->getMeshes()) + for(Mesh *mesh : node->getMeshes()){ + delete mesh->getMaterial(); mesh->setMaterial(nullptr); + } while(!children.empty()){ Node *c = children[children.size() - 1]; diff --git a/quad.cpp b/quad.cpp index 3181479..96adad6 100755 --- a/quad.cpp +++ b/quad.cpp @@ -18,19 +18,22 @@ namespace vb01{ Vector2 subQuadSize = Vector2(size.x / numHorQuads, size.y / numVertQuads); MeshData::Vertex *vertices = new MeshData::Vertex[numVerts]; + Vector3 *faceVertPos = new Vector3[6]; + + Vector3 *norm = new Vector3(Vector3::VEC_J); for(int i = 0; i < numHorQuads; i++){ for(int j = 0; j < numVertQuads; j++){ int id = i * 6 * numHorQuads + j * 6; Vector2 offset = Vector2(subQuadSize.x, subQuadSize.y); - Vector3 faceVertPos[]{ - Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * i), - Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * (i + 1)), - Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * i), - Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * i), - Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * (i + 1)), - Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * (i + 1)) - }; + + faceVertPos[0] = Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * i); + faceVertPos[1] = Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * (i + 1)); + faceVertPos[2] = Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * i); + faceVertPos[3] = Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * i); + faceVertPos[4] = Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * (i + 1)); + faceVertPos[5] = Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * (i + 1)); + Vector2 faceVertUv[]{ Vector2(float(j) / numHorQuads, float(i) / numVertQuads), Vector2(float(j + 1) / numHorQuads, float(i + 1) / numVertQuads), @@ -48,10 +51,10 @@ namespace vb01{ } MeshData::Vertex v; - v.pos = faceVertPos[k]; + v.pos = &faceVertPos[k]; v.uv = faceVertUv[k]; v.tan = Vector3::VEC_I; - v.norm = Vector3::VEC_J; + v.norm = norm; v.biTan = Vector3::VEC_K; vertices[id + k] = v; } @@ -63,6 +66,6 @@ namespace vb01{ for(int i = 0; i < numVerts; i++) indices[i] = i; - meshBase = MeshData(vertices, indices, numTris); + meshBase = MeshData((Vector3*)faceVertPos, nullptr, nullptr, nullptr, 6, nullptr, vertices, indices, numTris); } } diff --git a/rayCaster.cpp b/rayCaster.cpp index 43e09df..dc6f6d2 100644 --- a/rayCaster.cpp +++ b/rayCaster.cpp @@ -42,7 +42,7 @@ namespace vb01{ bool skip = false; for(int j = 0; j < 3; j++){ - Vector3 rayPosToVert = (vertices[i * 3 + j].pos - rayPos); + Vector3 rayPosToVert = (*(vertices[i * 3 + j].pos) - rayPos); float angle = rayDir.getAngleBetween(rayPosToVert.norm()); if(angle > PI / 2) angle = PI - angle; @@ -58,7 +58,7 @@ namespace vb01{ if(skip) continue; } - Vector3 pointA = pos + rot * vertices[indices[i * 3]].pos, pointB = pos + rot * vertices[indices[i * 3 + 1]].pos, pointC = pos + rot * vertices[indices[i * 3 + 2]].pos; + Vector3 pointA = pos + rot * *(vertices[indices[i * 3]].pos), pointB = pos + rot * *(vertices[indices[i * 3 + 1]].pos), pointC = pos + rot * *(vertices[indices[i * 3 + 2]].pos); Vector3 hypVec = pointA - rayPos; Vector3 perpVec = (pointB - pointA).cross(pointC - pointA).norm(); float a1 = hypVec.norm().getAngleBetween(perpVec); diff --git a/xmlModelReader.cpp b/xmlModelReader.cpp index 234c27f..8c2c879 100644 --- a/xmlModelReader.cpp +++ b/xmlModelReader.cpp @@ -59,44 +59,61 @@ namespace vb01{ } } + //TODO remove the 4 literal for bone weights and indices Mesh* XmlModelReader::processMesh(XMLElement *meshEl){ - vector vertPos, vertNorm; - vector weights; + int numVertPos = 0; + vector vp; + vector w; const char *tagName = "vertdata"; - for(XMLElement *vertEl = meshEl->FirstChildElement(tagName); vertEl && strcmp(vertEl->Name(), tagName) == 0; vertEl = vertEl->NextSiblingElement()){ + for(XMLElement *vertEl = meshEl->FirstChildElement(tagName); vertEl && strcmp(vertEl->Name(), tagName) == 0; vertEl = vertEl->NextSiblingElement(), numVertPos++){ float posX = atof(vertEl->Attribute("px")); float posY = atof(vertEl->Attribute("py")); float posZ = atof(vertEl->Attribute("pz")); - vertPos.push_back(Vector3(posX, posY, posZ)); - - float normX = atof(vertEl->Attribute("nx")); - float normY = atof(vertEl->Attribute("ny")); - float normZ = atof(vertEl->Attribute("nz")); - vertNorm.push_back(Vector3(normX, normY, normZ)); + vp.push_back(Vector3(posX, posY, posZ)); for(XMLElement *weightEl = vertEl->FirstChildElement("weight"); weightEl; weightEl = weightEl->NextSiblingElement()) - weights.push_back(atof(weightEl->Attribute("value"))); + w.push_back(atof(weightEl->Attribute("value"))); } int numVertexGroups = atoi(meshEl->Attribute("num_vertex_groups")); + + Vector3 *vertPos = new Vector3[numVertPos]; + float **weights = new float*[numVertPos]; + int **boneIndices = new int*[numVertPos]; + + for(int i = 0; i < vp.size(); i++){ + vertPos[i] = vp[i]; + weights[i] = new float[4]; + boneIndices[i] = new int[4]; + + for(int j = 0; j < 4; j++){ + weights[i][j] = 0; + //weights[i][j] = w[i * numVertexGroups + j]; + boneIndices[i][j] = -1; + } + } + + vp.clear(); + w.clear(); + string *vertexGroups = new string[numVertexGroups]; tagName = "vertexgroup"; int i = 0; for(XMLElement *vertGroupEl = meshEl->FirstChildElement(tagName); vertGroupEl && strcmp(vertGroupEl->Name(), tagName) == 0; vertGroupEl = vertGroupEl->NextSiblingElement(), i++) - vertexGroups[i] = (vertGroupEl->Attribute("name")); + vertexGroups[i] = (vertGroupEl->Attribute("name")); i = 0; int numVertices = 3 * atoi(meshEl->Attribute("num_faces")); MeshData::Vertex *vertices = new MeshData::Vertex[numVertices]; tagName = "vert"; - vector vertIds; u32 *indices = new u32[numVertices]; + Vector3 *normals = new Vector3[numVertices]; + for(XMLElement *vertEl = meshEl->FirstChildElement(tagName); vertEl && strcmp(vertEl->Name(), tagName) == 0; vertEl = vertEl->NextSiblingElement(), i++){ int id = atoi(vertEl->Attribute("id")); - vertIds.push_back(id); float uvX = atof(vertEl->Attribute("uvx")); float uvY = atof(vertEl->Attribute("uvy")); @@ -112,30 +129,39 @@ namespace vb01{ float biTanZ = atof(vertEl->Attribute("bz")); Vector3 biTan = Vector3(biTanX, biTanY, biTanZ); + normals[i] = tan.cross(biTan); + MeshData::Vertex vertex; - vertex.pos = vertPos[id]; - vertex.norm = vertNorm[id]; + vertex.pos = &vertPos[id]; + vertex.norm = &normals[id]; vertex.uv = uv; vertex.tan = tan; vertex.biTan = biTan; for(int j = 0, boneIndex = 0; j < numVertexGroups; j++){ - if(weights[id * numVertexGroups + j] > 0){ - vertex.boneIndices[boneIndex] = j; - vertex.weights[boneIndex] = weights[id * numVertexGroups + j]; + if(weights[id][j] > 0){ + boneIndices[id][boneIndex] = j; + //weights[id][boneIndex] = weights[id * numVertexGroups + j]; boneIndex++; } } + vertex.weights = weights[id]; + vertex.boneIndices = boneIndices[id]; + indices[i] = i; vertices[i] = vertex; } - vertNorm.clear(); tagName = "shapekey"; i = 0; int numShapeKeys = atoi(meshEl->Attribute("num_shape_keys")); MeshData::ShapeKey *shapeKeys = new MeshData::ShapeKey[numShapeKeys]; + Vector3 **shapeKeyOffsets = new Vector3*[numVertPos]; + + for(int i = 0; i < numVertPos; i++){ + shapeKeyOffsets[i] = new Vector3[100]; + } for(XMLElement *shapeKeyEl = meshEl->FirstChildElement(tagName); shapeKeyEl && strcmp(shapeKeyEl->Name(), tagName) == 0; shapeKeyEl = shapeKeyEl->NextSiblingElement(), i++){ string name = shapeKeyEl->Attribute("name"); @@ -159,13 +185,19 @@ namespace vb01{ shapeKeyPos.push_back(Vector3(posX, posY, posZ)); } - for(int j = 0; j < numVertices; j++) - vertices[j].shapeKeyOffsets[i] = shapeKeyPos[vertIds[j]] - vertPos[vertIds[j]]; + /* + for(int j = 0; j < numVertPos; j++){ + shapeKeyOffsets[i] = new Vector3[100]; + + for(int k = 0; k < 100; k++) + shapeKeyOffsets[i][k] = shapeKeyPos[vertIds[j]] - vertPos[vertIds[j]]; + } + */ } const char *fullSkeletonName = meshEl->Attribute("skeleton"); string name = string(meshEl->Parent()->ToElement()->Attribute("name")) + string(meshEl->Attribute("name")); - Mesh *mesh = new Mesh(MeshData(vertices, indices, numVertices / 3, name, vertexGroups, numVertexGroups, (fullSkeletonName ? fullSkeletonName : ""), shapeKeys, numShapeKeys)); + Mesh *mesh = new Mesh(MeshData(vertPos, weights, boneIndices, shapeKeyOffsets, numVertPos, normals, vertices, indices, numVertices / 3, name, vertexGroups, numVertexGroups, (fullSkeletonName ? fullSkeletonName : ""), shapeKeys, numShapeKeys)); return mesh; } @@ -293,7 +325,7 @@ namespace vb01{ XMLElement *meshTag = xmlEl->FirstChildElement("mesh"); if(meshTag) - node->attachMesh(processMesh(meshTag)); + node->attachMesh(processMesh(meshTag)); XMLElement *lightTag = xmlEl->FirstChildElement("light"); From 36f15b6dd944705e4e455c14a863e68e00d03d79 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 1 Sep 2024 16:13:53 +0300 Subject: [PATCH 3/5] improved model parsing --- box.cpp | 38 +- meshData.cpp | 5 +- meshData.h | 2 +- samples/models/kek.xml | 2068 ++++++++++++++++++++-------------------- script.py | 2 + xmlModelReader.cpp | 131 ++- 6 files changed, 1119 insertions(+), 1127 deletions(-) diff --git a/box.cpp b/box.cpp index 118cf2f..671656b 100755 --- a/box.cpp +++ b/box.cpp @@ -9,27 +9,23 @@ namespace vb01{ void Box::setSize(Vector3 size){ this->size = size; - Vector3 pos[] = { - Vector3(size.x / 2, size.y / 2, size.z / 2), - Vector3(-size.x / 2, size.y / 2, size.z / 2), - Vector3(-size.x / 2, size.y / 2, -size.z / 2), - Vector3(size.x / 2, size.y / 2, -size.z / 2), + Vector3 *pos = new Vector3[8]; + pos[0] = Vector3(size.x / 2, size.y / 2, size.z / 2); + pos[1] = Vector3(-size.x / 2, size.y / 2, size.z / 2); + pos[2] = Vector3(-size.x / 2, size.y / 2, -size.z / 2); + pos[3] = Vector3(size.x / 2, size.y / 2, -size.z / 2); + pos[4] = Vector3(size.x / 2, -size.y / 2, size.z / 2); + pos[5] = Vector3(-size.x / 2, -size.y / 2, size.z / 2); + pos[6] = Vector3(-size.x / 2, -size.y / 2, -size.z / 2); + pos[7] = Vector3(size.x / 2, -size.y / 2, -size.z / 2); - Vector3(size.x / 2, -size.y / 2, size.z / 2), - Vector3(-size.x / 2, -size.y / 2, size.z / 2), - Vector3(-size.x / 2, -size.y / 2, -size.z / 2), - Vector3(size.x / 2, -size.y / 2, -size.z / 2) - }; - - Vector3 norm[] = { - Vector3(0, 0, 1), - Vector3(0, 1, 0), - Vector3(1, 0, 0), - - Vector3(0, 0, -1), - Vector3(0, -1, 0), - Vector3(-1, 0, 0) - }; + Vector3 *norm = new Vector3[6]; + norm[0] = Vector3(0, 0, 1); + norm[1] = Vector3(0, 1, 0); + norm[2] = Vector3(1, 0, 0); + norm[3] = Vector3(0, 0, -1); + norm[4] = Vector3(0, -1, 0); + norm[5] = Vector3(-1, 0, 0); Vector2 tex[] = { Vector2(1, 1), @@ -71,6 +67,6 @@ namespace vb01{ indices[i] = i; } - meshBase = MeshData((Vector3*)pos, nullptr, nullptr, nullptr, 8, (Vector3*)norm, vertices, indices, numTris); + meshBase = MeshData(pos, nullptr, nullptr, nullptr, 8, norm, vertices, indices, numTris); } } diff --git a/meshData.cpp b/meshData.cpp index 7ae627d..f3d17bb 100644 --- a/meshData.cpp +++ b/meshData.cpp @@ -33,10 +33,11 @@ namespace vb01{ vertData[i].weights[j] = vertices[i].weights[j]; vertData[i].boneIndices[j] = vertices[i].boneIndices[j]; } - } - //if(shapeKeyOffsets) for(int j = 0; j < 100; j++) vertData[i].shapeKeyOffsets[j] = vertices[i].shapeKeyOffsets[j]; + if(shapeKeyOffsets) + for(int j = 0; j < 100; j++) + vertData[i].shapeKeyOffsets[j] = vertices[i].shapeKeyOffsets[j]; } return vertData; diff --git a/meshData.h b/meshData.h index a8738d8..568047f 100644 --- a/meshData.h +++ b/meshData.h @@ -14,7 +14,7 @@ namespace vb01{ std::string name; float minValue, value, maxValue; - ShapeKey(std::string n, float v, float mnv, float mxv) : name(n), value(v), minValue(mnv), maxValue(mxv), Animatable(Animatable::SHAPE_KEY, name){} + ShapeKey(std::string n, float v, float mnv, float mxv) : name(n), value(v), minValue(mnv), maxValue(mxv), Animatable(Animatable::SHAPE_KEY, n){} ShapeKey() : Animatable(Animatable::SHAPE_KEY){} void animate(float, KeyframeChannel); }; diff --git a/samples/models/kek.xml b/samples/models/kek.xml index 25080a7..a510627 100644 --- a/samples/models/kek.xml +++ b/samples/models/kek.xml @@ -1,6 +1,6 @@ - - + + @@ -144,9 +144,9 @@ - - - + + + @@ -162,7 +162,7 @@ - + @@ -178,7 +178,7 @@ - + @@ -194,7 +194,7 @@ - + @@ -210,7 +210,7 @@ - + @@ -226,7 +226,7 @@ - + @@ -242,7 +242,7 @@ - + @@ -258,7 +258,7 @@ - + @@ -274,7 +274,7 @@ - + @@ -290,7 +290,7 @@ - + @@ -306,7 +306,7 @@ - + @@ -322,7 +322,7 @@ - + @@ -338,7 +338,7 @@ - + @@ -354,7 +354,7 @@ - + @@ -370,7 +370,7 @@ - + @@ -386,7 +386,7 @@ - + @@ -402,7 +402,7 @@ - + @@ -418,7 +418,7 @@ - + @@ -434,7 +434,7 @@ - + @@ -450,7 +450,7 @@ - + @@ -466,7 +466,7 @@ - + @@ -482,7 +482,7 @@ - + @@ -498,7 +498,7 @@ - + @@ -514,7 +514,7 @@ - + @@ -530,7 +530,7 @@ - + @@ -546,7 +546,7 @@ - + @@ -562,7 +562,7 @@ - + @@ -578,7 +578,7 @@ - + @@ -594,7 +594,7 @@ - + @@ -610,7 +610,7 @@ - + @@ -626,7 +626,7 @@ - + @@ -642,7 +642,7 @@ - + @@ -658,7 +658,7 @@ - + @@ -674,7 +674,7 @@ - + @@ -690,7 +690,7 @@ - + @@ -706,7 +706,7 @@ - + @@ -722,7 +722,7 @@ - + @@ -738,7 +738,7 @@ - + @@ -754,7 +754,7 @@ - + @@ -770,7 +770,7 @@ - + @@ -786,7 +786,7 @@ - + @@ -802,7 +802,7 @@ - + @@ -818,7 +818,7 @@ - + @@ -834,7 +834,7 @@ - + @@ -850,7 +850,7 @@ - + @@ -866,7 +866,7 @@ - + @@ -882,7 +882,7 @@ - + @@ -898,7 +898,7 @@ - + @@ -914,7 +914,7 @@ - + @@ -930,7 +930,7 @@ - + @@ -946,7 +946,7 @@ - + @@ -962,7 +962,7 @@ - + @@ -978,7 +978,7 @@ - + @@ -994,7 +994,7 @@ - + @@ -1010,7 +1010,7 @@ - + @@ -1026,7 +1026,7 @@ - + @@ -1042,7 +1042,7 @@ - + @@ -1058,7 +1058,7 @@ - + @@ -1074,7 +1074,7 @@ - + @@ -1090,7 +1090,7 @@ - + @@ -1106,7 +1106,7 @@ - + @@ -1122,7 +1122,7 @@ - + @@ -1138,7 +1138,7 @@ - + @@ -1154,7 +1154,7 @@ - + @@ -1170,7 +1170,7 @@ - + @@ -1186,7 +1186,7 @@ - + @@ -1202,7 +1202,7 @@ - + @@ -1218,7 +1218,7 @@ - + @@ -1234,7 +1234,7 @@ - + @@ -1250,7 +1250,7 @@ - + @@ -1266,7 +1266,7 @@ - + @@ -1282,7 +1282,7 @@ - + @@ -1298,7 +1298,7 @@ - + @@ -1314,7 +1314,7 @@ - + @@ -1330,7 +1330,7 @@ - + @@ -1346,7 +1346,7 @@ - + @@ -1362,7 +1362,7 @@ - + @@ -1378,7 +1378,7 @@ - + @@ -1394,7 +1394,7 @@ - + @@ -1410,7 +1410,7 @@ - + @@ -1426,7 +1426,7 @@ - + @@ -1442,7 +1442,7 @@ - + @@ -1458,7 +1458,7 @@ - + @@ -1474,7 +1474,7 @@ - + @@ -1490,7 +1490,7 @@ - + @@ -1506,7 +1506,7 @@ - + @@ -1522,7 +1522,7 @@ - + @@ -1538,7 +1538,7 @@ - + @@ -1554,7 +1554,7 @@ - + @@ -1570,7 +1570,7 @@ - + @@ -1586,7 +1586,7 @@ - + @@ -1602,7 +1602,7 @@ - + @@ -1618,7 +1618,7 @@ - + @@ -1634,7 +1634,7 @@ - + @@ -1650,7 +1650,7 @@ - + @@ -1666,7 +1666,7 @@ - + @@ -1682,7 +1682,7 @@ - + @@ -1698,7 +1698,7 @@ - + @@ -1714,7 +1714,7 @@ - + @@ -1730,7 +1730,7 @@ - + @@ -1746,7 +1746,7 @@ - + @@ -1762,7 +1762,7 @@ - + @@ -1778,7 +1778,7 @@ - + @@ -1794,7 +1794,7 @@ - + @@ -1810,7 +1810,7 @@ - + @@ -1826,7 +1826,7 @@ - + @@ -1842,7 +1842,7 @@ - + @@ -1858,7 +1858,7 @@ - + @@ -1874,7 +1874,7 @@ - + @@ -1890,7 +1890,7 @@ - + @@ -1906,7 +1906,7 @@ - + @@ -1922,7 +1922,7 @@ - + @@ -1938,7 +1938,7 @@ - + @@ -1954,7 +1954,7 @@ - + @@ -1970,7 +1970,7 @@ - + @@ -1986,7 +1986,7 @@ - + @@ -2002,7 +2002,7 @@ - + @@ -2018,7 +2018,7 @@ - + @@ -2034,7 +2034,7 @@ - + @@ -2050,7 +2050,7 @@ - + @@ -2066,7 +2066,7 @@ - + @@ -2082,7 +2082,7 @@ - + @@ -2098,7 +2098,7 @@ - + @@ -2114,7 +2114,7 @@ - + @@ -2130,7 +2130,7 @@ - + @@ -2146,7 +2146,7 @@ - + @@ -2162,7 +2162,7 @@ - + @@ -2178,7 +2178,7 @@ - + @@ -2194,7 +2194,7 @@ - + @@ -2210,7 +2210,7 @@ - + @@ -2226,7 +2226,7 @@ - + @@ -2242,7 +2242,7 @@ - + @@ -2258,7 +2258,7 @@ - + @@ -2274,7 +2274,7 @@ - + @@ -2290,7 +2290,7 @@ - + @@ -2306,7 +2306,7 @@ - + @@ -2322,7 +2322,7 @@ - + @@ -2338,7 +2338,7 @@ - + @@ -2354,7 +2354,7 @@ - + @@ -2370,7 +2370,7 @@ - + @@ -2386,7 +2386,7 @@ - + @@ -2402,7 +2402,7 @@ - + @@ -2418,7 +2418,7 @@ - + @@ -2434,7 +2434,7 @@ - + @@ -2450,7 +2450,7 @@ - + @@ -2466,7 +2466,7 @@ - + @@ -2482,7 +2482,7 @@ - + @@ -2498,7 +2498,7 @@ - + @@ -2514,7 +2514,7 @@ - + @@ -2530,7 +2530,7 @@ - + @@ -2546,7 +2546,7 @@ - + @@ -2562,7 +2562,7 @@ - + @@ -2578,7 +2578,7 @@ - + @@ -2594,7 +2594,7 @@ - + @@ -2610,7 +2610,7 @@ - + @@ -2626,7 +2626,7 @@ - + @@ -2642,7 +2642,7 @@ - + @@ -2658,7 +2658,7 @@ - + @@ -2674,7 +2674,7 @@ - + @@ -2690,7 +2690,7 @@ - + @@ -2706,7 +2706,7 @@ - + @@ -2722,7 +2722,7 @@ - + @@ -2738,7 +2738,7 @@ - + @@ -2754,7 +2754,7 @@ - + @@ -2770,7 +2770,7 @@ - + @@ -2786,7 +2786,7 @@ - + @@ -2802,7 +2802,7 @@ - + @@ -2818,7 +2818,7 @@ - + @@ -2834,7 +2834,7 @@ - + @@ -2850,7 +2850,7 @@ - + @@ -2866,7 +2866,7 @@ - + @@ -2882,7 +2882,7 @@ - + @@ -2898,7 +2898,7 @@ - + @@ -2914,7 +2914,7 @@ - + @@ -2930,7 +2930,7 @@ - + @@ -2946,7 +2946,7 @@ - + @@ -2962,7 +2962,7 @@ - + @@ -2978,7 +2978,7 @@ - + @@ -2994,7 +2994,7 @@ - + @@ -3010,7 +3010,7 @@ - + @@ -3026,7 +3026,7 @@ - + @@ -3042,7 +3042,7 @@ - + @@ -3058,7 +3058,7 @@ - + @@ -3074,7 +3074,7 @@ - + @@ -3090,7 +3090,7 @@ - + @@ -3106,7 +3106,7 @@ - + @@ -3122,7 +3122,7 @@ - + @@ -3138,7 +3138,7 @@ - + @@ -3154,7 +3154,7 @@ - + @@ -3170,7 +3170,7 @@ - + @@ -3186,7 +3186,7 @@ - + @@ -3202,7 +3202,7 @@ - + @@ -3218,7 +3218,7 @@ - + @@ -3234,7 +3234,7 @@ - + @@ -3250,7 +3250,7 @@ - + @@ -3266,7 +3266,7 @@ - + @@ -3282,7 +3282,7 @@ - + @@ -3298,7 +3298,7 @@ - + @@ -3314,7 +3314,7 @@ - + @@ -3330,7 +3330,7 @@ - + @@ -3346,7 +3346,7 @@ - + @@ -3362,7 +3362,7 @@ - + @@ -3378,7 +3378,7 @@ - + @@ -3394,7 +3394,7 @@ - + @@ -3410,7 +3410,7 @@ - + @@ -3426,7 +3426,7 @@ - + @@ -3442,7 +3442,7 @@ - + @@ -3458,7 +3458,7 @@ - + @@ -3474,7 +3474,7 @@ - + @@ -3490,7 +3490,7 @@ - + @@ -3506,7 +3506,7 @@ - + @@ -3522,7 +3522,7 @@ - + @@ -3538,7 +3538,7 @@ - + @@ -3554,7 +3554,7 @@ - + @@ -3570,7 +3570,7 @@ - + @@ -3586,7 +3586,7 @@ - + @@ -3602,7 +3602,7 @@ - + @@ -3618,7 +3618,7 @@ - + @@ -3634,7 +3634,7 @@ - + @@ -3650,7 +3650,7 @@ - + @@ -3666,7 +3666,7 @@ - + @@ -3682,7 +3682,7 @@ - + @@ -3698,7 +3698,7 @@ - + @@ -3714,7 +3714,7 @@ - + @@ -3730,7 +3730,7 @@ - + @@ -3746,7 +3746,7 @@ - + @@ -3762,7 +3762,7 @@ - + @@ -3778,7 +3778,7 @@ - + @@ -3794,7 +3794,7 @@ - + @@ -3810,7 +3810,7 @@ - + @@ -3826,7 +3826,7 @@ - + @@ -3842,7 +3842,7 @@ - + @@ -3858,7 +3858,7 @@ - + @@ -3874,7 +3874,7 @@ - + @@ -3890,7 +3890,7 @@ - + @@ -3906,7 +3906,7 @@ - + @@ -3922,7 +3922,7 @@ - + @@ -3938,7 +3938,7 @@ - + @@ -3954,7 +3954,7 @@ - + @@ -3970,7 +3970,7 @@ - + @@ -3986,7 +3986,7 @@ - + @@ -4002,7 +4002,7 @@ - + @@ -4018,7 +4018,7 @@ - + @@ -4034,7 +4034,7 @@ - + @@ -4050,7 +4050,7 @@ - + @@ -4066,7 +4066,7 @@ - + @@ -4082,7 +4082,7 @@ - + @@ -4098,7 +4098,7 @@ - + @@ -4114,7 +4114,7 @@ - + @@ -4130,7 +4130,7 @@ - + @@ -4146,7 +4146,7 @@ - + @@ -4162,7 +4162,7 @@ - + @@ -4178,7 +4178,7 @@ - + @@ -4194,7 +4194,7 @@ - + @@ -4210,7 +4210,7 @@ - + @@ -4226,7 +4226,7 @@ - + @@ -4256,61 +4256,61 @@ - - + + - - + + - - + + - - + + - + - - + + - - - + + + - - - + + + - - - - - + + + + + - - + + - + - + - + @@ -4325,311 +4325,311 @@ - - + + - + - + - - + + - + - + - + - + - - + + - + - - + + - + - + - - + + - + - - - - + + + + - - + + - + - - - - - - + + + + + + - - + + - - + + - - - + + + - - - - - - - - - - - + + + + + + + + + + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - - - + + + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - + + - - + + - - - - + + + + - - + + - + - - + + - - + + - - + + - + - + - - + + - - + + - - - - + + + + - + - - + + - - + + - + - - + + - - + + - - + + - - - - - + + + + + - - + + - + - - + + - + - - + + - - - - - + + + + + - - - - - + + + + + - - + + - + - - - - - + + + + + - + - + - + - + - + - - + + - + - - + + - + - + @@ -4642,18 +4642,18 @@ - - - + + + - - + + - + - + @@ -4679,8 +4679,8 @@ - - + + @@ -4688,351 +4688,351 @@ - - + + - - + + - - + + - + - + - + - + - - + + - - + + - + - + - - + + - + - - - - + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - - - - - - - - + + + + + + + + + - - - - - + + + + + - - + + - - - - - - - - + + + + + + + + - - + + - + - + - - + + - - + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - + - - + + - - + + - - - - - - - - + + + + + + + + - - + + - - + + - - + + - + - + - + - + - + - - + + - - + + - + - - - - + + + + - - + + - + - - + + - - + + - - + + - - + + - - + + - - - - + + + + - - + + - + - - + + - - + + - - + + - - + + - + - - - - + + + + - + - - + + - - - - + + + + - - + + - + - - - - + + + + - + - - + + - - + + - - + + - + - + - - + + - + - + - + - + - + - - - - - + + + + + - + - - - - + + + + - - + + - - + + - + @@ -5041,8 +5041,8 @@ - - + + @@ -5057,312 +5057,312 @@ - + - - + + - + - + - - + + - + - + - + - + - - + + - + - - + + - + - + - + - - - + + + - + - + - - + + - - + + - - - - - + + + + + - + - + - - - - + + + + - - - - - + + + + + - - - - - + + + + + - - + + - - - - - - - - + + + + + + + + - - - - + + + + - - - - - + + + + + - - + + - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - + - - + + - + - - + + - - + + - - + + - + - + - + - + - + - + - + - - + + - + - + - - - + + + - - + + - + - + - + - + - + - - + + - - + + - + - + - + - - - + + + - + - + - + - - + + - - + + - - + + - - - - - + + + + + - + - - + + - - - - + + + + - + - + - - + + - - + + - + - + - + - + - + - + @@ -5375,18 +5375,18 @@ - + - - + + - - - + + + - + @@ -5411,315 +5411,315 @@ - + - + - + - - + + - - + + - + - + - + - + - + - - + + - - + + - + - + - + - - - + + + - + - - - + + + - + - - - - + + + + - - + + - - + + - - - - - - - - - - - - - - + + + + + + + + + + + + + + - - - - - + + + + + - - + + - - + + - - - - - - - - - - - + + + + + + + + + + + - - + + - - + + - - - - - + + + + + - - + + - + - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + - - + + - - + + - - + + - + - - + + - + - + - + - + - + - + - - + + - - + + - - + + - - - + + + - + - - + + - - + + - + - - - + + + - - + + - + - - + + - + - - + + - - + + - + - + - - + + - - + + - + - - + + - + - + - - - + + + - + - - + + - - - - - + + + + + - - + + - + - - + + - + - - + + - + - + - + - + @@ -6251,4 +6251,4 @@ - \ No newline at end of file + diff --git a/script.py b/script.py index 5af0a9a..a7a85e7 100644 --- a/script.py +++ b/script.py @@ -166,6 +166,7 @@ def export(node, parentTag): if node.type == 'MESH': mesh = node.data + numVertexPos = len(mesh.vertices) numFaces = len(mesh.polygons) numVerts = len(mesh.vertices) numGroups = len(node.vertex_groups) @@ -176,6 +177,7 @@ def export(node, parentTag): meshTag = ET.SubElement(nodeTag, 'mesh') meshTag.set('name', node.name + mesh.name_full) + meshTag.set('num_vertex_pos', str(numVertexPos)) meshTag.set('num_faces', str(numFaces)) meshTag.set('num_vertex_groups', str(numGroups)) meshTag.set('num_shape_keys', str(numShapeKeys)) diff --git a/xmlModelReader.cpp b/xmlModelReader.cpp index 8c2c879..25eeb38 100644 --- a/xmlModelReader.cpp +++ b/xmlModelReader.cpp @@ -61,48 +61,49 @@ namespace vb01{ //TODO remove the 4 literal for bone weights and indices Mesh* XmlModelReader::processMesh(XMLElement *meshEl){ - int numVertPos = 0; - vector vp; - vector w; - const char *tagName = "vertdata"; - - for(XMLElement *vertEl = meshEl->FirstChildElement(tagName); vertEl && strcmp(vertEl->Name(), tagName) == 0; vertEl = vertEl->NextSiblingElement(), numVertPos++){ - float posX = atof(vertEl->Attribute("px")); - float posY = atof(vertEl->Attribute("py")); - float posZ = atof(vertEl->Attribute("pz")); - vp.push_back(Vector3(posX, posY, posZ)); - - for(XMLElement *weightEl = vertEl->FirstChildElement("weight"); weightEl; weightEl = weightEl->NextSiblingElement()) - w.push_back(atof(weightEl->Attribute("value"))); - } - + int numVertPos = atoi(meshEl->Attribute("num_vertex_pos")); int numVertexGroups = atoi(meshEl->Attribute("num_vertex_groups")); + const char *tagName = "vertdata"; + Vector3 *vertPos = new Vector3[numVertPos]; float **weights = new float*[numVertPos]; - int **boneIndices = new int*[numVertPos]; + int **boneIndices = new int*[numVertPos], i = 0; - for(int i = 0; i < vp.size(); i++){ - vertPos[i] = vp[i]; - weights[i] = new float[4]; - boneIndices[i] = new int[4]; + for(XMLElement *vertEl = meshEl->FirstChildElement(tagName); vertEl && strcmp(vertEl->Name(), tagName) == 0, i < numVertPos; vertEl = vertEl->NextSiblingElement(), i++){ + float posX = atof(vertEl->Attribute("px")); + float posY = atof(vertEl->Attribute("py")); + float posZ = atof(vertEl->Attribute("pz")); + vertPos[i] = Vector3(posX, posY, posZ); - for(int j = 0; j < 4; j++){ - weights[i][j] = 0; - //weights[i][j] = w[i * numVertexGroups + j]; - boneIndices[i][j] = -1; - } + weights[i] = new float[4]; + boneIndices[i] = new int[4]; + + for(int j = 0; j < 4; j++){ + weights[i][j] = 0; + boneIndices[i][j] = -1; + } + + int boneIndex = 0, j = 0; + + for(XMLElement *weightEl = vertEl->FirstChildElement("weight"); weightEl, j < numVertexGroups; weightEl = weightEl->NextSiblingElement(), j++){ + float w = atof(weightEl->Attribute("value")); + + if(w > 0){ + weights[i][boneIndex] = w; + boneIndices[i][boneIndex] = j; + boneIndex++; + } + } } - vp.clear(); - w.clear(); - string *vertexGroups = new string[numVertexGroups]; tagName = "vertexgroup"; - int i = 0; + i = 0; - for(XMLElement *vertGroupEl = meshEl->FirstChildElement(tagName); vertGroupEl && strcmp(vertGroupEl->Name(), tagName) == 0; vertGroupEl = vertGroupEl->NextSiblingElement(), i++) - vertexGroups[i] = (vertGroupEl->Attribute("name")); + if(numVertexGroups > 0) + for(XMLElement *vertGroupEl = meshEl->FirstChildElement(tagName); vertGroupEl && strcmp(vertGroupEl->Name(), tagName) == 0 && i < numVertexGroups; vertGroupEl = vertGroupEl->NextSiblingElement(), i++) + vertexGroups[i] = vertGroupEl->Attribute("name"); i = 0; int numVertices = 3 * atoi(meshEl->Attribute("num_faces")); @@ -111,9 +112,11 @@ namespace vb01{ u32 *indices = new u32[numVertices]; Vector3 *normals = new Vector3[numVertices]; + vector vertIds; for(XMLElement *vertEl = meshEl->FirstChildElement(tagName); vertEl && strcmp(vertEl->Name(), tagName) == 0; vertEl = vertEl->NextSiblingElement(), i++){ int id = atoi(vertEl->Attribute("id")); + vertIds.push_back(id); float uvX = atof(vertEl->Attribute("uvx")); float uvY = atof(vertEl->Attribute("uvy")); @@ -138,14 +141,6 @@ namespace vb01{ vertex.tan = tan; vertex.biTan = biTan; - for(int j = 0, boneIndex = 0; j < numVertexGroups; j++){ - if(weights[id][j] > 0){ - boneIndices[id][boneIndex] = j; - //weights[id][boneIndex] = weights[id * numVertexGroups + j]; - boneIndex++; - } - } - vertex.weights = weights[id]; vertex.boneIndices = boneIndices[id]; @@ -156,43 +151,41 @@ namespace vb01{ tagName = "shapekey"; i = 0; int numShapeKeys = atoi(meshEl->Attribute("num_shape_keys")); - MeshData::ShapeKey *shapeKeys = new MeshData::ShapeKey[numShapeKeys]; - Vector3 **shapeKeyOffsets = new Vector3*[numVertPos]; + MeshData::ShapeKey *shapeKeys = nullptr; + Vector3 **shapeKeyOffsets = nullptr; - for(int i = 0; i < numVertPos; i++){ - shapeKeyOffsets[i] = new Vector3[100]; - } + if(numShapeKeys > 0){ + shapeKeys = new MeshData::ShapeKey[numShapeKeys]; + shapeKeyOffsets = new Vector3*[numVertPos]; - for(XMLElement *shapeKeyEl = meshEl->FirstChildElement(tagName); shapeKeyEl && strcmp(shapeKeyEl->Name(), tagName) == 0; shapeKeyEl = shapeKeyEl->NextSiblingElement(), i++){ - string name = shapeKeyEl->Attribute("name"); - float minValue = atof(shapeKeyEl->Attribute("min")); - float maxValue = atof(shapeKeyEl->Attribute("max")); - shapeKeys[i] = MeshData::ShapeKey(name, minValue, minValue, maxValue); + for(int i = 0; i < numVertPos; i++) + shapeKeyOffsets[i] = new Vector3[100]; - XMLElement *driverEl = shapeKeyEl->FirstChildElement("driver"); - KeyframeChannel channel = processKeyframeChannells(driverEl)[0]; - const char *nodeName = driverEl->Attribute("obj"); - const char *boneName = driverEl->Attribute("bone"); - Driver::VariableType type = Driver::getDriverVariableType(driverEl->Attribute("type")); - driversByNodeNames.push_back(make_pair(string(nodeName) + (boneName ? "." + string(boneName) : ""), new Driver(&shapeKeys[i], channel, type))); + for(XMLElement *shapeKeyEl = meshEl->FirstChildElement(tagName); shapeKeyEl && strcmp(shapeKeyEl->Name(), tagName) == 0 && i < numShapeKeys; shapeKeyEl = shapeKeyEl->NextSiblingElement(), i++){ + string name = shapeKeyEl->Attribute("name"); + float minValue = atof(shapeKeyEl->Attribute("min")); + float maxValue = atof(shapeKeyEl->Attribute("max")); + shapeKeys[i] = MeshData::ShapeKey(name, minValue, minValue, maxValue); - vector shapeKeyPos; + XMLElement *driverEl = shapeKeyEl->FirstChildElement("driver"); + KeyframeChannel channel = processKeyframeChannells(driverEl)[0]; + const char *nodeName = driverEl->Attribute("obj"); + const char *boneName = driverEl->Attribute("bone"); + Driver::VariableType type = Driver::getDriverVariableType(driverEl->Attribute("type")); + driversByNodeNames.push_back(make_pair(string(nodeName) + (boneName ? "." + string(boneName) : ""), new Driver(&shapeKeys[i], channel, type))); - for(XMLElement *vertEl = shapeKeyEl->FirstChildElement("vert"); vertEl; vertEl = vertEl->NextSiblingElement()){ - float posX = atof(vertEl->Attribute("px")); - float posY = atof(vertEl->Attribute("py")); - float posZ = atof(vertEl->Attribute("pz")); - shapeKeyPos.push_back(Vector3(posX, posY, posZ)); - } + int j = 0; - /* - for(int j = 0; j < numVertPos; j++){ - shapeKeyOffsets[i] = new Vector3[100]; + for(XMLElement *vertEl = shapeKeyEl->FirstChildElement("vert"); vertEl; vertEl = vertEl->NextSiblingElement(), j++){ + float posX = atof(vertEl->Attribute("px")); + float posY = atof(vertEl->Attribute("py")); + float posZ = atof(vertEl->Attribute("pz")); + shapeKeyOffsets[j][i] = Vector3(posX, posY, posZ) - vertPos[j]; + } + } - for(int k = 0; k < 100; k++) - shapeKeyOffsets[i][k] = shapeKeyPos[vertIds[j]] - vertPos[vertIds[j]]; - } - */ + for(int i = 0; i < numVertices; i++) + vertices[i].shapeKeyOffsets = shapeKeyOffsets[vertIds[i]]; } const char *fullSkeletonName = meshEl->Attribute("skeleton"); From 112a73fb8a1417771585997e59b7751ea8ad9d62 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 1 Sep 2024 16:14:01 +0300 Subject: [PATCH 4/5] fixed quad subdivision bugs --- mesh.cpp | 5 +++- quad.cpp | 78 ++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 52 insertions(+), 31 deletions(-) diff --git a/mesh.cpp b/mesh.cpp index 1ac93b4..1728d9b 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -95,7 +95,10 @@ namespace vb01{ void Mesh::updateVerts(MeshData meshData){ glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(MeshData::Vertex) * meshBase.numTris, meshData.vertices); + + MeshData::OldVertex *glVertData = meshBase.toGlVerts(); + glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(MeshData::OldVertex) * meshBase.numTris, glVertData); + delete glVertData; } void Mesh::update(){ diff --git a/quad.cpp b/quad.cpp index 96adad6..076b313 100755 --- a/quad.cpp +++ b/quad.cpp @@ -1,6 +1,10 @@ #include "quad.h" +#include + namespace vb01{ + using namespace std; + Quad::Quad(Vector3 size, bool spatial, int numVertDiv, int numHorDiv){ this->spatial = spatial; this->numVertDiv = numVertDiv; @@ -14,58 +18,72 @@ namespace vb01{ this->size = size; const int numHorQuads = numVertDiv + 1, numVertQuads = numHorDiv + 1; - const int numTris = 2 * numHorQuads * numVertQuads, numVerts = 3 * numTris; + const int numVertPos = (numHorQuads + 1) * (numVertQuads + 1), numTris = 2 * numHorQuads * numVertQuads, numVerts = 3 * numTris; Vector2 subQuadSize = Vector2(size.x / numHorQuads, size.y / numVertQuads); MeshData::Vertex *vertices = new MeshData::Vertex[numVerts]; - Vector3 *faceVertPos = new Vector3[6]; - Vector3 *norm = new Vector3(Vector3::VEC_J); + if(!meshBase.positions){ + meshBase.positions = new Vector3[numVertPos]; + meshBase.normals = new Vector3(Vector3::VEC_J); + meshBase.indices = new u32[numVerts]; + + for(int i = 0; i < numVerts; i++) + meshBase.indices[i] = i; + } + + Vector3 *faceVertPos = meshBase.positions; + Vector3 *norm = meshBase.normals; + u32 *indices = meshBase.indices; for(int i = 0; i < numHorQuads; i++){ for(int j = 0; j < numVertQuads; j++){ - int id = i * 6 * numHorQuads + j * 6; + int vertPosId = i * (numHorQuads + 1) + j; + int vertPosId2 = (i + 1) * (numHorQuads + 1) + j; Vector2 offset = Vector2(subQuadSize.x, subQuadSize.y); - faceVertPos[0] = Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * i); - faceVertPos[1] = Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * (i + 1)); - faceVertPos[2] = Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * i); - faceVertPos[3] = Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * i); - faceVertPos[4] = Vector3(-.5 * size.x + offset.x * j, 0, -.5 * size.y + offset.y * (i + 1)); - faceVertPos[5] = Vector3(-.5 * size.x + offset.x * (j + 1), 0, -.5 * size.y + offset.y * (i + 1)); + faceVertPos[vertPosId] = Vector3(-.5 * size.x + offset.x * i, 0, -.5 * size.y + offset.y * j); + faceVertPos[vertPosId + 1] = Vector3(-.5 * size.x + offset.x * i, 0, -.5 * size.y + offset.y * (j + 1)); + faceVertPos[vertPosId2] = Vector3(-.5 * size.x + offset.x * (i + 1), 0, -.5 * size.y + offset.y * j); + faceVertPos[vertPosId2 + 1] = Vector3(-.5 * size.x + offset.x * (i + 1), 0, -.5 * size.y + offset.y * (j + 1)); + + if(!spatial){ + int ids[4]{vertPosId, vertPosId + 1, vertPosId2, vertPosId2 + 1}; + + swap(faceVertPos[ids[0]].z, faceVertPos[ids[1]].z); + swap(faceVertPos[ids[2]].z, faceVertPos[ids[3]].z); + + for(int k = 0; k < 4; k++){ + faceVertPos[ids[k]].y = -faceVertPos[ids[k]].z; + faceVertPos[ids[k]].z = 0; + faceVertPos[ids[k]] += .5 * Vector3(size.x, size.y, 0); + } + } Vector2 faceVertUv[]{ - Vector2(float(j) / numHorQuads, float(i) / numVertQuads), - Vector2(float(j + 1) / numHorQuads, float(i + 1) / numVertQuads), - Vector2(float(j + 1) / numHorQuads, float(i) / numVertQuads), - Vector2(float(j) / numHorQuads, float(i) / numVertQuads), - Vector2(float(j) / numHorQuads, float(i + 1) / numVertQuads), - Vector2(float(j + 1) / numHorQuads, float(i + 1) / numVertQuads), + Vector2(float(i) / numHorQuads, 1 - float(j) / numVertQuads), + Vector2(float(i) / numHorQuads, 1 - float(j + 1) / numVertQuads), + Vector2(float(i + 1) / numHorQuads, 1 - float(j + 1) / numVertQuads), + Vector2(float(i + 1) / numHorQuads, 1 - float(j + 1) / numVertQuads), + Vector2(float(i + 1) / numHorQuads, 1 - float(j) / numVertQuads), + Vector2(float(i) / numHorQuads, 1 - float(j) / numVertQuads), }; - for(int k = 0; k < 6; k++){ - if(!spatial){ - faceVertPos[k].y = -faceVertPos[k].z; - faceVertPos[k].z = 0; - faceVertPos[k] += .5 * Vector3(size.x, size.y, 0); - } + int quadId = 6 * (i * numHorQuads + j); + int vertPosIds[6]{vertPosId, vertPosId + 1, vertPosId2 + 1, vertPosId2 + 1, vertPosId2, vertPosId}; + for(int k = 0; k < 6; k++){ MeshData::Vertex v; - v.pos = &faceVertPos[k]; + v.pos = &faceVertPos[vertPosIds[k]]; v.uv = faceVertUv[k]; v.tan = Vector3::VEC_I; v.norm = norm; v.biTan = Vector3::VEC_K; - vertices[id + k] = v; + vertices[quadId + k] = v; } } } - u32 *indices = new u32[numVerts]; - - for(int i = 0; i < numVerts; i++) - indices[i] = i; - - meshBase = MeshData((Vector3*)faceVertPos, nullptr, nullptr, nullptr, 6, nullptr, vertices, indices, numTris); + meshBase = MeshData(faceVertPos, nullptr, nullptr, nullptr, 6, norm, vertices, indices, numTris); } } From 8eca2ecc708546f8acd5465cfa5073436ff6dad7 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Thu, 5 Sep 2024 20:35:09 +0300 Subject: [PATCH 5/5] renamed struct --- mesh.cpp | 22 +++++++++++----------- meshData.cpp | 4 ++-- meshData.h | 4 ++-- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/mesh.cpp b/mesh.cpp index 1728d9b..64de30e 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -44,12 +44,12 @@ namespace vb01{ glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); - u32 size = sizeof(MeshData::OldVertex); + u32 size = sizeof(MeshData::GpuVertex); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - MeshData::OldVertex *glVertData = meshBase.toGlVerts(); + MeshData::GpuVertex *glVertData = meshBase.toGpuVerts(); glBufferData(GL_ARRAY_BUFFER, 3 * meshBase.numTris * size, glVertData, GL_DYNAMIC_DRAW); delete glVertData; @@ -58,21 +58,21 @@ namespace vb01{ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, size, (void*)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, norm))); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, norm))); glEnableVertexAttribArray(1); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, uv))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, uv))); glEnableVertexAttribArray(2); - glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, tan))); + glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, tan))); glEnableVertexAttribArray(3); - glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, biTan))); + glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, biTan))); glEnableVertexAttribArray(4); - glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, weights))); + glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, weights))); glEnableVertexAttribArray(5); - glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, boneIndices))); + glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, boneIndices))); glEnableVertexAttribArray(6); for(int i = 0; i < meshBase.numShapeKeys; i++){ - glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::OldVertex, shapeKeyOffsets) + 3 * i * sizeof(float))); + glVertexAttribPointer(7 + i, 3, GL_FLOAT, GL_FALSE, size, (void*)(offsetof(MeshData::GpuVertex, shapeKeyOffsets) + 3 * i * sizeof(float))); glEnableVertexAttribArray(7 + i); } } @@ -96,8 +96,8 @@ namespace vb01{ void Mesh::updateVerts(MeshData meshData){ glBindBuffer(GL_ARRAY_BUFFER, VBO); - MeshData::OldVertex *glVertData = meshBase.toGlVerts(); - glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(MeshData::OldVertex) * meshBase.numTris, glVertData); + MeshData::GpuVertex *glVertData = meshBase.toGpuVerts(); + glBufferSubData(GL_ARRAY_BUFFER, 0, 3 * sizeof(MeshData::GpuVertex) * meshBase.numTris, glVertData); delete glVertData; } diff --git a/meshData.cpp b/meshData.cpp index f3d17bb..ba7572b 100644 --- a/meshData.cpp +++ b/meshData.cpp @@ -17,9 +17,9 @@ namespace vb01{ } } - MeshData::OldVertex* MeshData::toGlVerts(){ + MeshData::GpuVertex* MeshData::toGpuVerts(){ int numVertices = 3 * numTris; - OldVertex *vertData = new OldVertex[numVertices]; + GpuVertex *vertData = new GpuVertex[numVertices]; for(int i = 0; i < numVertices; i++){ vertData[i].pos = *vertices[i].pos; diff --git a/meshData.h b/meshData.h index 568047f..6ca0ced 100644 --- a/meshData.h +++ b/meshData.h @@ -19,7 +19,7 @@ namespace vb01{ void animate(float, KeyframeChannel); }; - struct OldVertex{ + struct GpuVertex{ Vector3 pos, norm, tan, biTan; Vector2 uv; float weights[4]{0, 0, 0, 0}; @@ -35,7 +35,7 @@ namespace vb01{ Vector3 *shapeKeyOffsets = nullptr; }; - OldVertex* toGlVerts(); + GpuVertex* toGpuVerts(); Vector3 *positions = nullptr, *normals = nullptr, **shapeKeyOffsets = nullptr; float **weights = nullptr;