diff options
| author | git perforce import user <a@b> | 2016-10-25 12:29:14 -0600 |
|---|---|---|
| committer | Sheikh Dawood Abdul Ajees <Sheikh Dawood Abdul Ajees> | 2016-10-25 18:56:37 -0500 |
| commit | 3dfe2108cfab31ba3ee5527e217d0d8e99a51162 (patch) | |
| tree | fa6485c169e50d7415a651bf838f5bcd0fd3bfbd /APEX_1.4/samples_v2/SampleParticlesHelloWorld | |
| download | physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.tar.xz physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.zip | |
Initial commit:
PhysX 3.4.0 Update @ 21294896
APEX 1.4.0 Update @ 21275617
[CL 21300167]
Diffstat (limited to 'APEX_1.4/samples_v2/SampleParticlesHelloWorld')
5 files changed, 496 insertions, 0 deletions
diff --git a/APEX_1.4/samples_v2/SampleParticlesHelloWorld/Main.cpp b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/Main.cpp new file mode 100644 index 00000000..2948e391 --- /dev/null +++ b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/Main.cpp @@ -0,0 +1,51 @@ +#include "Utils.h" + +#include <DirectXMath.h> +#include "XInput.h" +#include "DXUTMisc.h" +#include "DXUTCamera.h" + + +#include "ApexController.h" +#include "ApexRenderer.h" +#include "CommonUIController.h" +#include "SampleUIController.h" +#include "SampleSceneController.h" + +#include "SampleManager.h" + +int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) +{ + PX_UNUSED(hInstance); + PX_UNUSED(hPrevInstance); + PX_UNUSED(lpCmdLine); + PX_UNUSED(nCmdShow); + +// Enable run-time memory check for debug builds. +#if defined(DEBUG) | defined(_DEBUG) + _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); + AllocConsole(); +#endif + + SampleManager* sampleManager = new SampleManager(L"APEX Particles Sample: Hello World"); + + CFirstPersonCamera camera; + + auto apexController = ApexController(PxDefaultSimulationFilterShader, &camera); + auto apexRender = ApexRenderer(&camera, apexController); + auto sceneController = SampleSceneController(&camera, apexController); + auto commonUiController = CommonUIController(&camera, &apexRender, &apexController); + auto sampleUIController = SampleUIController(&sceneController, &commonUiController); + + sampleManager->addControllerToFront(&apexController); + sampleManager->addControllerToFront(&apexRender); + sampleManager->addControllerToFront(&sceneController); + sampleManager->addControllerToFront(&sampleUIController); + sampleManager->addControllerToFront(&commonUiController); + + int result = sampleManager->run(); + + delete sampleManager; + + return result; +} diff --git a/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleSceneController.cpp b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleSceneController.cpp new file mode 100644 index 00000000..306badce --- /dev/null +++ b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleSceneController.cpp @@ -0,0 +1,221 @@ +/* +* 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 "SampleSceneController.h" +#include "ApexRenderer.h" // for matrix conversion +#include <DirectXMath.h> +#include "XInput.h" +#include "DXUTMisc.h" +#pragma warning(push) +#pragma warning(disable : 4481) // Suppress "nonstandard extension used" warning +#include "DXUTCamera.h" +#pragma warning(pop) + +#include "PxPhysicsAPI.h" +#include "PxMath.h" + +#include "ApexResourceCallback.h" +#include "PhysXPrimitive.h" + +using namespace physx; + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// Scenes Setup +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +SampleSceneController::AssetDescription SampleSceneController::ASSETS[] = +{ + { "defaultEffectPackage", "defaultEffectPackage" }, + { "hotlava", "hotlava" }, + { "pulseblast", "pulseblast" }, + { "bigtesteffect", "bigtesteffect" }, + { "ThreeEmitterScaleTest", "ThreeEmitterScaleTest" }, + { "smoketest", "smoketest" } +}; + +int SampleSceneController::getAssetsCount() +{ + return sizeof(ASSETS) / sizeof(ASSETS[0]); +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +SampleSceneController::SampleSceneController(CFirstPersonCamera* camera, ApexController& apex) + : mApex(apex), mCamera(camera), mDraggingActor(NULL) +{ +} + +SampleSceneController::~SampleSceneController() +{ +} + +void SampleSceneController::onSampleStart() +{ + // setup camera + DirectX::XMVECTORF32 lookAtPt = { 0, 2, 0, 0 }; + DirectX::XMVECTORF32 eyePt = { 0, 5, 10, 0 }; + mCamera->SetViewParams(eyePt, lookAtPt); + mCamera->SetRotateButtons(false, false, true, false); + mCamera->SetEnablePositionMovement(true); + + // load effect package DB + loadEffectPackageDatabase(); + + // spawn actor + setCurrentAsset(0); +} + +void SampleSceneController::loadEffectPackageDatabase() +{ + NvParameterized::Interface *iface; + + ApexResourceCallback* resourceCallback = mApex.getResourceCallback(); + ModuleParticles* moduleParticles = mApex.getModuleParticles(); + + // Load the EffectPackage database + iface = resourceCallback->deserializeFromFile("EffectPackageEffectPackagesDatabase.apb"); + PX_ASSERT(iface); + if (iface) + { + moduleParticles->setEffectPackageDatabase(iface); + iface->destroy(); + } + + // Load the Emitter database + iface = resourceCallback->deserializeFromFile("EffectPackageEmittersDatabase.apb"); + PX_ASSERT(iface); + if (iface) + { + moduleParticles->setEffectPackageEmitterDatabase(iface); + iface->destroy(); + } + + // Load the FieldSampler database + iface = resourceCallback->deserializeFromFile("EffectPackageFieldSamplersDatabase.apb"); + PX_ASSERT(iface); + if (iface) + { + moduleParticles->setEffectPackageFieldSamplerDatabase(iface); + iface->destroy(); + } + // Load the GraphicsEffects database (IOFX) + iface = resourceCallback->deserializeFromFile("EffectPackageGraphicsEffectsDatabase.apb"); + PX_ASSERT(iface); + if (iface) + { + moduleParticles->setEffectPackageIOFXDatabase(iface); + iface->destroy(); + } + + // Load the GraphicsMaterials database + iface = resourceCallback->deserializeFromFile("EffectPackageGraphicsMaterialsDatabase.apb"); + PX_ASSERT(iface); + if (iface) + { + moduleParticles->setEffectPackageGraphicsMaterialsDatabase(iface); + iface->destroy(); + } + + // Load the Particle Simulations (IOS) database + iface = resourceCallback->deserializeFromFile("EffectPackageParticleSimulationsDatabase.apb"); + PX_ASSERT(iface); + if (iface) + { + moduleParticles->setEffectPackageIOSDatabase(iface); + iface->destroy(); + } +} + +void SampleSceneController::setCurrentAsset(int num) +{ + int assetsCount = getAssetsCount(); + num = nvidia::PxClamp(num, 0, assetsCount - 1); + + mCurrentAsset = num; + if (mActor != NULL) + { + mApex.removeActor(mActor); + mActor = NULL; + } + mActor = mApex.spawnEffectPackageActor(ASSETS[num].model); +} + +void SampleSceneController::Animate(double dt) +{ + if (mDraggingActor != NULL) + { + const float DRAGGING_FORCE_FACTOR = 10.0f; + const float DRAGGING_VELOCITY_FACTOR = 2.0f; + PxVec3 direction = (mDragAttractionPoint - mDraggingActor->getGlobalPose().transform(mDraggingActorHookLocalPoint)); + nvidia::PxVec3 force = (direction * DRAGGING_FORCE_FACTOR - DRAGGING_VELOCITY_FACTOR * mDraggingActor->getLinearVelocity()) * mDraggingActor->getMass() * dt; + physx::PxRigidBodyExt::addForceAtLocalPos(*mDraggingActor, force, mDraggingActorHookLocalPoint, physx::PxForceMode::eIMPULSE, true); + } +} + +void SampleSceneController::throwCube() +{ + PxVec3 eyePos = XMVECTORToPxVec4(mCamera->GetEyePt()).getXYZ(); + PxVec3 lookAtPos = XMVECTORToPxVec4(mCamera->GetLookAtPt()).getXYZ(); + PhysXPrimitive* box = mApex.spawnPhysXPrimitiveBox(PxTransform(eyePos)); + PxRigidDynamic* rigidDynamic = box->getActor()->is<PxRigidDynamic>(); + + const float CUBE_SPEED = 30.0f; + PxVec3 dir = (lookAtPos - eyePos).getNormalized(); + rigidDynamic->setLinearVelocity(dir * CUBE_SPEED); +} + +void SampleSceneController::onTouchEvent(TouchEvent touchEvent, float mouseX, float mouseY) +{ + PxVec3 eyePos, pickDir; + mApex.getEyePoseAndPickDir(mouseX, mouseY, eyePos, pickDir); + pickDir = pickDir.getNormalized(); + + if (touchEvent == TouchEvent::ePRESS) + { + if (pickDir.magnitude() > 0) + { + physx::PxRaycastHit hit; + physx::PxRaycastBuffer rcBuffer(&hit, 1); + bool isHit = mApex.getApexScene()->getPhysXScene()->raycast(eyePos, pickDir, PX_MAX_F32, rcBuffer, physx::PxHitFlag::ePOSITION, physx::PxQueryFilterData(physx::PxQueryFlag::eDYNAMIC)); + if (isHit) + { + mDragDistance = (eyePos - hit.position).magnitude(); + mDraggingActor = hit.actor->is<PxRigidDynamic>(); + mDraggingActorHookLocalPoint = mDraggingActor->getGlobalPose().getInverse().transform(hit.position); + mDraggingActor->setLinearVelocity(PxVec3(0, 0, 0)); + mDraggingActor->setAngularVelocity(PxVec3(0, 0, 0)); + } + } + } + else if (touchEvent == TouchEvent::eDRAG) + { + physx::PxRaycastHit hit; + physx::PxRaycastBuffer rcBuffer(&hit, 1); + bool isHit = mApex.getApexScene()->getPhysXScene()->raycast(eyePos, pickDir, PX_MAX_F32, rcBuffer, physx::PxHitFlag::ePOSITION, physx::PxQueryFilterData(physx::PxQueryFlag::eSTATIC)); + if (isHit) + { + mDragDistance = PxMin(mDragDistance, (eyePos - hit.position).magnitude()); + } + + mDragAttractionPoint = eyePos + pickDir * mDragDistance; + } + else if (touchEvent == TouchEvent::eRELEASE) + { + mDraggingActor = NULL; + } +} + + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + diff --git a/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleSceneController.h b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleSceneController.h new file mode 100644 index 00000000..3bf18c79 --- /dev/null +++ b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleSceneController.h @@ -0,0 +1,77 @@ +/* +* 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. +*/ + +#ifndef SCENE_CONTROLLER_H +#define SCENE_CONTROLLER_H + +#include "ApexController.h" +#include "SampleManager.h" +#include "PxPhysicsAPI.h" + +class CFirstPersonCamera; + +using namespace physx; +using namespace nvidia; + +class SampleSceneController : public ISampleController +{ +public: + SampleSceneController(CFirstPersonCamera* camera, ApexController& apex); + virtual ~SampleSceneController(); + + struct AssetDescription + { + const char* model; + const char* uiName; + }; + static AssetDescription ASSETS[]; + static int getAssetsCount(); + + void setCurrentAsset(int); + + int getCurrentAsset() + { + return mCurrentAsset; + } + + virtual void onSampleStart(); + virtual void Animate(double dt); + + void throwCube(); + + enum TouchEvent + { + ePRESS, + eDRAG, + eRELEASE + }; + + void onTouchEvent(TouchEvent touchEvent, float mouseX, float mouseY); + +private: + SampleSceneController& operator= (SampleSceneController&); + + void loadEffectPackageDatabase(); + + int mCurrentAsset; + + PxRigidDynamic* mDraggingActor; + nvidia::PxVec3 mDraggingActorHookLocalPoint; + nvidia::PxVec3 mDragAttractionPoint; + float mDragDistance; + + ApexController& mApex; + CFirstPersonCamera* mCamera; + + EffectPackageActor* mActor; + +}; + +#endif
\ No newline at end of file diff --git a/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleUIController.cpp b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleUIController.cpp new file mode 100644 index 00000000..4c074bdc --- /dev/null +++ b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleUIController.cpp @@ -0,0 +1,105 @@ +/* +* 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 "SampleUIController.h" +#include "SampleSceneController.h" +#include "CommonUIController.h" + +SampleUIController::SampleUIController(SampleSceneController* s, CommonUIController* c) : mScene(s), mCommonUIController(c) +{ +} + +void SampleUIController::onInitialize() +{ + TwBar* sampleBar = TwNewBar("Sample"); + PX_UNUSED(sampleBar); + TwDefine("Sample color='19 25 59' alpha=128 text=light size='200 150' iconified=false valueswidth=150 position='12 480' label='Select Asset'"); + + UINT assetsCount = (UINT)SampleSceneController::getAssetsCount(); + TwEnumVal* enumAssets = new TwEnumVal[assetsCount]; + for (UINT i = 0; i < assetsCount; i++) + { + enumAssets[i].Value = (int32_t)i; + enumAssets[i].Label = SampleSceneController::ASSETS[i].uiName; + } + TwType enumSceneType = TwDefineEnum("Assets", enumAssets, assetsCount); + delete[] enumAssets; + TwAddVarCB(sampleBar, "Assets", enumSceneType, SampleUIController::setCurrentScene, + SampleUIController::getCurrentScene, this, "group='Select Scene'"); + + mCommonUIController->addPhysXDebugRenderParam(PxVisualizationParameter::ePARTICLE_SYSTEM_POSITION); + mCommonUIController->addPhysXDebugRenderParam(PxVisualizationParameter::ePARTICLE_SYSTEM_VELOCITY); + mCommonUIController->addPhysXDebugRenderParam(PxVisualizationParameter::ePARTICLE_SYSTEM_COLLISION_NORMAL); + mCommonUIController->addPhysXDebugRenderParam(PxVisualizationParameter::ePARTICLE_SYSTEM_BOUNDS); + mCommonUIController->addPhysXDebugRenderParam(PxVisualizationParameter::ePARTICLE_SYSTEM_GRID); + mCommonUIController->addPhysXDebugRenderParam(PxVisualizationParameter::ePARTICLE_SYSTEM_BROADPHASE_BOUNDS); + mCommonUIController->addPhysXDebugRenderParam(PxVisualizationParameter::ePARTICLE_SYSTEM_MAX_MOTION_DISTANCE); + + mCommonUIController->addApexDebugRenderParam("VISUALIZE_TURBULENCE_FS_VELOCITY", "TurbulenceFS", 1.0f, "TurbulenceFS velocities"); + mCommonUIController->addApexDebugRenderParam("VISUALIZE_TURBULENCE_FS_BBOX", "TurbulenceFS", 1.0f, "TurbulenceFS BBOX"); + mCommonUIController->addApexDebugRenderParam("VISUALIZE_TURBULENCE_FS_ACTOR_NAME", "TurbulenceFS", 1.0f, "TurbulenceFS name"); + mCommonUIController->addApexDebugRenderParam("VISUALIZE_TURBULENCE_FS_VELOCITY_FIELD", "TurbulenceFS", 1.0f, "TurbulenceFS velocity field"); + mCommonUIController->addApexDebugRenderParam("VISUALIZE_TURBULENCE_FS_STREAMLINES", "TurbulenceFS", 1.0f, "TurbulenceFS streamlines"); + mCommonUIController->addApexDebugRenderParam("VISUALIZE_IOFX_ACTOR", "Iofx", 1.0f, "IOFX actor"); + mCommonUIController->addApexDebugRenderParam("apexEmitterParameters.VISUALIZE_APEX_EMITTER_ACTOR", "Emitter", 1.0f, "Emitter actor"); + + mCommonUIController->addHintLine("Throw cube - SPACE"); + mCommonUIController->addHintLine("Hook cube - LMB"); +} + +LRESULT SampleUIController::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + PX_UNUSED(hWnd); + PX_UNUSED(wParam); + PX_UNUSED(lParam); + + if(uMsg == WM_LBUTTONDOWN || uMsg == WM_MOUSEMOVE || uMsg == WM_LBUTTONUP) + { + short mouseX = (short)LOWORD(lParam); + short mouseY = (short)HIWORD(lParam); + SampleSceneController::TouchEvent touchEvent = uMsg == WM_LBUTTONDOWN ? SampleSceneController::ePRESS : (uMsg == WM_MOUSEMOVE ? SampleSceneController::eDRAG : SampleSceneController::eRELEASE); + mScene->onTouchEvent(touchEvent, mouseX / static_cast<float>(mWidth), mouseY / static_cast<float>(mHeight)); + } + + if (uMsg == WM_KEYDOWN) + { + int iKeyPressed = static_cast<int>(wParam); + if (iKeyPressed == VK_SPACE) + { + mScene->throwCube(); + } + } + + return 1; +} + +void SampleUIController::BackBufferResized(ID3D11Device* pDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc) +{ + PX_UNUSED(pDevice); + + mWidth = pBackBufferSurfaceDesc->Width; + mHeight = pBackBufferSurfaceDesc->Height; +} + +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// UI Callbacks +/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +void TW_CALL SampleUIController::setCurrentScene(const void* value, void* clientData) +{ + SampleUIController* controller = static_cast<SampleUIController*>(clientData); + controller->mScene->setCurrentAsset(*static_cast<const int*>(value)); +} + +void TW_CALL SampleUIController::getCurrentScene(void* value, void* clientData) +{ + SampleUIController* controller = static_cast<SampleUIController*>(clientData); + *static_cast<int*>(value) = controller->mScene->getCurrentAsset(); +}
\ No newline at end of file diff --git a/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleUIController.h b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleUIController.h new file mode 100644 index 00000000..aba590cd --- /dev/null +++ b/APEX_1.4/samples_v2/SampleParticlesHelloWorld/SampleUIController.h @@ -0,0 +1,42 @@ +/* +* 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. +*/ + +#ifndef SAMPLE_UI_CONTROLLER_H +#define SAMPLE_UI_CONTROLLER_H + +#include "SampleManager.h" +#include <DirectXMath.h> +#include "AntTweakBar.h" + +class SampleSceneController; +class CommonUIController; + +class SampleUIController : public ISampleController +{ + public: + SampleUIController(SampleSceneController* s, CommonUIController* c); + + virtual void onInitialize(); + virtual LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + virtual void BackBufferResized(ID3D11Device* pDevice, const DXGI_SURFACE_DESC* pBackBufferSurfaceDesc); + + static void TW_CALL setCurrentScene(const void* value, void* clientData); + static void TW_CALL getCurrentScene(void* value, void* clientData); + + private: + SampleSceneController* mScene; + CommonUIController* mCommonUIController; + TwBar* mSettingsBar; + + UINT mWidth; + UINT mHeight; +}; + +#endif
\ No newline at end of file |