aboutsummaryrefslogtreecommitdiff
path: root/NvCloth/samples/SampleBase/scene/SceneController.cpp
blob: 2eedfe4bab42a6ce82d2e7597c3526bb657c23c3 (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
/*
* 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 "SceneController.h"
#include "RenderUtils.h"
#include "Utils.h"

#include "Renderer.h"

#include "CommonUIController.h"

// initialization of NvCloth dll
#include <NvCloth/Callbacks.h>

// example allocator callbacks
#include "CallbackImplementations.h"

// low level cloth
//#include <NvCloth/Factory.h>
#include <NvCloth/Fabric.h>
#include <NvCloth/Cloth.h>
#include <NvCloth/Solver.h>

#include <NvClothExt/ClothFabricCooker.h>
#include "ClothMeshGenerator.h"

#include <algorithm>
#include <imgui.h>
#include <sstream>
#include <tuple>

#include "scene/Scene.h"
#include "scene/scenes/SimpleScene.h"
#include "scene/scenes/WindScene.h"

#include "utils/DebugLineRenderBuffer.h"

JobManager SceneController::sJobManager;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//													Controller
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SceneController::SceneController() : mTimeScale(1.0f), mStartDelay(0.f)
{
	mActivePlatform = (int)nv::cloth::Platform::CPU;
	mCUDAInitialized = false;
	mDXInitialized = false;
	mLeftOverTime = 0.0;
	mPaused = false;
	mSingleStep = 0;
}

SceneController::~SceneController()
{
}

void SceneController::onSampleStart()
{
	// setup camera
	CFirstPersonCamera* camera = &getRenderer().getCamera();
	DirectX::XMVECTORF32 lookAtPt = { 0, 10, 0, 0 };
	DirectX::XMVECTORF32 eyePt = { 0, 15, 25, 0 };
	camera->SetViewParams(eyePt, lookAtPt);
	camera->SetRotateButtons(false, false, true, false);
	camera->SetEnablePositionMovement(true);

	mStartDelay = 3.f;

	mPhysXPrimitiveRenderMaterial = new RenderMaterial(getRenderer().getResourceManager(), "physx_primitive", "");
	mPhysXPlaneRenderMaterial = new RenderMaterial(getRenderer().getResourceManager(), "physx_primitive_plane", "");
	mWeightedModelSkinRenderMaterial = new RenderMaterial(getRenderer().getResourceManager(), "weighted_model_skinned", "");
	

	/*{
		IRenderMesh* mesh = getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Plane);
		mPlane = getRenderer().createRenderable(*mesh, *mPhysXPlaneRenderMaterial);
		mPlane->setTransform(PxTransform(PxVec3(0.f, 0.f, 0.f), PxQuat(PxPiDivTwo, PxVec3(0.f, 0.f, 1.f))));
		mPlane->setScale(PxVec3(1000.f));
	}*/

	for (uint32_t i = 0; i < 0; ++i)
	{
		IRenderMesh* mesh = getRenderer().getPrimitiveRenderMesh(PrimitiveRenderMeshType::Box);
		auto box = getRenderer().createRenderable(*mesh, *mPhysXPrimitiveRenderMaterial);
		box->setColor(getRandomPastelColor());
		box->setScale(PxVec3(0.2f));
		mBoxes.push_back(box);
	}


	///

	mActiveScene = new SimpleScene(this);
	mActiveScene->onInitialize();
	mActiveSceneIndex = 0;
}

void SceneController::onInitialize()
{
	NvClothEnvironment::AllocateEnv();

	mFactories[(int)nv::cloth::Platform::CPU] = NvClothCreateFactoryCPU();
	mFactories[(int)nv::cloth::Platform::CUDA] = nullptr;
	mFactories[(int)nv::cloth::Platform::DX11] = nullptr;
	
	//CUDA
	do
	{
		assert(mCUDAInitialized == false);
		cuInit(0);
		int deviceCount = 0;
		mCUDAInitialized = true;
		mCUDAInitialized = (cuDeviceGetCount(&deviceCount) == CUDA_SUCCESS);
		mCUDAInitialized &= deviceCount >= 1;
		if(!mCUDAInitialized)
			break;
		mCUDAInitialized = cuCtxCreate(&mCUDAContext, 0, 0) == CUDA_SUCCESS;
		if(!mCUDAInitialized)
			break;

		mFactories[(int)nv::cloth::Platform::CUDA] = NvClothCreateFactoryCUDA(mCUDAContext);
	} while(false);

	//DX11
	do
	{
		assert(mDXInitialized == false);
		mDXInitialized = true;
		mDXDevice = GetDeviceManager()->GetDevice();

		mGraphicsContextManager = new DxContextManagerCallbackImpl(mDXDevice);
		mDXInitialized &= mGraphicsContextManager != nullptr;
		if(!mDXInitialized)
			break;

		mFactories[(int)nv::cloth::Platform::DX11] = NvClothCreateFactoryDX11(mGraphicsContextManager);
		mDXInitialized &= mFactories[(int)nv::cloth::Platform::DX11] != nullptr;
	} while(false);

	mDebugLineRenderBuffer = new DebugLineRenderBuffer;
}

