aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/snippets/SnippetCreateActor/SnippetCreateActor.cpp
blob: 949597ec001190635f8b928f6ca4eb8990acb27e (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
/*
 * Copyright (c) 2008-2017, NVIDIA CORPORATION.  All rights reserved.
 *
 * NVIDIA CORPORATION and its licensors retain all intellectual property
 * and proprietary rights in and to this software, 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.
 */

#include "../SnippetCommon/SnippetCommon.h"

#include "destructible/DestructibleAsset.h"
#include "destructible/DestructibleActor.h"
#include "nvparameterized/NvParameterized.h"
#include "nvparameterized/NvParamUtils.h"

#include "PsString.h"

#include <sys/stat.h>

using namespace nvidia::apex;

class MyResourceCallback;

ApexSDK*					gApexSDK = NULL;
DummyRenderResourceManager* gDummyRenderResourceManager = NULL;
MyResourceCallback*			gMyResourceCallback = NULL;

ModuleDestructible*			gModuleDestructible = NULL;

Scene*						gApexScene = NULL;

class MyResourceCallback : public ResourceCallback
{
public:
	virtual void* requestResource(const char* nameSpace, const char* name)
	{
		Asset* asset = 0;

		if (!physx::shdfnd::strcmp(nameSpace, DESTRUCTIBLE_AUTHORING_TYPE_NAME))
		{
			const char* path = name;

			// does file exists?
			struct stat info;
			if ((stat(path, &info) != -1) && (info.st_mode & (S_IFREG)) != 0)
			{
				PxFileBuf* stream = gApexSDK->createStream(path, PxFileBuf::OPEN_READ_ONLY);

				if (stream)
				{
					NvParameterized::Serializer::SerializeType serType = gApexSDK->getSerializeType(*stream);
					NvParameterized::Serializer::ErrorType serError;
					NvParameterized::Serializer*  ser = gApexSDK->createSerializer(serType);
					PX_ASSERT(ser);

					NvParameterized::Serializer::DeserializedData data;
					serError = ser->deserialize(*stream, data);

					if (serError == NvParameterized::Serializer::ERROR_NONE && data.size() == 1)
					{
						NvParameterized::Interface* params = data[0];
						asset = gApexSDK->createAsset(params, name);
						PX_ASSERT(asset && "ERROR Creating NvParameterized Asset");
					}
					else
					{
						PX_ASSERT(0 && "ERROR Deserializing NvParameterized Asset");
					}

					stream->release();
					ser->release();
				}
			}
		}

		PX_ASSERT(asset);
		return asset;
	}

	virtual void  releaseResource(const char*, const char*, void* resource)
	{
		Asset* asset = (Asset*)resource;
		gApexSDK->releaseAsset(*asset);
	}
};


void initApex()
{
	// Fill out the Apex SDKriptor
	ApexSDKDesc apexDesc;

	// Apex needs an allocator and error stream.  By default it uses those of the PhysX SDK.

	// Let Apex know about our PhysX SDK and cooking library
	apexDesc.physXSDK = gPhysics;
	apexDesc.cooking = gCooking;
	apexDesc.pvd = gPvd;

	// Our custom dummy render resource manager
	gDummyRenderResourceManager = new DummyRenderResourceManager();
	apexDesc.renderResourceManager = gDummyRenderResourceManager;

	// Our custom named resource handler
	gMyResourceCallback = new MyResourceCallback();
	apexDesc.resourceCallback = gMyResourceCallback;
	apexDesc.foundation = gFoundation;

	// Finally, create the Apex SDK
	ApexCreateError error;
	gApexSDK = CreateApexSDK(apexDesc, &error);
	PX_ASSERT(gApexSDK);

	// Initialize destructible module
	gModuleDestructible = static_cast<ModuleDestructible*>(gApexSDK->createModule("Destructible"));
	NvParameterized::Interface* params = gModuleDestructible->getDefaultModuleDesc();
	gModuleDestructible->init(*params);

	// Create Apex scene
	SceneDesc sceneDesc;
	sceneDesc.scene = gPhysicsScene;
	gApexScene = gApexSDK->createScene(sceneDesc);
}

Asset* loadApexAsset(const char* path)
{
	Asset* asset = static_cast<Asset*>(gApexSDK->getNamedResourceProvider()->getResource(DESTRUCTIBLE_AUTHORING_TYPE_NAME, path));
	return asset;
}

void releaseAPEX()
{
	gApexSDK->release();
	delete gDummyRenderResourceManager;
	delete gMyResourceCallback;
}

#include "Shlwapi.h"

int main(int, char**)
{
	initPhysX();
	initApex();

	LPTSTR cmd = GetCommandLine();
	PathRemoveFileSpec(cmd);
	strcat(cmd, "/../../snippets/SnippetCommon/cow.apb");

	Asset* asset = loadApexAsset(&cmd[1]);

	DestructibleAsset* destructibleAsset = static_cast<DestructibleAsset*>(asset);

	NvParameterized::Interface* actorDesc = destructibleAsset->getDefaultActorDesc();

	PxTransform t(PxIdentity);
	t.p = PxVec3(0, 5, 0);
	NvParameterized::setParamTransform(*actorDesc, "globalPose", t);
	NvParameterized::setParamBool(*actorDesc, "dynamic", true);

	shdfnd::printFormatted("isValidForActorCreation: %d\n", destructibleAsset->isValidForActorCreation(*actorDesc, *gApexScene));

	DestructibleActor* actor = static_cast<DestructibleActor*>(destructibleAsset->createApexActor(*actorDesc, *gApexScene));

	actor->release();
	asset->release();

	releaseAPEX();
	releasePhysX();

	return 0;
}