1
// Copyright (C) 2002-2011 Nikolaus Gebhardt
2
// This file is part of the "Irrlicht Engine".
3
// For conditions of distribution and use, see copyright notice in irrlicht.h
5
// This file was originally written by Salvatore Russo.
6
// I (Nikolaus Gebhardt) did some minor modifications changes to it and integrated
8
// - removed STL dependency
9
// - removed log file and replaced it with irrlicht logging
10
// - adapted code formatting a bit to Irrlicht style
11
// - removed memory leaks
12
// Thanks a lot to Salvatore for his work on this and that he gave me
13
// his permission to add it into Irrlicht.
15
// This support library has been made by Salvatore Russo and is released under GNU public license for general uses.
16
// For uses in Irrlicht core and only for Irrlicht related uses I release this library under zlib license.
18
#ifndef __DMF_SUPPORT_H_INCLUDED__
19
#define __DMF_SUPPORT_H_INCLUDED__
21
#include "irrString.h"
22
#include "fast_atof.h"
31
/** A structure representing some DeleD infos.
32
This structure contains data about DeleD level file like: version, ambient colour, number of objects etc...*/
36
core::stringc dmfName; //!<Scene name
37
f32 dmfVersion; //!<File version
38
video::SColor dmfAmbient; //!<Ambient color
39
f32 dmfShadow; //!<Shadow intensity
40
u32 numObjects; //!<Number of objects in this scene
41
u32 numMaterials; //!<Number of materials in this scene
42
u32 numVertices; //!<Total number of vertices faces*(vertices for each face)
43
u32 numFaces; //!<Total number of faces
44
u32 numLights; //!<Number of dynamic lights in this scene
45
u32 numWatVertices; //!<Total number of vertices of water plains watfaces*(vertices for each face)
46
u32 numWatFaces; //!<Total number of faces for water plains.Note that each water plane is a rectangle with one face only.
50
/** A structure representing a DeleD material.
51
This structure contains texture names, an ID and some flags.*/
54
u32 materialID;//!<This material unique ID.
55
u32 textureLayers;//!<First texture Flag (0=Normal, 1=Color).
56
u32 textureFlag;//!<First texture Flag (0=Normal, 1=Color).
57
u32 lightmapFlag;//!<Lightmap Flag (0=Normal, others not considered).
58
u32 textureBlend;//!<Texture Blend mode used to support alpha maps (4=Alpha map, others not implemented yet).
59
core::stringc pathName;//!<Name of path defined in path element.
60
core::stringc textureName;//!<Name of first texture (only file name, no path).
61
core::stringc lightmapName;//!<Name of lightmap (only file name, no path).
62
u32 lightmapBlend;//!<Blend mode used to support alpha maps (not implemented yet).
66
/** A structure representing a single face.
67
This structure contains first vertice index, number of vertices and the material used.*/
70
u32 firstVert;//!<First vertex index.
71
u32 numVerts;//!<Number of vertices for this face.
72
u32 materialID;//!<Material used for this face.
76
/** A structure representing a single vertice.
77
This structure contains vertice position coordinates and texture an lightmap UV.*/
80
core::vector3df pos;//!<Position of vertex
81
core::vector2df tc;//!<Texture UV coords
82
core::vector2df lc;//!<Lightmap UV coords
86
/** A structure representing a single dynamic light.
87
This structure contains light position coordinates, diffuse colour, specular colour and maximum radius of light.*/
90
core::vector3df pos;//!<Position of this light.
91
video::SColorf diffuseColor;//!<Diffuse color.
92
video::SColorf specularColor;//!<Specular color.
93
f32 radius;//!<Maximum radius of light.
96
/** A structure representing a single water plane.
97
This structure contains light position coordinates, diffuse colour, specular colour and maximum radius of light.*/
100
u32 waterID;//!<ID of specified water plane.
101
u32 numFaces;//!<number of faces that make this plain.Owing to the fact that this is a rectangle you'll have 1 every time.
102
u32 firstFace;//!<first face of this plain.
103
core::dimension2d<u32> tileNum;//!<number of tiles of this water plain.
104
f32 waveHeight;//!<height of waves.
105
f32 waveSpeed;//!<waves speed.
106
f32 waveLength;//!<waves length.
110
/** A function to convert a hexstring to a int.
111
This function converts an hex string (i.e. FF) to its int value (i.e. 255).
112
\return An int representing the hex input value.*/
113
int axtoi(const char *hexStg)
115
unsigned int intValue = 0; // integer value of hex string
116
sscanf(hexStg, "%x", &intValue);
120
typedef core::array<core::stringc> StringList;
122
//Loads a stringlist from a file
123
//note that each String added to StringList
124
//is separated by a \\n character and it's present
125
//at the end of line.
126
/** Loads a StringList from a file.
127
This function loads a StringList from a file where each string is divided by a \\n char.*/
128
void LoadFromFile(io::IReadFile* file, StringList& strlist)
130
const long sz = file->getSize();
131
char* buf = new char[sz+1];
141
core::stringc str(start, (u32)(p - start - 1));
143
strlist.push_back(str);
152
core::stringc str(start, (u32)(p - start - 1));
154
strlist.push_back(str);
160
//This function subdivides a string in a list of strings
161
/** This function subdivides strings divided by divider in a list of strings.
162
\return A StringList made of all strings divided by divider.*/
163
StringList SubdivideString(const core::stringc& str, const core::stringc& divider)
165
StringList strings; //returned StringList
166
strings.clear(); //clear returned stringlist
171
//process entire string
174
core::stringc resultstr;
176
//read characters until divider is encountered
177
while((str[c]!=divider[0]) && c<l)
183
//Remove spaces \t and \n from string in my implementation...
184
//pay attention or change it in dll.h if you don't want to remove
186
resultstr.trim();//trims string resultstr
187
strings.push_back(resultstr);//add trimmed string
195
//Get DeleD informations and convert in dmfHeader
196
/**This function extract a dmfHeader from a DMF file.
197
You must give in input a StringList representing a DMF file loaded with LoadFromFile.
198
\return true if function succeed or false on fail.*/
199
bool GetDMFHeader(const StringList& RawFile, dmfHeader& header)
202
RawFile[0].split(temp, ";"); //file info
203
// StringList temp=SubdivideString(RawFile[0],";"); //file info
205
if ( temp[0] != "DeleD Map File" )
206
return false; //not a deled file
209
temp = SubdivideString(RawFile[1]," ");//get version
210
StringList temp1=SubdivideString(temp[1],";");
212
header.dmfVersion = (float)atof(temp1[0].c_str());//save version
213
if (header.dmfVersion < 0.91)
214
return false;//not correct version
217
temp = SubdivideString(RawFile[2],";");//get name,ambient color and shadow opacity
218
header.dmfName=temp[0];//save name
221
header.dmfAmbient.set(axtoi(temp[1].c_str()));
223
//set Shadow intensity
224
header.dmfShadow = (float)atof(temp[2].c_str());
226
//set current position
229
//set Materials Number
230
header.numMaterials=atoi(RawFile[offs].c_str());
231
offs+=header.numMaterials;
235
header.numObjects=atoi(RawFile[offs].c_str());
237
//retrieve face and vertices number
238
header.numVertices=0;
240
header.numWatFaces=0;
241
header.numWatVertices=0;
247
for(i=0; i < (int)header.numObjects; i++)
249
StringList wat=SubdivideString(RawFile[offs],";");
250
StringList wat1=SubdivideString(wat[0],"_");
253
offs += atoi(RawFile[offs].c_str());
256
fac=atoi(RawFile[offs].c_str());
258
if(!(wat1[0]=="water" && wat[2]=="0"))
259
header.numFaces = header.numFaces + fac;
261
header.numWatFaces = header.numWatFaces + fac;
265
for(int j=0; j<fac; j++)
267
if(!(wat1[0] == "water" && wat[2] == "0"))
268
header.numVertices=header.numVertices + atoi(RawFile[offs+j].c_str());
270
header.numWatVertices=header.numWatVertices + atoi(RawFile[offs + j].c_str());
276
//retrieve number of dynamic lights
280
s32 lit = atoi(RawFile[offs].c_str());
282
for (i=0; i<lit; i++)
285
temp=SubdivideString(RawFile[offs],";");
287
if(atoi(temp[0].c_str())==1)
289
temp1=SubdivideString(temp[18],"_");
291
if(temp1[0]=="dynamic")
298
return true; //everything is OK so loading is correct
302
/**This function extract an array of dmfMaterial from a DMF file.
303
You must give in input a StringList representing a DMF file loaded with LoadFromFile.
304
\param RawFile StringList representing a DMF file.
305
\param materials Materials returned.
306
\param num_material Number of materials contained in DMF file.
307
\param use_material_dirs Here you can choose to use default DeleD structure for material dirs.
308
\return true if function succeed or false on fail.*/
309
bool GetDMFMaterials(const StringList& RawFile,
310
core::array<dmfMaterial>& materials,
313
// offset for already handled lines
319
// The number of materials is predetermined
320
materials.reallocate(num_material);
321
for(int i=0; i<num_material; ++i)
323
materials.push_back(dmfMaterial());
325
temp=SubdivideString(RawFile[offs+i],";");
326
// should be equal to first token
327
materials[i].materialID = i;
328
// The path used for the texture
329
materials[i].pathName = temp[2];
330
materials[i].pathName.replace('\\','/');
331
materials[i].pathName += "/";
332
// temp[3] is reserved, temp[4] is the number of texture layers
333
materials[i].textureLayers = core::strtoul10(temp[4].c_str());
334
// Three values are separated by commas
335
temp1=SubdivideString(temp[5],",");
337
materials[i].textureFlag = atoi(temp1[0].c_str());
338
materials[i].textureName=temp1[1];
339
materials[i].textureName.replace('\\','/');
340
materials[i].textureBlend = atoi(temp1[2].c_str());
343
temp1=SubdivideString(temp[temp.size() - 1],",");
344
materials[i].lightmapFlag=atoi(temp1[0].c_str());
345
materials[i].lightmapName=temp1[1];
346
materials[i].lightmapName.replace('\\','/');
347
materials[i].lightmapBlend = atoi(temp1[2].c_str());
351
materials[i].lightmapFlag=1;
352
materials[i].lightmapName="";
359
/**This function extract an array of dmfMaterial from a DMF file considering 1st an 2nd layer for water plains.
360
You must give in input a StringList representing a DMF file loaded with LoadFromFile.
361
\return true if function succeed or false on fail.*/
362
bool GetDMFWaterMaterials(const StringList& RawFile /**<StringList representing a DMF file.*/,
363
core::array<dmfMaterial>& materials/**<Materials returned.*/,
364
int num_material/**<Number of materials contained in DMF file.*/
371
//Checking if this is a DeleD map File of version >= 0.91
372
temp=SubdivideString(RawFile[0],";");//file info
374
if ( temp[0] != "DeleD Map File" )
375
return false;//not a deled file
378
temp=SubdivideString(RawFile[1]," ");//get version
379
temp1=SubdivideString(temp[1],";");
381
if (atof(temp1[0].c_str()) < 0.91)
382
return false;//not correct version
388
for(int i=0;i<num_material;i++)
390
temp = SubdivideString(RawFile[offs+i],";");
391
materials[i].materialID=i;
393
temp1 = SubdivideString(temp[5],",");
394
materials[i].textureFlag=atoi(temp1[0].c_str());
395
temp2 = SubdivideString(temp1[1],"\\");
397
materials[i].textureName=temp2.getLast();
403
temp1=SubdivideString(temp[6],",");
404
materials[i].lightmapFlag=atoi(temp1[0].c_str());
405
temp2=SubdivideString(temp1[1],"\\");
406
materials[i].lightmapName=temp2.getLast();
410
materials[i].lightmapFlag=1;
411
materials[i].lightmapName="FFFFFFFF";
420
/**This function extract an array of dmfVert and dmfFace from a DMF file.
421
You must give in input a StringList representing a DMF file loaded with LoadFromFile and two arrays long enough.
422
Please use GetDMFHeader() before this function to know number of vertices and faces.
423
\return true if function succeed or false on fail.*/
424
bool GetDMFVerticesFaces(const StringList& RawFile/**<StringList representing a DMF file.*/,
425
dmfVert vertices[]/**<Vertices returned*/,
426
dmfFace faces[]/**Faces returned*/
429
StringList temp,temp1;
432
s32 offs = 4 + atoi(RawFile[3].c_str());
434
const s32 objs = atoi(RawFile[offs].c_str());
436
#ifdef _IRR_DMF_DEBUG_
437
os::Printer::log("Reading objects", core::stringc(objs).c_str());
440
s32 vert_cnt=0, face_cnt=0;
441
for (int i=0; i<objs; ++i)
443
StringList wat=SubdivideString(RawFile[offs],";");
444
StringList wat1=SubdivideString(wat[0],"_");
445
#ifdef _IRR_DMF_DEBUG_
446
os::Printer::log("Reading object", wat[0].c_str());
451
core::array<core::vector3df> pos;
452
const u32 posCount = core::strtoul10(RawFile[offs].c_str());
454
pos.reallocate(posCount);
455
for (u32 i=0; i<posCount; ++i)
457
temp1=SubdivideString(RawFile[offs].c_str(),";");
458
pos.push_back(core::vector3df(core::fast_atof(temp1[0].c_str()),
459
core::fast_atof(temp1[1].c_str()),
460
-core::fast_atof(temp1[2].c_str())));
464
const u32 numFaces=core::strtoul10(RawFile[offs].c_str());
466
if(!(wat1[0]=="water" && wat[2]=="0"))
468
for(u32 j=0; j<numFaces; ++j)
470
temp=SubdivideString(RawFile[offs+j],";");
472
//first value is vertices number for this face
473
const u32 vert=core::strtoul10(temp[0].c_str());
474
faces[face_cnt].numVerts=vert;
475
//second is material ID
476
faces[face_cnt].materialID=core::strtoul10(temp[1].c_str());
477
//vertices are ordined
478
faces[face_cnt].firstVert=vert_cnt;
480
//now we'll create vertices structure
481
for(u32 k=0; k<vert; ++k)
484
vertices[vert_cnt].pos.set(pos[core::strtoul10(temp[2+k].c_str())]);
485
//get uv coords for tex and light if any
486
vertices[vert_cnt].tc.set(core::fast_atof(temp[2+vert+(2*k)].c_str()),
487
core::fast_atof(temp[2+vert+(2*k)+1].c_str()));
488
const u32 tmp_sz=temp.size();
489
vertices[vert_cnt].lc.set(core::fast_atof(temp[tmp_sz-(2*vert)+(2*k)].c_str()),
490
core::fast_atof(temp[tmp_sz-(2*vert)+(2*k)+1].c_str()));
505
/**This function extract an array of dmfLights from a DMF file.
506
You must give in input a StringList representing a DMF file loaded with
507
LoadFromFile and one array long enough. Please use GetDMFHeader() before this
508
function to know number of dynamic lights.
509
\return true if function succeed or false on fail.*/
510
bool GetDMFLights(const StringList& RawFile/**<StringList representing a DMF file.*/,
511
dmfLight lights[]/**<Lights returned.*/
515
StringList temp,temp1;
517
//Checking if this is a DeleD map File of version >= 0.91
518
temp=SubdivideString(RawFile[0],";");//file info
520
if ( temp[0] != "DeleD Map File" )
521
return false;//not a deled file
524
temp=SubdivideString(RawFile[1]," ");//get version
525
temp1=SubdivideString(temp[1],";");
527
if (atof(temp1[0].c_str()) < 0.91)
528
return false;//not correct version
534
offs=offs + atoi(RawFile[offs].c_str());
536
s32 objs = atoi(RawFile[offs].c_str());
541
//let's get position of lights in file
547
offs = offs + atoi(RawFile[offs].c_str());
550
offs = offs + atoi(RawFile[offs].c_str());
554
//let's find dynamic lights
555
lit = atoi(RawFile[offs].c_str());
560
temp=SubdivideString(RawFile[offs],";");
561
if(atoi(temp[0].c_str())==1)
563
temp1=SubdivideString(temp[18],"_");
564
if(temp1[0]=="dynamic")
566
lights[d_lit].radius = (float)atof(temp[4].c_str());
567
lights[d_lit].pos.set((float)atof(temp[5].c_str()),
568
(float)atof(temp[6].c_str()),
569
(float)-atof(temp[7].c_str()));
571
lights[d_lit].diffuseColor = video::SColorf(
572
video::SColor(255, atoi(temp[10].c_str()), atoi(temp[11].c_str()),
573
atoi(temp[12].c_str())));
575
lights[d_lit].specularColor = video::SColorf(
576
video::SColor(255, atoi(temp[13].c_str()), atoi(temp[14].c_str()),
577
atoi(temp[15].c_str())));
590
/**This function extracts an array of dmfWaterPlane,dmfVert and dmfFace from a DMF file.
591
You must give in input a StringList representing a DMF file loaded with LoadFromFile and three arrays long enough.
592
Please use GetDMFHeader() before this function to know number of water plains and water faces as well as water vertices.
593
\return true if function succeed or false on fail.*/
594
bool GetDMFWaterPlanes(const StringList& RawFile/**<StringList representing a DMF file.*/,
595
dmfWaterPlane wat_planes[]/**<Water planes returned.*/,
596
dmfVert vertices[]/**<Vertices returned*/,
597
dmfFace faces[]/**Faces returned*/
602
StringList temp,temp1;
604
//Checking if this is a DeleD map File of version >= 0.91
605
temp=SubdivideString(RawFile[0],";");//file info
607
if ( temp[0] != "DeleD Map File" )
608
return false;//not a deled file
611
temp=SubdivideString(RawFile[1]," ");//get version
612
temp1=SubdivideString(temp[1],";");
614
if (atof(temp1[0].c_str()) < 0.91)
615
return false;//not correct version
621
offs=offs+atoi(RawFile[offs].c_str());
623
s32 objs=atoi(RawFile[offs].c_str());
624
s32 fac=0,vert=0,tmp_sz=0,vert_cnt=0,face_cnt=0,wat_id=0;
625
core::dimension2d<u32> tilenum(40,40);
627
f32 wavespeed=300.0f;
628
f32 wavelength=80.0f;
631
for(int i=0;i<objs;i++)
633
StringList wat=SubdivideString(RawFile[offs],";");
634
StringList wat1=SubdivideString(wat[0],"_");
637
offs=offs+atoi(RawFile[offs].c_str());
640
fac=atoi(RawFile[offs].c_str());
643
if(wat1[0]=="water" && wat[2]=="0")
645
StringList userinfo=SubdivideString(wat[7],",");
649
for (j=0; j<(int)userinfo.size(); j++)
654
if(atoi(userinfo[0].c_str()))
655
tilenum.Width = atoi(userinfo[0].c_str());
658
if(atoi(userinfo[1].c_str()))
659
tilenum.Height = atoi(userinfo[1].c_str());
662
if(atof(userinfo[2].c_str()))
663
waveheight = (float)atof(userinfo[2].c_str());
666
if(atof(userinfo[3].c_str()))
667
wavespeed = (float)atof(userinfo[3].c_str());
670
if(atof(userinfo[4].c_str()))
671
wavelength = (float)atof(userinfo[4].c_str());
676
wat_planes[wat_id].waterID=wat_id;
677
wat_planes[wat_id].numFaces=fac;
678
wat_planes[wat_id].firstFace=face_cnt;
679
wat_planes[wat_id].tileNum=tilenum;
680
wat_planes[wat_id].waveHeight=waveheight;
681
wat_planes[wat_id].waveSpeed=wavespeed;
682
wat_planes[wat_id].waveLength=wavelength;
686
temp=SubdivideString(RawFile[offs+j],";");
688
//first value is vertices number for this face
689
faces[face_cnt].numVerts=atoi(temp[0].c_str());
690
vert=faces[face_cnt].numVerts;
691
//second is material ID
692
faces[face_cnt].materialID=atoi(temp[1].c_str());
693
//vertices are ordined
694
faces[face_cnt].firstVert=vert_cnt;
696
//now we'll create vertices structure
697
for(int k=0;k<vert;k++)
699
//get vertex position
700
temp1=SubdivideString(RawFile[offs1+atoi(temp[2+k].c_str())], ";");
703
vertices[vert_cnt].pos.set((float)atof(temp1[0].c_str()),
704
(float)atof(temp1[1].c_str()),
705
(float)-atof(temp1[2].c_str()));
707
//get uv coords for tex and light if any
708
vertices[vert_cnt].tc.set((float)atof(temp[2+vert+(2*k)].c_str()),
709
(float)atof(temp[2+vert+(2*k)+1].c_str()));
712
vertices[vert_cnt].lc.set((float)atof(temp[tmp_sz-(2*vert)+(2*k)].c_str()),
713
(float)atof(temp[tmp_sz-(2*vert)+(2*k)+1].c_str()));
728
} // end namespace scene
729
} // end namespace irr
731
#endif /* __DMF_SUPPORT_H__ */