void SceneController::onSampleStop()
{
	mActiveScene->onTerminate();
	delete mActiveScene;

	for (auto b : mBoxes)
		SAFE_DELETE(b);

	mBoxes.clear();

	NvClothDestroyFactory(mFactories[(int)nv::cloth::Platform::CPU]);
	NvClothDestroyFactory(mFactories[(int)nv::cloth::Platform::CUDA]);
	NvClothDestroyFactory(mFactories[(int)nv::cloth::Platform::DX11]);

	//SAFE_DELETE(mPlane);
	SAFE_DELETE(mPhysXPlaneRenderMaterial);
	SAFE_DELETE(mPhysXPrimitiveRenderMaterial);
}

void SceneController::onTerminate() 
{
}

void SceneController::changeScene(int index)
{
	mActiveScene->onTerminate();
	delete mActiveScene;

	mActiveSceneIndex = index;

	if(index < Scene::GetSceneCount())
		mActiveScene = Scene::CreateScene(index, this);
	else
	{
		mActiveSceneIndex = 0;
		mActiveScene = Scene::CreateScene(0, this);
	}
	mActiveScene->onInitialize();
	mDebugLineRenderBuffer->clear();
	mActiveScene->drawDebugVisualization();
}

void SceneController::Animate(double dt)
{
	if(mPaused && (mSingleStep <= 0))
	{
		if(mActiveScene->debugVisualizationUpdateRequested())
		{
			mDebugLineRenderBuffer->clear();
			mActiveScene->drawDebugVisualization();
		}
			//else Re render debug lines from last frame
		getRenderer().queueRenderBuffer(mDebugLineRenderBuffer);
		return;
	}
	if(mSingleStep > 0)
	{
		mSingleStep--;
		dt = 1.0 / 60.0;
	}

	mLeftOverTime += dt * mTimeScale;

	double simulationStep = 0.0;
	while(mLeftOverTime > 1.0 / 60.0)
		simulationStep += 1.0 / 60.0, mLeftOverTime -= 1.0 / 60.0;
	if(simulationStep >= 1.0 / 60.0)
		simulationStep = 1.0 / 60.0;

	mActiveScene->Animate(simulationStep);
	mDebugLineRenderBuffer->clear();
	mActiveScene->drawDebugVisualization();

	getRenderer().queueRenderBuffer(mDebugLineRenderBuffer);
}

LRESULT SceneController::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if(mActiveScene->HandleEvent(uMsg, wParam, lParam))
		return 1;

	if (uMsg == WM_KEYDOWN)
	{
		int iKeyPressed = static_cast<int>(wParam);

		if(iKeyPressed >= '1' && iKeyPressed <= '9')
		{
			changeScene(iKeyPressed - '1');
		}

		switch (iKeyPressed)
		{
		case 'P':
			mPaused = !mPaused;
			break;
		case 'O':
			mSingleStep++;
			break;
		default:
			break; 
		}
	}

	return 1;
}

void SceneController::drawUI()
{
	///////////////////////////////////////////////////////////////////////////////////////////
	// Cube
	///////////////////////////////////////////////////////////////////////////////////////////
	{
		ImGui::Text("Time Scale");
		ImGui::DragFloat("Scale", &mTimeScale, 0.1f, 0.0f, 100.0f);
	}

	bool pressed = false;

	pressed = pressed | ImGui::RadioButton("CPU", &mActivePlatform, (int)nv::cloth::Platform::CPU); ImGui::SameLine();
	pressed = pressed | ImGui::RadioButton("DX11", &mActivePlatform, (int)nv::cloth::Platform::DX11); ImGui::SameLine();
	pressed = pressed | ImGui::RadioButton("CUDA", &mActivePlatform, (int)nv::cloth::Platform::CUDA);

	if(!getFactory())
		mActivePlatform = (int)nv::cloth::Platform::CPU;

	for(int i = 0; i < Scene::GetSceneCount(); i++)
	{
		pressed = pressed | ImGui::RadioButton(Scene::GetSceneName(i), &mActiveSceneIndex, i);
		if(i == mActiveSceneIndex)
		{
			pressed = pressed | mActiveScene->drawSubScenes();
		}
	}
	if(pressed)
		changeScene(mActiveSceneIndex);

	mActiveScene->drawUI();
}

void SceneController::drawStatsUI()
{
	mActiveScene->drawStatsUI();
}