aboutsummaryrefslogtreecommitdiff
path: root/APEX_1.4/module/basicfs/src/VortexFSActorImpl.cpp
blob: 045ccd02d327b210091e54b289f0a231f3c74378 (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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * Copyright (c) 2008-2015, 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 "ApexDefs.h"
#include "RenderMeshActorDesc.h"
#include "RenderMeshActor.h"
#include "RenderMeshAsset.h"

#include "Apex.h"

#include "VortexFSActorImpl.h"
#include "VortexFSAsset.h"
#include "BasicFSScene.h"
#include "ApexSDKIntl.h"
#include "SceneIntl.h"
#include "RenderDebugInterface.h"

#include <PxScene.h>

#include "PsMathUtils.h"

#include <FieldSamplerManagerIntl.h>
#include "ApexResourceHelper.h"

namespace nvidia
{
namespace basicfs
{

#define NUM_DEBUG_POINTS 512

VortexFSActorImpl::VortexFSActorImpl(const VortexFSActorParams& params, VortexFSAsset& asset, ResourceList& list, BasicFSScene& scene)
	: BasicFSActor(scene)
	, mAsset(&asset)
{
	mFieldWeight = asset.mParams->fieldWeight;

	mPose					= params.initialPose;
	mScale					= params.initialScale;
	mAxis					= mAsset->mParams->axis;
	mBottomSphericalForce   = mAsset->mParams->bottomSphericalForce;
	mTopSphericalForce      = mAsset->mParams->topSphericalForce;
	mHeight					= mAsset->mParams->height;
	mBottomRadius			= mAsset->mParams->bottomRadius;
	mTopRadius				= mAsset->mParams->topRadius;
	mRotationalStrength		= mAsset->mParams->rotationalStrength;
	mRadialStrength			= mAsset->mParams->radialStrength;
	mLiftStrength			= mAsset->mParams->liftStrength;

	list.add(*this);			// Add self to asset's list of actors
	addSelfToContext(*scene.getApexScene().getApexContext());    // Add self to ApexScene
	addSelfToContext(scene);	// Add self to BasicFSScene's list of actors

	FieldSamplerManagerIntl* fieldSamplerManager = mScene->getInternalFieldSamplerManager();
	if (fieldSamplerManager != 0)
	{
		FieldSamplerDescIntl fieldSamplerDesc;
		if (asset.mParams->fieldDragCoeff > 0)
		{
			fieldSamplerDesc.type = FieldSamplerTypeIntl::VELOCITY_DRAG;
			fieldSamplerDesc.dragCoeff = asset.mParams->fieldDragCoeff;
		}
		else
		{
			fieldSamplerDesc.type = FieldSamplerTypeIntl::VELOCITY_DIRECT;
		}
		fieldSamplerDesc.gridSupportType = FieldSamplerGridSupportTypeIntl::VELOCITY_PER_CELL;
		fieldSamplerDesc.samplerFilterData = ApexResourceHelper::resolveCollisionGroup128(params.fieldSamplerFilterDataName ? params.fieldSamplerFilterDataName : mAsset->mParams->fieldSamplerFilterDataName);
		fieldSamplerDesc.boundaryFilterData = ApexResourceHelper::resolveCollisionGroup128(params.fieldBoundaryFilterDataName ? params.fieldBoundaryFilterDataName : mAsset->mParams->fieldBoundaryFilterDataName);
		fieldSamplerDesc.boundaryFadePercentage = mAsset->mParams->boundaryFadePercentage;

		fieldSamplerManager->registerFieldSampler(this, fieldSamplerDesc, mScene);
		mFieldSamplerChanged = true;
	}
	mDebugShapeChanged = true;
}

VortexFSActorImpl::~VortexFSActorImpl()
{
}

/* Must be defined inside CPP file, since they require knowledge of asset class */
Asset* 		VortexFSActorImpl::getOwner() const
{
	return static_cast<Asset*>(mAsset);
}

BasicFSAsset* 	VortexFSActorImpl::getVortexFSAsset() const
{
	READ_ZONE();
	return mAsset;
}

void				VortexFSActorImpl::release()
{
	if (mInRelease)
	{
		return;
	}
	destroy();
} 

void VortexFSActorImpl::destroy()
{
	{
		WRITE_ZONE();
		ApexActor::destroy();

		setPhysXScene(NULL);

		FieldSamplerManagerIntl* fieldSamplerManager = mScene->getInternalFieldSamplerManager();
		if (fieldSamplerManager != 0)
		{
			fieldSamplerManager->unregisterFieldSampler(this);
		}
	}
	delete this;
}

void VortexFSActorImpl::getLodRange(float& min, float& max, bool& intOnly) const
{
	PX_UNUSED(min);
	PX_UNUSED(max);
	PX_UNUSED(intOnly);
	APEX_INVALID_OPERATION("not implemented");
}

float VortexFSActorImpl::getActiveLod() const
{
	APEX_INVALID_OPERATION("NxExampleActor does not support this operation");
	return -1.0f;
}

void VortexFSActorImpl::forceLod(float lod)
{
	PX_UNUSED(lod);
	APEX_INVALID_OPERATION("not implemented");
}

// Called by game render thread
void VortexFSActorImpl::updateRenderResources(bool rewriteBuffers, void* userRenderData)
{
	PX_UNUSED(rewriteBuffers);
	PX_UNUSED(userRenderData);
}

// Called by game render thread
void VortexFSActorImpl::dispatchRenderResources(UserRenderer& renderer)
{
	PX_UNUSED(renderer);
}

bool VortexFSActorImpl::updateFieldSampler(FieldShapeDescIntl& shapeDesc, bool& isEnabled)
{
	isEnabled = mFieldSamplerEnabled;
	if (mFieldSamplerChanged)
	{
		mExecuteParams.bottomSphericalForce		= mBottomSphericalForce;
		mExecuteParams.topSphericalForce		= mTopSphericalForce;

		mExecuteParams.bottomRadius				= mScale * mBottomRadius;
		mExecuteParams.topRadius				= mScale * mTopRadius;
		mExecuteParams.height					= mScale * mHeight;
		mExecuteParams.rotationalStrength		= mScale * mRotationalStrength;
		mExecuteParams.radialStrength			= mScale * mRadialStrength;
		mExecuteParams.liftStrength				= mScale * mLiftStrength;

		const PxVec3 mUnit(0,1,0);
		const PxVec3 vecN = mPose.q.rotate(mAxis);

		const PxVec3 a = mUnit.cross(vecN);
		const PxQuat axisRot(a.x, a.y, a.z, sqrtf(mUnit.magnitudeSquared() * vecN.magnitudeSquared()) + mUnit.dot(vecN));

		mDirToWorld.q = axisRot.getNormalized();
		mDirToWorld.p = mPose.p;
		
		mExecuteParams.worldToDir = mDirToWorld.getInverse();

		shapeDesc.type = FieldShapeTypeIntl::CAPSULE;
		shapeDesc.dimensions = PxVec3(PxMax(mExecuteParams.bottomRadius, mExecuteParams.topRadius), mExecuteParams.height, 0);
		shapeDesc.worldToShape = mExecuteParams.worldToDir;
		shapeDesc.weight = mFieldWeight;

		mFieldSamplerChanged = false;
		return true;
	}
	return false;
}

void VortexFSActorImpl::simulate(float dt)
{
	PX_UNUSED(dt);
}

void VortexFSActorImpl::setRotationalStrength(float strength)
{
	WRITE_ZONE();
	mRotationalStrength = strength;
	mFieldSamplerChanged = true;
}

void VortexFSActorImpl::setRadialStrength(float strength)
{
	WRITE_ZONE();
	mRadialStrength = strength;
	mFieldSamplerChanged = true;
}

void VortexFSActorImpl::setLiftStrength(float strength)
{
	WRITE_ZONE();
	mLiftStrength = strength;
	mFieldSamplerChanged = true;
}

void VortexFSActorImpl::visualize()
{
#ifndef WITHOUT_DEBUG_VISUALIZE
	if ( !mEnableDebugVisualization ) return;
	RenderDebugInterface* debugRender = mScene->mDebugRender;
	BasicFSDebugRenderParams* debugRenderParams = mScene->mBasicFSDebugRenderParams;

	using RENDER_DEBUG::DebugColors;

	if (!debugRenderParams->VISUALIZE_VORTEX_FS_ACTOR)
	{
		return;
	}

	const physx::PxMat44& savedPose = *RENDER_DEBUG_IFACE(debugRender)->getPoseTyped();
	RENDER_DEBUG_IFACE(debugRender)->setIdentityPose();

	if (debugRenderParams->VISUALIZE_VORTEX_FS_ACTOR_NAME)
	{
		char buf[128];
		buf[sizeof(buf) - 1] = 0;
		APEX_SPRINTF_S(buf, sizeof(buf) - 1, " %s %s", mAsset->getObjTypeName(), mAsset->getName());

		PxVec3 textLocation = mPose.p;

		RENDER_DEBUG_IFACE(debugRender)->setCurrentTextScale(4.0f);
		RENDER_DEBUG_IFACE(debugRender)->setCurrentColor(RENDER_DEBUG_IFACE(debugRender)->getDebugColor(DebugColors::Blue));
		RENDER_DEBUG_IFACE(debugRender)->debugText(textLocation, buf);
	}

	if (debugRenderParams->VISUALIZE_VORTEX_FS_SHAPE)
	{
		RENDER_DEBUG_IFACE(debugRender)->setCurrentColor(RENDER_DEBUG_IFACE(debugRender)->getDebugColor(DebugColors::Blue));
		RENDER_DEBUG_IFACE(debugRender)->setPose(mDirToWorld);
		RENDER_DEBUG_IFACE(debugRender)->debugCapsuleTapered(mExecuteParams.topRadius, mExecuteParams.bottomRadius, mExecuteParams.height, 2);
		RENDER_DEBUG_IFACE(debugRender)->setPose(physx::PxMat44(physx::PxIdentity));
	}

	if (debugRenderParams->VISUALIZE_VORTEX_FS_POSE)
	{
		RENDER_DEBUG_IFACE(debugRender)->debugAxes(PxMat44(mPose), 1);
	}

	if (debugRenderParams->VISUALIZE_VORTEX_FS_FIELD)
	{
		if (mDebugShapeChanged || mDebugPoints.empty())
		{
			mDebugShapeChanged = false;
			mDebugPoints.resize(NUM_DEBUG_POINTS);
			for (uint32_t i = 0; i < NUM_DEBUG_POINTS; ++i)
			{
				float r1 = mBottomRadius;
				float r2 = mTopRadius;
				float h = mHeight;
				float maxR = PxMax(r1, r2);
				float rx, ry, rz;
				bool isInside = false;
				do
				{
					rx = physx::shdfnd::rand(-maxR, maxR);
					ry = physx::shdfnd::rand(-h/2 - r1, h/2 + r2);
					rz = physx::shdfnd::rand(-maxR, maxR);
					isInside = 2*ry <= h && -h <= 2*ry &&
						rx*rx + rz*rz <= physx::shdfnd::sqr(r1 + (ry / h + 0.5) * (r2-r1));
					isInside |= 2*ry < -h && rx*rx + rz*rz <= r1*r1 - (2*ry+h)*(2*ry+h)*0.25;
					isInside |= 2*ry > h && rx*rx + rz*rz <= r2*r2 - (2*ry-h)*(2*ry-h)*0.25;
				}
				while (!isInside);

				PxVec3& vec = mDebugPoints[i];

				// we need transform from local to world
				vec.x = rx;
				vec.y = ry;
				vec.z = rz;
			}
		}

		using RENDER_DEBUG::DebugColors;

		uint32_t c1 = RENDER_DEBUG_IFACE(mScene->mDebugRender)->getDebugColor(DebugColors::Blue);
		uint32_t c2 = RENDER_DEBUG_IFACE(mScene->mDebugRender)->getDebugColor(DebugColors::Red);

		for (uint32_t i = 0; i < NUM_DEBUG_POINTS; ++i)
		{
			PxVec3 localPos = mScale * mDebugPoints[i];
			PxVec3 pos = mDirToWorld.transform(localPos);
			PxVec3 fieldVec = executeVortexFS(mExecuteParams, pos/*, totalElapsedMS*/);
			RENDER_DEBUG_IFACE(debugRender)->debugGradientLine(pos, pos + fieldVec, c1, c2);
		}
	}
	RENDER_DEBUG_IFACE(debugRender)->setPose(savedPose);
#endif
}

/******************************** CPU Version ********************************/

VortexFSActorCPU::VortexFSActorCPU(const VortexFSActorParams& params, VortexFSAsset& asset, ResourceList& list, BasicFSScene& scene)
	: VortexFSActorImpl(params, asset, list, scene)
{
}

VortexFSActorCPU::~VortexFSActorCPU()
{
}

void VortexFSActorCPU::executeFieldSampler(const ExecuteData& data)
{
	for (uint32_t iter = 0; iter < data.count; ++iter)
	{
		uint32_t i = data.indices[iter & data.indicesMask] + (iter & ~data.indicesMask);
		PxVec3* pos = (PxVec3*)((uint8_t*)data.position + i * data.positionStride);
		data.resultField[iter] = executeVortexFS(mExecuteParams, *pos/*, totalElapsedMS*/);
	}
}

/******************************** GPU Version ********************************/

#if APEX_CUDA_SUPPORT


VortexFSActorGPU::VortexFSActorGPU(const VortexFSActorParams& params, VortexFSAsset& asset, ResourceList& list, BasicFSScene& scene)
	: VortexFSActorCPU(params, asset, list, scene)
	, mConstMemGroup(CUDA_OBJ(fieldSamplerStorage))
{
}

VortexFSActorGPU::~VortexFSActorGPU()
{
}

bool VortexFSActorGPU::updateFieldSampler(FieldShapeDescIntl& shapeDesc, bool& isEnabled)
{
	if (VortexFSActorImpl::updateFieldSampler(shapeDesc, isEnabled))
	{
		APEX_CUDA_CONST_MEM_GROUP_SCOPE(mConstMemGroup);

		if (mParamsHandle.isNull())
		{
			mParamsHandle.alloc(_storage_);
		}
		mParamsHandle.update(_storage_, mExecuteParams);
		return true;
	}
	return false;
}


#endif

}
} // end namespace nvidia::apex