Update to the LWO loader. Still WIP.

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@104 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
This commit is contained in:
aramis_acg
2008-08-10 23:58:21 +00:00
parent 6f49c4518c
commit 9bc8e8701f
4 changed files with 314 additions and 208 deletions

View File

@@ -70,9 +70,6 @@ LWOImporter::LWOImporter()
// Destructor, private as well
LWOImporter::~LWOImporter()
{
mFaces.clear();
mTempPoints.clear();
mSurfaces.clear();
}
// ------------------------------------------------------------------------------------------------
@@ -122,112 +119,122 @@ void LWOImporter::InternReadFile( const std::string& pFile,
mFileBuffer = &mBuffer[0] + 12;
fileSize -= 12;
try
// create temporary storage on the stack but store points to
// it in the class instance. Therefoe everything will be destructed
// properly if an exception is thrown.
PointList _mTempPoints;
mTempPoints = &_mTempPoints;
FaceList _mFaces;
mFaces = &_mFaces;
TagList _mTags;
mTags = &_mTags;
TagMappingTable _mMapping;
mMapping = &_mMapping;
SurfaceList _mSurfaces;
mSurfaces = &_mSurfaces;
// old lightwave file format (prior to v6)
if (AI_LWO_FOURCC_LWOB == fileType)this->LoadLWOBFile();
// new lightwave format
else if (AI_LWO_FOURCC_LWO2 == fileType)this->LoadLWO2File();
// we don't know this format
else
{
// old lightwave file format (prior to v6)
if (AI_LWO_FOURCC_LWOB == fileType)
this->LoadLWOBFile();
// new lightwave format
else if (AI_LWO_FOURCC_LWO2 == fileType)
this->LoadLWO2File();
// we don't know this format
else
{
char szBuff[5];
szBuff[0] = (char)(fileType >> 24u);
szBuff[1] = (char)(fileType >> 16u);
szBuff[2] = (char)(fileType >> 8u);
szBuff[3] = (char)(fileType);
throw new ImportErrorException(std::string("Unknown LWO sub format: ") + szBuff);
}
// generate a default surface if necessary
if (mSurfaces.empty())
mSurfaces.push_back(LWO::Surface());
// now sort all faces by the surfaces assigned to them
typedef std::vector<unsigned int> SortedRep;
std::vector<SortedRep> pSorted(mSurfaces.size());
unsigned int i = 0;
for (FaceList::iterator it = mFaces.begin(), end = mFaces.end();
it != end;++it,++i)
{
if ((*it).surfaceIndex >= mSurfaces.size())
{
DefaultLogger::get()->warn("LWO: Invalid face surface index");
(*it).surfaceIndex = mSurfaces.size()-1;
}
pSorted[(*it).surfaceIndex].push_back(i);
}
// now generate output meshes
for (unsigned int p = 0; p < mSurfaces.size();++p)
if (!pSorted[p].empty())pScene->mNumMeshes++;
if (!(pScene->mNumMaterials = pScene->mNumMeshes))
throw new ImportErrorException("LWO: There are no meshes");
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
for (unsigned int p = 0,i = 0;i < mSurfaces.size();++i)
{
SortedRep& sorted = pSorted[i];
if (sorted.empty())continue;
// generate the mesh
aiMesh* mesh = pScene->mMeshes[p] = new aiMesh();
mesh->mNumFaces = sorted.size();
for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
it != end;++it)
{
mesh->mNumVertices += mFaces[*it].mNumIndices;
}
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
mesh->mFaces = new aiFace[mesh->mNumFaces];
mesh->mMaterialIndex = p;
// now convert all faces
unsigned int vert = 0;
for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
it != end;++it)
{
LWO::Face& face = mFaces[*it];
// copy all vertices
for (unsigned int q = 0; q < face.mNumIndices;++q)
{
*mesh->mVertices++ = mTempPoints[face.mIndices[q]];
face.mIndices[q] = vert++;
}
mesh->mFaces->mIndices = face.mIndices;
mesh->mFaces->mNumIndices = face.mNumIndices;
face.mIndices = NULL; // make sure it won't be deleted
mesh->mFaces++;
}
mesh->mFaces -= mesh->mNumFaces;
mesh->mVertices -= mesh->mNumVertices;
// generate the corresponding material
MaterialHelper* pcMat = new MaterialHelper();
pScene->mMaterials[p] = pcMat;
// todo
++p;
}
char szBuff[5];
szBuff[0] = (char)(fileType >> 24u);
szBuff[1] = (char)(fileType >> 16u);
szBuff[2] = (char)(fileType >> 8u);
szBuff[3] = (char)(fileType);
throw new ImportErrorException(std::string("Unknown LWO sub format: ") + szBuff);
}
// make sure the arrays are cleaned up ...
catch (ImportErrorException* ex)
ResolveTags();
// now sort all faces by the surfaces assigned to them
typedef std::vector<unsigned int> SortedRep;
std::vector<SortedRep> pSorted(mSurfaces->size()+1);
unsigned int i = 0;
unsigned int iDefaultSurface = 0xffffffff;
for (FaceList::iterator it = mFaces->begin(), end = mFaces->end();
it != end;++it,++i)
{
this->~LWOImporter();
throw ex;
unsigned int idx = (*it).surfaceIndex;
if (idx >= mTags->size())
{
DefaultLogger::get()->warn("LWO: Invalid face surface index");
idx = mTags->size()-1;
}
if(0xffffffff == (idx = _mMapping[idx]))
{
if (0xffffffff == iDefaultSurface)
{
iDefaultSurface = mSurfaces->size();
mSurfaces->push_back(LWO::Surface());
LWO::Surface& surf = mSurfaces->back();
surf.mColor.r = surf.mColor.g = surf.mColor.b = 0.6f;
}
idx = iDefaultSurface;
}
pSorted[idx].push_back(i);
}
if (0xffffffff == iDefaultSurface)pSorted.erase(pSorted.end()-1);
// now generate output meshes
for (unsigned int p = 0; p < mSurfaces->size();++p)
if (!pSorted[p].empty())pScene->mNumMeshes++;
if (!(pScene->mNumMaterials = pScene->mNumMeshes))
throw new ImportErrorException("LWO: There are no meshes");
pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
for (unsigned int p = 0,i = 0;i < mSurfaces->size();++i)
{
SortedRep& sorted = pSorted[i];
if (sorted.empty())continue;
// generate the mesh
aiMesh* mesh = pScene->mMeshes[p] = new aiMesh();
mesh->mNumFaces = sorted.size();
for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
it != end;++it)
{
mesh->mNumVertices += _mFaces[*it].mNumIndices;
}
aiVector3D* pv = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
aiFace* pf = mesh->mFaces = new aiFace[mesh->mNumFaces];
mesh->mMaterialIndex = p;
// now convert all faces
unsigned int vert = 0;
for (SortedRep::const_iterator it = sorted.begin(), end = sorted.end();
it != end;++it)
{
LWO::Face& face = _mFaces[*it];
// copy all vertices
for (unsigned int q = 0; q < face.mNumIndices;++q)
{
*pv++ = _mTempPoints[face.mIndices[q]];
face.mIndices[q] = vert++;
}
pf->mIndices = face.mIndices;
pf->mNumIndices = face.mNumIndices;
face.mIndices = NULL; // make sure it won't be deleted
pf++;
}
// generate the corresponding material
MaterialHelper* pcMat = new MaterialHelper();
pScene->mMaterials[p] = pcMat;
//ConvertMaterial(mSurfaces[i],pcMat);
++p;
}
this->~LWOImporter();
}
// ------------------------------------------------------------------------------------------------
void LWOImporter::CountVertsAndFaces(unsigned int& verts, unsigned int& faces,
@@ -248,29 +255,34 @@ void LWOImporter::CountVertsAndFaces(unsigned int& verts, unsigned int& faces,
}
}
// ------------------------------------------------------------------------------------------------
void LWOImporter::CopyFaceIndices(LWOImporter::FaceList::iterator& it,LE_NCONST uint8_t*& cursor,
const uint8_t* const end, unsigned int max)
void LWOImporter::CopyFaceIndices(LWOImporter::FaceList::iterator& it,
LE_NCONST uint8_t*& cursor,
const uint8_t* const end,
unsigned int max)
{
while (cursor < end && max--)
{
LWO::Face& face = *it;++it;
face.mNumIndices = *((uint16_t*)cursor);
if (cursor + face.mNumIndices*2 + 4 >= end)break;
face.mIndices = new unsigned int[face.mNumIndices];
for (unsigned int i = 0; i < face.mNumIndices;++i)
if(face.mNumIndices = *((uint16_t*)cursor))
{
face.mIndices[i] = *((uint16_t*)(cursor+=2));
if (face.mIndices[i] >= mTempPoints.size())
if (cursor + face.mNumIndices*2 + 4 >= end)break;
face.mIndices = new unsigned int[face.mNumIndices];
for (unsigned int i = 0; i < face.mNumIndices;++i)
{
face.mIndices[i] = mTempPoints.size()-1;
DefaultLogger::get()->warn("LWO: Face index is out of range");
face.mIndices[i] = *((uint16_t*)(cursor+=2));
if (face.mIndices[i] >= mTempPoints->size())
{
face.mIndices[i] = mTempPoints->size()-1;
DefaultLogger::get()->warn("LWO: Face index is out of range");
}
}
}
else DefaultLogger::get()->warn("LWO: Face has 0 indices");
cursor+=2;
int16_t surface = *((uint16_t*)cursor);cursor+=2;
if (surface < 0)
{
surface *= -1;
surface = -surface;
// there are detail polygons
uint16_t numPolygons = *((uint16_t*)cursor);cursor+=2;
@@ -279,12 +291,81 @@ void LWOImporter::CopyFaceIndices(LWOImporter::FaceList::iterator& it,LE_NCONST
face.surfaceIndex = surface-1;
}
}
// ------------------------------------------------------------------------------------------------
void LWOImporter::ResolveTags()
{
mMapping->resize(mTags->size(),0xffffffff);
for (unsigned int a = 0; a < mTags->size();++a)
{
for (unsigned int i = 0; i < mSurfaces->size();++i)
{
if ((*mTags)[a] == (*mSurfaces)[i].mName)
{
(*mMapping)[a] = i;
break;
}
}
}
}
// ------------------------------------------------------------------------------------------------
void LWOImporter::ParseString(std::string& out,unsigned int max)
{
unsigned int iCursor = 0;
const char* in = (const char*)mFileBuffer,*sz = in;
while (*in)
{
if (++iCursor > max)
{
DefaultLogger::get()->warn("LWOB: Invalid file, texture name (TIMG) is too long");
break;
}
++in;
}
unsigned int len = unsigned int (in-sz);
out = std::string(sz,len);
}
// ------------------------------------------------------------------------------------------------
void LWOImporter::AdjustTexturePath(std::string& out)
{
if (::strstr(out.c_str(), "(sequence)"))
{
// remove the (sequence) and append 000
DefaultLogger::get()->info("LWO: Sequence of animated texture found. It will be ignored");
out = out.substr(0,out.length()-10) + "000";
}
}
// ------------------------------------------------------------------------------------------------
#define AI_LWO_VALIDATE_CHUNK_LENGTH(name,size) \
if (head->length < size) \
{ \
DefaultLogger::get()->warn("LWO: "#name" chunk is too small"); \
break; \
} \
// ------------------------------------------------------------------------------------------------
void LWOImporter::LoadLWOTags(unsigned int size)
{
const char* szCur = (const char*)mFileBuffer, *szLast = szCur;
const char* const szEnd = szLast+size;
while (szCur < szEnd)
{
if (!(*szCur++))
{
const unsigned int len = unsigned int(szCur-szLast);
mTags->push_back(std::string(szLast,len));
szCur += len & 1;
szLast = szCur;
}
}
}
// ------------------------------------------------------------------------------------------------
void LWOImporter::LoadLWOBSurface(unsigned int size)
{
uint32_t iCursor = 0;
mSurfaces.push_back( LWO::Surface () );
LWO::Surface& surf = mSurfaces.back();
mSurfaces->push_back( LWO::Surface () );
LWO::Surface& surf = mSurfaces->back();
LWO::Texture* pTex = NULL;
// at first we'll need to read the name of the surface
@@ -309,16 +390,13 @@ void LWOImporter::LoadLWOBSurface(unsigned int size)
"a surface sub chunk points behind the end of the file");
}
mFileBuffer += sizeof(IFF::ChunkHeader);
LE_NCONST uint8_t* next = mFileBuffer+head->length;
switch (head->type)
{
// diffuse color
case AI_LWO_COLR:
{
if (head->length < 3)
{
DefaultLogger::get()->warn("LWO: COLR chunk is expected to be at least 3 bytes in size");
break;
}
AI_LWO_VALIDATE_CHUNK_LENGTH(COLR,3);
surf.mColor.r = *mFileBuffer++ / 255.0f;
surf.mColor.g = *mFileBuffer++ / 255.0f;
surf.mColor.b = *mFileBuffer / 255.0f;
@@ -327,6 +405,7 @@ void LWOImporter::LoadLWOBSurface(unsigned int size)
// diffuse strength ... hopefully
case AI_LWO_DIFF:
{
AI_LWO_VALIDATE_CHUNK_LENGTH(DIFF,2);
AI_LSWAP2(mFileBuffer);
surf.mDiffuseValue = *((int16_t*)mFileBuffer) / 255.0f;
break;
@@ -334,6 +413,7 @@ void LWOImporter::LoadLWOBSurface(unsigned int size)
// specular strength ... hopefully
case AI_LWO_SPEC:
{
AI_LWO_VALIDATE_CHUNK_LENGTH(SPEC,2);
AI_LSWAP2(mFileBuffer);
surf.mSpecularValue = *((int16_t*)mFileBuffer) / 255.0f;
break;
@@ -341,6 +421,7 @@ void LWOImporter::LoadLWOBSurface(unsigned int size)
// transparency
case AI_LWO_TRAN:
{
AI_LWO_VALIDATE_CHUNK_LENGTH(TRAN,2);
AI_LSWAP2(mFileBuffer);
surf.mTransparency = *((int16_t*)mFileBuffer) / 255.0f;
break;
@@ -348,6 +429,7 @@ void LWOImporter::LoadLWOBSurface(unsigned int size)
// glossiness
case AI_LWO_GLOS:
{
AI_LWO_VALIDATE_CHUNK_LENGTH(GLOS,2);
AI_LSWAP2(mFileBuffer);
surf.mGlossiness = float(*((int16_t*)mFileBuffer));
break;
@@ -387,18 +469,9 @@ void LWOImporter::LoadLWOBSurface(unsigned int size)
{
if (pTex)
{
unsigned int iCursor = 0;
while (*mFileBuffer)
{
if (++iCursor > head->length)
{
DefaultLogger::get()->warn("LWOB: Invalid file, texture name (TIMG) is too long");
break;
}
++mFileBuffer;
}
unsigned int len = unsigned int (mFileBuffer-sz);
pTex->mFileName = std::string((const char*)sz,len);
ParseString(pTex->mFileName,head->length);
AdjustTexturePath(pTex->mFileName);
mFileBuffer += pTex->mFileName.length();
}
else DefaultLogger::get()->warn("LWOB: TIMG tag was encuntered although "
"there was no xTEX tag before");
@@ -407,13 +480,14 @@ void LWOImporter::LoadLWOBSurface(unsigned int size)
// texture strength
case AI_LWO_TVAL:
{
AI_LWO_VALIDATE_CHUNK_LENGTH(TVAL,1);
if (pTex)pTex->mStrength = *mFileBuffer / 255.0f;
else DefaultLogger::get()->warn("LWOB: TVAL tag was encuntered "
"although there was no xTEX tag before");
break;
}
}
mFileBuffer += head->length;
mFileBuffer = next;
}
}
// ------------------------------------------------------------------------------------------------
@@ -433,17 +507,18 @@ void LWOImporter::LoadLWOBFile()
break;
}
mFileBuffer += sizeof(IFF::ChunkHeader);
LE_NCONST uint8_t* next = mFileBuffer+head->length;
switch (head->type)
{
// vertex list
case AI_LWO_PNTS:
{
mTempPoints.resize( head->length / 12 );
mTempPoints->resize( head->length / 12 );
#ifndef AI_BUILD_BIG_ENDIAN
for (unsigned int i = 0; i < head->length>>2;++i)
ByteSwap::Swap4( mFileBuffer + (i << 2));
#endif
::memcpy(&mTempPoints[0],mFileBuffer,head->length);
::memcpy(&(*mTempPoints)[0],mFileBuffer,head->length);
break;
}
// face list
@@ -464,13 +539,21 @@ void LWOImporter::LoadLWOBFile()
// allocate the output array and copy face indices
if (iNumFaces)
{
this->mTempPoints.resize(iNumVertices);
this->mFaces.resize(iNumFaces);
FaceList::iterator it = this->mFaces.begin();
CopyFaceIndices(it,mFileBuffer,end);
cursor = mFileBuffer;
this->mTempPoints->resize(iNumVertices);
this->mFaces->resize(iNumFaces);
FaceList::iterator it = this->mFaces->begin();
CopyFaceIndices(it,cursor,end);
}
break;
}
// list of tags
case AI_LWO_SRFS:
{
LoadLWOTags(head->length);
break;
}
// surface chunk
case AI_LWO_SURF:
{
@@ -478,7 +561,7 @@ void LWOImporter::LoadLWOBFile()
break;
}
}
mFileBuffer += head->length;
mFileBuffer = next;
}
}
// ------------------------------------------------------------------------------------------------