Created
January 11, 2015 09:58
-
-
Save krupitskas/c667c14785f357c2cfba to your computer and use it in GitHub Desktop.
Exception
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <irrlicht.h> | |
#include <cstdlib> | |
#include <list> | |
using namespace irr; | |
#pragma comment(lib, "Irrlicht.lib") | |
//Havok init | |
// Math and base include | |
#include <Common/Base/hkBase.h> | |
#include <Common/Base/Memory/System/Util/hkMemoryInitUtil.h> | |
#include <Common/Base/System/Error/hkDefaultError.h> | |
#include <Common/Base/Monitor/hkMonitorStream.h> | |
#include <Common/Base/Config/hkConfigVersion.h> | |
#include <Common/Base/Memory/System/hkMemorySystem.h> | |
#include <Common/Base/Memory/Allocator/Malloc/hkMallocAllocator.h> | |
#include <Common/Base/Container/String/hkStringBuf.h> | |
// Dynamics includes | |
#include <Physics2012/Collide/hkpCollide.h> | |
#include <Physics2012/Collide/Agent/ConvexAgent/SphereBox/hkpSphereBoxAgent.h> | |
#include <Physics2012/Collide/Shape/Convex/Box/hkpBoxShape.h> | |
#include <Physics2012/Collide/Shape/Convex/Sphere/hkpSphereShape.h> | |
#include <Physics2012/Collide/Dispatch/hkpAgentRegisterUtil.h> | |
#include <Common/Internal/GeometryProcessing/ConvexHull/hkgpConvexHull.h> | |
#include <Physics2012/Collide/Shape/Convex/Cylinder/hkpCylinderShape.h> | |
#include <Physics2012/Collide/Shape/Convex/Capsule/hkpCapsuleShape.h> | |
#include <Physics2012/Collide/Shape/Convex/Triangle/hkpTriangleShape.h> | |
#include <Physics2012/Collide/Shape/Convex/ConvexVertices/hkpConvexVerticesShape.h> | |
#include <Physics2012/Dynamics/World/hkpWorld.h> | |
#include <Physics2012/Dynamics/Entity/hkpRigidBody.h> | |
#include <Physics2012/Utilities/Dynamics/Inertia/hkpInertiaTensorComputer.h> | |
// Visual Debugger includes | |
#include <Common/Visualize/hkVisualDebugger.h> | |
#include <Physics2012/Utilities/VisualDebugger/hkpPhysicsContext.h> | |
using namespace irr; | |
using namespace core; | |
using namespace scene; | |
using namespace video; | |
using namespace io; | |
using namespace gui; | |
void PlatformInit(); | |
void PlatformFileSystemInit(); | |
static void HK_CALL errorReport(const char* msg, void*) | |
{ | |
printf("%s", msg); | |
#ifdef HK_PLATFORM_WIN32 | |
#ifndef HK_PLATFORM_WINRT | |
OutputDebugStringA(msg); | |
#else | |
// Unicode only | |
int sLen = hkString::strLen(msg) + 1; | |
wchar_t* wideStr = hkAllocateStack<wchar_t>( sLen ); | |
mbstowcs_s(HK_NULL, wideStr, sLen, msg, sLen - 1); | |
OutputDebugString(wideStr); | |
hkDeallocateStack<wchar_t>( wideStr, sLen); | |
#endif | |
#endif | |
} | |
class IrrHBody; | |
// | |
// Forward declarations | |
// | |
void setupPhysics(hkpWorld* physicsWorld); | |
hkVisualDebugger* setupVisualDebugger(hkpPhysicsContext* worlds); | |
void stepVisualDebugger(hkVisualDebugger* vdb); | |
// Functions | |
//For now, there's not much here. Just add them in as we create them | |
static int GetRandInt(int TMax) { return rand() % TMax; } | |
void CreateBox(const hkVector4 &TPosition, const core::vector3df &TScale, hkReal TMass); | |
static void CreateStartScene(); | |
static void CreateBox(const hkVector4 &TPosition, const vector3df &TScale, hkReal TMass); | |
static void CreateSphere(const hkVector4 &TPosition, hkReal TRadius, hkReal TMass); | |
static void UpdatePhysics(u32 TDeltaTime); | |
static void UpdateRender(IrrHBody *TObject); | |
// Globals | |
static bool Done = false; | |
static hkpWorld *World; | |
static IrrlichtDevice *irrDevice; | |
static IVideoDriver *irrDriver; | |
static ISceneManager *irrScene; | |
static IGUIEnvironment *irrGUI; | |
static IFileSystem *irrFile; | |
static ITimer *irrTimer; | |
static ILogger *irrLog; | |
static std::list<IrrHBody > Objects; | |
// Event receiver | |
class EventReceiverClass : public IEventReceiver { | |
public: | |
virtual bool OnEvent(const SEvent &TEvent) { | |
if(TEvent.EventType == EET_KEY_INPUT_EVENT && !TEvent.KeyInput.PressedDown) { | |
switch(TEvent.KeyInput.Key) { | |
case KEY_ESCAPE: | |
Done = true; | |
break; | |
case KEY_KEY_1: | |
CreateSphere(hkVector4(GetRandInt(10) - 5.0f, 7.0f, GetRandInt(10) - 5.0f), GetRandInt(5) / 5.0f + 0.2f, 1.0f); | |
default: | |
return false; | |
break; | |
} | |
return true; | |
} | |
return false; | |
} | |
}; | |
class IrrHBody | |
{ | |
public: | |
IrrHBody() { Node = nullptr, Phys = nullptr;}; | |
scene::ISceneNode *Node; | |
hkpRigidBody* Phys ; | |
}; | |
int main() | |
{ | |
PlatformInit(); | |
hkMemorySystem::FrameInfo finfo(500 * 1024); // Allocate 500KB of Physics solver buffer | |
hkMemoryRouter* memoryRouter = hkMemoryInitUtil::initDefault(hkMallocAllocator::m_defaultMallocAllocator, finfo); | |
hkBaseSystem::init( memoryRouter, errorReport ); | |
irrDevice = createDevice(video::EDT_OPENGL, core::dimension2d<u32>(640, 480), 32, false, false, false, 0); | |
if (irrDevice == 0) | |
return 1; // could not create selected driver. | |
irrGUI = irrDevice->getGUIEnvironment(); | |
irrTimer = irrDevice->getTimer(); | |
irrDriver = irrDevice->getVideoDriver(); | |
irrScene = irrDevice->getSceneManager(); | |
irrDevice->setWindowCaption(L"Irrlicht"); | |
//Add an FPS camera move it up and back and point it at the origin | |
scene::ICameraSceneNode *Camera = irrScene->addCameraSceneNodeFPS(0, 100, 10); | |
Camera->setPosition(core::vector3df(0, 5, -5)); | |
Camera->setTarget(core::vector3df(0, 0, 0)); | |
//Make the mouse cursor invisible | |
irrDevice->getCursorControl()->setVisible(false); | |
//Setting up physics world | |
hkpWorld* physicsWorld; | |
{ | |
hkpWorldCinfo worldInfo; | |
worldInfo.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); | |
worldInfo.m_gravity = hkVector4(0.0f, -9.8f, 0.0f); | |
worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_FIX_ENTITY; // just fix the entity if the object falls off too far | |
// You must specify the size of the broad phase - objects should not be simulated outside this region | |
worldInfo.setBroadPhaseWorldSize(1000.0f); | |
physicsWorld = new hkpWorld(worldInfo); | |
} | |
// Main loop | |
// Create the initial scene | |
irrScene->addLightSceneNode(0, core::vector3df(2, 5, -2), SColorf(4, 4, 4, 1)); | |
CreateStartScene(); | |
u32 TimeStamp = irrTimer->getTime(), DeltaTime = 0; | |
while(!Done) { | |
DeltaTime = irrTimer->getTime() - TimeStamp; | |
TimeStamp = irrTimer->getTime(); | |
UpdatePhysics(DeltaTime); | |
irrDriver->beginScene(true, true, SColor(255, 20, 0, 0)); | |
irrScene->drawAll(); | |
irrGUI->drawAll(); | |
irrDriver->endScene(); | |
irrDevice->run(); | |
} | |
//Delete the Irrlicht device | |
irrDevice->drop(); | |
return 0; | |
} | |
hkVisualDebugger* setupVisualDebugger(hkpPhysicsContext* physicsWorlds) | |
{ | |
// Setup the visual debugger | |
hkArray<hkProcessContext*> contexts; | |
contexts.pushBack(physicsWorlds); | |
hkVisualDebugger* vdb = new hkVisualDebugger(contexts); | |
vdb->serve(); | |
// Allocate memory for internal profiling information | |
// You can discard this if you do not want Havok profiling information | |
hkMonitorStream& stream = hkMonitorStream::getInstance(); | |
stream.resize( 500 * 1024 ); // 500K for timer info | |
stream.reset(); | |
return vdb; | |
} | |
void stepVisualDebugger(hkVisualDebugger* vdb) | |
{ | |
// Step the debugger | |
vdb->step(); | |
// Reset internal profiling info for next frame | |
hkMonitorStream::getInstance().reset(); | |
} | |
void CreateBox(const hkVector4 &TPosition, const core::vector3df &TScale, hkReal TMass) | |
{ | |
} | |
void CreateSphere(const hkVector4 &TPosition, hkReal TRadius, hkReal TMass) { | |
IrrHBody body; | |
// Create an Irrlicht sphere | |
body.Node = irrScene->addSphereSceneNode(TRadius, 32); | |
body.Node->setMaterialFlag(video::EMF_LIGHTING, 1); | |
body.Node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true); | |
// Create a moving sphere | |
hkReal sphereRadius = 0.5f; | |
hkpConvexShape* sphereShape = new hkpSphereShape(sphereRadius); | |
hkpRigidBodyCinfo sphereInfo; | |
sphereInfo.m_shape = sphereShape; | |
sphereInfo.m_position.set(0.0f, 50.0f, 0.0f); | |
sphereInfo.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA; | |
// Compute mass properties | |
hkReal sphereMass = 10.0f; | |
hkMassProperties sphereMassProperties; | |
hkpInertiaTensorComputer::computeSphereVolumeMassProperties(sphereRadius, sphereMass, sphereMassProperties); | |
sphereInfo.m_inertiaTensor = sphereMassProperties.m_inertiaTensor; | |
sphereInfo.m_centerOfMass = sphereMassProperties.m_centerOfMass; | |
sphereInfo.m_mass = sphereMassProperties.m_mass; | |
// Create sphere RigidBody | |
body.Phys = new hkpRigidBody(sphereInfo); | |
sphereShape->removeReference(); | |
World->addEntity(body.Phys); | |
Objects.push_back(body); | |
} | |
// Creates a base box | |
void CreateStartScene() { | |
CreateSphere(hkVector4(0,0,0),20,1); | |
} | |
// Passes bullet's orientation to irrlicht | |
void UpdateRender(IrrHBody TObject) { | |
// Set position | |
hkVector4 Point = TObject.Phys->getCenterOfMassInWorld(); | |
TObject.Node->setPosition(vector3df((f32)Point.getComponent(0), (f32)Point.getComponent(1), (f32)Point.getComponent(2))); | |
// Set rotation | |
vector3df Euler; | |
const hkQuaternion TQuat = TObject.Phys->getRotation(); | |
hkVector4 axisStore; | |
TQuat.getAxis(axisStore); | |
quaternion q(axisStore.getComponent(0), | |
axisStore.getComponent(1), | |
axisStore.getComponent(2), | |
TQuat.getAngle()); | |
q.toEuler(Euler); | |
Euler *= RADTODEG; | |
TObject.Node->setRotation(Euler); | |
} | |
// Runs the physics simulation. | |
// - TDeltaTime tells the simulation how much time has passed since the last frame so the simulation can run independently of the frame rate. | |
void UpdatePhysics(u32 TDeltaTime) { | |
World->stepDeltaTime(TDeltaTime * 0.001f); | |
// Relay the object's orientation to irrlicht | |
for(std::list<IrrHBody>::iterator Iterator = Objects.begin(); Iterator != Objects.end(); ++Iterator) { | |
UpdateRender(*Iterator); | |
} | |
} | |
// Keycode | |
#include <Common/Base/keycode.cxx> | |
// This excludes libraries that are not going to be linked | |
// from the project configuration, even if the keycodes are | |
// present | |
#undef HK_FEATURE_PRODUCT_AI | |
#undef HK_FEATURE_PRODUCT_ANIMATION | |
#undef HK_FEATURE_PRODUCT_CLOTH | |
#undef HK_FEATURE_PRODUCT_DESTRUCTION_2012 | |
#undef HK_FEATURE_PRODUCT_DESTRUCTION | |
#undef HK_FEATURE_PRODUCT_BEHAVIOR | |
#undef HK_FEATURE_PRODUCT_SIMULATION | |
#undef HK_FEATURE_PRODUCT_PHYSICS | |
#define HK_EXCLUDE_LIBRARY_hkpVehicle | |
#define HK_EXCLUDE_LIBRARY_hkCompat | |
#define HK_EXCLUDE_LIBRARY_hkSceneData | |
#define HK_EXCLUDE_LIBRARY_hkcdCollide | |
// | |
// Common | |
// | |
#define HK_EXCLUDE_FEATURE_SerializeDeprecatedPre700 | |
#define HK_EXCLUDE_FEATURE_RegisterVersionPatches | |
//#define HK_EXCLUDE_FEATURE_MemoryTracker | |
// | |
// Physics | |
// | |
#define HK_EXCLUDE_FEATURE_hkpHeightField | |
//#define HK_EXCLUDE_FEATURE_hkpSimulation | |
//#define HK_EXCLUDE_FEATURE_hkpContinuousSimulation | |
//#define HK_EXCLUDE_FEATURE_hkpMultiThreadedSimulation | |
#define HK_EXCLUDE_FEATURE_hkpAccurateInertiaTensorComputer | |
#define HK_EXCLUDE_FEATURE_hkpUtilities | |
#define HK_EXCLUDE_FEATURE_hkpVehicle | |
#define HK_EXCLUDE_FEATURE_hkpCompressedMeshShape | |
#define HK_EXCLUDE_FEATURE_hkpConvexPieceMeshShape | |
#define HK_EXCLUDE_FEATURE_hkpExtendedMeshShape | |
#define HK_EXCLUDE_FEATURE_hkpMeshShape | |
#define HK_EXCLUDE_FEATURE_hkpSimpleMeshShape | |
#define HK_EXCLUDE_FEATURE_hkpPoweredChainData | |
#define HK_EXCLUDE_FEATURE_hkMonitorStream | |
#include <Common/Base/Config/hkProductFeatures.cxx> | |
// Platform specific initialization | |
#include <Common/Base/System/Init/PlatformInit.cxx> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment