2023-10-03 20:37:00 +02:00
|
|
|
// Copyright (C) 2002-2012 Nikolaus Gebhardt
|
|
|
|
// This file is part of the "Irrlicht Engine".
|
|
|
|
// For conditions of distribution and use, see copyright notice in irrlicht.h
|
|
|
|
|
|
|
|
#include "CXMeshFileLoader.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
#include "fast_atof.h"
|
|
|
|
#include "coreutil.h"
|
|
|
|
#include "ISceneManager.h"
|
|
|
|
#include "IVideoDriver.h"
|
|
|
|
#include "IReadFile.h"
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define _XREADER_DEBUG
|
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
// #define BETTER_MESHBUFFER_SPLITTING_FOR_X
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
#define SET_ERR_AND_RETURN() \
|
|
|
|
do { \
|
|
|
|
ErrorState = true; \
|
|
|
|
return false; \
|
|
|
|
} while (0)
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
//! Constructor
|
2024-03-20 19:35:52 +01:00
|
|
|
CXMeshFileLoader::CXMeshFileLoader(scene::ISceneManager *smgr) :
|
|
|
|
AnimatedMesh(0), Buffer(0), P(0), End(0), BinaryNumCount(0), Line(0), ErrorState(false),
|
|
|
|
CurFrame(0), MajorVersion(0), MinorVersion(0), BinaryFormat(false), FloatSize(0)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
#ifdef _DEBUG
|
2023-10-03 20:37:00 +02:00
|
|
|
setDebugName("CXMeshFileLoader");
|
2024-03-20 19:35:52 +01:00
|
|
|
#endif
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//! returns true if the file maybe is able to be loaded by this class
|
|
|
|
//! based on the file extension (e.g. ".bsp")
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::isALoadableFileExtension(const io::path &filename) const
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
return core::hasFileExtension(filename, "x");
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//! creates/loads an animated mesh from the file.
|
|
|
|
//! \return Pointer to the created mesh. Returns 0 if loading failed.
|
|
|
|
//! If you no longer need the mesh, you should call IAnimatedMesh::drop().
|
|
|
|
//! See IReferenceCounted::drop() for more information.
|
2024-03-20 19:35:52 +01:00
|
|
|
IAnimatedMesh *CXMeshFileLoader::createMesh(io::IReadFile *file)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
u32 time = os::Timer::getRealTime();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AnimatedMesh = new CSkinnedMesh();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (load(file)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
AnimatedMesh->finalize();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
AnimatedMesh->drop();
|
|
|
|
AnimatedMesh = 0;
|
|
|
|
}
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
time = os::Timer::getRealTime() - time;
|
|
|
|
core::stringc tmpString = "Time to load ";
|
|
|
|
tmpString += BinaryFormat ? "binary" : "ascii";
|
|
|
|
tmpString += " X file: ";
|
|
|
|
tmpString += time;
|
|
|
|
tmpString += "ms";
|
|
|
|
os::Printer::log(tmpString.c_str());
|
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
// Clear up
|
|
|
|
|
|
|
|
MajorVersion = 0;
|
|
|
|
MinorVersion = 0;
|
|
|
|
BinaryFormat = 0;
|
|
|
|
BinaryNumCount = 0;
|
|
|
|
FloatSize = 0;
|
|
|
|
P = 0;
|
|
|
|
End = 0;
|
|
|
|
CurFrame = 0;
|
|
|
|
|
|
|
|
delete[] Buffer;
|
2023-10-03 20:37:00 +02:00
|
|
|
Buffer = 0;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < Meshes.size(); ++i)
|
2023-10-03 20:37:00 +02:00
|
|
|
delete Meshes[i];
|
|
|
|
Meshes.clear();
|
|
|
|
|
|
|
|
return AnimatedMesh;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::load(io::IReadFile *file)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
if (!readFileIntoMemory(file))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!parseFile())
|
|
|
|
return false;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 n = 0; n < Meshes.size(); ++n) {
|
|
|
|
SXMesh *mesh = Meshes[n];
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
// default material if nothing loaded
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!mesh->Materials.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->Materials.push_back(video::SMaterial());
|
|
|
|
mesh->Materials[0].DiffuseColor.set(0xff777777);
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh->Materials[0].Shininess = 0.f;
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->Materials[0].SpecularColor.set(0xff777777);
|
|
|
|
mesh->Materials[0].EmissiveColor.set(0xff000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
mesh->Buffers.reallocate(mesh->Materials.size());
|
|
|
|
#ifndef BETTER_MESHBUFFER_SPLITTING_FOR_X
|
|
|
|
const u32 bufferOffset = AnimatedMesh->getMeshBufferCount();
|
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->Materials.size(); ++i) {
|
|
|
|
mesh->Buffers.push_back(AnimatedMesh->addMeshBuffer());
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->Buffers.getLast()->Material = mesh->Materials[i];
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!mesh->HasSkinning) {
|
|
|
|
// Set up rigid animation
|
|
|
|
if (mesh->AttachedJointID != -1) {
|
|
|
|
AnimatedMesh->getAllJoints()[mesh->AttachedJointID]->AttachedMeshes.push_back(AnimatedMesh->getMeshBuffers().size() - 1);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!mesh->FaceMaterialIndices.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->FaceMaterialIndices.set_used(mesh->Indices.size() / 3);
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->FaceMaterialIndices.size(); ++i)
|
|
|
|
mesh->FaceMaterialIndices[i] = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!mesh->HasVertexColors) {
|
|
|
|
for (u32 j = 0; j < mesh->FaceMaterialIndices.size(); ++j) {
|
|
|
|
for (u32 id = j * 3 + 0; id <= j * 3 + 2; ++id) {
|
|
|
|
mesh->Vertices[mesh->Indices[id]].Color = mesh->Buffers[mesh->FaceMaterialIndices[j]]->Material.DiffuseColor;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
#ifdef BETTER_MESHBUFFER_SPLITTING_FOR_X
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
// the same vertex can be used in many different meshbuffers, but it's slow to work out
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
core::array<core::array<u32>> verticesLinkIndex;
|
2023-10-03 20:37:00 +02:00
|
|
|
verticesLinkIndex.reallocate(mesh->Vertices.size());
|
2024-03-20 19:35:52 +01:00
|
|
|
core::array<core::array<u16>> verticesLinkBuffer;
|
2023-10-03 20:37:00 +02:00
|
|
|
verticesLinkBuffer.reallocate(mesh->Vertices.size());
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->Vertices.size(); ++i) {
|
|
|
|
verticesLinkIndex.push_back(core::array<u32>());
|
|
|
|
verticesLinkBuffer.push_back(core::array<u16>());
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->FaceMaterialIndices.size(); ++i) {
|
|
|
|
for (u32 id = i * 3 + 0; id <= i * 3 + 2; ++id) {
|
|
|
|
core::array<u16> &Array = verticesLinkBuffer[mesh->Indices[id]];
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
for (u32 j = 0; j < Array.size(); ++j) {
|
|
|
|
if (Array[j] == mesh->FaceMaterialIndices[i]) {
|
|
|
|
found = true;
|
2023-10-03 20:37:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
2024-03-20 19:35:52 +01:00
|
|
|
Array.push_back(mesh->FaceMaterialIndices[i]);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < verticesLinkBuffer.size(); ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!verticesLinkBuffer[i].size())
|
|
|
|
verticesLinkBuffer[i].push_back(0);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->Vertices.size(); ++i) {
|
|
|
|
core::array<u16> &Array = verticesLinkBuffer[i];
|
2023-10-03 20:37:00 +02:00
|
|
|
verticesLinkIndex[i].reallocate(Array.size());
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 j = 0; j < Array.size(); ++j) {
|
|
|
|
scene::SSkinMeshBuffer *buffer = mesh->Buffers[Array[j]];
|
|
|
|
verticesLinkIndex[i].push_back(buffer->Vertices_Standard.size());
|
|
|
|
buffer->Vertices_Standard.push_back(mesh->Vertices[i]);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->FaceMaterialIndices.size(); ++i) {
|
|
|
|
scene::SSkinMeshBuffer *buffer = mesh->Buffers[mesh->FaceMaterialIndices[i]];
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 id = i * 3 + 0; id <= i * 3 + 2; ++id) {
|
|
|
|
core::array<u16> &Array = verticesLinkBuffer[mesh->Indices[id]];
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 j = 0; j < Array.size(); ++j) {
|
|
|
|
if (Array[j] == mesh->FaceMaterialIndices[i])
|
|
|
|
buffer->Indices.push_back(verticesLinkIndex[mesh->Indices[id]][j]);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 j = 0; j < mesh->WeightJoint.size(); ++j) {
|
|
|
|
ISkinnedMesh::SJoint *joint = AnimatedMesh->getAllJoints()[mesh->WeightJoint[j]];
|
|
|
|
ISkinnedMesh::SWeight &weight = joint->Weights[mesh->WeightNum[j]];
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
u32 id = weight.vertex_id;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (id >= verticesLinkIndex.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("X loader: Weight id out of range", ELL_WARNING);
|
2024-03-20 19:35:52 +01:00
|
|
|
id = 0;
|
|
|
|
weight.strength = 0.f;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (verticesLinkBuffer[id].size() == 1) {
|
|
|
|
weight.vertex_id = verticesLinkIndex[id][0];
|
|
|
|
weight.buffer_id = verticesLinkBuffer[id][0];
|
|
|
|
} else if (verticesLinkBuffer[id].size() != 0) {
|
|
|
|
for (u32 k = 1; k < verticesLinkBuffer[id].size(); ++k) {
|
|
|
|
ISkinnedMesh::SWeight *WeightClone = AnimatedMesh->addWeight(joint);
|
2023-10-03 20:37:00 +02:00
|
|
|
WeightClone->strength = weight.strength;
|
|
|
|
WeightClone->vertex_id = verticesLinkIndex[id][k];
|
|
|
|
WeightClone->buffer_id = verticesLinkBuffer[id][k];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
#else
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
core::array<u32> verticesLinkIndex;
|
|
|
|
core::array<s16> verticesLinkBuffer;
|
2023-10-03 20:37:00 +02:00
|
|
|
verticesLinkBuffer.set_used(mesh->Vertices.size());
|
|
|
|
|
|
|
|
// init with 0
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->Vertices.size(); ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// watch out for vertices which are not part of the mesh
|
|
|
|
// they will keep the -1 and can lead to out-of-bounds access
|
2024-03-20 19:35:52 +01:00
|
|
|
verticesLinkBuffer[i] = -1;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool warned = false;
|
|
|
|
// store meshbuffer number per vertex
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->FaceMaterialIndices.size(); ++i) {
|
|
|
|
for (u32 id = i * 3 + 0; id <= i * 3 + 2; ++id) {
|
|
|
|
if ((verticesLinkBuffer[mesh->Indices[id]] != -1) && (verticesLinkBuffer[mesh->Indices[id]] != (s16)mesh->FaceMaterialIndices[i])) {
|
|
|
|
if (!warned) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("X loader", "Duplicated vertex, animation might be corrupted.", ELL_WARNING);
|
2024-03-20 19:35:52 +01:00
|
|
|
warned = true;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
const u32 tmp = mesh->Vertices.size();
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh->Vertices.push_back(mesh->Vertices[mesh->Indices[id]]);
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->Indices[id] = tmp;
|
|
|
|
verticesLinkBuffer.set_used(mesh->Vertices.size());
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
verticesLinkBuffer[mesh->Indices[id]] = mesh->FaceMaterialIndices[i];
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (mesh->FaceMaterialIndices.size() != 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// store vertices in buffers and remember relation in verticesLinkIndex
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 *vCountArray = new u32[mesh->Buffers.size()];
|
|
|
|
memset(vCountArray, 0, mesh->Buffers.size() * sizeof(u32));
|
2023-10-03 20:37:00 +02:00
|
|
|
// count vertices in each buffer and reallocate
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->Vertices.size(); ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (verticesLinkBuffer[i] != -1)
|
|
|
|
++vCountArray[verticesLinkBuffer[i]];
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if (mesh->TCoords2.size()) {
|
|
|
|
for (i = 0; i != mesh->Buffers.size(); ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->Buffers[i]->Vertices_2TCoords.reallocate(vCountArray[i]);
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh->Buffers[i]->VertexType = video::EVT_2TCOORDS;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
|
|
|
for (i = 0; i != mesh->Buffers.size(); ++i)
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->Buffers[i]->Vertices_Standard.reallocate(vCountArray[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
verticesLinkIndex.set_used(mesh->Vertices.size());
|
|
|
|
// actually store vertices
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->Vertices.size(); ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// if a vertex is missing for some reason, just skip it
|
2024-03-20 19:35:52 +01:00
|
|
|
if (verticesLinkBuffer[i] == -1)
|
2023-10-03 20:37:00 +02:00
|
|
|
continue;
|
2024-03-20 19:35:52 +01:00
|
|
|
scene::SSkinMeshBuffer *buffer = mesh->Buffers[verticesLinkBuffer[i]];
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (mesh->TCoords2.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
verticesLinkIndex[i] = buffer->Vertices_2TCoords.size();
|
2024-03-20 19:35:52 +01:00
|
|
|
buffer->Vertices_2TCoords.push_back(mesh->Vertices[i]);
|
2023-10-03 20:37:00 +02:00
|
|
|
// We have a problem with correct tcoord2 handling here
|
|
|
|
// crash fixed for now by checking the values
|
2024-03-20 19:35:52 +01:00
|
|
|
buffer->Vertices_2TCoords.getLast().TCoords2 = (i < mesh->TCoords2.size()) ? mesh->TCoords2[i] : mesh->Vertices[i].TCoords;
|
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
verticesLinkIndex[i] = buffer->Vertices_Standard.size();
|
2024-03-20 19:35:52 +01:00
|
|
|
buffer->Vertices_Standard.push_back(mesh->Vertices[i]);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// count indices per buffer and reallocate
|
2024-03-20 19:35:52 +01:00
|
|
|
memset(vCountArray, 0, mesh->Buffers.size() * sizeof(u32));
|
|
|
|
for (i = 0; i < mesh->FaceMaterialIndices.size(); ++i)
|
|
|
|
++vCountArray[mesh->FaceMaterialIndices[i]];
|
|
|
|
for (i = 0; i != mesh->Buffers.size(); ++i)
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->Buffers[i]->Indices.reallocate(vCountArray[i]);
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] vCountArray;
|
2023-10-03 20:37:00 +02:00
|
|
|
// create indices per buffer
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < mesh->FaceMaterialIndices.size(); ++i) {
|
|
|
|
scene::SSkinMeshBuffer *buffer = mesh->Buffers[mesh->FaceMaterialIndices[i]];
|
|
|
|
for (u32 id = i * 3 + 0; id != i * 3 + 3; ++id) {
|
|
|
|
buffer->Indices.push_back(verticesLinkIndex[mesh->Indices[id]]);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 j = 0; j < mesh->WeightJoint.size(); ++j) {
|
|
|
|
ISkinnedMesh::SWeight &weight = (AnimatedMesh->getAllJoints()[mesh->WeightJoint[j]]->Weights[mesh->WeightNum[j]]);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
u32 id = weight.vertex_id;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (id >= verticesLinkIndex.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("X loader: Weight id out of range", ELL_WARNING);
|
2024-03-20 19:35:52 +01:00
|
|
|
id = 0;
|
|
|
|
weight.strength = 0.f;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
weight.vertex_id = verticesLinkIndex[id];
|
|
|
|
weight.buffer_id = verticesLinkBuffer[id] + bufferOffset;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
#endif
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Reads file into memory
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::readFileIntoMemory(io::IReadFile *file)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
const long size = file->getSize();
|
2024-03-20 19:35:52 +01:00
|
|
|
if (size < 12) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("X File is too small.", ELL_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
Buffer = new c8[size + 1];
|
2023-10-03 20:37:00 +02:00
|
|
|
Buffer[size] = 0x0; // null-terminate
|
|
|
|
|
|
|
|
//! read all into memory
|
2024-03-20 19:35:52 +01:00
|
|
|
if (file->read(Buffer, size) != static_cast<size_t>(size)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Could not read from x file.", ELL_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Line = 1;
|
|
|
|
End = Buffer + size;
|
|
|
|
|
|
|
|
//! check header "xof "
|
2024-03-20 19:35:52 +01:00
|
|
|
if (strncmp(Buffer, "xof ", 4) != 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Not an x file, wrong header.", ELL_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! read minor and major version, e.g. 0302 or 0303
|
|
|
|
c8 tmp[3];
|
|
|
|
tmp[0] = Buffer[4];
|
|
|
|
tmp[1] = Buffer[5];
|
|
|
|
tmp[2] = 0x0;
|
|
|
|
MajorVersion = core::strtoul10(tmp);
|
|
|
|
|
|
|
|
tmp[0] = Buffer[6];
|
|
|
|
tmp[1] = Buffer[7];
|
|
|
|
MinorVersion = core::strtoul10(tmp);
|
|
|
|
|
|
|
|
//! read format
|
2024-03-20 19:35:52 +01:00
|
|
|
if (strncmp(&Buffer[8], "txt ", 4) == 0)
|
2023-10-03 20:37:00 +02:00
|
|
|
BinaryFormat = false;
|
2024-03-20 19:35:52 +01:00
|
|
|
else if (strncmp(&Buffer[8], "bin ", 4) == 0)
|
2023-10-03 20:37:00 +02:00
|
|
|
BinaryFormat = true;
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Only uncompressed x files currently supported.", ELL_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
BinaryNumCount = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
//! read float size
|
2024-03-20 19:35:52 +01:00
|
|
|
if (strncmp(&Buffer[12], "0032", 4) == 0)
|
2023-10-03 20:37:00 +02:00
|
|
|
FloatSize = 4;
|
2024-03-20 19:35:52 +01:00
|
|
|
else if (strncmp(&Buffer[12], "0064", 4) == 0)
|
2023-10-03 20:37:00 +02:00
|
|
|
FloatSize = 8;
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Float size not supported.", ELL_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
P = &Buffer[16];
|
|
|
|
|
|
|
|
readUntilEndOfLine();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Parses the file
|
|
|
|
bool CXMeshFileLoader::parseFile()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
while (parseDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// loop
|
|
|
|
}
|
|
|
|
|
|
|
|
return !ErrorState;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Parses the next Data object in the file
|
|
|
|
bool CXMeshFileLoader::parseDataObject()
|
|
|
|
{
|
|
|
|
core::stringc objectName = getNextToken();
|
|
|
|
|
|
|
|
if (objectName.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// parse specific object
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("debug DataObject", objectName.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (objectName == "template")
|
|
|
|
return parseDataObjectTemplate();
|
2024-03-20 19:35:52 +01:00
|
|
|
else if (objectName == "Frame") {
|
|
|
|
return parseDataObjectFrame(0);
|
|
|
|
} else if (objectName == "Mesh") {
|
2023-10-03 20:37:00 +02:00
|
|
|
// some meshes have no frames at all
|
2024-03-20 19:35:52 +01:00
|
|
|
// CurFrame = AnimatedMesh->addJoint(0);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
SXMesh *mesh = new SXMesh;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// mesh->Buffer=AnimatedMesh->addMeshBuffer();
|
2023-10-03 20:37:00 +02:00
|
|
|
Meshes.push_back(mesh);
|
|
|
|
|
|
|
|
return parseDataObjectMesh(*mesh);
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "AnimationSet") {
|
2023-10-03 20:37:00 +02:00
|
|
|
return parseDataObjectAnimationSet();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "AnimTicksPerSecond") {
|
2023-10-03 20:37:00 +02:00
|
|
|
return parseDataObjectAnimationTicksPerSecond();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "Material") {
|
2023-10-03 20:37:00 +02:00
|
|
|
return parseUnknownDataObject();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "}") {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("} found in dataObject", ELL_WARNING);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
os::Printer::log("Unknown data object in animation of .x file", objectName.c_str(), ELL_WARNING);
|
|
|
|
|
|
|
|
return parseUnknownDataObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectTemplate()
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading template", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// parse a template data object. Currently not stored.
|
|
|
|
core::stringc name;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject(&name)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Left delimiter in template data object missing.",
|
2024-03-20 19:35:52 +01:00
|
|
|
name.c_str(), ELL_WARNING);
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read GUID
|
|
|
|
getNextToken();
|
|
|
|
|
|
|
|
// read and ignore data members
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc s = getNextToken();
|
|
|
|
|
|
|
|
if (s == "}")
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (s.size() == 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectFrame(CSkinnedMesh::SJoint *Parent)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading frame", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// A coordinate frame, or "frame of reference." The Frame template
|
|
|
|
// is open and can contain any object. The Direct3D extensions (D3DX)
|
|
|
|
// mesh-loading functions recognize Mesh, FrameTransformMatrix, and
|
|
|
|
// Frame template instances as child objects when loading a Frame
|
|
|
|
// instance.
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 JointID = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
core::stringc name;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject(&name)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Frame found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
CSkinnedMesh::SJoint *joint = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-02-06 20:22:44 +01:00
|
|
|
if (name.size()) {
|
|
|
|
auto n = AnimatedMesh->getJointNumber(name.c_str());
|
|
|
|
if (n.has_value()) {
|
|
|
|
JointID = *n;
|
|
|
|
joint = AnimatedMesh->getAllJoints()[JointID];
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!joint) {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("creating joint ", name.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
joint = AnimatedMesh->addJoint(Parent);
|
|
|
|
joint->Name = name.c_str();
|
|
|
|
JointID = AnimatedMesh->getAllJoints().size() - 1;
|
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("using joint ", name.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
if (Parent)
|
|
|
|
Parent->Children.push_back(joint);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now inside a frame.
|
|
|
|
// read tokens until closing brace is reached.
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc objectName = getNextToken();
|
|
|
|
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("debug DataObject in frame:", objectName.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (objectName.size() == 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unexpected ending found in Frame in x file.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "}") {
|
2023-10-03 20:37:00 +02:00
|
|
|
break; // frame finished
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "Frame") {
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
if (!parseDataObjectFrame(joint))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "FrameTransformMatrix") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectTransformationMatrix(joint->LocalMatrix))
|
|
|
|
return false;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// joint->LocalAnimatedMatrix
|
|
|
|
// joint->LocalAnimatedMatrix.makeInverse();
|
|
|
|
// joint->LocalMatrix=tmp*joint->LocalAnimatedMatrix;
|
|
|
|
} else if (objectName == "Mesh") {
|
2023-10-03 20:37:00 +02:00
|
|
|
/*
|
|
|
|
frame.Meshes.push_back(SXMesh());
|
|
|
|
if (!parseDataObjectMesh(frame.Meshes.getLast()))
|
|
|
|
return false;
|
|
|
|
*/
|
2024-03-20 19:35:52 +01:00
|
|
|
SXMesh *mesh = new SXMesh;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh->AttachedJointID = JointID;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
Meshes.push_back(mesh);
|
|
|
|
|
|
|
|
if (!parseDataObjectMesh(*mesh))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown data object in frame in x file", objectName.c_str(), ELL_WARNING);
|
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectTransformationMatrix(core::matrix4 &mat)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading Transformation Matrix", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Transformation Matrix found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
readMatrix(mat);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForOneFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Transformation Matrix found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in Transformation Matrix found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectMesh(SXMesh &mesh)
|
|
|
|
{
|
|
|
|
core::stringc name;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject(&name)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading mesh", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
os::Printer::log("No opening brace in Mesh found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading mesh", name.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// read vertex count
|
|
|
|
const u32 nVertices = readInt();
|
|
|
|
|
|
|
|
// read vertices
|
|
|
|
mesh.Vertices.set_used(nVertices);
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 n = 0; n < nVertices; ++n) {
|
2023-10-03 20:37:00 +02:00
|
|
|
readVector3(mesh.Vertices[n].Pos);
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.Vertices[n].Color = 0xFFFFFFFF;
|
|
|
|
mesh.Vertices[n].Normal = core::vector3df(0.0f);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForTwoFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Mesh Vertex Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// read faces
|
|
|
|
const u32 nFaces = readInt();
|
|
|
|
|
|
|
|
mesh.Indices.set_used(nFaces * 3);
|
|
|
|
mesh.IndexCountPerFace.set_used(nFaces);
|
|
|
|
|
|
|
|
core::array<u32> polygonfaces;
|
|
|
|
u32 currentIndex = 0;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 k = 0; k < nFaces; ++k) {
|
2023-10-03 20:37:00 +02:00
|
|
|
const u32 fcnt = readInt();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (fcnt != 3) {
|
|
|
|
if (fcnt < 3) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Invalid face count (<3) found in Mesh x file reader.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read face indices
|
|
|
|
polygonfaces.set_used(fcnt);
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 triangles = (fcnt - 2);
|
|
|
|
mesh.Indices.set_used(mesh.Indices.size() + ((triangles - 1) * 3));
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh.IndexCountPerFace[k] = (u16)(triangles * 3);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 f = 0; f < fcnt; ++f)
|
2023-10-03 20:37:00 +02:00
|
|
|
polygonfaces[f] = readInt();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 jk = 0; jk < triangles; ++jk) {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh.Indices[currentIndex++] = polygonfaces[0];
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.Indices[currentIndex++] = polygonfaces[jk + 1];
|
|
|
|
mesh.Indices[currentIndex++] = polygonfaces[jk + 2];
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: change face indices in material list
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh.Indices[currentIndex++] = readInt();
|
|
|
|
mesh.Indices[currentIndex++] = readInt();
|
|
|
|
mesh.Indices[currentIndex++] = readInt();
|
|
|
|
mesh.IndexCountPerFace[k] = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 j = 0; j < mesh.Indices.size(); j++) {
|
|
|
|
if (mesh.Indices[j] >= mesh.Vertices.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Out of range index found in Mesh x file reader.", ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForTwoFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Mesh Face Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
// here, other data objects may follow
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc objectName = getNextToken();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (objectName.size() == 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unexpected ending found in Mesh in x file.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "}") {
|
2023-10-03 20:37:00 +02:00
|
|
|
break; // mesh finished
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("debug DataObject in mesh", objectName.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (objectName == "MeshNormals") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectMeshNormals(mesh))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "MeshTextureCoords") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectMeshTextureCoords(mesh))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "MeshVertexColors") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectMeshVertexColors(mesh))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "MeshMaterialList") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectMeshMaterialList(mesh))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "VertexDuplicationIndices") {
|
2023-10-03 20:37:00 +02:00
|
|
|
// we'll ignore vertex duplication indices
|
|
|
|
// TODO: read them
|
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "DeclData") {
|
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No starting brace in DeclData found.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
// arbitrary vertex attributes
|
|
|
|
// first comes the number of element definitions
|
|
|
|
// then the vertex element type definitions
|
|
|
|
// with format type;tesselator;semantics;usageindex
|
|
|
|
// we want to support 2;0;6;0 == tangent
|
|
|
|
// 2;0;7;0 == binormal
|
|
|
|
// 2;0;3;0 == normal
|
|
|
|
// 1/2;0;5;0 == 1st uv coord
|
|
|
|
// and 1/2;0;5;1 == 2nd uv coord
|
|
|
|
// type==2 is 3xf32, type==1 is 2xf32
|
|
|
|
u32 j;
|
|
|
|
const u32 dcnt = readInt();
|
|
|
|
u16 size = 0;
|
|
|
|
s16 normalpos = -1;
|
|
|
|
s16 uvpos = -1;
|
|
|
|
s16 uv2pos = -1;
|
|
|
|
s16 tangentpos = -1;
|
|
|
|
s16 binormalpos = -1;
|
|
|
|
s16 normaltype = -1;
|
|
|
|
s16 uvtype = -1;
|
|
|
|
s16 uv2type = -1;
|
|
|
|
s16 tangenttype = -1;
|
|
|
|
s16 binormaltype = -1;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
(void)tangentpos; // disable unused variable warnings
|
|
|
|
(void)binormalpos; // disable unused variable warnings
|
|
|
|
(void)tangenttype; // disable unused variable warnings
|
2023-10-03 20:37:00 +02:00
|
|
|
(void)binormaltype; // disable unused variable warnings
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (j = 0; j < dcnt; ++j) {
|
2023-10-03 20:37:00 +02:00
|
|
|
const u32 type = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
// const u32 tesselator = readInt();
|
2023-10-03 20:37:00 +02:00
|
|
|
readInt();
|
|
|
|
const u32 semantics = readInt();
|
|
|
|
const u32 index = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (semantics) {
|
2023-10-03 20:37:00 +02:00
|
|
|
case 3:
|
|
|
|
normalpos = size;
|
|
|
|
normaltype = type;
|
|
|
|
break;
|
|
|
|
case 5:
|
2024-03-20 19:35:52 +01:00
|
|
|
if (index == 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
uvpos = size;
|
|
|
|
uvtype = type;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (index == 1) {
|
2023-10-03 20:37:00 +02:00
|
|
|
uv2pos = size;
|
|
|
|
uv2type = type;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
tangentpos = size;
|
|
|
|
tangenttype = type;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
binormalpos = size;
|
|
|
|
binormaltype = type;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (type) {
|
2023-10-03 20:37:00 +02:00
|
|
|
case 0:
|
|
|
|
size += 4;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
size += 8;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
size += 12;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
size += 16;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
size += 4;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
size += 8;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
case 9:
|
|
|
|
size += 4;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
size += 8;
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
size += 4;
|
|
|
|
break;
|
|
|
|
case 12:
|
|
|
|
size += 8;
|
|
|
|
break;
|
|
|
|
case 13:
|
|
|
|
size += 4;
|
|
|
|
break;
|
|
|
|
case 14:
|
|
|
|
size += 4;
|
|
|
|
break;
|
|
|
|
case 15:
|
|
|
|
size += 4;
|
|
|
|
break;
|
|
|
|
case 16:
|
|
|
|
size += 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const u32 datasize = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 *data = new u32[datasize];
|
|
|
|
for (j = 0; j < datasize; ++j)
|
|
|
|
data[j] = readInt();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForOneFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in DeclData found.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in DeclData.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] data;
|
2023-10-03 20:37:00 +02:00
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
u8 *dataptr = (u8 *)data;
|
2023-10-03 20:37:00 +02:00
|
|
|
if ((uv2pos != -1) && (uv2type == 1))
|
|
|
|
mesh.TCoords2.reallocate(mesh.Vertices.size());
|
2024-03-20 19:35:52 +01:00
|
|
|
for (j = 0; j < mesh.Vertices.size(); ++j) {
|
2023-10-03 20:37:00 +02:00
|
|
|
if ((normalpos != -1) && (normaltype == 2))
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.Vertices[j].Normal.set(*((core::vector3df *)(dataptr + normalpos)));
|
2023-10-03 20:37:00 +02:00
|
|
|
if ((uvpos != -1) && (uvtype == 1))
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.Vertices[j].TCoords.set(*((core::vector2df *)(dataptr + uvpos)));
|
2023-10-03 20:37:00 +02:00
|
|
|
if ((uv2pos != -1) && (uv2type == 1))
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.TCoords2.push_back(*((core::vector2df *)(dataptr + uv2pos)));
|
2023-10-03 20:37:00 +02:00
|
|
|
dataptr += size;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] data;
|
|
|
|
} else if (objectName == "FVFData") {
|
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No starting brace in FVFData found.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
const u32 dataformat = readInt();
|
|
|
|
const u32 datasize = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 *data = new u32[datasize];
|
|
|
|
for (u32 j = 0; j < datasize; ++j)
|
|
|
|
data[j] = readInt();
|
|
|
|
if (dataformat & 0x102) // 2nd uv set
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
mesh.TCoords2.reallocate(mesh.Vertices.size());
|
2024-03-20 19:35:52 +01:00
|
|
|
u8 *dataptr = (u8 *)data;
|
|
|
|
const u32 size = ((dataformat >> 8) & 0xf) * sizeof(core::vector2df);
|
|
|
|
for (u32 j = 0; j < mesh.Vertices.size(); ++j) {
|
|
|
|
mesh.TCoords2.push_back(*((core::vector2df *)(dataptr)));
|
2023-10-03 20:37:00 +02:00
|
|
|
dataptr += size;
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] data;
|
|
|
|
if (!checkForOneFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in FVFData found.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in FVFData found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "XSkinMeshHeader") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectSkinMeshHeader(mesh))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "SkinWeights") {
|
|
|
|
// mesh.SkinWeights.push_back(SXSkinWeight());
|
|
|
|
// if (!parseDataObjectSkinWeights(mesh.SkinWeights.getLast()))
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectSkinWeights(mesh))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown data object in mesh in x file", objectName.c_str(), ELL_WARNING);
|
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectSkinWeights(SXMesh &mesh)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading mesh skin weights", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Skin Weights found in .x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
core::stringc TransformNodeName;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!getNextTokenAsString(TransformNodeName)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown syntax while reading transform node name string in .x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.HasSkinning = true;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-02-06 20:22:44 +01:00
|
|
|
auto n = AnimatedMesh->getJointNumber(TransformNodeName.c_str());
|
|
|
|
CSkinnedMesh::SJoint *joint = n.has_value() ? AnimatedMesh->getAllJoints()[*n] : nullptr;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!joint) {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("creating joint for skinning ", TransformNodeName.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
n = AnimatedMesh->getAllJoints().size();
|
2024-03-20 19:35:52 +01:00
|
|
|
joint = AnimatedMesh->addJoint(0);
|
|
|
|
joint->Name = TransformNodeName.c_str();
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read vertex weights
|
|
|
|
const u32 nWeights = readInt();
|
|
|
|
|
|
|
|
// read vertex indices
|
|
|
|
u32 i;
|
|
|
|
|
|
|
|
const u32 jointStart = joint->Weights.size();
|
2024-03-20 19:35:52 +01:00
|
|
|
joint->Weights.reallocate(jointStart + nWeights);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.WeightJoint.reallocate(mesh.WeightJoint.size() + nWeights);
|
|
|
|
mesh.WeightNum.reallocate(mesh.WeightNum.size() + nWeights);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = 0; i < nWeights; ++i) {
|
2024-02-06 20:22:44 +01:00
|
|
|
mesh.WeightJoint.push_back(*n);
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh.WeightNum.push_back(joint->Weights.size());
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
CSkinnedMesh::SWeight *weight = AnimatedMesh->addWeight(joint);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
weight->buffer_id = 0;
|
|
|
|
weight->vertex_id = readInt();
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read vertex weights
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (i = jointStart; i < jointStart + nWeights; ++i)
|
2023-10-03 20:37:00 +02:00
|
|
|
joint->Weights[i].strength = readFloat();
|
|
|
|
|
|
|
|
// read matrix offset
|
|
|
|
|
|
|
|
// transforms the mesh vertices to the space of the bone
|
|
|
|
// When concatenated to the bone's transform, this provides the
|
|
|
|
// world space coordinates of the mesh as affected by the bone
|
2024-03-20 19:35:52 +01:00
|
|
|
core::matrix4 &MatrixOffset = joint->GlobalInversedMatrix;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
readMatrix(MatrixOffset);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForOneFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Skin Weights found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in Skin Weights found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::parseDataObjectSkinMeshHeader(SXMesh &mesh)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading skin mesh header", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Skin Mesh header found in .x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
mesh.MaxSkinWeightsPerVertex = readInt();
|
|
|
|
mesh.MaxSkinWeightsPerFace = readInt();
|
|
|
|
mesh.BoneCount = readInt();
|
|
|
|
|
|
|
|
if (!BinaryFormat)
|
|
|
|
getNextToken(); // skip semicolon
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in skin mesh header in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectMeshNormals(SXMesh &mesh)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: reading mesh normals", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Mesh Normals found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read count
|
|
|
|
const u32 nNormals = readInt();
|
|
|
|
core::array<core::vector3df> normals;
|
|
|
|
normals.set_used(nNormals);
|
|
|
|
|
|
|
|
// read normals
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < nNormals; ++i)
|
2023-10-03 20:37:00 +02:00
|
|
|
readVector3(normals[i]);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForTwoFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Mesh Normals Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
|
|
|
core::array<u32> normalIndices;
|
|
|
|
normalIndices.set_used(mesh.Indices.size());
|
|
|
|
|
|
|
|
// read face normal indices
|
|
|
|
const u32 nFNormals = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
// if (nFNormals >= mesh.IndexCountPerFace.size())
|
2023-10-03 20:37:00 +02:00
|
|
|
if (0) // this condition doesn't work for some reason
|
|
|
|
{
|
|
|
|
os::Printer::log("Too many face normals found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 normalidx = 0;
|
|
|
|
core::array<u32> polygonfaces;
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 k = 0; k < nFNormals; ++k) {
|
2023-10-03 20:37:00 +02:00
|
|
|
const u32 fcnt = readInt();
|
|
|
|
u32 triangles = fcnt - 2;
|
|
|
|
u32 indexcount = triangles * 3;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (indexcount != mesh.IndexCountPerFace[k]) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Not matching normal and face index count found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (indexcount == 3) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// default, only one triangle in this face
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 h = 0; h < 3; ++h) {
|
2023-10-03 20:37:00 +02:00
|
|
|
const u32 normalnum = readInt();
|
|
|
|
mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[normalnum]);
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
polygonfaces.set_used(fcnt);
|
|
|
|
// multiple triangles in this face
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 h = 0; h < fcnt; ++h)
|
2023-10-03 20:37:00 +02:00
|
|
|
polygonfaces[h] = readInt();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 jk = 0; jk < triangles; ++jk) {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[0]]);
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[jk + 1]]);
|
|
|
|
mesh.Vertices[mesh.Indices[normalidx++]].Normal.set(normals[polygonfaces[jk + 2]]);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForTwoFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Mesh Face Normals Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in Mesh Normals found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectMeshTextureCoords(SXMesh &mesh)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: reading mesh texture coordinates", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Mesh Texture Coordinates found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 nCoords = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
// if (nCoords >= mesh.Vertices.size())
|
2023-10-03 20:37:00 +02:00
|
|
|
if (0) // this condition doesn't work for some reason
|
|
|
|
{
|
|
|
|
os::Printer::log("Too many texture coords found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < nCoords; ++i)
|
2023-10-03 20:37:00 +02:00
|
|
|
readVector2(mesh.Vertices[i].TCoords);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForTwoFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Mesh Texture Coordinates Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in Mesh Texture Coordinates Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectMeshVertexColors(SXMesh &mesh)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: reading mesh vertex colors", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace for Mesh Vertex Colors found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.HasVertexColors = true;
|
2023-10-03 20:37:00 +02:00
|
|
|
const u32 nColors = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < nColors; ++i) {
|
|
|
|
const u32 Index = readInt();
|
|
|
|
if (Index >= mesh.Vertices.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("index value in parseDataObjectMeshVertexColors out of bounds", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
readRGBA(mesh.Vertices[Index].Color);
|
|
|
|
checkForOneFollowingSemicolons();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForOneFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No finishing semicolon in Mesh Vertex Colors Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in Mesh Texture Coordinates Array found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectMeshMaterialList(SXMesh &mesh)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading mesh material list", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Mesh Material List found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read material count
|
|
|
|
const u32 nMaterials = readInt();
|
|
|
|
mesh.Materials.reallocate(nMaterials);
|
|
|
|
|
|
|
|
// read non triangulated face material index count
|
|
|
|
const u32 nFaceIndices = readInt();
|
|
|
|
|
|
|
|
// There seems to be a compact representation of "all faces the same material"
|
|
|
|
// being represented as 1;1;0;; which means 1 material, 1 face with first material
|
|
|
|
// all the other faces have to obey then, so check is disabled
|
2024-03-20 19:35:52 +01:00
|
|
|
// if (nFaceIndices != mesh.IndexCountPerFace.size())
|
2023-10-03 20:37:00 +02:00
|
|
|
// os::Printer::log("Index count per face not equal to face material index count in x file.", ELL_WARNING);
|
|
|
|
|
|
|
|
// read non triangulated face indices and create triangulated ones
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh.FaceMaterialIndices.set_used(mesh.Indices.size() / 3);
|
2023-10-03 20:37:00 +02:00
|
|
|
u32 triangulatedindex = 0;
|
|
|
|
u32 ind = 0;
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 tfi = 0; tfi < mesh.IndexCountPerFace.size(); ++tfi) {
|
|
|
|
if (tfi < nFaceIndices)
|
2023-10-03 20:37:00 +02:00
|
|
|
ind = readInt();
|
2024-03-20 19:35:52 +01:00
|
|
|
if (ind >= core::max_(nMaterials, 1U)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Out of range index found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
const u32 fc = mesh.IndexCountPerFace[tfi] / 3;
|
|
|
|
for (u32 k = 0; k < fc; ++k)
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh.FaceMaterialIndices[triangulatedindex++] = ind;
|
|
|
|
}
|
|
|
|
|
|
|
|
// in version 03.02, the face indices end with two semicolons.
|
|
|
|
// commented out version check, as version 03.03 exported from blender also has 2 semicolons
|
|
|
|
if (!BinaryFormat) // && MajorVersion == 3 && MinorVersion <= 2)
|
|
|
|
{
|
|
|
|
if (P[0] == ';')
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read following data objects
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc objectName = getNextToken();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (objectName.size() == 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unexpected ending found in Mesh Material list in .x file.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "}") {
|
2023-10-03 20:37:00 +02:00
|
|
|
break; // material list finished
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "{") {
|
2023-10-03 20:37:00 +02:00
|
|
|
// template materials now available thanks to joeWright
|
|
|
|
objectName = getNextToken();
|
|
|
|
mesh.Materials.push_back(video::SMaterial());
|
|
|
|
getNextToken(); // skip }
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "Material") {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh.Materials.push_back(video::SMaterial());
|
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == ";") {
|
2023-10-03 20:37:00 +02:00
|
|
|
// ignore
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown data object in material list in x file", objectName.c_str(), ELL_WARNING);
|
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectAnimationSet()
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: Reading animation set", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
core::stringc AnimationName;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject(&AnimationName)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Animation Set found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
os::Printer::log("Reading animationset ", AnimationName, ELL_DEBUG);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc objectName = getNextToken();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (objectName.size() == 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unexpected ending found in Animation set in x file.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "}") {
|
2023-10-03 20:37:00 +02:00
|
|
|
break; // animation set finished
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "Animation") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectAnimation())
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown data object in animation set in x file", objectName.c_str(), ELL_WARNING);
|
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectAnimationTicksPerSecond()
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: reading AnimationTicksPerSecond", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Animation found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 ticks = readInt();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForOneFollowingSemicolons()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing semicolon in AnimationTicksPerSecond in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in AnimationTicksPerSecond in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimatedMesh->setAnimationSpeed(static_cast<irr::f32>(ticks));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectAnimation()
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: reading animation", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Animation found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// anim.closed = true;
|
|
|
|
// anim.linearPositionQuality = true;
|
2023-10-03 20:37:00 +02:00
|
|
|
CSkinnedMesh::SJoint animationDump;
|
|
|
|
|
|
|
|
core::stringc FrameName;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc objectName = getNextToken();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (objectName.size() == 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unexpected ending found in Animation in x file.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "}") {
|
2023-10-03 20:37:00 +02:00
|
|
|
break; // animation finished
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "AnimationKey") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseDataObjectAnimationKey(&animationDump))
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "AnimationOptions") {
|
|
|
|
// TODO: parse options.
|
2023-10-03 20:37:00 +02:00
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (objectName == "{") {
|
2023-10-03 20:37:00 +02:00
|
|
|
// read frame name
|
|
|
|
FrameName = getNextToken();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unexpected ending found in Animation in x file.", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown data object in animation in x file", objectName.c_str(), ELL_WARNING);
|
|
|
|
if (!parseUnknownDataObject())
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (FrameName.size() != 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("frame name", FrameName.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
2024-02-06 20:22:44 +01:00
|
|
|
auto n = AnimatedMesh->getJointNumber(FrameName.c_str());
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-02-06 20:22:44 +01:00
|
|
|
CSkinnedMesh::SJoint *joint;
|
|
|
|
if (n.has_value()) {
|
|
|
|
joint = AnimatedMesh->getAllJoints()[*n];
|
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("creating joint for animation ", FrameName.c_str(), ELL_DEBUG);
|
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
joint = AnimatedMesh->addJoint(0);
|
|
|
|
joint->Name = FrameName.c_str();
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
joint->PositionKeys.reallocate(joint->PositionKeys.size() + animationDump.PositionKeys.size());
|
|
|
|
for (u32 n = 0; n < animationDump.PositionKeys.size(); ++n) {
|
2023-10-03 20:37:00 +02:00
|
|
|
joint->PositionKeys.push_back(animationDump.PositionKeys[n]);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
joint->ScaleKeys.reallocate(joint->ScaleKeys.size() + animationDump.ScaleKeys.size());
|
|
|
|
for (u32 n = 0; n < animationDump.ScaleKeys.size(); ++n) {
|
2023-10-03 20:37:00 +02:00
|
|
|
joint->ScaleKeys.push_back(animationDump.ScaleKeys[n]);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
joint->RotationKeys.reallocate(joint->RotationKeys.size() + animationDump.RotationKeys.size());
|
|
|
|
for (u32 n = 0; n < animationDump.RotationKeys.size(); ++n) {
|
2023-10-03 20:37:00 +02:00
|
|
|
joint->RotationKeys.push_back(animationDump.RotationKeys[n]);
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("joint name was never given", ELL_WARNING);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseDataObjectAnimationKey(ISkinnedMesh::SJoint *joint)
|
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: reading animation key", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Animation Key found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read key type
|
|
|
|
|
|
|
|
const u32 keyType = readInt();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (keyType > 4) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown key type found in Animation Key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read number of keys
|
|
|
|
const u32 numberOfKeys = readInt();
|
|
|
|
|
|
|
|
// eat the semicolon after the "0". if there are keys present, readInt()
|
|
|
|
// does this for us. If there aren't, we need to do it explicitly
|
|
|
|
if (numberOfKeys == 0)
|
|
|
|
checkForOneFollowingSemicolons();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < numberOfKeys; ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// read time
|
|
|
|
const f32 time = (f32)readInt();
|
|
|
|
|
|
|
|
// read keys
|
2024-03-20 19:35:52 +01:00
|
|
|
switch (keyType) {
|
|
|
|
case 0: // rotation
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
// read quaternions
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// read count
|
|
|
|
if (readInt() != 4) {
|
|
|
|
os::Printer::log("Expected 4 numbers in animation key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
f32 W = -readFloat();
|
|
|
|
f32 X = -readFloat();
|
|
|
|
f32 Y = -readFloat();
|
|
|
|
f32 Z = -readFloat();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForTwoFollowingSemicolons()) {
|
|
|
|
os::Printer::log("No finishing semicolon after quaternion animation key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
ISkinnedMesh::SRotationKey *key = AnimatedMesh->addRotationKey(joint);
|
|
|
|
key->frame = time;
|
|
|
|
key->rotation.set(X, Y, Z, W);
|
|
|
|
key->rotation.normalize();
|
|
|
|
} break;
|
|
|
|
case 1: // scale
|
|
|
|
case 2: // position
|
|
|
|
{
|
|
|
|
// read vectors
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// read count
|
|
|
|
if (readInt() != 3) {
|
|
|
|
os::Printer::log("Expected 3 numbers in animation key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
core::vector3df vector;
|
|
|
|
readVector3(vector);
|
|
|
|
|
|
|
|
if (!checkForTwoFollowingSemicolons()) {
|
|
|
|
os::Printer::log("No finishing semicolon after vector animation key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
|
|
|
|
if (keyType == 2) {
|
|
|
|
ISkinnedMesh::SPositionKey *key = AnimatedMesh->addPositionKey(joint);
|
|
|
|
key->frame = time;
|
|
|
|
key->position = vector;
|
|
|
|
} else {
|
|
|
|
ISkinnedMesh::SScaleKey *key = AnimatedMesh->addScaleKey(joint);
|
|
|
|
key->frame = time;
|
|
|
|
key->scale = vector;
|
|
|
|
}
|
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
case 3:
|
2024-03-20 19:35:52 +01:00
|
|
|
case 4: {
|
|
|
|
// read matrix
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// read count
|
|
|
|
if (readInt() != 16) {
|
|
|
|
os::Printer::log("Expected 16 numbers in animation key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// read matrix
|
|
|
|
core::matrix4 mat(core::matrix4::EM4CONST_NOTHING);
|
|
|
|
readMatrix(mat);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// mat=joint->LocalMatrix*mat;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForOneFollowingSemicolons()) {
|
|
|
|
os::Printer::log("No finishing semicolon after matrix animation key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
}
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// core::vector3df rotation = mat.getRotationDegrees();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
ISkinnedMesh::SRotationKey *keyR = AnimatedMesh->addRotationKey(joint);
|
|
|
|
keyR->frame = time;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
// IRR_TEST_BROKEN_QUATERNION_USE: TODO - switched from mat to mat.getTransposed() for downward compatibility.
|
|
|
|
// Not tested so far if this was correct or wrong before quaternion fix!
|
|
|
|
keyR->rotation = core::quaternion(mat.getTransposed());
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
ISkinnedMesh::SPositionKey *keyP = AnimatedMesh->addPositionKey(joint);
|
|
|
|
keyP->frame = time;
|
|
|
|
keyP->position = mat.getTranslation();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
/*
|
|
|
|
core::vector3df scale=mat.getScale();
|
|
|
|
|
|
|
|
if (scale.X==0)
|
|
|
|
scale.X=1;
|
|
|
|
if (scale.Y==0)
|
|
|
|
scale.Y=1;
|
|
|
|
if (scale.Z==0)
|
|
|
|
scale.Z=1;
|
|
|
|
ISkinnedMesh::SScaleKey *keyS=AnimatedMesh->addScaleKey(joint);
|
|
|
|
keyS->frame=time;
|
|
|
|
keyS->scale=scale;
|
|
|
|
*/
|
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
} // end switch
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkForOneFollowingSemicolons())
|
|
|
|
--P;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in animation key in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::parseDataObjectTextureFilename(core::stringc &texturename)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
#ifdef _XREADER_DEBUG
|
|
|
|
os::Printer::log("CXFileReader: reading texture filename", ELL_DEBUG);
|
|
|
|
#endif
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!readHeadOfDataObject()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No opening brace in Texture filename found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!getNextTokenAsString(texturename)) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Unknown syntax while reading texture filename string in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!checkForClosingBrace()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("No closing brace in Texture filename found in x file", ELL_WARNING);
|
|
|
|
os::Printer::log("Line", core::stringc(Line).c_str(), ELL_WARNING);
|
|
|
|
SET_ERR_AND_RETURN();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CXMeshFileLoader::parseUnknownDataObject()
|
|
|
|
{
|
|
|
|
// find opening delimiter
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc t = getNextToken();
|
|
|
|
|
|
|
|
if (t.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (t == "{")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 counter = 1;
|
|
|
|
|
|
|
|
// parse until closing delimiter
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (counter) {
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc t = getNextToken();
|
|
|
|
|
|
|
|
if (t.size() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (t == "{")
|
|
|
|
++counter;
|
2024-03-20 19:35:52 +01:00
|
|
|
else if (t == "}")
|
2023-10-03 20:37:00 +02:00
|
|
|
--counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! checks for closing curly brace, returns false if not there
|
|
|
|
bool CXMeshFileLoader::checkForClosingBrace()
|
|
|
|
{
|
|
|
|
return (getNextToken() == "}");
|
|
|
|
}
|
|
|
|
|
|
|
|
//! checks for one following semicolon, returns false if not there
|
|
|
|
bool CXMeshFileLoader::checkForOneFollowingSemicolons()
|
|
|
|
{
|
|
|
|
if (BinaryFormat)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (getNextToken() == ";")
|
|
|
|
return true;
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
2023-10-03 20:37:00 +02:00
|
|
|
--P;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! checks for two following semicolons, returns false if they are not there
|
|
|
|
bool CXMeshFileLoader::checkForTwoFollowingSemicolons()
|
|
|
|
{
|
|
|
|
if (BinaryFormat)
|
|
|
|
return true;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 k = 0; k < 2; ++k) {
|
|
|
|
if (getNextToken() != ";") {
|
2023-10-03 20:37:00 +02:00
|
|
|
--P;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! reads header of dataobject including the opening brace.
|
|
|
|
//! returns false if error happened, and writes name of object
|
|
|
|
//! if there is one
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::readHeadOfDataObject(core::stringc *outname)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
core::stringc nameOrBrace = getNextToken();
|
2024-03-20 19:35:52 +01:00
|
|
|
if (nameOrBrace != "{") {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (outname)
|
|
|
|
(*outname) = nameOrBrace;
|
|
|
|
|
|
|
|
if (getNextToken() != "{")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! returns next parseable token. Returns empty string if no token there
|
|
|
|
core::stringc CXMeshFileLoader::getNextToken()
|
|
|
|
{
|
|
|
|
core::stringc s;
|
|
|
|
|
|
|
|
// process binary-formatted file
|
2024-03-20 19:35:52 +01:00
|
|
|
if (BinaryFormat) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// in binary mode it will only return NAME and STRING token
|
|
|
|
// and (correctly) skip over other tokens.
|
|
|
|
|
|
|
|
s16 tok = readBinWord();
|
|
|
|
u32 len;
|
|
|
|
|
|
|
|
// standalone tokens
|
|
|
|
switch (tok) {
|
2024-03-20 19:35:52 +01:00
|
|
|
case 1:
|
|
|
|
// name token
|
|
|
|
len = readBinDWord();
|
|
|
|
s = core::stringc(P, len);
|
|
|
|
P += len;
|
|
|
|
return s;
|
|
|
|
case 2:
|
|
|
|
// string token
|
|
|
|
len = readBinDWord();
|
|
|
|
s = core::stringc(P, len);
|
|
|
|
P += (len + 2);
|
|
|
|
return s;
|
|
|
|
case 3:
|
|
|
|
// integer token
|
|
|
|
P += 4;
|
|
|
|
return "<integer>";
|
|
|
|
case 5:
|
|
|
|
// GUID token
|
|
|
|
P += 16;
|
|
|
|
return "<guid>";
|
|
|
|
case 6:
|
|
|
|
len = readBinDWord();
|
|
|
|
P += (len * 4);
|
|
|
|
return "<int_list>";
|
|
|
|
case 7:
|
|
|
|
len = readBinDWord();
|
|
|
|
P += (len * FloatSize);
|
|
|
|
return "<flt_list>";
|
|
|
|
case 0x0a:
|
|
|
|
return "{";
|
|
|
|
case 0x0b:
|
|
|
|
return "}";
|
|
|
|
case 0x0c:
|
|
|
|
return "(";
|
|
|
|
case 0x0d:
|
|
|
|
return ")";
|
|
|
|
case 0x0e:
|
|
|
|
return "[";
|
|
|
|
case 0x0f:
|
|
|
|
return "]";
|
|
|
|
case 0x10:
|
|
|
|
return "<";
|
|
|
|
case 0x11:
|
|
|
|
return ">";
|
|
|
|
case 0x12:
|
|
|
|
return ".";
|
|
|
|
case 0x13:
|
|
|
|
return ",";
|
|
|
|
case 0x14:
|
|
|
|
return ";";
|
|
|
|
case 0x1f:
|
|
|
|
return "template";
|
|
|
|
case 0x28:
|
|
|
|
return "WORD";
|
|
|
|
case 0x29:
|
|
|
|
return "DWORD";
|
|
|
|
case 0x2a:
|
|
|
|
return "FLOAT";
|
|
|
|
case 0x2b:
|
|
|
|
return "DOUBLE";
|
|
|
|
case 0x2c:
|
|
|
|
return "CHAR";
|
|
|
|
case 0x2d:
|
|
|
|
return "UCHAR";
|
|
|
|
case 0x2e:
|
|
|
|
return "SWORD";
|
|
|
|
case 0x2f:
|
|
|
|
return "SDWORD";
|
|
|
|
case 0x30:
|
|
|
|
return "void";
|
|
|
|
case 0x31:
|
|
|
|
return "string";
|
|
|
|
case 0x32:
|
|
|
|
return "unicode";
|
|
|
|
case 0x33:
|
|
|
|
return "cstring";
|
|
|
|
case 0x34:
|
|
|
|
return "array";
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// process text-formatted file
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
2023-10-03 20:37:00 +02:00
|
|
|
findNextNoneWhiteSpace();
|
|
|
|
|
|
|
|
if (P >= End)
|
|
|
|
return s;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while ((P < End) && !core::isspace(P[0])) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// either keep token delimiters when already holding a token, or return if first valid char
|
2024-03-20 19:35:52 +01:00
|
|
|
if (P[0] == ';' || P[0] == '}' || P[0] == '{' || P[0] == ',') {
|
|
|
|
if (!s.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
s.append(P[0]);
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
break; // stop for delimiter
|
|
|
|
}
|
|
|
|
s.append(P[0]);
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! places pointer to next begin of a token, which must be a number,
|
|
|
|
// and ignores comments
|
|
|
|
void CXMeshFileLoader::findNextNoneWhiteSpaceNumber()
|
|
|
|
{
|
|
|
|
if (BinaryFormat)
|
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while ((P < End) && (P[0] != '-') && (P[0] != '.') &&
|
|
|
|
!(core::isdigit(P[0]))) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// check if this is a comment
|
|
|
|
if ((P[0] == '/' && P[1] == '/') || P[0] == '#')
|
|
|
|
readUntilEndOfLine();
|
|
|
|
else
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// places pointer to next begin of a token, and ignores comments
|
|
|
|
void CXMeshFileLoader::findNextNoneWhiteSpace()
|
|
|
|
{
|
|
|
|
if (BinaryFormat)
|
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (true) {
|
|
|
|
while ((P < End) && core::isspace(P[0])) {
|
|
|
|
if (*P == '\n')
|
2023-10-03 20:37:00 +02:00
|
|
|
++Line;
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (P >= End)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// check if this is a comment
|
|
|
|
if ((P[0] == '/' && P[1] == '/') ||
|
2024-03-20 19:35:52 +01:00
|
|
|
P[0] == '#')
|
2023-10-03 20:37:00 +02:00
|
|
|
readUntilEndOfLine();
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! reads a x file style string
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::getNextTokenAsString(core::stringc &out)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (BinaryFormat) {
|
|
|
|
out = getNextToken();
|
2023-10-03 20:37:00 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
findNextNoneWhiteSpace();
|
|
|
|
|
|
|
|
if (P >= End)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (P[0] != '"')
|
|
|
|
return false;
|
|
|
|
++P;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (P < End && P[0] != '"') {
|
2023-10-03 20:37:00 +02:00
|
|
|
out.append(P[0]);
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (P[1] != ';' || P[0] != '"')
|
2023-10-03 20:37:00 +02:00
|
|
|
return false;
|
2024-03-20 19:35:52 +01:00
|
|
|
P += 2;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CXMeshFileLoader::readUntilEndOfLine()
|
|
|
|
{
|
|
|
|
if (BinaryFormat)
|
|
|
|
return;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (P < End) {
|
|
|
|
if (P[0] == '\n' || P[0] == '\r') {
|
2023-10-03 20:37:00 +02:00
|
|
|
++P;
|
|
|
|
++Line;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
++P;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 CXMeshFileLoader::readBinWord()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (P >= End)
|
2023-10-03 20:37:00 +02:00
|
|
|
return 0;
|
|
|
|
#ifdef __BIG_ENDIAN__
|
|
|
|
const u16 tmp = os::Byteswap::byteswap(*(u16 *)P);
|
|
|
|
#else
|
|
|
|
const u16 tmp = *(u16 *)P;
|
|
|
|
#endif
|
|
|
|
P += 2;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 CXMeshFileLoader::readBinDWord()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (P >= End)
|
2023-10-03 20:37:00 +02:00
|
|
|
return 0;
|
|
|
|
#ifdef __BIG_ENDIAN__
|
|
|
|
const u32 tmp = os::Byteswap::byteswap(*(u32 *)P);
|
|
|
|
#else
|
|
|
|
const u32 tmp = *(u32 *)P;
|
|
|
|
#endif
|
|
|
|
P += 4;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 CXMeshFileLoader::readInt()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (BinaryFormat) {
|
|
|
|
if (!BinaryNumCount) {
|
2023-10-03 20:37:00 +02:00
|
|
|
const u16 tmp = readBinWord(); // 0x06 or 0x03
|
|
|
|
if (tmp == 0x06)
|
|
|
|
BinaryNumCount = readBinDWord();
|
|
|
|
else
|
|
|
|
BinaryNumCount = 1; // single int
|
|
|
|
}
|
|
|
|
--BinaryNumCount;
|
|
|
|
return readBinDWord();
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
findNextNoneWhiteSpaceNumber();
|
|
|
|
return core::strtoul10(P, &P);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f32 CXMeshFileLoader::readFloat()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (BinaryFormat) {
|
|
|
|
if (!BinaryNumCount) {
|
2023-10-03 20:37:00 +02:00
|
|
|
const u16 tmp = readBinWord(); // 0x07 or 0x42
|
|
|
|
if (tmp == 0x07)
|
|
|
|
BinaryNumCount = readBinDWord();
|
|
|
|
else
|
|
|
|
BinaryNumCount = 1; // single int
|
|
|
|
}
|
|
|
|
--BinaryNumCount;
|
2024-03-20 19:35:52 +01:00
|
|
|
if (FloatSize == 8) {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef __BIG_ENDIAN__
|
2024-03-20 19:35:52 +01:00
|
|
|
// TODO: Check if data is properly converted here
|
2023-10-03 20:37:00 +02:00
|
|
|
f32 ctmp[2];
|
2024-03-20 19:35:52 +01:00
|
|
|
ctmp[1] = os::Byteswap::byteswap(*(f32 *)P);
|
|
|
|
ctmp[0] = os::Byteswap::byteswap(*(f32 *)P + 4);
|
|
|
|
const f32 tmp = (f32)(*(f64 *)(void *)ctmp);
|
2023-10-03 20:37:00 +02:00
|
|
|
#else
|
|
|
|
const f32 tmp = (f32)(*(f64 *)P);
|
|
|
|
#endif
|
|
|
|
P += 8;
|
|
|
|
return tmp;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef __BIG_ENDIAN__
|
|
|
|
const f32 tmp = os::Byteswap::byteswap(*(f32 *)P);
|
|
|
|
#else
|
|
|
|
const f32 tmp = *(f32 *)P;
|
|
|
|
#endif
|
|
|
|
P += 4;
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
findNextNoneWhiteSpaceNumber();
|
|
|
|
f32 ftmp;
|
|
|
|
P = core::fast_atof_move(P, ftmp);
|
|
|
|
return ftmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read 2-dimensional vector. Stops at semicolon after second value for text file format
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::readVector2(core::vector2df &vec)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
vec.X = readFloat();
|
|
|
|
vec.Y = readFloat();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read 3-dimensional vector. Stops at semicolon after third value for text file format
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::readVector3(core::vector3df &vec)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
vec.X = readFloat();
|
|
|
|
vec.Y = readFloat();
|
|
|
|
vec.Z = readFloat();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read color without alpha value. Stops after second semicolon after blue value
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::readRGB(video::SColor &color)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
video::SColorf tmpColor;
|
|
|
|
tmpColor.r = readFloat();
|
|
|
|
tmpColor.g = readFloat();
|
|
|
|
tmpColor.b = readFloat();
|
|
|
|
color = tmpColor.toSColor();
|
|
|
|
return checkForOneFollowingSemicolons();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read color with alpha value. Stops after second semicolon after blue value
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::readRGBA(video::SColor &color)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
video::SColorf tmpColor;
|
|
|
|
tmpColor.r = readFloat();
|
|
|
|
tmpColor.g = readFloat();
|
|
|
|
tmpColor.b = readFloat();
|
|
|
|
tmpColor.a = readFloat();
|
|
|
|
color = tmpColor.toSColor();
|
|
|
|
return checkForOneFollowingSemicolons();
|
|
|
|
}
|
|
|
|
|
|
|
|
// read matrix from list of floats
|
2024-03-20 19:35:52 +01:00
|
|
|
bool CXMeshFileLoader::readMatrix(core::matrix4 &mat)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < 16; ++i)
|
2023-10-03 20:37:00 +02:00
|
|
|
mat[i] = readFloat();
|
|
|
|
return checkForOneFollowingSemicolons();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|