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 "COBJMeshFileLoader.h"
|
|
|
|
#include "IMeshManipulator.h"
|
|
|
|
#include "IVideoDriver.h"
|
|
|
|
#include "SMesh.h"
|
|
|
|
#include "SMeshBuffer.h"
|
|
|
|
#include "SAnimatedMesh.h"
|
|
|
|
#include "IReadFile.h"
|
|
|
|
#include "IAttributes.h"
|
|
|
|
#include "fast_atof.h"
|
|
|
|
#include "coreutil.h"
|
|
|
|
#include "os.h"
|
|
|
|
|
|
|
|
namespace irr
|
|
|
|
{
|
|
|
|
namespace scene
|
|
|
|
{
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define _IRR_DEBUG_OBJ_LOADER_
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//! Constructor
|
2024-03-20 19:35:52 +01:00
|
|
|
COBJMeshFileLoader::COBJMeshFileLoader(scene::ISceneManager *smgr) :
|
|
|
|
SceneManager(smgr)
|
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("COBJMeshFileLoader");
|
2024-03-20 19:35:52 +01:00
|
|
|
#endif
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//! destructor
|
|
|
|
COBJMeshFileLoader::~COBJMeshFileLoader()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//! 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 COBJMeshFileLoader::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, "obj");
|
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 *COBJMeshFileLoader::createMesh(io::IReadFile *file)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
if (!file)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const long filesize = file->getSize();
|
|
|
|
if (!filesize)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
const u32 WORD_BUFFER_LENGTH = 512;
|
|
|
|
|
|
|
|
core::array<core::vector3df> vertexBuffer(1000);
|
|
|
|
core::array<core::vector3df> normalsBuffer(1000);
|
|
|
|
core::array<core::vector2df> textureCoordBuffer(1000);
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
SObjMtl *currMtl = new SObjMtl();
|
2023-10-03 20:37:00 +02:00
|
|
|
Materials.push_back(currMtl);
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 smoothingGroup = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
const io::path fullName = file->getFileName();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
c8 *buf = new c8[filesize + 1]; // plus null-terminator
|
|
|
|
memset(buf, 0, filesize + 1);
|
|
|
|
file->read((void *)buf, filesize);
|
|
|
|
const c8 *const bufEnd = buf + filesize;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
// Process obj information
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *bufPtr = buf;
|
2023-10-03 20:37:00 +02:00
|
|
|
core::stringc grpName, mtlName;
|
2024-03-20 19:35:52 +01:00
|
|
|
bool mtlChanged = false;
|
2023-10-03 20:37:00 +02:00
|
|
|
bool useGroups = !SceneManager->getParameters()->getAttributeAsBool(OBJ_LOADER_IGNORE_GROUPS);
|
|
|
|
bool useMaterials = !SceneManager->getParameters()->getAttributeAsBool(OBJ_LOADER_IGNORE_MATERIAL_FILES);
|
2024-03-20 19:35:52 +01:00
|
|
|
[[maybe_unused]] irr::u32 lineNr = 1; // only counts non-empty lines, still useful in debugging to locate errors
|
2023-10-03 20:37:00 +02:00
|
|
|
core::array<int> faceCorners;
|
|
|
|
faceCorners.reallocate(32); // should be large enough
|
|
|
|
const core::stringc TAG_OFF = "off";
|
|
|
|
irr::u32 degeneratedFaces = 0;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
while (bufPtr != bufEnd) {
|
|
|
|
switch (bufPtr[0]) {
|
|
|
|
case 'm': // mtllib (material)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
if (useMaterials) {
|
2023-10-03 20:37:00 +02:00
|
|
|
c8 name[WORD_BUFFER_LENGTH];
|
|
|
|
bufPtr = goAndCopyNextWord(name, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
|
|
|
#ifdef _IRR_DEBUG_OBJ_LOADER_
|
2024-03-20 19:35:52 +01:00
|
|
|
os::Printer::log("Ignoring material file", name);
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
case 'v': // v, vn, vt
|
|
|
|
switch (bufPtr[1]) {
|
|
|
|
case ' ': // vertex
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
core::vector3df vec;
|
|
|
|
bufPtr = readVec3(bufPtr, vec, bufEnd);
|
|
|
|
vertexBuffer.push_back(vec);
|
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
case 'n': // normal
|
|
|
|
{
|
|
|
|
core::vector3df vec;
|
|
|
|
bufPtr = readVec3(bufPtr, vec, bufEnd);
|
|
|
|
normalsBuffer.push_back(vec);
|
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
case 't': // texcoord
|
|
|
|
{
|
|
|
|
core::vector2df vec;
|
|
|
|
bufPtr = readUV(bufPtr, vec, bufEnd);
|
|
|
|
textureCoordBuffer.push_back(vec);
|
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g': // group name
|
2024-03-20 19:35:52 +01:00
|
|
|
{
|
|
|
|
c8 grp[WORD_BUFFER_LENGTH];
|
|
|
|
bufPtr = goAndCopyNextWord(grp, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _IRR_DEBUG_OBJ_LOADER_
|
2024-03-20 19:35:52 +01:00
|
|
|
os::Printer::log("Loaded group start", grp, ELL_DEBUG);
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
if (useGroups) {
|
|
|
|
if (0 != grp[0])
|
|
|
|
grpName = grp;
|
|
|
|
else
|
|
|
|
grpName = "default";
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
mtlChanged = true;
|
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
case 's': // smoothing can be a group or off (equiv. to 0)
|
2024-03-20 19:35:52 +01:00
|
|
|
{
|
|
|
|
c8 smooth[WORD_BUFFER_LENGTH];
|
|
|
|
bufPtr = goAndCopyNextWord(smooth, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
2023-10-03 20:37:00 +02:00
|
|
|
#ifdef _IRR_DEBUG_OBJ_LOADER_
|
2024-03-20 19:35:52 +01:00
|
|
|
os::Printer::log("Loaded smoothing group start", smooth, ELL_DEBUG);
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
if (TAG_OFF == smooth)
|
|
|
|
smoothingGroup = 0;
|
|
|
|
else
|
|
|
|
smoothingGroup = core::strtoul10(smooth);
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
(void)smoothingGroup; // disable unused variable warnings
|
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
case 'u': // usemtl
|
|
|
|
// get name of material
|
|
|
|
{
|
|
|
|
c8 matName[WORD_BUFFER_LENGTH];
|
|
|
|
bufPtr = goAndCopyNextWord(matName, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
|
|
|
#ifdef _IRR_DEBUG_OBJ_LOADER_
|
2024-03-20 19:35:52 +01:00
|
|
|
os::Printer::log("Loaded material start", matName, ELL_DEBUG);
|
2023-10-03 20:37:00 +02:00
|
|
|
#endif
|
2024-03-20 19:35:52 +01:00
|
|
|
mtlName = matName;
|
|
|
|
mtlChanged = true;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
case 'f': // face
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
c8 vertexWord[WORD_BUFFER_LENGTH]; // for retrieving vertex data
|
|
|
|
video::S3DVertex v;
|
|
|
|
// Assign vertex color from currently active material's diffuse color
|
2024-03-20 19:35:52 +01:00
|
|
|
if (mtlChanged) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// retrieve the material
|
|
|
|
SObjMtl *useMtl = findMtl(mtlName, grpName);
|
|
|
|
// only change material if we found it
|
|
|
|
if (useMtl)
|
|
|
|
currMtl = useMtl;
|
2024-03-20 19:35:52 +01:00
|
|
|
mtlChanged = false;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
if (currMtl)
|
|
|
|
v.Color = currMtl->Meshbuffer->Material.DiffuseColor;
|
|
|
|
|
|
|
|
// get all vertices data in this face (current line of obj file)
|
|
|
|
const core::stringc wordBuffer = copyLine(bufPtr, bufEnd);
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *linePtr = wordBuffer.c_str();
|
|
|
|
const c8 *const endPtr = linePtr + wordBuffer.size();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
faceCorners.set_used(0); // fast clear
|
|
|
|
|
|
|
|
// read in all vertices
|
|
|
|
linePtr = goNextWord(linePtr, endPtr);
|
2024-03-20 19:35:52 +01:00
|
|
|
while (0 != linePtr[0]) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// Array to communicate with retrieveVertexIndices()
|
|
|
|
// sends the buffer sizes and gets the actual indices
|
|
|
|
// if index not set returns -1
|
|
|
|
s32 Idx[3];
|
|
|
|
Idx[0] = Idx[1] = Idx[2] = -1;
|
|
|
|
|
|
|
|
// read in next vertex's data
|
|
|
|
u32 wlength = copyWord(vertexWord, linePtr, WORD_BUFFER_LENGTH, endPtr);
|
|
|
|
// this function will also convert obj's 1-based index to c++'s 0-based index
|
2024-03-20 19:35:52 +01:00
|
|
|
retrieveVertexIndices(vertexWord, Idx, vertexWord + wlength + 1, vertexBuffer.size(), textureCoordBuffer.size(), normalsBuffer.size());
|
|
|
|
if (Idx[0] >= 0 && Idx[0] < (irr::s32)vertexBuffer.size())
|
2023-10-03 20:37:00 +02:00
|
|
|
v.Pos = vertexBuffer[Idx[0]];
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Invalid vertex index in this line", wordBuffer.c_str(), ELL_ERROR);
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] buf;
|
2023-10-03 20:37:00 +02:00
|
|
|
cleanUp();
|
|
|
|
return 0;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if (Idx[1] >= 0 && Idx[1] < (irr::s32)textureCoordBuffer.size())
|
2023-10-03 20:37:00 +02:00
|
|
|
v.TCoords = textureCoordBuffer[Idx[1]];
|
|
|
|
else
|
2024-03-20 19:35:52 +01:00
|
|
|
v.TCoords.set(0.0f, 0.0f);
|
|
|
|
if (Idx[2] >= 0 && Idx[2] < (irr::s32)normalsBuffer.size())
|
2023-10-03 20:37:00 +02:00
|
|
|
v.Normal = normalsBuffer[Idx[2]];
|
2024-03-20 19:35:52 +01:00
|
|
|
else {
|
|
|
|
v.Normal.set(0.0f, 0.0f, 0.0f);
|
|
|
|
currMtl->RecalculateNormals = true;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int vertLocation;
|
|
|
|
auto n = currMtl->VertMap.find(v);
|
2024-03-20 19:35:52 +01:00
|
|
|
if (n != currMtl->VertMap.end()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
vertLocation = n->second;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
currMtl->Meshbuffer->Vertices.push_back(v);
|
2024-03-20 19:35:52 +01:00
|
|
|
vertLocation = currMtl->Meshbuffer->Vertices.size() - 1;
|
2023-10-03 20:37:00 +02:00
|
|
|
currMtl->VertMap.emplace(v, vertLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
faceCorners.push_back(vertLocation);
|
|
|
|
|
|
|
|
// go to next vertex
|
|
|
|
linePtr = goNextWord(linePtr, endPtr);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (faceCorners.size() < 3) {
|
2023-10-03 20:37:00 +02:00
|
|
|
os::Printer::log("Too few vertices in this line", wordBuffer.c_str(), ELL_ERROR);
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] buf;
|
2023-10-03 20:37:00 +02:00
|
|
|
cleanUp();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// triangulate the face
|
|
|
|
const int c = faceCorners[0];
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 1; i < faceCorners.size() - 1; ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// Add a triangle
|
|
|
|
const int a = faceCorners[i + 1];
|
|
|
|
const int b = faceCorners[i];
|
2024-03-20 19:35:52 +01:00
|
|
|
if (a != b && a != c && b != c) // ignore degenerated faces. We can get them when we merge vertices above in the VertMap.
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
currMtl->Meshbuffer->Indices.push_back(a);
|
|
|
|
currMtl->Meshbuffer->Indices.push_back(b);
|
|
|
|
currMtl->Meshbuffer->Indices.push_back(c);
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
++degeneratedFaces;
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} break;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
case '#': // comment
|
|
|
|
default:
|
|
|
|
break;
|
2024-03-20 19:35:52 +01:00
|
|
|
} // end switch(bufPtr[0])
|
2023-10-03 20:37:00 +02:00
|
|
|
// eat up rest of line
|
|
|
|
bufPtr = goNextLine(bufPtr, bufEnd);
|
|
|
|
++lineNr;
|
2024-03-20 19:35:52 +01:00
|
|
|
} // end while(bufPtr && (bufPtr-buf<filesize))
|
2023-10-03 20:37:00 +02:00
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
if (degeneratedFaces > 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
irr::core::stringc log(degeneratedFaces);
|
|
|
|
log += " degenerated faces removed in ";
|
|
|
|
log += irr::core::stringc(fullName);
|
|
|
|
os::Printer::log(log.c_str(), ELL_INFORMATION);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
SMesh *mesh = new SMesh();
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
// Combine all the groups (meshbuffers) into the mesh
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 m = 0; m < Materials.size(); ++m) {
|
|
|
|
if (Materials[m]->Meshbuffer->getIndexCount() > 0) {
|
2023-10-03 20:37:00 +02:00
|
|
|
Materials[m]->Meshbuffer->recalculateBoundingBox();
|
|
|
|
if (Materials[m]->RecalculateNormals)
|
|
|
|
SceneManager->getMeshManipulator()->recalculateNormals(Materials[m]->Meshbuffer);
|
2024-03-20 19:35:52 +01:00
|
|
|
mesh->addMeshBuffer(Materials[m]->Meshbuffer);
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the Animated mesh if there's anything in the mesh
|
2024-03-20 19:35:52 +01:00
|
|
|
SAnimatedMesh *animMesh = 0;
|
|
|
|
if (0 != mesh->getMeshBufferCount()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
mesh->recalculateBoundingBox();
|
|
|
|
animMesh = new SAnimatedMesh();
|
|
|
|
animMesh->Type = EAMT_OBJ;
|
|
|
|
animMesh->addMesh(mesh);
|
|
|
|
animMesh->recalculateBoundingBox();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up the allocate obj file contents
|
2024-03-20 19:35:52 +01:00
|
|
|
delete[] buf;
|
2023-10-03 20:37:00 +02:00
|
|
|
// more cleaning up
|
|
|
|
cleanUp();
|
|
|
|
mesh->drop();
|
|
|
|
|
|
|
|
return animMesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Read RGB color
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::readColor(const c8 *bufPtr, video::SColor &color, const c8 *const bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
const u32 COLOR_BUFFER_LENGTH = 16;
|
|
|
|
c8 colStr[COLOR_BUFFER_LENGTH];
|
|
|
|
|
|
|
|
bufPtr = goAndCopyNextWord(colStr, bufPtr, COLOR_BUFFER_LENGTH, bufEnd);
|
2024-03-20 19:35:52 +01:00
|
|
|
color.setRed((u32)core::round32(core::fast_atof(colStr) * 255.f));
|
|
|
|
bufPtr = goAndCopyNextWord(colStr, bufPtr, COLOR_BUFFER_LENGTH, bufEnd);
|
|
|
|
color.setGreen((u32)core::round32(core::fast_atof(colStr) * 255.f));
|
|
|
|
bufPtr = goAndCopyNextWord(colStr, bufPtr, COLOR_BUFFER_LENGTH, bufEnd);
|
|
|
|
color.setBlue((u32)core::round32(core::fast_atof(colStr) * 255.f));
|
2023-10-03 20:37:00 +02:00
|
|
|
return bufPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Read 3d vector of floats
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::readVec3(const c8 *bufPtr, core::vector3df &vec, const c8 *const bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
const u32 WORD_BUFFER_LENGTH = 256;
|
|
|
|
c8 wordBuffer[WORD_BUFFER_LENGTH];
|
|
|
|
|
|
|
|
bufPtr = goAndCopyNextWord(wordBuffer, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
2024-03-20 19:35:52 +01:00
|
|
|
vec.X = -core::fast_atof(wordBuffer); // change handedness
|
2023-10-03 20:37:00 +02:00
|
|
|
bufPtr = goAndCopyNextWord(wordBuffer, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
2024-03-20 19:35:52 +01:00
|
|
|
vec.Y = core::fast_atof(wordBuffer);
|
2023-10-03 20:37:00 +02:00
|
|
|
bufPtr = goAndCopyNextWord(wordBuffer, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
2024-03-20 19:35:52 +01:00
|
|
|
vec.Z = core::fast_atof(wordBuffer);
|
2023-10-03 20:37:00 +02:00
|
|
|
return bufPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Read 2d vector of floats
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::readUV(const c8 *bufPtr, core::vector2df &vec, const c8 *const bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
const u32 WORD_BUFFER_LENGTH = 256;
|
|
|
|
c8 wordBuffer[WORD_BUFFER_LENGTH];
|
|
|
|
|
|
|
|
bufPtr = goAndCopyNextWord(wordBuffer, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
2024-03-20 19:35:52 +01:00
|
|
|
vec.X = core::fast_atof(wordBuffer);
|
2023-10-03 20:37:00 +02:00
|
|
|
bufPtr = goAndCopyNextWord(wordBuffer, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
|
2024-03-20 19:35:52 +01:00
|
|
|
vec.Y = 1 - core::fast_atof(wordBuffer); // change handedness
|
2023-10-03 20:37:00 +02:00
|
|
|
return bufPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Read boolean value represented as 'on' or 'off'
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::readBool(const c8 *bufPtr, bool &tf, const c8 *const bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
const u32 BUFFER_LENGTH = 8;
|
|
|
|
c8 tfStr[BUFFER_LENGTH];
|
|
|
|
|
|
|
|
bufPtr = goAndCopyNextWord(tfStr, bufPtr, BUFFER_LENGTH, bufEnd);
|
|
|
|
tf = strcmp(tfStr, "off") != 0;
|
|
|
|
return bufPtr;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
COBJMeshFileLoader::SObjMtl *COBJMeshFileLoader::findMtl(const core::stringc &mtlName, const core::stringc &grpName)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
COBJMeshFileLoader::SObjMtl *defMaterial = 0;
|
2023-10-03 20:37:00 +02:00
|
|
|
// search existing Materials for best match
|
|
|
|
// exact match does return immediately, only name match means a new group
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < Materials.size(); ++i) {
|
|
|
|
if (Materials[i]->Name == mtlName) {
|
|
|
|
if (Materials[i]->Group == grpName)
|
2023-10-03 20:37:00 +02:00
|
|
|
return Materials[i];
|
|
|
|
else
|
|
|
|
defMaterial = Materials[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// we found a partial match
|
2024-03-20 19:35:52 +01:00
|
|
|
if (defMaterial) {
|
2023-10-03 20:37:00 +02:00
|
|
|
Materials.push_back(new SObjMtl(*defMaterial));
|
|
|
|
Materials.getLast()->Group = grpName;
|
|
|
|
return Materials.getLast();
|
|
|
|
}
|
2023-04-01 17:50:08 +02:00
|
|
|
// we found a new group for a non-existent material
|
2024-03-20 19:35:52 +01:00
|
|
|
else if (grpName.size()) {
|
2023-10-03 20:37:00 +02:00
|
|
|
Materials.push_back(new SObjMtl(*Materials[0]));
|
|
|
|
Materials.getLast()->Group = grpName;
|
|
|
|
return Materials.getLast();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! skip space characters and stop on first non-space
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::goFirstWord(const c8 *buf, const c8 *const bufEnd, bool acrossNewlines)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
// skip space characters
|
|
|
|
if (acrossNewlines)
|
2024-03-20 19:35:52 +01:00
|
|
|
while ((buf != bufEnd) && core::isspace(*buf))
|
2023-10-03 20:37:00 +02:00
|
|
|
++buf;
|
|
|
|
else
|
2024-03-20 19:35:52 +01:00
|
|
|
while ((buf != bufEnd) && core::isspace(*buf) && (*buf != '\n'))
|
2023-10-03 20:37:00 +02:00
|
|
|
++buf;
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! skip current word and stop at beginning of next one
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::goNextWord(const c8 *buf, const c8 *const bufEnd, bool acrossNewlines)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
// skip current word
|
2024-03-20 19:35:52 +01:00
|
|
|
while ((buf != bufEnd) && !core::isspace(*buf))
|
2023-10-03 20:37:00 +02:00
|
|
|
++buf;
|
|
|
|
|
|
|
|
return goFirstWord(buf, bufEnd, acrossNewlines);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Read until line break is reached and stop at the next non-space character
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::goNextLine(const c8 *buf, const c8 *const bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
// look for newline characters
|
2024-03-20 19:35:52 +01:00
|
|
|
while (buf != bufEnd) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// found it, so leave
|
2024-03-20 19:35:52 +01:00
|
|
|
if (*buf == '\n' || *buf == '\r')
|
2023-10-03 20:37:00 +02:00
|
|
|
break;
|
|
|
|
++buf;
|
|
|
|
}
|
|
|
|
return goFirstWord(buf, bufEnd);
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 COBJMeshFileLoader::copyWord(c8 *outBuf, const c8 *const inBuf, u32 outBufLength, const c8 *const bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
if (!outBufLength)
|
|
|
|
return 0;
|
2024-03-20 19:35:52 +01:00
|
|
|
if (!inBuf) {
|
2023-10-03 20:37:00 +02:00
|
|
|
*outBuf = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 i = 0;
|
2024-03-20 19:35:52 +01:00
|
|
|
while (inBuf[i]) {
|
2023-10-03 20:37:00 +02:00
|
|
|
if (core::isspace(inBuf[i]) || &(inBuf[i]) == bufEnd)
|
|
|
|
break;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
u32 length = core::min_(i, outBufLength - 1);
|
|
|
|
for (u32 j = 0; j < length; ++j)
|
2023-10-03 20:37:00 +02:00
|
|
|
outBuf[j] = inBuf[j];
|
|
|
|
|
|
|
|
outBuf[length] = 0;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
core::stringc COBJMeshFileLoader::copyLine(const c8 *inBuf, const c8 *bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
if (!inBuf)
|
|
|
|
return core::stringc();
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *ptr = inBuf;
|
|
|
|
while (ptr < bufEnd) {
|
|
|
|
if (*ptr == '\n' || *ptr == '\r')
|
2023-10-03 20:37:00 +02:00
|
|
|
break;
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
// we must avoid the +1 in case the array is used up
|
2024-03-20 19:35:52 +01:00
|
|
|
return core::stringc(inBuf, (u32)(ptr - inBuf + ((ptr < bufEnd) ? 1 : 0)));
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *COBJMeshFileLoader::goAndCopyNextWord(c8 *outBuf, const c8 *inBuf, u32 outBufLength, const c8 *bufEnd)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
inBuf = goNextWord(inBuf, bufEnd, false);
|
|
|
|
copyWord(outBuf, inBuf, outBufLength, bufEnd);
|
|
|
|
return inBuf;
|
|
|
|
}
|
|
|
|
|
2024-03-20 19:35:52 +01:00
|
|
|
bool COBJMeshFileLoader::retrieveVertexIndices(c8 *vertexData, s32 *idx, const c8 *bufEnd, u32 vbsize, u32 vtsize, u32 vnsize)
|
2023-10-03 20:37:00 +02:00
|
|
|
{
|
|
|
|
const u32 BUFFER_LENGTH = 16;
|
|
|
|
c8 word[BUFFER_LENGTH];
|
2024-03-20 19:35:52 +01:00
|
|
|
const c8 *p = goFirstWord(vertexData, bufEnd);
|
|
|
|
u32 idxType = 0; // 0 = posIdx, 1 = texcoordIdx, 2 = normalIdx
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
u32 i = 0;
|
2024-03-20 19:35:52 +01:00
|
|
|
while (p != bufEnd) {
|
|
|
|
if (i >= BUFFER_LENGTH) {
|
2023-10-03 20:37:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
if ((core::isdigit(*p)) || (*p == '-')) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// build up the number
|
|
|
|
word[i++] = *p;
|
2024-03-20 19:35:52 +01:00
|
|
|
} else if (*p == '/' || *p == ' ' || *p == '\0') {
|
2023-10-03 20:37:00 +02:00
|
|
|
// number is completed. Convert and store it
|
|
|
|
word[i] = '\0';
|
|
|
|
// if no number was found index will become 0 and later on -1 by decrement
|
|
|
|
idx[idxType] = core::strtol10(word);
|
2024-03-20 19:35:52 +01:00
|
|
|
if (idx[idxType] < 0) {
|
|
|
|
switch (idxType) {
|
|
|
|
case 0:
|
|
|
|
idx[idxType] += vbsize;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
idx[idxType] += vtsize;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
idx[idxType] += vnsize;
|
|
|
|
break;
|
2023-10-03 20:37:00 +02:00
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else
|
|
|
|
idx[idxType] -= 1;
|
2023-10-03 20:37:00 +02:00
|
|
|
|
|
|
|
// reset the word
|
|
|
|
word[0] = '\0';
|
|
|
|
i = 0;
|
|
|
|
|
|
|
|
// go to the next kind of index type
|
2024-03-20 19:35:52 +01:00
|
|
|
if (*p == '/') {
|
|
|
|
if (++idxType > 2) {
|
2023-10-03 20:37:00 +02:00
|
|
|
// error checking, shouldn't reach here unless file is wrong
|
|
|
|
idxType = 0;
|
|
|
|
}
|
2024-03-20 19:35:52 +01:00
|
|
|
} else {
|
2023-10-03 20:37:00 +02:00
|
|
|
// set all missing values to disable (=-1)
|
|
|
|
while (++idxType < 3)
|
2024-03-20 19:35:52 +01:00
|
|
|
idx[idxType] = -1;
|
2023-10-03 20:37:00 +02:00
|
|
|
++p;
|
|
|
|
break; // while
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// go to the next char
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void COBJMeshFileLoader::cleanUp()
|
|
|
|
{
|
2024-03-20 19:35:52 +01:00
|
|
|
for (u32 i = 0; i < Materials.size(); ++i) {
|
2023-10-03 20:37:00 +02:00
|
|
|
Materials[i]->Meshbuffer->drop();
|
|
|
|
delete Materials[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
Materials.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace scene
|
|
|
|
} // end namespace irr
|