-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFbxToLwo.cpp
More file actions
241 lines (195 loc) · 8.13 KB
/
Copy pathFbxToLwo.cpp
File metadata and controls
241 lines (195 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
#include <filesystem>
#include <algorithm>
#include "openfbx/ofbx.h"
#include "export/Lwo2Exporter.h"
#include "FbxSurface.h"
inline ArbitraryMeshVertex ConstructMeshVertex(const ofbx::Geometry& geometry, int index)
{
auto vertices = geometry.getVertices();
auto normals = geometry.getNormals();
auto uvs = geometry.getUVs();
auto colours = geometry.getColors();
return ArbitraryMeshVertex(
Vertex3f(vertices[index].x, vertices[index].y, vertices[index].z),
normals != nullptr ? Normal3f(normals[index].x, normals[index].y, normals[index].z) : Normal3f(1, 0, 0),
uvs != nullptr ? TexCoord2f(uvs[index].x, 1.0 - uvs[index].y) : TexCoord2f(0, 0), // invert v
colours != nullptr ? Vector3(colours[index].x, colours[index].y, colours[index].z) : Vector3(1, 1, 1)
);
}
void ExportFbxMesh(ofbx::IScene& scene, model::Lwo2Exporter& exporter)
{
for (int meshIndex = 0; meshIndex < scene.getMeshCount(); ++meshIndex)
{
auto mesh = scene.getMesh(meshIndex);
auto geometry = mesh->getGeometry();
std::cout << "Exporting FBX Mesh with " << geometry->getVertexCount() << " vertices\n";
std::vector<model::FbxSurface> surfacesByMaterial(mesh->getMaterialCount());
if (mesh->getMaterialCount() == 0)
{
surfacesByMaterial.emplace_back().material = "Material"; // create at least one surface
}
// Assign the surface name for each material
for (int m = 0; m < mesh->getMaterialCount(); ++m)
{
auto material = mesh->getMaterial(m);
surfacesByMaterial[m].material = material->name;
}
auto materials = geometry->getMaterials();
auto faceIndices = geometry->getFaceIndices();
for (int i = 0; i < geometry->getIndexCount(); i += 3)
{
// Material index is assigned per triangle
auto polyIndex = i / 3;
auto materialIndex = materials ? materials[polyIndex] : 0; // put into first material by default
// Reverse the poly indices to get the CCW order
auto indexA = (faceIndices[i+2] * -1) - 1; // last index is negative and 1-based
auto indexB = faceIndices[i+1];
auto indexC = faceIndices[i+0];
surfacesByMaterial[materialIndex].addVertex(ConstructMeshVertex(*geometry, indexA));
surfacesByMaterial[materialIndex].addVertex(ConstructMeshVertex(*geometry, indexB));
surfacesByMaterial[materialIndex].addVertex(ConstructMeshVertex(*geometry, indexC));
}
// Apply the global transformation matrix
auto t = geometry->getGlobalTransform();
#if 0
auto transform = Matrix4::byColumns(
t.m[0], t.m[1], t.m[2], t.m[3],
t.m[4], t.m[5], t.m[6], t.m[7],
t.m[8], t.m[9], t.m[10], t.m[11],
t.m[12], t.m[13], t.m[14], t.m[15]
);
#endif
// "Objects in the FBX SDK are always created in the right handed, Y-Up axis system"
auto transform = Matrix4::getIdentity();
if (scene.getGlobalSettings()->UpAxis == ofbx::UpVector_AxisY)
{
transform = transform.getPremultipliedBy(Matrix4::getRotationForEulerXYZDegrees(Vector3(90, 0, 0)));
}
std::cout << "Generated " << surfacesByMaterial.size() << " triangulated surfaces\n";
for (const auto& surface : surfacesByMaterial)
{
std::cout << " - " << surface.material << std::endl;
exporter.addSurface(surface, transform);
}
}
}
void ConvertFbxToLwo(const std::filesystem::path& inputPath, const std::filesystem::path& outputPath)
{
std::ifstream ifs(inputPath, std::ios::binary | std::ios::ate);
std::ifstream::pos_type pos = ifs.tellg();
std::vector<char> content(pos);
ifs.seekg(0, std::ios::beg);
ifs.read(content.data(), pos);
auto scene = ofbx::load(reinterpret_cast<ofbx::u8*>(content.data()),
static_cast<int>(content.size()), (ofbx::u64)ofbx::LoadFlags::TRIANGULATE);
if (!scene)
{
std::cerr << ofbx::getError() << std::endl;
return;
}
try
{
auto exporter = std::make_shared<model::Lwo2Exporter>();
ExportFbxMesh(*scene, *exporter);
// Ensure the folders exist
std::filesystem::create_directories(outputPath.parent_path());
std::cout << "Exporting LWO to " << outputPath.string() << std::endl;
exporter->exportToPath(outputPath.parent_path().string(), outputPath.filename().string());
scene->destroy();
}
catch (const std::exception&)
{
scene->destroy();
throw;
}
}
namespace string
{
std::string toLower(std::string s)
{
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
return s;
}
}
int main(int argc, char* argv[])
{
if (argc == 1)
{
std::cout << "Single File Usage: FbxToLwo <file1.fbx> <file2.fbx> <...>" << std::endl;
std::cout << " Single specified FBX files will be converted to LWO, which will be placed right next to the FBX files." << std::endl;
std::cout << " Example: FbxToLwo c:\\temp\\model.fbx c:\\temp\\model2.fbx" << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Batch Folder Conversion Usage: FbxToLwo -input <path> -output <path>" << std::endl;
std::cout << " Every FBX in the input folder and all its child folders will be converted to LWO, which will be placed" << std::endl;
std::cout << " in the same relative path in the output folder." << std::endl;
std::cout << " Example: FbxToLwo -input c:\\temp\fbx_files -output c:\\temp\\lwo_files" << std::endl;
return -1;
}
std::filesystem::path inputFolder;
std::filesystem::path outputFolder;
for (int i = 1; i < argc; ++i)
{
if (string::toLower(argv[i]) == "-input")
{
if (argc <= i + 1)
{
std::cerr << "No input folder specified";
return -1;
}
inputFolder = argv[i + 1];
++i;
}
else if (string::toLower(argv[i]) == "-output")
{
if (argc <= i + 1)
{
std::cerr << "No output folder specified";
return -1;
}
outputFolder = argv[i + 1];
++i;
}
}
if (inputFolder.empty() ^ outputFolder.empty())
{
std::cerr << "Both input and output folders must be specified" << std::endl;
return -1;
}
if (std::filesystem::is_directory(inputFolder) && !outputFolder.empty())
{
std::cout << "Batch-converting the FBX files in directory " << inputFolder.string() << " to " << outputFolder.string() << std::endl;
for (auto i = std::filesystem::recursive_directory_iterator(inputFolder); i != std::filesystem::recursive_directory_iterator(); ++i)
{
if (string::toLower(i->path().extension().string()) == ".fbx")
{
auto outputPath = outputFolder / std::filesystem::relative(i->path(), inputFolder);
outputPath.replace_extension("lwo");
std::cout << "Converting: " << i->path().string() << " => " << outputPath.string() << std::endl;
ConvertFbxToLwo(i->path(), outputPath);
}
}
return 0;
}
for (int i = 1; i < argc; ++i)
{
std::filesystem::path inputPath = argv[i];
try
{
if (!std::filesystem::exists(inputPath)) throw std::runtime_error("Path does not exist " + inputPath.string());
if (std::filesystem::is_regular_file(inputPath))
{
std::cout << "Trying to convert file " << inputPath.string() << std::endl;
std::filesystem::path outputPath = inputPath;
outputPath.replace_extension("lwo");
ConvertFbxToLwo(inputPath, outputPath);
}
}
catch (const std::exception& ex)
{
std::cerr << "Failed to handle file " << inputPath << ": " << ex.what() << std::endl;
}
}
return 0;
}