aboutsummaryrefslogtreecommitdiff
path: root/NvCloth/samples/SampleBase/scene/Scene.cpp
diff options
context:
space:
mode:
authormtamis <[email protected]>2017-02-28 18:24:59 +0100
committermtamis <[email protected]>2017-02-28 18:24:59 +0100
commit5581909a4d19db97304449f66404ff99a0429d3f (patch)
treea90f7eb85c095a8aba45cf5e909c82c1cdbed77d /NvCloth/samples/SampleBase/scene/Scene.cpp
parentFix cmake visual studio project generation (locate_gw_root.bat) (diff)
downloadnvcloth-5581909a4d19db97304449f66404ff99a0429d3f.tar.xz
nvcloth-5581909a4d19db97304449f66404ff99a0429d3f.zip
Add visual samples.
Diffstat (limited to 'NvCloth/samples/SampleBase/scene/Scene.cpp')
-rw-r--r--NvCloth/samples/SampleBase/scene/Scene.cpp162
1 files changed, 162 insertions, 0 deletions
diff --git a/NvCloth/samples/SampleBase/scene/Scene.cpp b/NvCloth/samples/SampleBase/scene/Scene.cpp
new file mode 100644
index 0000000..130dad3
--- /dev/null
+++ b/NvCloth/samples/SampleBase/scene/Scene.cpp
@@ -0,0 +1,162 @@
+/*
+* 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 "Scene.h"
+#include <NvCloth/Cloth.h>
+#include <NvCloth/Solver.h>
+#include <NvCloth/Fabric.h>
+#include "Renderer.h"
+
+std::vector<SceneFactory> Scene::sSceneFactories;
+
+Scene::~Scene()
+{
+
+}
+
+namespace
+{
+template <typename T> void trackT(std::vector<T>& list, T object)
+{
+ list.push_back(object);
+}
+
+template <typename T> void untrackT(std::vector<T>& list, T object)
+{
+ for(auto it = list.begin(); it != list.end(); ++it)
+ {
+ if(*it == object)
+ {
+ list.erase(it);
+ break;
+ }
+ }
+}
+}
+void Scene::trackClothActor(ClothActor* clothActor)
+{
+ trackT(mClothList, clothActor);
+}
+void Scene::untrackClothActor(ClothActor* clothActor)
+{
+ untrackT(mClothList, clothActor);
+}
+void Scene::trackSolver(nv::cloth::Solver* solver)
+{
+ trackT(mSolverList, solver);
+ mSolverHelpers[solver].Initialize(solver, mSceneController->getJobManager());
+}
+void Scene::untrackSolver(nv::cloth::Solver* solver)
+{
+ untrackT(mSolverList, solver);
+ mSolverHelpers.erase(solver);
+}
+void Scene::trackFabric(nv::cloth::Fabric* fabric)
+{
+ trackT(mFabricList, fabric);
+}
+void Scene::untrackFabric(nv::cloth::Fabric* fabric)
+{
+ untrackT(mFabricList, fabric);
+}
+
+void Scene::addClothToSolver(ClothActor* clothActor, nv::cloth::Solver* solver)
+{
+ solver->addCloth(clothActor->mCloth);
+ assert(mClothSolverMap.find(clothActor) == mClothSolverMap.end());
+ mClothSolverMap[clothActor] = solver;
+}
+
+void Scene::trackRenderable(Renderable* renderable)
+{
+ trackT(mRenderableList, renderable);
+}
+void Scene::untrackRenderable(Renderable* renderable)
+{
+ untrackT(mRenderableList, renderable);
+}
+
+void Scene::autoDeinitialize()
+{
+ //Remove all cloths from solvers
+ for (auto it : mClothSolverMap)
+ {
+ it.second->removeCloth(it.first->mCloth);
+ }
+ mClothSolverMap.clear();
+
+ //Destroy all solvers
+ for (auto it : mSolverList)
+ {
+ delete it;
+ }
+ mSolverList.clear();
+ mSolverHelpers.clear();
+
+ //Destroy all cloths
+ for (auto it : mClothList)
+ {
+ delete it->mCloth;
+ mSceneController->getRenderer().removeRenderable(it->mClothRenderable);
+ delete it->mClothRenderMesh;
+ delete it;
+ }
+ mClothList.clear();
+
+ //Destroy all fabrics
+ for (auto it : mFabricList)
+ {
+ it->decRefCount();
+ }
+ mFabricList.clear();
+
+ //Destroy all renderables
+ for(auto it : mRenderableList)
+ {
+ mSceneController->getRenderer().removeRenderable(it);
+ }
+ mRenderableList.clear();
+}
+
+void Scene::doSimulationStep(float dt)
+{
+ startSimulationStep(dt);
+ waitForSimulationStep();
+ updateSimulationGraphics();
+}
+
+void Scene::startSimulationStep(float dt)
+{
+ for(auto it = mSolverHelpers.begin(); it != mSolverHelpers.end(); ++it)
+ {
+ it->second.StartSimulation(dt);
+ }
+}
+void Scene::waitForSimulationStep()
+{
+ for(auto it = mSolverHelpers.begin(); it != mSolverHelpers.end(); ++it)
+ {
+ it->second.WaitForSimulation();
+ }
+}
+
+void Scene::updateSimulationGraphics()
+{
+ for each (auto actor in mClothList)
+ {
+ nv::cloth::MappedRange<physx::PxVec4> particles = actor->mCloth->getCurrentParticles();
+ std::vector<PxVec3> particles3(particles.size());
+ for(uint32_t i = 0; i < particles.size(); ++i)
+ particles3[i] = particles[i].getXYZ();
+
+ actor->mClothRenderMesh->update(particles3.data(), particles.size());
+ }
+}
+