improved model parsing

This commit is contained in:
devZoGok
2024-09-01 16:13:53 +03:00
parent 19990e48f4
commit 36f15b6dd9
6 changed files with 1119 additions and 1127 deletions
+17 -21
View File
@@ -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);
}
}
+3 -2
View File
@@ -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;
+1 -1
View File
@@ -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);
};
+1033 -1033
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -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))
+42 -49
View File
@@ -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<Vector3> vp;
vector<float> 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(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 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();
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++;
}
}
}
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<int> 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,14 +151,17 @@ 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++){
if(numShapeKeys > 0){
shapeKeys = new MeshData::ShapeKey[numShapeKeys];
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++){
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"));
@@ -176,23 +174,18 @@ namespace vb01{
Driver::VariableType type = Driver::getDriverVariableType(driverEl->Attribute("type"));
driversByNodeNames.push_back(make_pair(string(nodeName) + (boneName ? "." + string(boneName) : ""), new Driver(&shapeKeys[i], channel, type)));
vector<Vector3> shapeKeyPos;
int j = 0;
for(XMLElement *vertEl = shapeKeyEl->FirstChildElement("vert"); vertEl; vertEl = vertEl->NextSiblingElement()){
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"));
shapeKeyPos.push_back(Vector3(posX, posY, posZ));
shapeKeyOffsets[j][i] = Vector3(posX, posY, posZ) - vertPos[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]];
}
*/
for(int i = 0; i < numVertices; i++)
vertices[i].shapeKeyOffsets = shapeKeyOffsets[vertIds[i]];
}
const char *fullSkeletonName = meshEl->Attribute("skeleton");