aboutsummaryrefslogtreecommitdiff
path: root/NvBlast/tools/ApexImporter/src/Main.cpp
blob: 157e92344d829125723c3ebe4a0206f8e3b2c22c (plain) (blame)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#if NV_VC
#pragma warning(push)
#pragma warning(disable: 4996) // 'fopen' unsafe warning, from NxFileBuffer.h
#endif

#include "PxPhysicsAPI.h"
#include "Apex.h"
#include <ModuleDestructible.h>
#include <DestructibleAsset.h>
#include "NullRenderer.h"
#include "NvBlastExtApexImportTool.h"
#include "Log.h"
#include <string>
#include <iostream>
#include "tclap/CmdLine.h"
#include "ApexDestructibleObjExporter.h"
#include "NvBlastTkFramework.h"
#include "NvBlastExtPxAsset.h"
#include "NvBlastExtPxManager.h"
#include <BlastDataExporter.h>
#include "windows.h"
#include <NvBlastTkAsset.h>
#define DEFAULT_INPUT_FILE "../../../tools/ApexImporter/resources/assets/table.apb"
#define DEFAULT_OUTPUT_DIR "C:/TestFracturer/"
#define DEFAULT_ASSET_NAME "table"

using namespace Nv::Blast;
using namespace Nv::Blast::ApexImporter;

void loggingCallback(int type, const char* msg, const char* file, int line)
{
	if (type == NvBlastMessage::Info)
		lout() << Log::TYPE_INFO << msg << " FILE:" << file << " Line: " << line << "\n";
	else
		lout() << Log::TYPE_ERROR << msg << " FILE:" << file << " Line: " << line << "\n";
}

bool isDirectoryExist(std::string path)
{
	DWORD attributes = GetFileAttributesA(path.c_str());
	if ((attributes != INVALID_FILE_ATTRIBUTES) && (attributes & FILE_ATTRIBUTE_DIRECTORY))
	{
		return true;
	}
	return false;
}

bool mkDirRecursively(std::string path)
{
	if (isDirectoryExist(path))
	{
		return true;
	}
	auto indx = path.find_first_of("\\/");
	while (indx != std::string::npos)
	{
		std::string subfolder = path.substr(0, indx);
		CreateDirectory(subfolder.c_str(), NULL);
		indx = path.find_first_of("\\/", indx + 1);
	}
	return isDirectoryExist(path);
}

