using only the neeeded number of vertex positions

This commit is contained in:
devZoGok
2024-08-19 18:23:09 +03:00
parent 6ce16b0837
commit 19990e48f4
8 changed files with 145 additions and 72 deletions
+55 -23
View File
@@ -59,44 +59,61 @@ namespace vb01{
}
}
//TODO remove the 4 literal for bone weights and indices
Mesh* XmlModelReader::processMesh(XMLElement *meshEl){
vector<Vector3> vertPos, vertNorm;
vector<float> weights;
int numVertPos = 0;
vector<Vector3> vp;
vector<float> 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<u32> 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");