Commit 40abacae authored by hybrid's avatar hybrid

Cleaned the my3d loader to avoid corrupted char arrays.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@754 dfc29bdd-3216-0410-991c-e03cc46cb475
parent 020eae4f
......@@ -12,7 +12,7 @@
//----------------------------------------------------------------------
//**********************************************************************
// some usefull functions
// some useful functions
//**********************************************************************
#ifndef __C_MY3D_HELPER_H_INCLUDED__
......@@ -24,106 +24,6 @@ namespace irr
namespace core
{
//---------------------------------------------------------------------
inline f32 fmax(f32 v1, f32 v2)
{ return (v1>v2)?v1:v2;
}
//----------------------------------------------------------------------------
template <class T>
s32 strlen(T* str)
{ int len = 0;
while(true)
{ if (*(str+len)==0)
break;
len++;
}
return len;
}
//----------------------------------------------------------------------------
template <class T>
T* strcpy (T* src, T* dst)
{ if (src&&dst)
{ s32 len = strlen(src);
for (s32 i=0; i<len+1; i++)
dst[i] = src[i];
return dst;
}
return NULL;
}
//----------------------------------------------------------------------------
template <class T>
T* strcpy (T* src, T* dst, s32 startPos, s32 endPos)
{ if (src&&dst)
{ s32 len = strlen(src);
s32 charIndex=0;
for (s32 i=startPos; i<=endPos && endPos<len; i++)
{ dst[charIndex] = src[i];
charIndex++;
}
dst[charIndex]=0;
return dst;
}
return NULL;
}
//----------------------------------------------------------------------------
template <class T>
s32 findLast(T* Str, c8 ch)
{
s32 lenSrc = strlen(Str);
s32 n=lenSrc-1;
s32 lastCharInStringPos = -1;
while(true)
{ if (Str[n] == ch)
{ lastCharInStringPos = n;
break;
}
if (n==0) break;
n--;
}
return lastCharInStringPos;
}
//----------------------------------------------------------------------------
template <class T>
s32 findLastDelimiter(T* Str)
{
s32 lenSrc = strlen(Str);
s32 n=lenSrc-1;
s32 lastDelimiterInStringPos = -1;
while(true)
{ if (Str[n] == '\\' || Str[n] == '/')
{ lastDelimiterInStringPos = n;
break;
}
if (n==0) break;
n--;
}
return lastDelimiterInStringPos;
}
//----------------------------------------------------------------------------
template <class T>
T* extractFilePath(T* sourceStr, T* buffer, s32 bufSize)
{
s32 lastDelimiterInStringPos = findLastDelimiter(sourceStr);
if (lastDelimiterInStringPos>=0 && lastDelimiterInStringPos<bufSize)
strcpy(sourceStr, buffer, 0, lastDelimiterInStringPos);
return buffer;
}
//----------------------------------------------------------------------------
template <class T>
T* extractFileName(T* sourceStr, T* buffer, s32 bufSize)
{ s32 lastDelimiterInStringPos = findLastDelimiter(sourceStr);
s32 lenSrc = strlen(sourceStr);
if (lastDelimiterInStringPos>=-1 && (lenSrc-lastDelimiterInStringPos)<bufSize)
strcpy(sourceStr, buffer, lastDelimiterInStringPos+1, lenSrc-1);
return buffer;
}
//-----------------RLE stuff-----------------------------------------
int rle_encode (
......
......@@ -15,8 +15,6 @@
#include "IReadFile.h"
#include "IAttributes.h"
#include "CMY3DStuff.h"
#include "CMY3DHelper.h"
#include "os.h"
......@@ -69,20 +67,13 @@ IAnimatedMesh* CMY3DMeshFileLoader::createMesh(io::IReadFile* file)
core::stringc file_name = file->getFileName();
// working directory (from wich we loading the scene)
c8 WorkDir[1024];
core::extractFilePath((c8*)file_name.c_str(), (c8*)WorkDir, 1024);
// working directory (from which we load the scene)
core::stringc filepath = FileSystem->getFileDir(file_name);
filepath.append("/");
core::stringc msg="";
//msg="Loading 3d data from ";
//msg.append(file_name);
//os::Printer::log(msg.c_str(), ELL_INFORMATION);
// read file into memory
u16 id;
c8 name[256];
SMyFileHeader fileHeader;
file->read(&fileHeader, sizeof(SMyFileHeader));
......@@ -92,11 +83,13 @@ IAnimatedMesh* CMY3DMeshFileLoader::createMesh(io::IReadFile* file)
return 0;
}
u16 id;
file->read(&id, sizeof(id));
if (id!=MY_SCENE_HEADER_ID)
{
os::Printer::log("Can not find MY_SCENE_HEADER_ID, loading failed!", ELL_ERROR);
os::Printer::log("Cannot find MY_SCENE_HEADER_ID, loading failed!", ELL_ERROR);
return 0;
}
......@@ -119,34 +112,28 @@ IAnimatedMesh* CMY3DMeshFileLoader::createMesh(io::IReadFile* file)
return 0;
}
// loading materials and textures
//c8 ch[255];
//sprintf(ch, "Loading materials (%d to go) and textures...", sceneHeader.MaterialCount);
//os::Printer::log(ch, ELL_INFORMATION);
core::stringc texturePath =
SceneManager->getParameters()->getAttributeAsString(MY3D_TEXTURE_PATH);
s32 texCount=0, ligCount=0, matCount=0;
file->read(&id, sizeof(id));
int m;
for (m=0; m<sceneHeader.MaterialCount; m++)
s32 texCount=0, ligCount=0, matCount=0;
c8 name[256];
for (s32 m=0; m<sceneHeader.MaterialCount; ++m)
{
if (id!=MY_MAT_HEADER_ID)
{ os::Printer::log("Can not find MY_MAT_HEADER_ID, loading failed!", ELL_ERROR);
{
os::Printer::log("Cannot find MY_MAT_HEADER_ID, loading failed!", ELL_ERROR);
return 0;
}
matCount++;
SMyMaterialEntry me;
// read material header
SMyMaterialHeader materialHeader;
file->read(&materialHeader, sizeof(SMyMaterialHeader));
SMyMaterialEntry me;
me.Header = materialHeader;
// read next identificator
......@@ -163,7 +150,8 @@ IAnimatedMesh* CMY3DMeshFileLoader::createMesh(io::IReadFile* file)
{
file->read(&id, sizeof(id));
if (id!=MY_TEXDATA_HEADER_ID)
{ os::Printer::log("Can not find MY_TEXDATA_HEADER_ID, loading failed!", ELL_ERROR);
{
os::Printer::log("Can not find MY_TEXDATA_HEADER_ID, loading failed!", ELL_ERROR);
return 0;
}
......@@ -171,7 +159,7 @@ IAnimatedMesh* CMY3DMeshFileLoader::createMesh(io::IReadFile* file)
file->read(&texDataHeader, sizeof(SMyTexDataHeader));
core::strcpy(texDataHeader.Name, name);
strcpy(texDataHeader.Name, name);
char LightMapName[255];
sprintf(LightMapName,"My3D.Lightmap.%d",++LightMapIndex);
......@@ -199,8 +187,7 @@ IAnimatedMesh* CMY3DMeshFileLoader::createMesh(io::IReadFile* file)
return 0;
}
s32 num_pixels = texDataHeader.Width*texDataHeader.Height;
u32 num_pixels = texDataHeader.Width*texDataHeader.Height;
void* data = 0;
......@@ -272,14 +259,16 @@ IAnimatedMesh* CMY3DMeshFileLoader::createMesh(io::IReadFile* file)
{
file->read(&nToRead, sizeof(nToRead));
if ((s32)(nReadedPixels+nToRead)>(s32)num_pixels) break;
if ((nReadedPixels+nToRead) > num_pixels)
break;
if (texDataHeader.PixelFormat == MY_PIXEL_FORMAT_24)
{
SMyPixelColor24 col24;
file->read(&col24, sizeof(SMyPixelColor24));
for (u32 p=0; p<nToRead; p++)
{ ((SMyPixelColor24*)data)[nReadedPixels+p] =
for (u32 p=0; p<nToRead; p++)
{
((SMyPixelColor24*)data)[nReadedPixels+p] =
SMyPixelColor24(col24.r, col24.g, col24.b);
}
}
......@@ -288,16 +277,18 @@ for (u32 p=0; p<nToRead; p++)
SMyPixelColor16 col16;
file->read(&col16, sizeof(SMyPixelColor16));
for (u32 p=0; p<nToRead; p++)
{ ((SMyPixelColor16*)data)[nReadedPixels+p].argb = col16.argb;
{
((SMyPixelColor16*)data)[nReadedPixels+p].argb = col16.argb;
}
}
nReadedPixels+=nToRead;
if ((s32)nReadedPixels>=(s32)num_pixels) break;
if (nReadedPixels >= num_pixels)
break;
}
if ((s32)nReadedPixels!=(s32)num_pixels)
if (nReadedPixels != num_pixels)
{
os::Printer::log("Image data seems to be corrupted, loading failed!", ELL_ERROR);
return 0;
......@@ -341,9 +332,7 @@ for (u32 p=0; p<nToRead; p++)
int pos2 = Name.findLast('.');
core::stringc LightingMapStr = "LightingMap";
int ls = LightingMapStr.size();
core::stringc sub = Name.subString((s32)core::fmax((f32)0, (f32)(pos2 - ls)), ls);
core::stringc texFName;
core::stringc sub = Name.subString(core::max_(0, (pos2 - ls)), ls);
if ((sub == LightingMapStr || (Name[pos2-1]=='m' &&
Name[pos2-2]=='l' && Name[pos2-3]=='_')) &&
!GetLightMap)
......@@ -351,14 +340,13 @@ for (u32 p=0; p<nToRead; p++)
bool oldMipMapState = Driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
Driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
texFName = texturePath.size() ? texturePath : core::stringc(WorkDir);
texFName.append("Lightmaps/");
texFName.append(Name);
me.Texture2FileName = texFName;
me.Texture2FileName = texturePath.size() ? texturePath : filepath;
me.Texture2FileName.append("Lightmaps/");
me.Texture2FileName.append(Name);
if (Name.size()>0)
{
me.Texture2 = Driver->getTexture(texFName.c_str());
me.Texture2 = Driver->getTexture(me.Texture2FileName.c_str());
ligCount++;
}
......@@ -371,13 +359,12 @@ for (u32 p=0; p<nToRead; p++)
else
if (!GetLightMap&&GetMainMap)
{
texFName = texturePath.size() ? texturePath : core::stringc(WorkDir);
texFName.append(Name);
me.Texture2FileName = texFName;
me.Texture2FileName = texturePath.size() ? texturePath : filepath;
me.Texture2FileName.append(Name);
if (Name.size()>0)
if (Name.size())
{
me.Texture2 = Driver->getTexture(texFName.c_str());
me.Texture2 = Driver->getTexture(me.Texture2FileName.c_str());
ligCount++;
}
......@@ -386,12 +373,11 @@ for (u32 p=0; p<nToRead; p++)
else
if (!GetMainMap && !GetLightMap )
{
texFName = WorkDir;
texFName.append(Name);
me.Texture1FileName = texFName;
if (Name.size()>0)
me.Texture1FileName = filepath;
me.Texture1FileName.append(Name);
if (Name.size())
{
me.Texture1 = Driver->getTexture(texFName.c_str());
me.Texture1 = Driver->getTexture(me.Texture1FileName.c_str());
texCount++;
}
......@@ -408,56 +394,22 @@ for (u32 p=0; p<nToRead; p++)
}
// override materials types from they names
if (me.Header.Name[0] =='A' &&
me.Header.Name[1] =='l' &&
me.Header.Name[2] =='p' &&
me.Header.Name[3] =='h' &&
me.Header.Name[4] =='a' &&
me.Header.Name[5] =='C' &&
me.Header.Name[6] =='h' &&
me.Header.Name[7] =='a' &&
me.Header.Name[8] =='n' &&
me.Header.Name[9] =='n' &&
me.Header.Name[10]=='e' &&
me.Header.Name[11]=='l' &&
me.Header.Name[12]=='-'
)
{
if (!strcmp(me.Header.Name, "AlphaChannel-"))
me.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
}
else
if (me.Header.Name[0] =='S' &&
me.Header.Name[1] =='p' &&
me.Header.Name[2] =='h' &&
me.Header.Name[3] =='e' &&
me.Header.Name[4] =='r' &&
me.Header.Name[5] =='e' &&
me.Header.Name[6] =='M' &&
me.Header.Name[7] =='a' &&
me.Header.Name[8] =='p' &&
me.Header.Name[9] =='-'
)
{
if (!strcmp(me.Header.Name, "SphereMap-"))
me.MaterialType = video::EMT_SPHERE_MAP;
}
MaterialEntry.push_back(me);
}
// loading meshes
//sprintf(ch, " Loaded: materials (%d), textures (%d), lightmaps (%d).", matCount, texCount, ligCount);
// os::Printer::log(ch, ELL_INFORMATION);
if (Mesh)
Mesh->drop();
Mesh = new SMesh();
//sprintf(ch, "Loading meshes (%d to go) ...", sceneHeader.MeshCount);
//os::Printer::log(ch, ELL_INFORMATION);
if (id!=MY_MESH_LIST_ID)
{
os::Printer::log("Can not find MY_MESH_LIST_ID, loading failed!", ELL_ERROR);
......@@ -829,9 +781,9 @@ for (u32 p=0; p<nToRead; p++)
// creating mesh
for (m=0; m<(s32)MeshBufferEntry.size(); m++)
for (u32 num=0; num<MeshBufferEntry.size(); ++num)
{
SMeshBufferLightMap* buffer = MeshBufferEntry[m].MeshBuffer;
SMeshBufferLightMap* buffer = MeshBufferEntry[num].MeshBuffer;
if (!buffer)
continue;
......@@ -861,33 +813,29 @@ for (u32 p=0; p<nToRead; p++)
Mesh->drop();
Mesh = 0;
//msg="3D data successfully loaded from ";
//msg.append(file_name);
//os::Printer::log(msg.c_str(), ELL_INFORMATION);
return am;
}
CMY3DMeshFileLoader::SMyMaterialEntry* CMY3DMeshFileLoader::getMaterialEntryByIndex(u32 matInd)
{
for (int m=0; m<(s32)MaterialEntry.size(); m++)
for (u32 m=0; m<MaterialEntry.size(); ++m)
if (MaterialEntry[m].Header.Index == matInd)
return &MaterialEntry[m];
return NULL;
return 0;
}
SMeshBufferLightMap* CMY3DMeshFileLoader::getMeshBufferByMaterialIndex(u32 matInd)
{
for (int m=0; m<(s32)MeshBufferEntry.size(); m++)
for (u32 m=0; m<MeshBufferEntry.size(); ++m)
{
if (MeshBufferEntry[m].MaterialIndex == (s32)matInd)
return MeshBufferEntry[m].MeshBuffer;
}
return NULL;
return 0;
}
......
......@@ -204,20 +204,23 @@ namespace scene
//! work best with your new terrain size.
virtual bool overrideLODDistance( s32 LOD, f64 newDistance );
//! Scales the base texture, similar to makePlanarTextureMapping
virtual void scaleTexture(f32 scale = 1.0f, f32 scale2=0.0f );
//! Scales the two textures
virtual void scaleTexture(f32 scale = 1.0f, f32 scale2 = 0.0f);
//! Returns type of the scene node
virtual ESCENE_NODE_TYPE getType() const { return ESNT_TERRAIN; }
virtual ESCENE_NODE_TYPE getType() const {return ESNT_TERRAIN;}
//! Writes attributes of the scene node.
virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0);
virtual void serializeAttributes(io::IAttributes* out,
io::SAttributeReadWriteOptions* options=0);
//! Reads attributes of the scene node.
virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0);
virtual void deserializeAttributes(io::IAttributes* in,
io::SAttributeReadWriteOptions* options=0);
//! Creates a clone of this scene node and its children.
virtual ISceneNode* clone(ISceneNode* newParent, ISceneManager* newManager);
virtual ISceneNode* clone(ISceneNode* newParent,
ISceneManager* newManager);
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment