Commit be924b27 authored by hybrid's avatar hybrid

Fixed core dumps and loading errors. Some speedup and simplifications.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@1629 dfc29bdd-3216-0410-991c-e03cc46cb475
parent f180825e
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
#include "IrrCompileConfig.h" #include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_DMF_LOADER_ #ifdef _IRR_COMPILE_WITH_DMF_LOADER_
#ifdef _DEBUG
#define _IRR_DMF_DEBUG_
#include "os.h"
#endif
#include "CDMFLoader.h" #include "CDMFLoader.h"
#include "ISceneManager.h" #include "ISceneManager.h"
#include "IAttributes.h" #include "IAttributes.h"
...@@ -41,37 +46,6 @@ CDMFLoader::CDMFLoader(ISceneManager* smgr, io::IFileSystem* filesys) ...@@ -41,37 +46,6 @@ CDMFLoader::CDMFLoader(ISceneManager* smgr, io::IFileSystem* filesys)
} }
/** Given first three points of a face, returns a face normal*/
void CDMFLoader::GetFaceNormal( f32 a[3], //First point
f32 b[3], //Second point
f32 c[3], //Third point
f32 out[3]) //Normal computed
{
f32 v1[3], v2[3];
v1[0] = a[0] - b[0];
v1[1] = a[1] - b[1];
v1[2] = a[2] - b[2];
v2[0] = b[0] - c[0];
v2[1] = b[1] - c[1];
v2[2] = b[2] - c[2];
out[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
out[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
out[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
f32 dist = (f32)sqrtf((out[0] * out[0]) + (out[1] * out[1]) + (out[2] * out[2]));
if (dist == 0.0f)
dist = 0.001f;
out[0] /= dist;
out[1] /= dist;
out[2] /= dist;
}
/**Creates/loads an animated mesh from the file. /**Creates/loads an animated mesh from the file.
\return Pointer to the created mesh. Returns 0 if loading failed. \return Pointer to the created mesh. Returns 0 if loading failed.
If you no longer need the mesh, you should call IAnimatedMesh::drop(). If you no longer need the mesh, you should call IAnimatedMesh::drop().
...@@ -101,20 +75,28 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -101,20 +75,28 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
SceneMgr->setAmbientLight(header.dmfAmbient); SceneMgr->setAmbientLight(header.dmfAmbient);
//let's create the correct number of materials, vertices and faces //let's create the correct number of materials, vertices and faces
dmfMaterial *materiali=new dmfMaterial[header.numMaterials]; core::array<dmfMaterial> materiali;
dmfVert *verts=new dmfVert[header.numVertices]; dmfVert *verts=new dmfVert[header.numVertices];
dmfFace *faces=new dmfFace[header.numFaces]; dmfFace *faces=new dmfFace[header.numFaces];
//let's get the materials //let's get the materials
bool use_mat_dirs=false; const bool use_mat_dirs=SceneMgr->getParameters()->getAttributeAsBool(DMF_USE_MATERIALS_DIRS);
use_mat_dirs=SceneMgr->getParameters()->getAttributeAsBool(DMF_USE_MATERIALS_DIRS);
GetDMFMaterials(dmfRawFile , materiali,header.numMaterials,use_mat_dirs); #ifdef _IRR_DMF_DEBUG_
os::Printer::log("Loading materials", core::stringc(header.numMaterials).c_str());
#endif
GetDMFMaterials(dmfRawFile, materiali, header.numMaterials, use_mat_dirs);
//let's get vertices and faces //let's get vertices and faces
GetDMFVerticesFaces(dmfRawFile, verts,faces); #ifdef _IRR_DMF_DEBUG_
os::Printer::log("Loading geometry");
#endif
GetDMFVerticesFaces(dmfRawFile, verts, faces);
//create a meshbuffer for each material, then we'll remove empty ones //create a meshbuffer for each material, then we'll remove empty ones
#ifdef _IRR_DMF_DEBUG_
os::Printer::log("Creating meshbuffers.");
#endif
for (i=0; i<header.numMaterials; i++) for (i=0; i<header.numMaterials; i++)
{ {
//create a new SMeshBufferLightMap for each material //create a new SMeshBufferLightMap for each material
...@@ -127,38 +109,38 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -127,38 +109,38 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
} }
// Build the mesh buffers // Build the mesh buffers
#ifdef _IRR_DMF_DEBUG_
os::Printer::log("Adding geometry to mesh.");
#endif
for (i = 0; i < header.numFaces; i++) for (i = 0; i < header.numFaces; i++)
{ {
#ifdef _IRR_DMF_DEBUG_
os::Printer::log("Polygon with #vertices", core::stringc(faces[i].numVerts).c_str());
#endif
if (faces[i].numVerts < 3) if (faces[i].numVerts < 3)
continue; continue;
f32 normal[3]; const core::vector3df normal =
core::triangle3df(verts[faces[i].firstVert].pos,
GetFaceNormal(verts[faces[i].firstVert].pos, verts[faces[i].firstVert+1].pos,
verts[faces[i].firstVert+1].pos, verts[faces[i].firstVert+2].pos, normal); verts[faces[i].firstVert+2].pos).getNormal().normalize();
SMeshBufferLightMap * meshBuffer = (SMeshBufferLightMap*)mesh->getMeshBuffer( SMeshBufferLightMap * meshBuffer = (SMeshBufferLightMap*)mesh->getMeshBuffer(
faces[i].materialID); faces[i].materialID);
u32 base = meshBuffer->Vertices.size(); const u32 base = meshBuffer->Vertices.size();
// Add this face's verts // Add this face's verts
u32 v; u32 v;
for (v = 0; v < faces[i].numVerts; v++) for (v = 0; v < faces[i].numVerts; v++)
{ {
dmfVert * vv = &verts[faces[i].firstVert + v]; const dmfVert& vv = verts[faces[i].firstVert + v];
video::S3DVertex2TCoords vert(vv->pos[0], vv->pos[1], vv->pos[2], video::S3DVertex2TCoords vert(vv.pos,
normal[0], normal[1], normal[2], video::SColor(0,255,255,255), 0.0f, 0.0f); normal, video::SColor(255,255,255,255), vv.tc, vv.lc);
if ( materiali[faces[i].materialID].textureBlend==4 && if (materiali[faces[i].materialID].textureBlend==4 &&
SceneMgr->getParameters()->getAttributeAsBool(DMF_FLIP_ALPHA_TEXTURES)) SceneMgr->getParameters()->getAttributeAsBool(DMF_FLIP_ALPHA_TEXTURES))
{ {
vert.TCoords.set(vv->tc[0],-vv->tc[1]); vert.TCoords.set(vv.tc.X,-vv.tc.Y);
vert.TCoords2.set(vv->lc[0],vv->lc[1]);
}
else
{
vert.TCoords.set(vv->tc[0], vv->tc[1]);
vert.TCoords2.set(vv->lc[0], vv->lc[1]);
} }
meshBuffer->Vertices.push_back(vert); meshBuffer->Vertices.push_back(vert);
} }
...@@ -166,22 +148,22 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -166,22 +148,22 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
// Now add the indices // Now add the indices
// This weird loop turns convex polygons into triangle strips. // This weird loop turns convex polygons into triangle strips.
// I do it this way instead of a simple fan because it usually // I do it this way instead of a simple fan because it usually
// looks a lot better in wireframe, for example. // looks a lot better in wireframe, for example.
u32 h = faces[i].numVerts - 1, l = 0, c; // High, Low, Center u32 h = faces[i].numVerts - 1, l = 0, c; // High, Low, Center
for (v = 0; v < faces[i].numVerts - 2; v++) for (v = 0; v < faces[i].numVerts - 2; v++)
{ {
if (v & 1) if (v & 1) // odd
c = h - 1; c = h - 1;
else else // even
c = l + 1; c = l + 1;
meshBuffer->Indices.push_back(base + h); meshBuffer->Indices.push_back(base + h);
meshBuffer->Indices.push_back(base + l); meshBuffer->Indices.push_back(base + l);
meshBuffer->Indices.push_back(base + c); meshBuffer->Indices.push_back(base + c);
if (v & 1) if (v & 1) // odd
h--; h--;
else else // even
l++; l++;
} }
} }
...@@ -189,6 +171,9 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -189,6 +171,9 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
//load textures and lightmaps in materials. //load textures and lightmaps in materials.
//don't worry if you receive a could not load texture, cause if you don't need //don't worry if you receive a could not load texture, cause if you don't need
//a particular material in your scene it will be loaded and then destroyed. //a particular material in your scene it will be loaded and then destroyed.
#ifdef _IRR_DMF_DEBUG_
os::Printer::log("Loading textures.");
#endif
for (i=0; i<header.numMaterials; i++) for (i=0; i<header.numMaterials; i++)
{ {
core::stringc path; core::stringc path;
...@@ -207,27 +192,14 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -207,27 +192,14 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
//Primary texture is normal //Primary texture is normal
if ((materiali[i].textureFlag==0) || (materiali[i].textureBlend==4)) if ((materiali[i].textureFlag==0) || (materiali[i].textureBlend==4))
{
driver->setTextureCreationFlag(ETCF_ALWAYS_32_BIT,true); driver->setTextureCreationFlag(ETCF_ALWAYS_32_BIT,true);
tex = driver->getTexture((path+materiali[i].textureName).c_str()); tex = driver->getTexture((path+materiali[i].textureName).c_str());
}
//Primary texture is just a colour //Primary texture is just a colour
if(materiali[i].textureFlag==1) else if(materiali[i].textureFlag==1)
{ {
String colour(materiali[i].textureName); SColor color(axtoi(materiali[i].textureName.c_str()));
String alpha,red,green,blue;
alpha.append((char*)&colour[0]);
alpha.append((char*)&colour[1]);
blue.append((char*)&colour[2]);
blue.append((char*)&colour[3]);
green.append((char*)&colour[4]);
green.append((char*)&colour[5]);
red.append((char*)&colour[6]);
red.append((char*)&colour[7]);
SColor color(axtoi(alpha.c_str()),
axtoi(red.c_str()),axtoi(green.c_str()),
axtoi(blue.c_str()));
//just for compatibility with older Irrlicht versions //just for compatibility with older Irrlicht versions
//to support transparent materials //to support transparent materials
...@@ -246,7 +218,6 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -246,7 +218,6 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
buffer->Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; buffer->Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
buffer->Material.MaterialTypeParam =(((f32) (color.getAlpha()-1))/255.0f); buffer->Material.MaterialTypeParam =(((f32) (color.getAlpha()-1))/255.0f);
} }
immagine->drop();
} }
//Lightmap is present //Lightmap is present
...@@ -260,28 +231,21 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -260,28 +231,21 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
buffer->Material.AmbientColor=header.dmfAmbient.getInterpolated(SColor(255,0,0,0),mult/100.f); buffer->Material.AmbientColor=header.dmfAmbient.getInterpolated(SColor(255,0,0,0),mult/100.f);
} }
if(materiali[i].textureBlend==4) if (materiali[i].textureBlend==4)
{ {
buffer->Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; buffer->Material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
buffer->Material.MaterialTypeParam =SceneMgr->getParameters()->getAttributeAsFloat(DMF_ALPHA_CHANNEL_REF); buffer->Material.MaterialTypeParam =
SceneMgr->getParameters()->getAttributeAsFloat(DMF_ALPHA_CHANNEL_REF);
} }
core::dimension2d<s32> texsize; //if texture is present mirror vertically owing to DeleD representation
core::dimension2d<s32> ligsize;
if (tex && header.dmfVersion<1.1)
texsize=tex->getSize();
if (lig && header.dmfVersion<1.1)
ligsize=lig->getSize();
//if texture is present mirror vertically owing to DeleD rapresentation
if (tex && header.dmfVersion<1.1) if (tex && header.dmfVersion<1.1)
{ {
const core::dimension2d<s32> texsize = tex->getSize();
void* pp = tex->lock(); void* pp = tex->lock();
if (pp) if (pp)
{ {
video::ECOLOR_FORMAT format = tex->getColorFormat(); const video::ECOLOR_FORMAT format = tex->getColorFormat();
if (format == video::ECF_A1R5G5B5) if (format == video::ECF_A1R5G5B5)
{ {
s16* p = (s16*)pp; s16* p = (s16*)pp;
...@@ -308,10 +272,6 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -308,10 +272,6 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
} }
} }
} }
}
if(tex && header.dmfVersion<1.1)
{
tex->unlock(); tex->unlock();
tex->regenerateMipMapLevels(); tex->regenerateMipMapLevels();
} }
...@@ -319,6 +279,7 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -319,6 +279,7 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
//if lightmap is present mirror vertically owing to DeleD rapresentation //if lightmap is present mirror vertically owing to DeleD rapresentation
if (lig && header.dmfVersion<1.1) if (lig && header.dmfVersion<1.1)
{ {
const core::dimension2d<s32> ligsize=lig->getSize();
void* pp = lig->lock(); void* pp = lig->lock();
if (pp) if (pp)
{ {
...@@ -352,10 +313,6 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -352,10 +313,6 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
} }
} }
} }
}
if (lig && header.dmfVersion<1.1)
{
lig->unlock(); lig->unlock();
lig->regenerateMipMapLevels(); lig->regenerateMipMapLevels();
} }
...@@ -366,16 +323,17 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file) ...@@ -366,16 +323,17 @@ IAnimatedMesh* CDMFLoader::createMesh(io::IReadFile* file)
delete verts; delete verts;
delete faces; delete faces;
delete materiali;
} }
// delete all buffers without geometry in it. // delete all buffers without geometry in it.
#ifdef _IRR_DMF_DEBUG_
os::Printer::log("Cleaning meshbuffers.");
#endif
i = 0; i = 0;
while(i < mesh->MeshBuffers.size()) while(i < mesh->MeshBuffers.size())
{ {
if (mesh->MeshBuffers[i]->getVertexCount() == 0 || if (mesh->MeshBuffers[i]->getVertexCount() == 0 ||
mesh->MeshBuffers[i]->getIndexCount() == 0 || mesh->MeshBuffers[i]->getIndexCount() == 0)
mesh->MeshBuffers[i]->getMaterial().getTexture(0) == 0)
{ {
// Meshbuffer is empty -- drop it // Meshbuffer is empty -- drop it
mesh->MeshBuffers[i]->drop(); mesh->MeshBuffers[i]->drop();
......
...@@ -71,16 +71,13 @@ namespace scene ...@@ -71,16 +71,13 @@ namespace scene
Note that loaded water plains from DeleD must have the suffix \b water_ and must be \b rectangle (with just 1 rectangular face). Note that loaded water plains from DeleD must have the suffix \b water_ and must be \b rectangle (with just 1 rectangular face).
Irrlicht correctly loads position and rotation of water plain as well as texture layers. Irrlicht correctly loads position and rotation of water plain as well as texture layers.
\return number of water plains loaded or 0 if loading failed.*/ \return number of water plains loaded or 0 if loading failed.*/
int loadWaterPlains ( const c8 *filename, int loadWaterPlains(const c8 *filename,
ISceneManager* smgr, ISceneManager* smgr,
ISceneNode * parent = 0, ISceneNode * parent = 0,
s32 base_id = 2000, s32 base_id = 2000,
bool mode = true); bool mode = true);
private: private:
void GetFaceNormal(f32 a[3], f32 b[3], f32 c[3], f32 out[3]);
ISceneManager* SceneMgr; ISceneManager* SceneMgr;
io::IFileSystem* FileSystem; io::IFileSystem* FileSystem;
}; };
......
...@@ -31,7 +31,7 @@ struct dmfHeader ...@@ -31,7 +31,7 @@ struct dmfHeader
{ {
//main file header //main file header
f32 dmfVersion; //!<File version f32 dmfVersion; //!<File version
char dmfName[128]; //!<Scene name core::stringc dmfName; //!<Scene name
SColor dmfAmbient; //!<Ambient color SColor dmfAmbient; //!<Ambient color
f32 dmfShadow; //!<Shadow intensity f32 dmfShadow; //!<Shadow intensity
u32 numObjects; //!<Number of objects in this scene u32 numObjects; //!<Number of objects in this scene
...@@ -44,16 +44,17 @@ struct dmfHeader ...@@ -44,16 +44,17 @@ struct dmfHeader
}; };
/** A structure rapresenting a DeleD material. /** A structure representing a DeleD material.
This structure contains texture names, an ID and some flags.*/ This structure contains texture names, an ID and some flags.*/
struct dmfMaterial struct dmfMaterial
{ {
u32 materialID;//!<This material unique ID. u32 materialID;//!<This material unique ID.
u32 textureLayers;//!<First texture Flag (0=Normal, 1=Color).
u32 textureFlag;//!<First texture Flag (0=Normal, 1=Color). u32 textureFlag;//!<First texture Flag (0=Normal, 1=Color).
u32 lightmapFlag;//!<Lightmap Flag (0=Normal, others not considered). u32 lightmapFlag;//!<Lightmap Flag (0=Normal, others not considered).
u32 textureBlend;//!<Texture Blend mode used to support alpha maps (4=Alpha map, others not implemented yet). u32 textureBlend;//!<Texture Blend mode used to support alpha maps (4=Alpha map, others not implemented yet).
char textureName[64];//!<Name of first texture (only file name, no path). core::stringc textureName;//!<Name of first texture (only file name, no path).
char lightmapName[64];//!<Name of lightmap (only file name, no path). core::stringc lightmapName;//!<Name of lightmap (only file name, no path).
}; };
...@@ -71,13 +72,13 @@ struct dmfFace ...@@ -71,13 +72,13 @@ struct dmfFace
This structure contains vertice position coordinates and texture an lightmap UV.*/ This structure contains vertice position coordinates and texture an lightmap UV.*/
struct dmfVert struct dmfVert
{ {
f32 tc[2];//!<Texture UV coords. core::vector3df pos;//!<Position of vertice x,y,z
f32 lc[2];//!<Lightmap UV coords. core::vector2df tc;//!<Texture UV coords.
f32 pos[3];//!<Position of vertice x,y,z core::vector2df lc;//!<Lightmap UV coords.
}; };
/** A structure rapresenting a single dynamic light. /** A structure representing a single dynamic light.
This structure contains light position coordinates, diffuse colour, specular colour and maximum radius of light.*/ This structure contains light position coordinates, diffuse colour, specular colour and maximum radius of light.*/
struct dmfLight struct dmfLight
{ {
...@@ -191,7 +192,7 @@ public: ...@@ -191,7 +192,7 @@ public:
//This function subdivides a string in a list of strings //This function subdivides a string in a list of strings
/** This function subdivides strings divided by divider in a list of strings. /** This function subdivides strings divided by divider in a list of strings.
\return A StringList made of all strings divided by divider.*/ \return A StringList made of all strings divided by divider.*/
StringList SubdivideString(String str, String divider) StringList SubdivideString(const String& str, const String& divider)
{ {
StringList strings; //returned StringList StringList strings; //returned StringList
strings.clear(); //clear returned stringlist strings.clear(); //clear returned stringlist
...@@ -227,7 +228,7 @@ StringList SubdivideString(String str, String divider) ...@@ -227,7 +228,7 @@ StringList SubdivideString(String str, String divider)
/**This function extract a dmfHeader from a DMF file. /**This function extract a dmfHeader from a DMF file.
You must give in input a StringList representing a DMF file loaded with LoadFromFile. You must give in input a StringList representing a DMF file loaded with LoadFromFile.
\return true if function succeed or false on fail.*/ \return true if function succeed or false on fail.*/
bool GetDMFHeader (StringList RawFile, dmfHeader & header) bool GetDMFHeader(const StringList& RawFile, dmfHeader& header)
{ {
StringList temp=SubdivideString(RawFile[0],String(";")); //file info StringList temp=SubdivideString(RawFile[0],String(";")); //file info
...@@ -238,27 +239,16 @@ bool GetDMFHeader (StringList RawFile, dmfHeader & header) ...@@ -238,27 +239,16 @@ bool GetDMFHeader (StringList RawFile, dmfHeader & header)
temp = SubdivideString(RawFile[1],String(" "));//get version temp = SubdivideString(RawFile[1],String(" "));//get version
StringList temp1=SubdivideString(temp[1],String(";")); StringList temp1=SubdivideString(temp[1],String(";"));
if (atof(temp1[0].c_str()) < 0.91) header.dmfVersion = (float)atof(temp1[0].c_str());//save version
if (header.dmfVersion < 0.91)
return false;//not correct version return false;//not correct version
header.dmfVersion = (float)atof(temp1[0].c_str());//save version
temp.clear(); temp.clear();
temp = SubdivideString(RawFile[2],String(";"));//get name,ambient color and shadow opacity temp = SubdivideString(RawFile[2],String(";"));//get name,ambient color and shadow opacity
sprintf(header.dmfName,"%s",temp[0].c_str());//save name header.dmfName=temp[0];//save name
String red=String(""), green=String(""), blue=String("");
//get color value in hex
blue.append((char*)&temp[1][2]);
blue.append((char*)&temp[1][3]);
green.append((char*)&temp[1][4]);
green.append((char*)&temp[1][5]);
red.append((char*)&temp[1][6]);
red.append((char*)&temp[1][7]);
//set ambient color //set ambient color
header.dmfAmbient.set(0, axtoi(red.c_str()), header.dmfAmbient.set(axtoi(temp[1].c_str()));
axtoi(green.c_str()), axtoi(blue.c_str()));
//set Shadow intensity //set Shadow intensity
header.dmfShadow = (float)atof(temp[2].c_str()); header.dmfShadow = (float)atof(temp[2].c_str());
...@@ -339,52 +329,41 @@ bool GetDMFHeader (StringList RawFile, dmfHeader & header) ...@@ -339,52 +329,41 @@ bool GetDMFHeader (StringList RawFile, dmfHeader & header)
} }
/**This function extract an array of dmfMaterial from a DMF file. /**This function extract an array of dmfMaterial from a DMF file.
You must give in input a StringList representing a DMF file loaded with LoadFromFile. You must give in input a StringList representing a DMF file loaded with LoadFromFile.
\param RawFile StringList representing a DMF file.
\param materials Materials returned.
\param num_material Number of materials contained in DMF file.
\param use_material_dirs Here you can choose to use default DeleD structure for material dirs.
\return true if function succeed or false on fail.*/ \return true if function succeed or false on fail.*/
bool GetDMFMaterials(StringList RawFile /**<StringList representing a DMF file.*/, bool GetDMFMaterials(const StringList& RawFile,
dmfMaterial materials[]/**<Materials returned.*/, core::array<dmfMaterial>& materials,
int num_material/**<Number of materials contained in DMF file.*/, int num_material,
bool use_material_dirs=false/**<Here you can choose to use default DeleD structure for material dirs.*/) bool use_material_dirs=false)
{ {
int offs=4; int offs=4;
StringList temp; StringList temp;
StringList temp1; StringList temp1;
StringList temp2; StringList temp2;
//Checking if this is a DeleD map File of version >= 0.91 materials.reallocate(num_material);
temp=SubdivideString(RawFile[0],String(";"));//file info for(int i=0; i<num_material; ++i)
if ( temp[0] != String("DeleD Map File") )
return false;//not a deled file
temp.clear();
temp=SubdivideString(RawFile[1],String(" "));//get version
temp1=SubdivideString(temp[1],String(";"));
if (atof(temp1[0].c_str()) < 0.91)
return false;//not correct version
//end checking
temp.clear();
temp1.clear();
for(int i=0;i<num_material; ++i)
{ {
materials.push_back(dmfMaterial());
temp=SubdivideString(RawFile[offs+i],";"); temp=SubdivideString(RawFile[offs+i],";");
materials[i].materialID = i; materials[i].materialID = i;
materials[i].textureLayers = atoi(temp[3].c_str());
temp1=SubdivideString(temp[5],","); temp1=SubdivideString(temp[5],",");
materials[i].textureFlag = atoi(temp1[0].c_str()); materials[i].textureFlag = atoi(temp1[0].c_str());
if(!use_material_dirs) if(!use_material_dirs && !materials[i].textureFlag)
{ {
temp2=SubdivideString(temp1[1],"\\"); temp2=SubdivideString(temp1[1],"\\");
sprintf(materials[i].textureName, "%s", temp2[temp2.size()-1].c_str()); materials[i].textureName=temp2.getLast();
} }
else else
sprintf(materials[i].textureName, "%s", temp1[1].c_str()); materials[i].textureName=temp1[1];
materials[i].textureBlend = atoi(temp1[2].c_str()); materials[i].textureBlend = atoi(temp1[2].c_str());
temp1.clear(); temp1.clear();
temp2.clear(); temp2.clear();
...@@ -396,15 +375,15 @@ bool GetDMFMaterials(StringList RawFile /**<StringList representing a DMF file.* ...@@ -396,15 +375,15 @@ bool GetDMFMaterials(StringList RawFile /**<StringList representing a DMF file.*
if(!use_material_dirs) if(!use_material_dirs)
{ {
temp2=SubdivideString(temp1[1],"\\"); temp2=SubdivideString(temp1[1],"\\");
sprintf(materials[i].lightmapName,"%s",temp2[temp2.size() - 1].c_str()); materials[i].lightmapName=temp2.getLast();
} }
else else
sprintf(materials[i].lightmapName,"%s",temp1[1].c_str()); materials[i].lightmapName=temp1[1];
} }
else else
{ {
materials[i].lightmapFlag=1; materials[i].lightmapFlag=1;
materials[i].lightmapName[0]=0; materials[i].lightmapName="";
} }
temp1.clear(); temp1.clear();
temp2.clear(); temp2.clear();
...@@ -416,8 +395,8 @@ bool GetDMFMaterials(StringList RawFile /**<StringList representing a DMF file.* ...@@ -416,8 +395,8 @@ bool GetDMFMaterials(StringList RawFile /**<StringList representing a DMF file.*
/**This function extract an array of dmfMaterial from a DMF file considering 1st an 2nd layer for water plains. /**This function extract an array of dmfMaterial from a DMF file considering 1st an 2nd layer for water plains.
You must give in input a StringList representing a DMF file loaded with LoadFromFile. You must give in input a StringList representing a DMF file loaded with LoadFromFile.
\return true if function succeed or false on fail.*/ \return true if function succeed or false on fail.*/
bool GetDMFWaterMaterials(StringList RawFile /**<StringList representing a DMF file.*/, bool GetDMFWaterMaterials(const StringList& RawFile /**<StringList representing a DMF file.*/,
dmfMaterial materials[]/**<Materials returned.*/, core::array<dmfMaterial>& materials/**<Materials returned.*/,
int num_material/**<Number of materials contained in DMF file.*/ int num_material/**<Number of materials contained in DMF file.*/
) )
{ {
...@@ -451,7 +430,7 @@ bool GetDMFWaterMaterials(StringList RawFile /**<StringList representing a DMF f ...@@ -451,7 +430,7 @@ bool GetDMFWaterMaterials(StringList RawFile /**<StringList representing a DMF f
materials[i].textureFlag=atoi(temp1[0].c_str()); materials[i].textureFlag=atoi(temp1[0].c_str());
temp2 = SubdivideString(temp1[1],"\\"); temp2 = SubdivideString(temp1[1],"\\");
sprintf(materials[i].textureName,"%s",temp2[temp2.size()-1].c_str()); materials[i].textureName=temp2.getLast();
temp1.clear(); temp1.clear();
temp2.clear(); temp2.clear();
int a=temp.size(); int a=temp.size();
...@@ -460,12 +439,12 @@ bool GetDMFWaterMaterials(StringList RawFile /**<StringList representing a DMF f ...@@ -460,12 +439,12 @@ bool GetDMFWaterMaterials(StringList RawFile /**<StringList representing a DMF f
temp1=SubdivideString(temp[6],","); temp1=SubdivideString(temp[6],",");
materials[i].lightmapFlag=atoi(temp1[0].c_str()); materials[i].lightmapFlag=atoi(temp1[0].c_str());
temp2=SubdivideString(temp1[1],"\\"); temp2=SubdivideString(temp1[1],"\\");
sprintf(materials[i].lightmapName,"%s",temp2[temp2.size() - 1].c_str()); materials[i].lightmapName=temp2.getLast();
} }
else else
{ {
materials[i].lightmapFlag=1; materials[i].lightmapFlag=1;
sprintf(materials[i].lightmapName,"FFFFFFFF"); materials[i].lightmapName="FFFFFFFF";
} }
temp1.clear(); temp1.clear();
temp2.clear(); temp2.clear();
...@@ -473,83 +452,73 @@ bool GetDMFWaterMaterials(StringList RawFile /**<StringList representing a DMF f ...@@ -473,83 +452,73 @@ bool GetDMFWaterMaterials(StringList RawFile /**<StringList representing a DMF f
return true; return true;
} }
/**This function extract an array of dmfVert and dmfFace from a DMF file. /**This function extract an array of dmfVert and dmfFace from a DMF file.
You must give in input a StringList representing a DMF file loaded with LoadFromFile and two arrays long enough. You must give in input a StringList representing a DMF file loaded with LoadFromFile and two arrays long enough.
Please use GetDMFHeader() before this function to know number of vertices and faces. Please use GetDMFHeader() before this function to know number of vertices and faces.
\return true if function succeed or false on fail.*/ \return true if function succeed or false on fail.*/
bool GetDMFVerticesFaces(StringList RawFile/**<StringList representing a DMF file.*/, bool GetDMFVerticesFaces(const StringList& RawFile/**<StringList representing a DMF file.*/,
dmfVert vertices[]/**<Vertices returned*/, dmfVert vertices[]/**<Vertices returned*/,
dmfFace faces[]/**Faces returned*/ dmfFace faces[]/**Faces returned*/
) )
{ {
int offs=3;
int offs1=0;
StringList temp,temp1; StringList temp,temp1;
//Checking if this is a DeleD map File of version >= 0.91 // skip materials
temp=SubdivideString(RawFile[0],String(";"));//file info s32 offs = 4 + atoi(RawFile[3].c_str());
if ( temp[0] != String("DeleD Map File") )
return false;//not a deled file
temp.clear();
temp=SubdivideString(RawFile[1],String(" "));//get version
temp1=SubdivideString(temp[1],String(";"));
if (atof(temp1[0].c_str()) < 0.91) const s32 objs = atoi(RawFile[offs].c_str());
return false;//not correct version #ifdef _IRR_DMF_DEBUG_
os::Printer::log("Reading objects", core::stringc(objs).c_str());
#endif
//end checking s32 vert=0, tmp_sz=0, vert_cnt=0, face_cnt=0;
temp.clear();
temp1.clear();
offs=offs + atoi(RawFile[offs].c_str());
offs++; offs++;
s32 objs = atoi(RawFile[offs].c_str()); for (int i=0; i<objs; ++i)
s32 fac=0, vert=0, tmp_sz=0, vert_cnt=0, face_cnt=0;
offs++;
for (int i=0; i<objs; i++)
{ {
StringList wat=SubdivideString(RawFile[offs],";"); StringList wat=SubdivideString(RawFile[offs],";");
StringList wat1=SubdivideString(wat[0],"_"); StringList wat1=SubdivideString(wat[0],"_");
#ifdef _IRR_DMF_DEBUG_
os::Printer::log("Reading object", wat[0].c_str());
#endif
offs++; offs++;
offs1=offs; const s32 vrtxPos=offs+1;
offs=offs + atoi(RawFile[offs].c_str()); // skip vertices
offs += atoi(RawFile[offs].c_str());
offs++; offs++;
offs1++; const s32 numFaces=atoi(RawFile[offs].c_str());
fac=atoi(RawFile[offs].c_str());
offs++; offs++;
if(!(wat1[0]==String("water") && wat[2]==String("0"))) if(!(wat1[0]==String("water") && wat[2]==String("0")))
{ {
for(int j=0;j<fac;j++) for(s32 j=0; j<numFaces; ++j)
{ {
temp=SubdivideString(RawFile[offs+j],";"); temp=SubdivideString(RawFile[offs+j],";");
//first value is vertices number for this face //first value is vertices number for this face
faces[face_cnt].numVerts=atoi(temp[0].c_str()); vert=atoi(temp[0].c_str());
vert=faces[face_cnt].numVerts; faces[face_cnt].numVerts=vert;
//second is material ID //second is material ID
faces[face_cnt].materialID=atoi(temp[1].c_str()); faces[face_cnt].materialID=atoi(temp[1].c_str());
//vertices are ordined //vertices are ordined
faces[face_cnt].firstVert=vert_cnt; faces[face_cnt].firstVert=vert_cnt;
//now we'll create vertices structure //now we'll create vertices structure
for(int k=0;k<vert;k++) for(s32 k=0; k<vert; ++k)
{ {
//get vertex position //get vertex position
temp1=SubdivideString(RawFile[offs1+atoi(temp[2+k].c_str())],";"); temp1=SubdivideString(RawFile[vrtxPos+atoi(temp[2+k].c_str())],";");
//copy x,y,z values //copy x,y,z values
vertices[vert_cnt].pos[0] = (float)atof(temp1[0].c_str()); vertices[vert_cnt].pos.set((float)atof(temp1[0].c_str()),
vertices[vert_cnt].pos[1] = (float)atof(temp1[1].c_str()); (float)atof(temp1[1].c_str()),
vertices[vert_cnt].pos[2] = (float)-atof(temp1[2].c_str()); (float)-atof(temp1[2].c_str()));
//get uv coords for tex and light if any //get uv coords for tex and light if any
vertices[vert_cnt].tc[0] = (float)atof(temp[2+vert+(2*k)].c_str()); vertices[vert_cnt].tc.set((float)atof(temp[2+vert+(2*k)].c_str()),
vertices[vert_cnt].tc[1] = (float)atof(temp[2+vert+(2*k)+1].c_str()); (float)atof(temp[2+vert+(2*k)+1].c_str()));
tmp_sz=temp.size(); tmp_sz=temp.size();
vertices[vert_cnt].lc[0] = (float)atof(temp[tmp_sz-(2*vert)+(2*k)].c_str()); vertices[vert_cnt].lc.set((float)atof(temp[tmp_sz-(2*vert)+(2*k)].c_str()),
vertices[vert_cnt].lc[1] = (float)atof(temp[tmp_sz-(2*vert)+(2*k)+1].c_str()); (float)atof(temp[tmp_sz-(2*vert)+(2*k)+1].c_str()));
vert_cnt++; vert_cnt++;
temp1.clear(); temp1.clear();
} }
...@@ -559,7 +528,7 @@ bool GetDMFVerticesFaces(StringList RawFile/**<StringList representing a DMF fil ...@@ -559,7 +528,7 @@ bool GetDMFVerticesFaces(StringList RawFile/**<StringList representing a DMF fil
} }
} }
offs=offs+fac; offs=offs+numFaces;
} }
return true; return true;
...@@ -570,7 +539,7 @@ You must give in input a StringList representing a DMF file loaded with ...@@ -570,7 +539,7 @@ You must give in input a StringList representing a DMF file loaded with
LoadFromFile and one array long enough. Please use GetDMFHeader() before this LoadFromFile and one array long enough. Please use GetDMFHeader() before this
function to know number of dynamic lights. function to know number of dynamic lights.
\return true if function succeed or false on fail.*/ \return true if function succeed or false on fail.*/
bool GetDMFLights(StringList RawFile/**<StringList representing a DMF file.*/, bool GetDMFLights(const StringList& RawFile/**<StringList representing a DMF file.*/,
dmfLight lights[]/**<Lights returned.*/ dmfLight lights[]/**<Lights returned.*/
) )
{ {
...@@ -649,11 +618,11 @@ bool GetDMFLights(StringList RawFile/**<StringList representing a DMF file.*/, ...@@ -649,11 +618,11 @@ bool GetDMFLights(StringList RawFile/**<StringList representing a DMF file.*/,
return true; return true;
} }
/**This function extract an array of dmfWaterPlain,dmfVert and dmfFace from a DMF file. /**This function extracts an array of dmfWaterPlain,dmfVert and dmfFace from a DMF file.
You must give in input a StringList representing a DMF file loaded with LoadFromFile and three arrays long enough. You must give in input a StringList representing a DMF file loaded with LoadFromFile and three arrays long enough.
Please use GetDMFHeader() before this function to know number of water plains and water faces as well as water vertices. Please use GetDMFHeader() before this function to know number of water plains and water faces as well as water vertices.
\return true if function succeed or false on fail.*/ \return true if function succeed or false on fail.*/
bool GetDMFWaterPlains(StringList RawFile/**<StringList representing a DMF file.*/, bool GetDMFWaterPlains(const StringList& RawFile/**<StringList representing a DMF file.*/,
dmfWaterPlain wat_planes[]/**<Water planes returned.*/, dmfWaterPlain wat_planes[]/**<Water planes returned.*/,
dmfVert vertices[]/**<Vertices returned*/, dmfVert vertices[]/**<Vertices returned*/,
dmfFace faces[]/**Faces returned*/ dmfFace faces[]/**Faces returned*/
...@@ -762,21 +731,21 @@ bool GetDMFWaterPlains(StringList RawFile/**<StringList representing a DMF file. ...@@ -762,21 +731,21 @@ bool GetDMFWaterPlains(StringList RawFile/**<StringList representing a DMF file.
temp1=SubdivideString(RawFile[offs1+atoi(temp[2+k].c_str())], ";"); temp1=SubdivideString(RawFile[offs1+atoi(temp[2+k].c_str())], ";");
//copy x,y,z values //copy x,y,z values
vertices[vert_cnt].pos[0]=(float)atof(temp1[0].c_str()); vertices[vert_cnt].pos.set((float)atof(temp1[0].c_str()),
vertices[vert_cnt].pos[1]=(float)atof(temp1[1].c_str()); (float)atof(temp1[1].c_str()),
vertices[vert_cnt].pos[2]=(float)-atof(temp1[2].c_str()); (float)-atof(temp1[2].c_str()));
//get uv coords for tex and light if any //get uv coords for tex and light if any
vertices[vert_cnt].tc[0]=(float)atof(temp[2+vert+(2*k)].c_str()); vertices[vert_cnt].tc.set((float)atof(temp[2+vert+(2*k)].c_str()),
vertices[vert_cnt].tc[1]=(float)atof(temp[2+vert+(2*k)+1].c_str()); (float)atof(temp[2+vert+(2*k)+1].c_str()));
tmp_sz=temp.size(); tmp_sz=temp.size();
vertices[vert_cnt].lc[0]=(float)atof(temp[tmp_sz-(2*vert)+(2*k)].c_str()); vertices[vert_cnt].lc.set((float)atof(temp[tmp_sz-(2*vert)+(2*k)].c_str()),
vertices[vert_cnt].lc[1]=(float)atof(temp[tmp_sz-(2*vert)+(2*k)+1].c_str()); (float)atof(temp[tmp_sz-(2*vert)+(2*k)+1].c_str()));
vert_cnt++; ++vert_cnt;
temp1.clear(); temp1.clear();
} }
face_cnt++; ++face_cnt;
temp.clear(); temp.clear();
} }
} }
......
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