RenderInstancingDemo.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef RENDER_INSTANCING_DEMO_H
  2. #define RENDER_INSTANCING_DEMO_H
  3. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  4. #include "Bullet3Common/b3Quaternion.h"
  5. #include "Bullet3Common/b3AlignedObjectArray.h"
  6. #include "../CommonInterfaces/CommonRenderInterface.h"
  7. #include "../CommonInterfaces/CommonExampleInterface.h"
  8. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  9. ///quick demo showing the right-handed coordinate system and positive rotations around each axis
  10. class RenderInstancingDemo : public CommonExampleInterface
  11. {
  12. CommonGraphicsApp* m_app;
  13. float m_x;
  14. float m_y;
  15. float m_z;
  16. b3AlignedObjectArray<int> m_movingInstances;
  17. enum
  18. {
  19. numCubesX = 20,
  20. numCubesY = 20
  21. };
  22. public:
  23. RenderInstancingDemo(CommonGraphicsApp* app)
  24. : m_app(app),
  25. m_x(0),
  26. m_y(0),
  27. m_z(0)
  28. {
  29. m_app->setUpAxis(2);
  30. {
  31. b3Vector3 extents = b3MakeVector3(100, 100, 100);
  32. extents[m_app->getUpAxis()] = 1;
  33. int xres = 20;
  34. int yres = 20;
  35. b3Vector4 color0 = b3MakeVector4(0.1, 0.1, 0.1, 1);
  36. b3Vector4 color1 = b3MakeVector4(0.6, 0.6, 0.6, 1);
  37. m_app->registerGrid(xres, yres, color0, color1);
  38. }
  39. {
  40. int boxId = m_app->registerCubeShape(0.1, 0.1, 0.1);
  41. for (int i = -numCubesX / 2; i < numCubesX / 2; i++)
  42. {
  43. for (int j = -numCubesY / 2; j < numCubesY / 2; j++)
  44. {
  45. b3Vector3 pos = b3MakeVector3(i, j, j);
  46. pos[app->getUpAxis()] = 1;
  47. b3Quaternion orn(0, 0, 0, 1);
  48. b3Vector4 color = b3MakeVector4(0.3, 0.3, 0.3, 1);
  49. b3Vector3 scaling = b3MakeVector3(1, 1, 1);
  50. int instanceId = m_app->m_renderer->registerGraphicsInstance(boxId, pos, orn, color, scaling);
  51. m_movingInstances.push_back(instanceId);
  52. }
  53. }
  54. }
  55. m_app->m_renderer->writeTransforms();
  56. }
  57. virtual ~RenderInstancingDemo()
  58. {
  59. }
  60. virtual void physicsDebugDraw(int debugDrawMode)
  61. {
  62. }
  63. virtual void initPhysics()
  64. {
  65. }
  66. virtual void exitPhysics()
  67. {
  68. }
  69. virtual void stepSimulation(float deltaTime)
  70. {
  71. m_x += 0.01f;
  72. m_y += 0.01f;
  73. m_z += 0.01f;
  74. int index = 0;
  75. for (int i = -numCubesX / 2; i < numCubesX / 2; i++)
  76. {
  77. for (int j = -numCubesY / 2; j < numCubesY / 2; j++)
  78. {
  79. b3Vector3 pos = b3MakeVector3(i, j, j);
  80. pos[m_app->getUpAxis()] = 1 + 1 * b3Sin(m_x + i - j);
  81. float orn[4] = {0, 0, 0, 1};
  82. m_app->m_renderer->writeSingleInstanceTransformToCPU(pos, orn, m_movingInstances[index++]);
  83. }
  84. }
  85. m_app->m_renderer->writeTransforms();
  86. }
  87. virtual void renderScene()
  88. {
  89. m_app->m_renderer->renderScene();
  90. }
  91. virtual void physicsDebugDraw()
  92. {
  93. }
  94. virtual bool mouseMoveCallback(float x, float y)
  95. {
  96. return false;
  97. }
  98. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  99. {
  100. return false;
  101. }
  102. virtual bool keyboardCallback(int key, int state)
  103. {
  104. return false;
  105. }
  106. virtual void resetCamera()
  107. {
  108. float dist = 13;
  109. float pitch = -13;
  110. float yaw = 50;
  111. float targetPos[3] = {-1, 0, -0.3};
  112. if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
  113. {
  114. m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
  115. m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
  116. m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
  117. m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
  118. }
  119. }
  120. };
  121. class CommonExampleInterface* RenderInstancingCreateFunc(struct CommonExampleOptions& options)
  122. {
  123. return new RenderInstancingDemo(options.m_guiHelper->getAppInterface());
  124. }
  125. #endif //RENDER_INSTANCING_DEMO_H