// Create an Asset from the APEX destructible asset
void run(const std::string& inFilepath, const std::string& outDir, const std::string& assetName, uint32_t mode, bool llFlag, bool tkFlag, bool extPxFlag, bool obj, bool fbx, bool fbxascii, bool ue4)
{
	std::string inputDir = inFilepath.substr(0, inFilepath.find_last_of("/\\"));

	// load APEX
	ApexImportTool blast(loggingCallback);

	lout() << Log::TYPE_INFO << "ApexImportTool initialization" << std::endl;
	blast.initialize();
	if (!blast.isValid())
	{
		lout() << Log::TYPE_ERROR << "Failed to create BlastSDK" << std::endl;
		return;
	}

	// load asset
	lout() << Log::TYPE_INFO << "Loading asset: " << inFilepath << std::endl;
	physx::PxFileBuf* apexAssetStream = nvidia::apex::GetApexSDK()->createStream(inFilepath.c_str(), physx::PxFileBuf::OPEN_READ_ONLY);
	nvidia::apex::DestructibleAsset* apexAsset = blast.loadAssetFromFile(apexAssetStream);
	if (!apexAsset)
	{
		return;
	}  
	apexAssetStream->release();

	ApexImporterConfig config;
	config.infSearchMode = static_cast<ApexImporterConfig::InterfaceSearchMode>(mode);

	std::vector<uint32_t> chunkReorderInvMap;
	TkFrameworkDesc frameworkDesc =
	{
		nvidia::apex::GetApexSDK()->getErrorCallback(),
		nvidia::apex::GetApexSDK()->getAllocator()
	};
	TkFramework* framework = NvBlastTkFrameworkCreate(frameworkDesc);
	if (framework == nullptr)
	{
		lout() << Log::TYPE_ERROR << "Failed to create TkFramework" << std::endl;
		return;
	}

	std::vector<NvBlastChunkDesc>	chunkDesc;
	std::vector<NvBlastBondDesc>	bondDescs;
	std::vector<uint32_t>			flags;

	std::vector<ExtPxAssetDesc::ChunkDesc> physicsChunks;
	std::vector<ExtPxAssetDesc::SubchunkDesc> physicsSubchunks;

	bool result = blast.importApexAsset(chunkReorderInvMap, apexAsset, chunkDesc, bondDescs, flags, config);
	if (!result)
	{
		lout() << Log::TYPE_ERROR << "Failed to build Blast asset data" << std::endl;
		return;
	};

	result = blast.getCollisionGeometry(apexAsset, static_cast<uint32_t>(chunkDesc.size()), chunkReorderInvMap, flags, physicsChunks, physicsSubchunks);
	if (!result)
	{
		lout() << Log::TYPE_ERROR << "Failed to build physics data" << std::endl;
		return;
	};


	// save asset
	lout() << Log::TYPE_INFO << "Saving blast asset: " << outDir << std::endl;
	BlastDataExporter blExpr(framework, nvidia::apex::GetApexSDK()->getCookingInterface(), loggingCallback);
	NvBlastAsset* llAsset = blExpr.createLlBlastAsset(bondDescs, chunkDesc);

	std::cout <<"Chunk count: " << NvBlastAssetGetChunkCount(llAsset, NULL) << std::endl;

	if (llAsset == nullptr && (llFlag || fbx))
	{
		lout() << Log::TYPE_ERROR << "Failed to build low-level asset" << std::endl;
		return;
	}

	if (llFlag)
	{
		std::ostringstream outBlastFilePathStream;
		outBlastFilePathStream << outDir << "/" << assetName << ".llasset";
		std::string outBlastFilePath = outBlastFilePathStream.str();
		blExpr.saveBlastLLAsset(outBlastFilePath, llAsset);
		std::cout << "Wrote NvBlastAsset to " << outBlastFilePath << std::endl;
	}
	if (tkFlag)
	{
		TkAsset* tkAsset = blExpr.createTkBlastAsset(bondDescs, chunkDesc);
		std::ostringstream outBlastFilePathStream;
		outBlastFilePathStream << outDir << "/" << assetName << ".tkasset";
		std::string outBlastFilePath = outBlastFilePathStream.str();
		blExpr.saveBlastTkAsset(outBlastFilePath, tkAsset);
		std::cout << "Wrote TkAsset to " << outBlastFilePath << std::endl;
		tkAsset->release();
	}
	if (extPxFlag)
	{
		ExtPxAsset* pxAsset = blExpr.createExtBlastAsset(bondDescs, chunkDesc, physicsChunks);
		std::ostringstream outBlastFilePathStream;
		outBlastFilePathStream << outDir << "/" << assetName << ".bpxa";
		std::string outBlastFilePath = outBlastFilePathStream.str();
		blExpr.saveBlastExtAsset(outBlastFilePath, pxAsset);
		std::cout << "Wrote ExtPxAsset to " << outBlastFilePath << std::endl;
		pxAsset->release();
	}	
	
	lout() << Log::TYPE_INFO << "Saving model file: " << outDir << std::endl;
	ApexDestructibleGeometryExporter objSaver(inputDir, outDir);
	objSaver.exportToFile(llAsset, *apexAsset, assetName, chunkReorderInvMap, fbx, obj, fbxascii, ue4);
	_aligned_free(llAsset);
}


int main(int argc, const char* const* argv)
{
	try 
	{
		// setup cmd line
		TCLAP::CmdLine cmd("Blast SDK: APEX Importer", ' ', "0.1");

		TCLAP::ValueArg<std::string> infileArg("f", "file", "File to load", true, DEFAULT_INPUT_FILE, "infile");
		cmd.add(infileArg);

		TCLAP::ValueArg<std::string> outDirArg("o", "outputDir", "Output directory", false, DEFAULT_OUTPUT_DIR, "output directory");
		cmd.add(outDirArg);

		TCLAP::ValueArg<std::string> outAssetName("n", "outAssetName", "Output asset name", true, DEFAULT_ASSET_NAME, "output asset name");
		cmd.add(outAssetName);


		TCLAP::ValueArg<uint32_t> interfaceSearchMode("m", "mode", "Interface search mode", false, 0, "0 - EXACT, 1 - FORCED, for detailed description see docs.");
		cmd.add(interfaceSearchMode);

		TCLAP::SwitchArg debugSwitch("d", "debug", "Print debug output", cmd, false);

		TCLAP::SwitchArg bpxaOutputArg("", "bpxa", "Output ExtPxAsset to the output directory (ext: bpxa)", false);
		cmd.add(bpxaOutputArg);

		TCLAP::SwitchArg tkOutputArg("", "tk", "Output TkAsset to the output directory (ext: tkasset)", false);
		cmd.add(tkOutputArg);

		TCLAP::SwitchArg llOutputArg("", "ll", "Output LL Blast asset to the output directory (ext: llasset)", false);
		cmd.add(llOutputArg);

		TCLAP::SwitchArg ue4OutputArg("", "ue4", "Output FBX with UE4 coordinate system", false);
		cmd.add(ue4OutputArg);

		TCLAP::SwitchArg fbxAsciiArg("", "fbxascii", "Output FBX as an ascii file (defaults to binary output)", false);
		cmd.add(fbxAsciiArg);

		TCLAP::SwitchArg objOutputArg("", "obj", "Output a OBJ mesh to the output directory", false);
		cmd.add(objOutputArg);

		TCLAP::SwitchArg fbxOutputArg("", "fbx", "Output a FBX mesh to the output directory", false);
		cmd.add(fbxOutputArg);


		// parse cmd input
		cmd.parse(argc, argv);

		bool bOutputBPXA = bpxaOutputArg.getValue();
		bool bOutputTK = tkOutputArg.getValue();
		bool bOutputLL = llOutputArg.getValue();

		bool bUE4CoordSystem = ue4OutputArg.isSet();
		bool bOutputFBXAscii = fbxAsciiArg.isSet();

		bool bOutputObjFile = objOutputArg.isSet();
		bool bOutputFbxFile = fbxOutputArg.isSet();


		// Did we specify no output formats?
		if (!bpxaOutputArg.isSet() && !tkOutputArg.isSet() && !llOutputArg.isSet())
		{
			std::cout << "Didn't specify an output format on the command line, so defaulting to outputting BPXA" << std::endl;
			bOutputBPXA = true;
		}
		// Did we specify no geometry output formats?
		if (!bOutputObjFile && !bOutputFbxFile)
		{
			std::cout << "Didn't specify an output geometry format on the command line, so defaulting to outputting .OBJ" << std::endl;
			bOutputObjFile = true;
		}

		// get cmd parse results
		std::string infile = infileArg.getValue();
		std::string outDir;
		std::string assetName = outAssetName.getValue();
		
		/**
			Set output dir, make sure that it exist.
		*/
		if (outDirArg.isSet())
		{
			outDir = outDirArg.getValue();
			std::string temp = outDir + '/';
			if (!isDirectoryExist(outDir.data()))
			{
				std::cout << "Output directory doesn't exist. It will be created." << std::endl;
				if (!mkDirRecursively(temp.data()))
				{
					std::cout << "Directory creation failed!" << std::endl;
					return -1;
				}
			}
		}
		else
		{
			auto idx = infile.find_last_of("/\\");
			if (idx == 0 || idx == std::string::npos)
			{
				outDir = ".";
			}
			else
			{
				outDir = infile.substr(0, idx);
			}
		}

		bool debug = debugSwitch.getValue();
		int mode = interfaceSearchMode.getValue();
		// do stuff
		if (debug)
			lout().setMinVerbosity(Log::MOST_VERBOSE);

		run(infile, outDir, assetName, mode, bOutputLL, bOutputTK, bOutputBPXA, bOutputObjFile, bOutputFbxFile, bOutputFBXAscii, bUE4CoordSystem);
	}
	catch (TCLAP::ArgException &e)  // catch any exceptions
	{
		lout() << Log::TYPE_ERROR << "error: " << e.error() << " for arg " << e.argId() << std::endl;
	}

	return 0;
}