Skip to content

Commit 765e200

Browse files
committed
Bug fix conversion without material and improvements
- Added new way to convert dff models by passing only the file path - Bug fix material assignment without having any txd or material inside
1 parent 3750295 commit 765e200

File tree

5 files changed

+51
-18
lines changed

5 files changed

+51
-18
lines changed

src/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,7 @@ install(TARGETS dff_converter
3737
install(TARGETS dff_converter_static
3838
DESTINATION ${DFF_CONVERTER_LIBRARY_OUTPUT_DIRECTORY}
3939
PUBLIC_HEADER DESTINATION ${DFF_CONVERTER_INCLUDE_OUTPUT_DIRECTORY}
40-
)
40+
)
41+
42+
target_include_directories (dff_converter PUBLIC ${DFF_CONVERTER_INCLUDE_OUTPUT_DIRECTORY})
43+
target_include_directories (dff_converter_static PUBLIC ${DFF_CONVERTER_INCLUDE_OUTPUT_DIRECTORY})

src/converter/Converter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
class Converter
1111
{
12+
virtual bool convert(char* output, char* inputDff, char* inputTxd) = 0;
1213
virtual bool convert(char* output, rw::Clump &dff, rw::TextureDictionary &txd) = 0;
1314
virtual bool convert(char* output, rw::Clump &dff) = 0;
1415
};

src/converter/ConverterGLTF.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "util.h"
44
#include <lodepng.h>
55

6+
#include <fstream>
67
#include <algorithm>
78
#include <climits>
89
#include <cfloat>
@@ -429,6 +430,20 @@ int ConverterGLTF::insertIndices(std::vector<BYTE>& buffer, rw::Split* split, in
429430
return bytesLength;
430431
}
431432

433+
bool ConverterGLTF::convert(char* output, char* inputDff, char* inputTxd)
434+
{
435+
std::ifstream dff(inputDff, std::ios::binary);
436+
std::ifstream txd(inputTxd, std::ios::binary);
437+
438+
rw::Clump dffStruct;
439+
rw::TextureDictionary txdStruct;
440+
441+
dffStruct.read(dff);
442+
txdStruct.read(txd);
443+
444+
return convert(output, dffStruct, txdStruct);
445+
}
446+
432447
bool ConverterGLTF::convert(char* output, rw::Clump& dff)
433448
{
434449
rw::TextureDictionary emptyTXD;
@@ -464,6 +479,11 @@ bool ConverterGLTF::convert(char* output, rw::Clump& dff, rw::TextureDictionary&
464479
int splits = geometry->splits.size();
465480
int curBytesOffset = 0;
466481

482+
int positionsIndex = -1;
483+
int normalsIndex = -1;
484+
int texCoordIndex = -1;
485+
486+
467487
materials.clear();
468488

469489
//TEXTURES
@@ -499,10 +519,19 @@ bool ConverterGLTF::convert(char* output, rw::Clump& dff, rw::TextureDictionary&
499519

500520
//VERTICI
501521
curBytesOffset += insertVertices(buffer.data, indeces, geometry, curBytesOffset);
522+
positionsIndex = currentAccessor - 1;
502523
//COORDINATE UV
503-
curBytesOffset += insertUVTexture(buffer.data, indeces, geometry, curBytesOffset);
524+
if (materials.size() > 0)
525+
{
526+
curBytesOffset += insertUVTexture(buffer.data, indeces, geometry, curBytesOffset);
527+
texCoordIndex = currentAccessor - 1;
528+
}
504529
//NORMALI
505530
curBytesOffset += insertNormals(buffer.data, indeces, geometry, curBytesOffset);
531+
normalsIndex = currentAccessor - 1;
532+
533+
534+
506535
//OSSA DELLO SCHELETRO
507536
//curBytesOffset += insertBones(buffer.data, mesh, geometry, curBytesOffset);
508537

@@ -522,17 +551,20 @@ bool ConverterGLTF::convert(char* output, rw::Clump& dff, rw::TextureDictionary&
522551
primitive.indices = currentAccessor - 1;
523552

524553
//VERTICI
525-
primitive.attributes["POSITION"] = 0;
554+
primitive.attributes["POSITION"] = positionsIndex;
526555

527556
//NORMALI
528557
if(geometry->hasNormals)
529-
primitive.attributes["NORMAL"] = 2;
558+
primitive.attributes["NORMAL"] = normalsIndex;
530559

531560
//COORDINATE UV
532-
std::string texCoord = "TEXCOORD_0";
533-
primitive.attributes[texCoord] = 1;
561+
if (texCoordIndex >= 0)
562+
{
563+
std::string texCoord = "TEXCOORD_0";
564+
primitive.attributes[texCoord] = 1;
565+
primitive.material = material;
566+
}
534567

535-
primitive.material = material;
536568
primitive.mode = TINYGLTF_MODE_TRIANGLES;
537569

538570
mesh.primitives.push_back(primitive);

src/converter/ConverterGLTF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ConverterGLTF : public Converter
3939
public:
4040
ConverterGLTF();
4141

42+
virtual bool convert(char* output, char* inputDff, char* inputTxd);
4243
virtual bool convert(char* output, rw::Clump& dff, rw::TextureDictionary& txd);
4344
virtual bool convert(char* output, rw::Clump& dff);
4445
};

src/main.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,18 @@ int main()
4343
std::ifstream dff(inpDir + baseFile + ".dff", std::ios::binary);
4444
std::ifstream txd(inpDir + baseFile + ".txd", std::ios::binary);
4545

46-
if (dff.is_open() && txd.is_open())
47-
{
48-
rw::Clump dffStruct;
49-
rw::TextureDictionary txdStruct;
46+
rw::Clump dffStruct;
47+
rw::TextureDictionary txdStruct;
5048

51-
dffStruct.read(dff);
52-
txdStruct.read(txd);
49+
dffStruct.read(dff);
50+
txdStruct.read(txd);
5351

54-
output = outDir + baseFile + ".gltf";
55-
56-
converter.convert((char*)output.c_str(), dffStruct, txdStruct);
52+
output = outDir + baseFile + ".gltf";
5753

54+
if(converter.convert((char*)output.c_str(), dffStruct, txdStruct))
5855
std::cout << baseFile << " converted" << std::endl;
59-
}
6056
else
61-
std::cout << "Cannot open " << baseFile << " ignoring..." << std::endl;
57+
std::cout << baseFile << " not converted!" << std::endl;
6258
}
6359
}
6460
}

0 commit comments

Comments
 (0)