GraphicsClientExample.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "GraphicsClientExample.h"
  2. #include "../CommonInterfaces/CommonExampleInterface.h"
  3. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  4. #include "Bullet3Common/b3Logging.h"
  5. #include "GraphicsSharedMemoryCommands.h"
  6. #include "PosixSharedMemory.h"
  7. #include "Win32SharedMemory.h"
  8. #include "GraphicsSharedMemoryBlock.h"
  9. #include "Bullet3Common/b3Scalar.h"
  10. class GraphicsClientExample : public CommonExampleInterface
  11. {
  12. protected:
  13. GUIHelperInterface* m_guiHelper;
  14. bool m_waitingForServer;
  15. GraphicsSharedMemoryBlock* m_testBlock1;
  16. SharedMemoryInterface* m_sharedMemory;
  17. GraphicsSharedMemoryStatus m_lastServerStatus;
  18. int m_sharedMemoryKey;
  19. bool m_isConnected;
  20. public:
  21. GraphicsClientExample(GUIHelperInterface* helper, int options);
  22. virtual ~GraphicsClientExample();
  23. virtual void initPhysics();
  24. virtual void stepSimulation(float deltaTime);
  25. virtual void resetCamera()
  26. {
  27. float dist = 3.45;
  28. float pitch = -16.2;
  29. float yaw = 287;
  30. float targetPos[3] = {2.05, 0.02, 0.53}; //-3,2.8,-2.5};
  31. m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
  32. }
  33. virtual bool isConnected()
  34. {
  35. return m_isConnected;
  36. }
  37. bool canSubmitCommand() const
  38. {
  39. if (m_isConnected && !m_waitingForServer)
  40. {
  41. if (m_testBlock1->m_magicId == GRAPHICS_SHARED_MEMORY_MAGIC_NUMBER)
  42. {
  43. return true;
  44. }
  45. else
  46. {
  47. return false;
  48. }
  49. }
  50. return false;
  51. }
  52. struct GraphicsSharedMemoryCommand* getAvailableSharedMemoryCommand()
  53. {
  54. static int sequence = 0;
  55. if (m_testBlock1)
  56. {
  57. m_testBlock1->m_clientCommands[0].m_sequenceNumber = sequence++;
  58. return &m_testBlock1->m_clientCommands[0];
  59. }
  60. return 0;
  61. }
  62. bool submitClientCommand(const GraphicsSharedMemoryCommand& command)
  63. {
  64. /// at the moment we allow a maximum of 1 outstanding command, so we check for this
  65. // once the server processed the command and returns a status, we clear the flag
  66. // "m_data->m_waitingForServer" and allow submitting the next command
  67. if (!m_waitingForServer)
  68. {
  69. //printf("submit command of type %d\n", command.m_type);
  70. if (&m_testBlock1->m_clientCommands[0] != &command)
  71. {
  72. m_testBlock1->m_clientCommands[0] = command;
  73. }
  74. m_testBlock1->m_numClientCommands++;
  75. m_waitingForServer = true;
  76. return true;
  77. }
  78. return false;
  79. }
  80. const GraphicsSharedMemoryStatus* processServerStatus()
  81. {
  82. // SharedMemoryStatus* stat = 0;
  83. if (!m_testBlock1)
  84. {
  85. m_lastServerStatus.m_type = GFX_CMD_SHARED_MEMORY_NOT_INITIALIZED;
  86. return &m_lastServerStatus;
  87. }
  88. if (!m_waitingForServer)
  89. {
  90. return 0;
  91. }
  92. if (m_testBlock1->m_magicId != GRAPHICS_SHARED_MEMORY_MAGIC_NUMBER)
  93. {
  94. m_lastServerStatus.m_type = GFX_CMD_SHARED_MEMORY_NOT_INITIALIZED;
  95. return &m_lastServerStatus;
  96. }
  97. if (m_testBlock1->m_numServerCommands >
  98. m_testBlock1->m_numProcessedServerCommands)
  99. {
  100. B3_PROFILE("processServerCMD");
  101. b3Assert(m_testBlock1->m_numServerCommands ==
  102. m_testBlock1->m_numProcessedServerCommands + 1);
  103. const GraphicsSharedMemoryStatus& serverCmd = m_testBlock1->m_serverCommands[0];
  104. m_lastServerStatus = serverCmd;
  105. // EnumSharedMemoryServerStatus s = (EnumSharedMemoryServerStatus)serverCmd.m_type;
  106. // consume the command
  107. switch (serverCmd.m_type)
  108. {
  109. case GFX_CMD_CLIENT_COMMAND_COMPLETED:
  110. {
  111. B3_PROFILE("CMD_CLIENT_COMMAND_COMPLETED");
  112. break;
  113. }
  114. default:
  115. {
  116. }
  117. }
  118. m_testBlock1->m_numProcessedServerCommands++;
  119. // we don't have more than 1 command outstanding (in total, either server or client)
  120. b3Assert(m_testBlock1->m_numProcessedServerCommands ==
  121. m_testBlock1->m_numServerCommands);
  122. if (m_testBlock1->m_numServerCommands ==
  123. m_testBlock1->m_numProcessedServerCommands)
  124. {
  125. m_waitingForServer = false;
  126. }
  127. else
  128. {
  129. m_waitingForServer = true;
  130. }
  131. return &m_lastServerStatus;
  132. }
  133. return 0;
  134. }
  135. bool connect()
  136. {
  137. /// server always has to create and initialize shared memory
  138. bool allowCreation = false;
  139. m_testBlock1 = (GraphicsSharedMemoryBlock*)m_sharedMemory->allocateSharedMemory(
  140. m_sharedMemoryKey, GRAPHICS_SHARED_MEMORY_SIZE, allowCreation);
  141. if (m_testBlock1)
  142. {
  143. if (m_testBlock1->m_magicId != GRAPHICS_SHARED_MEMORY_MAGIC_NUMBER)
  144. {
  145. b3Error("Error connecting to shared memory: please start server before client\n");
  146. m_sharedMemory->releaseSharedMemory(m_sharedMemoryKey,
  147. GRAPHICS_SHARED_MEMORY_SIZE);
  148. m_testBlock1 = 0;
  149. return false;
  150. }
  151. else
  152. {
  153. m_isConnected = true;
  154. }
  155. }
  156. else
  157. {
  158. b3Warning("Cannot connect to shared memory");
  159. return false;
  160. }
  161. return true;
  162. }
  163. void disconnect()
  164. {
  165. if (m_isConnected && m_sharedMemory)
  166. {
  167. m_sharedMemory->releaseSharedMemory(m_sharedMemoryKey, GRAPHICS_SHARED_MEMORY_SIZE);
  168. }
  169. m_isConnected = false;
  170. }
  171. virtual void exitPhysics(){};
  172. virtual void physicsDebugDraw(int debugFlags)
  173. {
  174. }
  175. virtual void renderScene()
  176. {
  177. }
  178. virtual bool mouseMoveCallback(float x, float y)
  179. {
  180. return false;
  181. }
  182. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  183. {
  184. return false;
  185. }
  186. virtual bool keyboardCallback(int key, int state)
  187. {
  188. return false;
  189. }
  190. };
  191. GraphicsClientExample::GraphicsClientExample(GUIHelperInterface* helper, int options)
  192. : m_guiHelper(helper),
  193. m_waitingForServer(false),
  194. m_testBlock1(0)
  195. {
  196. #ifdef _WIN32
  197. m_sharedMemory = new Win32SharedMemoryClient();
  198. #else
  199. m_sharedMemory = new PosixSharedMemory();
  200. #endif
  201. m_sharedMemoryKey = GRAPHICS_SHARED_MEMORY_KEY;
  202. m_isConnected = false;
  203. b3Printf("Started GraphicsClientExample\n");
  204. connect();
  205. }
  206. GraphicsClientExample::~GraphicsClientExample()
  207. {
  208. disconnect();
  209. delete m_sharedMemory;
  210. }
  211. void GraphicsClientExample::initPhysics()
  212. {
  213. if (m_guiHelper && m_guiHelper->getParameterInterface())
  214. {
  215. int upAxis = 2;
  216. m_guiHelper->setUpAxis(upAxis);
  217. }
  218. }
  219. void GraphicsClientExample::stepSimulation(float deltaTime)
  220. {
  221. GraphicsSharedMemoryCommand* cmd = getAvailableSharedMemoryCommand();
  222. if (cmd)
  223. {
  224. cmd->m_updateFlags = 0;
  225. cmd->m_type = GFX_CMD_0;
  226. submitClientCommand(*cmd);
  227. }
  228. const GraphicsSharedMemoryStatus* status = processServerStatus();
  229. if (status)
  230. {
  231. //handle it
  232. }
  233. }
  234. class CommonExampleInterface* GraphicsClientCreateFunc(struct CommonExampleOptions& options)
  235. {
  236. GraphicsClientExample* example = new GraphicsClientExample(options.m_guiHelper, options.m_option);
  237. return example;
  238. }