2016-03-07 22:56:16 +00:00
|
|
|
|
|
|
|
#include "SharedMemoryInProcessPhysicsC_API.h"
|
2017-02-07 16:08:55 +00:00
|
|
|
#include "../Utils/b3Clock.h"
|
2016-04-14 15:51:20 +00:00
|
|
|
|
2016-03-07 22:56:16 +00:00
|
|
|
#include "PhysicsClientSharedMemory.h"
|
2016-03-07 23:12:33 +00:00
|
|
|
#include"../ExampleBrowser/InProcessExampleBrowser.h"
|
2017-07-14 22:12:16 +00:00
|
|
|
#include <stdio.h>
|
2017-08-15 00:02:20 +00:00
|
|
|
#include <string.h>
|
2017-05-31 02:54:55 +00:00
|
|
|
#include "PhysicsServerExampleBullet2.h"
|
|
|
|
|
2017-05-13 18:15:20 +00:00
|
|
|
#include "../CommonInterfaces/CommonExampleInterface.h"
|
|
|
|
#include "InProcessMemory.h"
|
2016-04-14 15:51:20 +00:00
|
|
|
|
2017-03-26 20:06:46 +00:00
|
|
|
#include "Bullet3Common/b3Logging.h"
|
2016-04-14 15:51:20 +00:00
|
|
|
class InProcessPhysicsClientSharedMemoryMainThread : public PhysicsClientSharedMemory
|
|
|
|
{
|
|
|
|
btInProcessExampleBrowserMainThreadInternalData* m_data;
|
2017-02-07 16:08:55 +00:00
|
|
|
b3Clock m_clock;
|
|
|
|
|
2016-04-14 15:51:20 +00:00
|
|
|
public:
|
|
|
|
|
2017-08-15 00:02:20 +00:00
|
|
|
InProcessPhysicsClientSharedMemoryMainThread(int argc, char* argv[], bool useInProcessMemory)
|
2016-04-14 15:51:20 +00:00
|
|
|
{
|
|
|
|
int newargc = argc+2;
|
|
|
|
char** newargv = (char**)malloc(sizeof(void*)*newargc);
|
|
|
|
for (int i=0;i<argc;i++)
|
|
|
|
newargv[i] = argv[i];
|
|
|
|
|
|
|
|
char* t0 = (char*)"--logtostderr";
|
|
|
|
char* t1 = (char*)"--start_demo_name=Physics Server";
|
|
|
|
newargv[argc] = t0;
|
|
|
|
newargv[argc+1] = t1;
|
2017-08-15 00:02:20 +00:00
|
|
|
m_data = btCreateInProcessExampleBrowserMainThread(newargc,newargv, useInProcessMemory);
|
2016-04-14 15:51:20 +00:00
|
|
|
SharedMemoryInterface* shMem = btGetSharedMemoryInterfaceMainThread(m_data);
|
|
|
|
|
|
|
|
setSharedMemoryInterface(shMem);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~InProcessPhysicsClientSharedMemoryMainThread()
|
|
|
|
{
|
|
|
|
setSharedMemoryInterface(0);
|
|
|
|
btShutDownExampleBrowserMainThread(m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return non-null if there is a status, nullptr otherwise
|
|
|
|
virtual const struct SharedMemoryStatus* processServerStatus()
|
|
|
|
{
|
2017-03-26 20:06:46 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
if (btIsExampleBrowserMainThreadTerminated(m_data))
|
|
|
|
{
|
|
|
|
PhysicsClientSharedMemory::disconnectSharedMemory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2017-08-12 00:14:52 +00:00
|
|
|
unsigned long int ms = m_clock.getTimeMilliseconds();
|
|
|
|
if (ms>2)
|
2017-03-26 20:06:46 +00:00
|
|
|
{
|
2017-08-12 00:14:52 +00:00
|
|
|
B3_PROFILE("m_clock.reset()");
|
2017-03-26 20:06:46 +00:00
|
|
|
|
2017-08-12 00:14:52 +00:00
|
|
|
btUpdateInProcessExampleBrowserMainThread(m_data);
|
|
|
|
m_clock.reset();
|
2017-03-26 20:06:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
b3Clock::usleep(0);
|
2017-02-16 22:19:09 +00:00
|
|
|
}
|
2017-03-26 20:06:46 +00:00
|
|
|
const SharedMemoryStatus* stat = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
stat = PhysicsClientSharedMemory::processServerStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
2016-04-19 15:29:20 +00:00
|
|
|
|
2016-04-14 15:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool submitClientCommand(const struct SharedMemoryCommand& command)
|
|
|
|
{
|
|
|
|
// btUpdateInProcessExampleBrowserMainThread(m_data);
|
|
|
|
return PhysicsClientSharedMemory::submitClientCommand(command);
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2017-09-21 06:18:18 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThread(int argc, char* argv[])
|
2016-04-14 15:51:20 +00:00
|
|
|
{
|
2017-08-16 19:33:25 +00:00
|
|
|
InProcessPhysicsClientSharedMemoryMainThread* cl = new InProcessPhysicsClientSharedMemoryMainThread(argc, argv, 1);
|
2017-09-13 20:30:16 +00:00
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY+1);
|
2016-04-14 15:51:20 +00:00
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle ) cl;
|
|
|
|
}
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2017-09-21 06:18:18 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectMainThreadSharedMemory(int argc, char* argv[])
|
2017-08-16 19:33:25 +00:00
|
|
|
{
|
|
|
|
InProcessPhysicsClientSharedMemoryMainThread* cl = new InProcessPhysicsClientSharedMemoryMainThread(argc, argv, 0);
|
2017-09-13 20:30:16 +00:00
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY+1);
|
2017-08-16 19:33:25 +00:00
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle ) cl;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-03-07 22:56:16 +00:00
|
|
|
class InProcessPhysicsClientSharedMemory : public PhysicsClientSharedMemory
|
|
|
|
{
|
|
|
|
btInProcessExampleBrowserInternalData* m_data;
|
2017-06-05 05:04:16 +00:00
|
|
|
char** m_newargv;
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2017-06-05 05:04:16 +00:00
|
|
|
public:
|
|
|
|
|
2017-08-15 00:02:20 +00:00
|
|
|
InProcessPhysicsClientSharedMemory(int argc, char* argv[], bool useInProcessMemory)
|
2016-03-07 22:56:16 +00:00
|
|
|
{
|
|
|
|
int newargc = argc+2;
|
2017-06-05 05:04:16 +00:00
|
|
|
m_newargv = (char**)malloc(sizeof(void*)*newargc);
|
2017-12-28 18:05:51 +00:00
|
|
|
char* t0 = (char*)"--unused";
|
|
|
|
m_newargv[0] = t0;
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2017-12-28 18:05:51 +00:00
|
|
|
for (int i=0;i<argc;i++)
|
|
|
|
m_newargv[i+1] = argv[i];
|
|
|
|
|
2016-03-07 22:56:16 +00:00
|
|
|
char* t1 = (char*)"--start_demo_name=Physics Server";
|
2017-06-05 05:04:16 +00:00
|
|
|
m_newargv[argc+1] = t1;
|
2017-08-15 00:02:20 +00:00
|
|
|
m_data = btCreateInProcessExampleBrowser(newargc,m_newargv, useInProcessMemory);
|
2016-03-10 22:36:46 +00:00
|
|
|
SharedMemoryInterface* shMem = btGetSharedMemoryInterface(m_data);
|
|
|
|
setSharedMemoryInterface(shMem);
|
2016-03-07 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~InProcessPhysicsClientSharedMemory()
|
|
|
|
{
|
2016-03-10 22:36:46 +00:00
|
|
|
setSharedMemoryInterface(0);
|
2016-03-07 22:56:16 +00:00
|
|
|
btShutDownExampleBrowser(m_data);
|
2017-06-05 05:04:16 +00:00
|
|
|
free(m_newargv);
|
2016-03-07 22:56:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-09-21 06:18:18 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnect(int argc, char* argv[])
|
2017-08-16 19:33:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
InProcessPhysicsClientSharedMemory* cl = new InProcessPhysicsClientSharedMemory(argc, argv, 1);
|
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY+1);
|
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle ) cl;
|
|
|
|
}
|
2017-09-21 06:18:18 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerAndConnectSharedMemory(int argc, char* argv[])
|
2017-08-15 00:02:20 +00:00
|
|
|
{
|
2016-03-07 22:56:16 +00:00
|
|
|
|
2017-08-16 19:33:25 +00:00
|
|
|
InProcessPhysicsClientSharedMemory* cl = new InProcessPhysicsClientSharedMemory(argc, argv, 0);
|
2017-08-15 00:02:20 +00:00
|
|
|
cl->setSharedMemoryKey(SHARED_MEMORY_KEY+1);
|
2016-03-07 22:56:16 +00:00
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle ) cl;
|
|
|
|
}
|
|
|
|
|
2017-05-13 18:15:20 +00:00
|
|
|
class InProcessPhysicsClientExistingExampleBrowser : public PhysicsClientSharedMemory
|
|
|
|
{
|
|
|
|
CommonExampleInterface* m_physicsServerExample;
|
|
|
|
SharedMemoryInterface* m_sharedMem;
|
|
|
|
b3Clock m_clock;
|
|
|
|
unsigned long long int m_prevTime;
|
|
|
|
public:
|
|
|
|
InProcessPhysicsClientExistingExampleBrowser (struct GUIHelperInterface* guiHelper)
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
m_sharedMem = new InProcessMemory;
|
|
|
|
CommonExampleOptions options(guiHelper);
|
|
|
|
options.m_sharedMem = m_sharedMem;
|
|
|
|
|
2017-05-31 02:54:55 +00:00
|
|
|
m_physicsServerExample = PhysicsServerCreateFuncBullet2(options);
|
2017-05-13 18:15:20 +00:00
|
|
|
m_physicsServerExample ->initPhysics();
|
|
|
|
m_physicsServerExample ->resetCamera();
|
|
|
|
setSharedMemoryInterface(m_sharedMem);
|
|
|
|
m_clock.reset();
|
|
|
|
m_prevTime = m_clock.getTimeMicroseconds();
|
|
|
|
|
|
|
|
}
|
|
|
|
virtual ~InProcessPhysicsClientExistingExampleBrowser()
|
|
|
|
{
|
|
|
|
m_physicsServerExample->exitPhysics();
|
|
|
|
//s_instancingRenderer->removeAllInstances();
|
|
|
|
delete m_physicsServerExample;
|
|
|
|
delete m_sharedMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return non-null if there is a status, nullptr otherwise
|
|
|
|
virtual const struct SharedMemoryStatus* processServerStatus()
|
|
|
|
{
|
2017-07-14 22:12:16 +00:00
|
|
|
m_physicsServerExample->updateGraphics();
|
2017-05-13 18:15:20 +00:00
|
|
|
|
|
|
|
unsigned long long int curTime = m_clock.getTimeMicroseconds();
|
|
|
|
unsigned long long int dtMicro = curTime - m_prevTime;
|
|
|
|
m_prevTime = curTime;
|
|
|
|
|
|
|
|
double dt = double(dtMicro)/1000000.;
|
|
|
|
|
|
|
|
m_physicsServerExample->stepSimulation(dt);
|
|
|
|
{
|
|
|
|
b3Clock::usleep(0);
|
|
|
|
}
|
|
|
|
const SharedMemoryStatus* stat = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
stat = PhysicsClientSharedMemory::processServerStatus();
|
|
|
|
}
|
|
|
|
|
|
|
|
return stat;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void renderScene()
|
|
|
|
{
|
|
|
|
m_physicsServerExample->renderScene();
|
|
|
|
}
|
2017-05-13 20:37:49 +00:00
|
|
|
virtual void debugDraw(int debugDrawMode)
|
|
|
|
{
|
|
|
|
m_physicsServerExample->physicsDebugDraw(debugDrawMode);
|
|
|
|
}
|
|
|
|
virtual bool mouseMoveCallback(float x, float y)
|
|
|
|
{
|
|
|
|
return m_physicsServerExample->mouseMoveCallback(x,y);
|
|
|
|
}
|
|
|
|
virtual bool mouseButtonCallback(int button, int state, float x, float y)
|
|
|
|
{
|
|
|
|
return m_physicsServerExample->mouseButtonCallback(button,state,x,y);
|
|
|
|
}
|
2017-05-13 18:15:20 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-05-13 20:37:49 +00:00
|
|
|
void b3InProcessDebugDrawInternal(b3PhysicsClientHandle clientHandle, int debugDrawMode)
|
|
|
|
{
|
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*) clientHandle;
|
|
|
|
cl->debugDraw(debugDrawMode);
|
|
|
|
}
|
2017-05-13 18:15:20 +00:00
|
|
|
void b3InProcessRenderSceneInternal(b3PhysicsClientHandle clientHandle)
|
|
|
|
{
|
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*) clientHandle;
|
|
|
|
cl->renderScene();
|
|
|
|
}
|
|
|
|
|
2017-05-13 20:37:49 +00:00
|
|
|
int b3InProcessMouseMoveCallback(b3PhysicsClientHandle clientHandle,float x,float y)
|
|
|
|
{
|
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*) clientHandle;
|
|
|
|
return cl->mouseMoveCallback(x,y);
|
|
|
|
}
|
|
|
|
int b3InProcessMouseButtonCallback(b3PhysicsClientHandle clientHandle, int button, int state, float x, float y)
|
|
|
|
{
|
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = (InProcessPhysicsClientExistingExampleBrowser*) clientHandle;
|
|
|
|
return cl->mouseButtonCallback(button,state, x,y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-21 06:18:18 +00:00
|
|
|
B3_SHARED_API b3PhysicsClientHandle b3CreateInProcessPhysicsServerFromExistingExampleBrowserAndConnect(void* guiHelperPtr)
|
2017-05-13 18:15:20 +00:00
|
|
|
{
|
2017-05-14 19:00:28 +00:00
|
|
|
GUIHelperInterface* guiHelper = (GUIHelperInterface*) guiHelperPtr;
|
2017-05-13 18:15:20 +00:00
|
|
|
InProcessPhysicsClientExistingExampleBrowser* cl = new InProcessPhysicsClientExistingExampleBrowser(guiHelper);
|
|
|
|
//InProcessPhysicsClientFromGuiHelper* cl = new InProcessPhysicsClientFromGuiHelper(guiHelper);
|
|
|
|
cl->connect();
|
|
|
|
return (b3PhysicsClientHandle ) cl;
|
2017-05-14 19:00:28 +00:00
|
|
|
}
|