mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 13:58:51 +02:00
wip OBJ loader rewrite; wip because it crashes.
This commit is contained in:
parent
5bf3da9a7f
commit
864184a107
1 changed files with 121 additions and 88 deletions
|
@ -440,105 +440,138 @@ FBXGeometry* OBJReader::readOBJ(QByteArray& model, const QVariantHash& mapping,
|
||||||
// add a new meshPart to the geometry's single mesh.
|
// add a new meshPart to the geometry's single mesh.
|
||||||
while (parseOBJGroup(tokenizer, mapping, geometry, scaleGuess)) {}
|
while (parseOBJGroup(tokenizer, mapping, geometry, scaleGuess)) {}
|
||||||
|
|
||||||
FBXMesh& mesh = geometry.meshes[0];
|
// FBXMesh& mesh = geometry.meshes[0];
|
||||||
mesh.meshIndex = 0;
|
geometry.joints.resize(1);
|
||||||
|
geometry.joints[0].isFree = false;
|
||||||
|
geometry.joints[0].parentIndex = -1;
|
||||||
|
geometry.joints[0].distanceToParent = 0;
|
||||||
|
geometry.joints[0].translation = glm::vec3(0, 0, 0);
|
||||||
|
geometry.joints[0].rotationMin = glm::vec3(0, 0, 0);
|
||||||
|
geometry.joints[0].rotationMax = glm::vec3(0, 0, 0);
|
||||||
|
geometry.joints[0].name = "OBJ";
|
||||||
|
geometry.joints[0].isSkeletonJoint = true;
|
||||||
|
|
||||||
geometry.joints.resize(1);
|
geometry.jointIndices["x"] = 1;
|
||||||
geometry.joints[0].isFree = false;
|
|
||||||
geometry.joints[0].parentIndex = -1;
|
|
||||||
geometry.joints[0].distanceToParent = 0;
|
|
||||||
geometry.joints[0].translation = glm::vec3(0, 0, 0);
|
|
||||||
geometry.joints[0].rotationMin = glm::vec3(0, 0, 0);
|
|
||||||
geometry.joints[0].rotationMax = glm::vec3(0, 0, 0);
|
|
||||||
geometry.joints[0].name = "OBJ";
|
|
||||||
geometry.joints[0].isSkeletonJoint = true;
|
|
||||||
|
|
||||||
geometry.jointIndices["x"] = 1;
|
FBXCluster cluster;
|
||||||
|
cluster.jointIndex = 0;
|
||||||
|
cluster.inverseBindMatrix = glm::mat4(1, 0, 0, 0,
|
||||||
|
0, 1, 0, 0,
|
||||||
|
0, 0, 1, 0,
|
||||||
|
0, 0, 0, 1);
|
||||||
|
|
||||||
FBXCluster cluster;
|
QMap<QString, int> materialMeshIdMap;
|
||||||
cluster.jointIndex = 0;
|
QVector<FBXMesh> fbxMeshes;
|
||||||
cluster.inverseBindMatrix = glm::mat4(1, 0, 0, 0,
|
for (int i = 0, meshPartCount = 0; i < geometry.meshes[0].parts.count(); i++, meshPartCount++) {
|
||||||
0, 1, 0, 0,
|
FBXMeshPart& meshPart = geometry.meshes[0].parts[i];
|
||||||
0, 0, 1, 0,
|
|
||||||
0, 0, 0, 1);
|
|
||||||
mesh.clusters.append(cluster);
|
|
||||||
|
|
||||||
for (int i = 0, meshPartCount = 0; i < mesh.parts.count(); i++, meshPartCount++) {
|
|
||||||
FBXMeshPart& meshPart = mesh.parts[i];
|
|
||||||
FaceGroup faceGroup = faceGroups[meshPartCount];
|
FaceGroup faceGroup = faceGroups[meshPartCount];
|
||||||
bool specifiesUV = false;
|
bool specifiesUV = false;
|
||||||
foreach(OBJFace face, faceGroup) {
|
foreach(OBJFace face, faceGroup) {
|
||||||
glm::vec3 v0 = checked_at(vertices, face.vertexIndices[0]);
|
// Go through all of the OBJ faces and determine the number of different materials necessary (each different material will be a unique mesh).
|
||||||
glm::vec3 v1 = checked_at(vertices, face.vertexIndices[1]);
|
// NOTE (trent/mittens 3/30/17): this seems hardcore wasteful and is slowed down a bit by iterating through the face group twice, but it's the best way I've thought of to hack multi-material support in an OBJ into this pipeline.
|
||||||
glm::vec3 v2 = checked_at(vertices, face.vertexIndices[2]);
|
if (!materialMeshIdMap.contains(face.materialName)) {
|
||||||
meshPart.triangleIndices.append(mesh.vertices.count()); // not face.vertexIndices into vertices
|
// Create a new FBXMesh for this material mapping.
|
||||||
mesh.vertices << v0;
|
materialMeshIdMap.insert(face.materialName, materialMeshIdMap.count());
|
||||||
meshPart.triangleIndices.append(mesh.vertices.count());
|
|
||||||
mesh.vertices << v1;
|
|
||||||
meshPart.triangleIndices.append(mesh.vertices.count());
|
|
||||||
mesh.vertices << v2;
|
|
||||||
|
|
||||||
glm::vec3 n0, n1, n2;
|
FBXMesh& meshNew = geometry.meshes[0];
|
||||||
if (face.normalIndices.count()) {
|
meshNew.meshIndex = 0;
|
||||||
n0 = checked_at(normals, face.normalIndices[0]);
|
meshNew.clusters.append(cluster);
|
||||||
n1 = checked_at(normals, face.normalIndices[1]);
|
|
||||||
n2 = checked_at(normals, face.normalIndices[2]);
|
|
||||||
} else { // generate normals from triangle plane if not provided
|
|
||||||
n0 = n1 = n2 = glm::cross(v1 - v0, v2 - v0);
|
|
||||||
}
|
|
||||||
mesh.normals << n0 << n1 << n2;
|
|
||||||
if (face.textureUVIndices.count()) {
|
|
||||||
specifiesUV = true;
|
|
||||||
mesh.texCoords <<
|
|
||||||
checked_at(textureUVs, face.textureUVIndices[0]) <<
|
|
||||||
checked_at(textureUVs, face.textureUVIndices[1]) <<
|
|
||||||
checked_at(textureUVs, face.textureUVIndices[2]);
|
|
||||||
} else {
|
|
||||||
glm::vec2 corner(0.0f, 1.0f);
|
|
||||||
mesh.texCoords << corner << corner << corner;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// All the faces in the same group will have the same name and material.
|
|
||||||
OBJFace leadFace = faceGroup[0];
|
|
||||||
QString groupMaterialName = leadFace.materialName;
|
|
||||||
if (groupMaterialName.isEmpty() && specifiesUV) {
|
|
||||||
#ifdef WANT_DEBUG
|
|
||||||
qCDebug(modelformat) << "OBJ Reader WARNING: " << url
|
|
||||||
<< " needs a texture that isn't specified. Using default mechanism.";
|
|
||||||
#endif
|
|
||||||
groupMaterialName = SMART_DEFAULT_MATERIAL_NAME;
|
|
||||||
}
|
|
||||||
if (!groupMaterialName.isEmpty()) {
|
|
||||||
OBJMaterial& material = materials[groupMaterialName];
|
|
||||||
if (specifiesUV) {
|
|
||||||
material.userSpecifiesUV = true; // Note might not be true in a later usage.
|
|
||||||
}
|
|
||||||
if (specifiesUV || (0 != groupMaterialName.compare("none", Qt::CaseInsensitive))) {
|
|
||||||
// Blender has a convention that a material named "None" isn't really used (or defined).
|
|
||||||
material.used = true;
|
|
||||||
needsMaterialLibrary = groupMaterialName != SMART_DEFAULT_MATERIAL_NAME;
|
|
||||||
}
|
|
||||||
materials[groupMaterialName] = material;
|
|
||||||
meshPart.materialID = groupMaterialName;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// Add this mesh part to the mesh.
|
||||||
|
meshNew.parts.append(meshPart);
|
||||||
|
|
||||||
|
// Add it to the mesh vector.
|
||||||
|
fbxMeshes.append(meshNew);
|
||||||
|
|
||||||
|
// Do some of the material logic (which previously lived below) now.
|
||||||
|
// All the faces in the same group will have the same name and material.
|
||||||
|
OBJFace leadFace = faceGroup[0];
|
||||||
|
QString groupMaterialName = leadFace.materialName;
|
||||||
|
if (groupMaterialName.isEmpty() && specifiesUV) {
|
||||||
|
#ifdef WANT_DEBUG
|
||||||
|
qCDebug(modelformat) << "OBJ Reader WARNING: " << url
|
||||||
|
<< " needs a texture that isn't specified. Using default mechanism.";
|
||||||
|
#endif
|
||||||
|
groupMaterialName = SMART_DEFAULT_MATERIAL_NAME;
|
||||||
|
}
|
||||||
|
if (!groupMaterialName.isEmpty()) {
|
||||||
|
OBJMaterial& material = materials[groupMaterialName];
|
||||||
|
if (specifiesUV) {
|
||||||
|
material.userSpecifiesUV = true; // Note might not be true in a later usage.
|
||||||
|
}
|
||||||
|
if (specifiesUV || (0 != groupMaterialName.compare("none", Qt::CaseInsensitive))) {
|
||||||
|
// Blender has a convention that a material named "None" isn't really used (or defined).
|
||||||
|
material.used = true;
|
||||||
|
needsMaterialLibrary = groupMaterialName != SMART_DEFAULT_MATERIAL_NAME;
|
||||||
|
}
|
||||||
|
materials[groupMaterialName] = material;
|
||||||
|
meshPart.materialID = groupMaterialName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now that each mesh has been created with its own unique material mappings, fill them with data (vertex data is duplicated, face data is not).
|
||||||
|
foreach(OBJFace face, faceGroup) {
|
||||||
|
FBXMesh& mesh = fbxMeshes[materialMeshIdMap[face.materialName]];
|
||||||
|
|
||||||
|
glm::vec3 v0 = checked_at(vertices, face.vertexIndices[0]);
|
||||||
|
glm::vec3 v1 = checked_at(vertices, face.vertexIndices[1]);
|
||||||
|
glm::vec3 v2 = checked_at(vertices, face.vertexIndices[2]);
|
||||||
|
|
||||||
|
// Scale the vertices if the OBJ file scale is specified as non-one.
|
||||||
|
if (scaleGuess != 1.0f) {
|
||||||
|
v0 *= scaleGuess;
|
||||||
|
v1 *= scaleGuess;
|
||||||
|
v2 *= scaleGuess;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add the vertices.
|
||||||
|
meshPart.triangleIndices.append(mesh.vertices.count()); // not face.vertexIndices into vertices
|
||||||
|
mesh.vertices << v0;
|
||||||
|
meshPart.triangleIndices.append(mesh.vertices.count());
|
||||||
|
mesh.vertices << v1;
|
||||||
|
meshPart.triangleIndices.append(mesh.vertices.count());
|
||||||
|
mesh.vertices << v2;
|
||||||
|
|
||||||
|
glm::vec3 n0, n1, n2;
|
||||||
|
if (face.normalIndices.count()) {
|
||||||
|
n0 = checked_at(normals, face.normalIndices[0]);
|
||||||
|
n1 = checked_at(normals, face.normalIndices[1]);
|
||||||
|
n2 = checked_at(normals, face.normalIndices[2]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// generate normals from triangle plane if not provided
|
||||||
|
n0 = n1 = n2 = glm::cross(v1 - v0, v2 - v0);
|
||||||
|
}
|
||||||
|
mesh.normals << n0 << n1 << n2;
|
||||||
|
if (face.textureUVIndices.count()) {
|
||||||
|
specifiesUV = true;
|
||||||
|
mesh.texCoords <<
|
||||||
|
checked_at(textureUVs, face.textureUVIndices[0]) <<
|
||||||
|
checked_at(textureUVs, face.textureUVIndices[1]) <<
|
||||||
|
checked_at(textureUVs, face.textureUVIndices[2]);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
glm::vec2 corner(0.0f, 1.0f);
|
||||||
|
mesh.texCoords << corner << corner << corner;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we got a hint about units, scale all the points
|
geometry.meshes.clear();
|
||||||
if (scaleGuess != 1.0f) {
|
foreach(FBXMesh mesh, fbxMeshes) {
|
||||||
for (int i = 0; i < mesh.vertices.size(); i++) {
|
geometry.meshes.append(mesh);
|
||||||
mesh.vertices[i] *= scaleGuess;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
mesh.meshExtents.reset();
|
mesh.meshExtents.reset();
|
||||||
foreach (const glm::vec3& vertex, mesh.vertices) {
|
foreach(const glm::vec3& vertex, mesh.vertices) {
|
||||||
mesh.meshExtents.addPoint(vertex);
|
mesh.meshExtents.addPoint(vertex);
|
||||||
geometry.meshExtents.addPoint(vertex);
|
geometry.meshExtents.addPoint(vertex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FBXReader::buildModelMesh(mesh, url.toString());
|
||||||
|
}
|
||||||
|
|
||||||
FBXReader::buildModelMesh(mesh, url.toString());
|
// fbxDebugDump(geometry);
|
||||||
// fbxDebugDump(geometry);
|
|
||||||
} catch(const std::exception& e) {
|
} catch(const std::exception& e) {
|
||||||
qCDebug(modelformat) << "OBJ reader fail: " << e.what();
|
qCDebug(modelformat) << "OBJ reader fail: " << e.what();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue