aboutsummaryrefslogtreecommitdiff
path: root/PhysX_3.4/Snippets/SnippetConvert/SnippetConvert.cpp
blob: d66a8ac7fb28a78607426981ab6115d5f264588e (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  


// ***********************************************************************************************
// This snippet illustrates how to convert PhysX 3 serialized binary files from one platform to
// another. The conversion requires three input files:
// 
// 1. A metadata file that was created on the source platform. This file specifies the 
//   source platform as well as the layout of PhysX data structures on the source platform.
// 2. A metadata file that was created on the target platform. This file specifies the target 
//   (destination) platform as well as the layout of PhysX data structures on the target platform. 
// 3. A source file containing a binary serialized collection. The platform this file was created
//   on needs to match with the platform the source metadata file has been created on.
//
// A set of pre-built binary metadata files for various platforms is included with the PhysX SDK 
// at [path to installed PhysX SDK]/Tools/BinaryMetaData.
//
// Optionally this snippet allows to create a example file with binary serialized data for the 
// platform the snippet runs on.
//
// The conversion snippet only compiles and runs on authoring platforms (windows, osx and linux).
//
// SnippetConvert is a simple command-line tool supporting the following options::
//
//  --srcMetadata=<filename>             Specify the source metadata (and the source platform)
//  --dstMetadata=<filename>             Specify the target metadata (and the target platform)
//  --srcBinFile=<filename>              Source binary file to convert (serialized on target platform)
//  --dstBinFile=<filename>              Outputs target binary file
//  --generateExampleFile=<filename>     Generates an example file
//  --dumpBinaryMetaData=<filename>      Dump binary meta data for current runtime platform
//  --verbose                            Enables verbose mode
//
// ***********************************************************************************************

#include "PxPhysicsAPI.h"

#include <iostream>

#include "../SnippetCommon/SnippetPrint.h"
#include "../SnippetUtils/SnippetUtils.h"


using namespace physx;
PxDefaultAllocator		 gAllocator;
PxDefaultErrorCallback	 gErrorCallback;

PxFoundation*			 gFoundation = NULL;
PxPhysics*				 gPhysics	= NULL;
PxSerializationRegistry* gSerializationRegistry = NULL;


struct CmdLineParameters
{
	bool			verbose;
	const char*		srcMetadata;		
	const char*		dstMetadata;			
	const char*		srcBinFile;		
	const char*		dstBinFile;		
	const char*		exampleFile;
	const char*		dumpMetaDataFile;

	CmdLineParameters()
		: verbose(false)
		, srcMetadata(NULL)
		, dstMetadata(NULL)
		, srcBinFile(NULL)
		, dstBinFile(NULL)
		, exampleFile(NULL)
		, dumpMetaDataFile(NULL)
	{
	}
};

static bool match(const char* opt, const char* ref)
{
	std::string s1(opt);
	std::string s2(ref);
	return !s1.compare(0, s2.length(), s2);
}

static void printHelpMsg()
{
	printf("SnippetConvert usage:\n"
		"SnippetConvert "
		"--srcMetadata=<filename> "
		"--dstMetadata=<filename> "
		"--srcBinFile=<filename> "
		"--dstBinFile=<filename> "
		"--generateExampleFile=<filename> "
		"--dumpBinaryMetaData=<filename> "
	    "--verbose \n"
		"A set of pre-built binary metadata is included with the PhysX SDK "
		"at [path to installed PhysX SDK]/Tools/BinaryMetaData.\n");

	printf("--srcMetadata=<filename>\n");
	printf("  Defines source metadata file\n");

	printf("--dstMetadata=<filename>\n");
	printf("  Defines target metadata file\n");

	printf("--srcBinFile=<filename>\n");
	printf("  Source binary file to convert\n");

	printf("--dstBinFile=<filename>\n");
	printf("  Outputs target binary file\n");

	printf("--generateExampleFile=<filename>\n");
	printf("  Generates an example file\n");

	printf("--dumpBinaryMetaData=<filename>\n");
	printf("  Dump binary meta data for current runtime platform\n");

	printf("--verbose\n");
	printf("  Enables verbose mode\n");
}

static bool parseCommandLine(CmdLineParameters& result, int argc, const char *const*argv)
{
	if( argc <= 1 )
    {
        printHelpMsg();
        return false;
    }
	
#define GET_PARAMETER(v, s)                                             \
    {                                                                   \
	   v = argv[i] + strlen(s);										    \
	   if( v == NULL )													\
	   {																\
	    printf("[ERROR] \"%s\" should have extra parameter\n", argv[i]);\
	    printHelpMsg();													\
	    return false;													\
	  }																	\
	}

	for(int i = 0; i < argc; ++i)
	{
		if(argv[i][0] != '-' || argv[i][1] != '-')
		{
			if( i > 0 )
			{
				printf( "[ERROR] Unknown command line parameter \"%s\"\n", argv[i] );
				printHelpMsg();
				return false;
			}

			continue;
		}
		
		if(match(argv[i], "--verbose"))
		{
			result.verbose = true;
		}
		else if(match(argv[i], "--srcMetadata="))
		{
			GET_PARAMETER(result.srcMetadata, "--srcMetadata=");
		}
		else if(match(argv[i], "--dstMetadata="))
		{
			GET_PARAMETER(result.dstMetadata, "--dstMetadata=");
		}
		else if(match(argv[i], "--srcBinFile="))
		{
			GET_PARAMETER(result.srcBinFile, "--srcBinFile=");
		}
		else if(match(argv[i], "--dstBinFile="))
		{
			GET_PARAMETER(result.dstBinFile, "--dstBinFile=");
		}
		else if(match(argv[i], "--generateExampleFile="))
		{
			GET_PARAMETER(result.exampleFile, "--generateExampleFile=");
			break;
		}
		else if (match(argv[i], "--dumpBinaryMetaData="))
		{
			GET_PARAMETER(result.dumpMetaDataFile, "--dumpBinaryMetaData=");
			break;
		}
		else
		{
			printf( "[ERROR] Unknown command line parameter \"%s\"\n", argv[i] );
			printHelpMsg();
			return false;
		}
	}
	
	if( result.exampleFile || result.dumpMetaDataFile)
		return true;

	if( !result.srcMetadata || !result.dstMetadata || !result.srcBinFile || !result.dstBinFile)
	{
		printf("[ERROR] Missed args!! \n");      
		printHelpMsg();													
	    return false;													
	}
	return true;
}

static bool generateExampleFile(const char* filename)
{
	PxCollection* collection = PxCreateCollection();
	PX_ASSERT( collection );
	
	PxMaterial *material = gPhysics->createMaterial(0.5f, 0.5f, 0.6f);
	PX_ASSERT( material );
	PxShape* shape = gPhysics->createShape(PxBoxGeometry(2.f, 2.f, 2.f), *material);
	PxRigidStatic* theStatic = PxCreateStatic(*gPhysics, PxTransform(PxIdentity), *shape);
	
	collection->add(*material);
	collection->add(*shape);
	collection->add(*theStatic);

	PxDefaultFileOutputStream s(filename);
	bool bret = PxSerialization::serializeCollectionToBinary(s, *collection, *gSerializationRegistry);	
	collection->release();
	return bret;
}

static bool dumpBinaryMetaData(const char* filename)
{
	PxDefaultFileOutputStream s(filename);
	PxSerialization::dumpBinaryMetaData(s, *gSerializationRegistry);
	return true;
}

static void initPhysics()
{
	gFoundation = PxCreateFoundation(PX_FOUNDATION_VERSION, gAllocator, gErrorCallback);
	
	gPhysics = PxCreatePhysics(PX_PHYSICS_VERSION, *gFoundation, PxTolerancesScale(),true);
	
	gSerializationRegistry = PxSerialization::createSerializationRegistry(*gPhysics);

	PxInitVehicleSDK(*gPhysics, gSerializationRegistry);
}

static void cleanupPhysics()
{
	PxCloseVehicleSDK(gSerializationRegistry);

	gSerializationRegistry->release();
	
	gPhysics->release();
	gFoundation->release();

	printf("SnippetConvert done.\n");
}

int snippetMain(int argc, const char *const*argv)
{
	CmdLineParameters result;
	if(!parseCommandLine(result, argc, argv))
		return 1;

	bool bret = false;
	initPhysics();
	if(result.exampleFile || result.dumpMetaDataFile)
	{
		if (result.exampleFile)
		{
			bret = generateExampleFile(result.exampleFile);
		}
		if (result.dumpMetaDataFile)
		{
			bret &= dumpBinaryMetaData(result.dumpMetaDataFile);
		}
	}
	else
	{
		PxBinaryConverter* binaryConverter = PxSerialization::createBinaryConverter();
		if(result.verbose)
			binaryConverter->setReportMode(PxConverterReportMode::eVERBOSE);
		else
			binaryConverter->setReportMode(PxConverterReportMode::eNORMAL);

		
		PxDefaultFileInputData srcMetaDataStream(result.srcMetadata);
		PxDefaultFileInputData dstMetaDataStream(result.dstMetadata);
		bret = binaryConverter->setMetaData(srcMetaDataStream, dstMetaDataStream);
		if(!bret)
		{
			printf("setMetaData failed\n");
		}
		else
		{
			PxDefaultFileInputData  srcBinaryDataStream(result.srcBinFile);
			PxDefaultFileOutputStream dstBinaryDataStream(result.dstBinFile);
			binaryConverter->convert(srcBinaryDataStream, srcBinaryDataStream.getLength(), dstBinaryDataStream);
		}

		binaryConverter->release();
	}

	cleanupPhysics();
	
	return !bret;
}