GpuCompoundScene.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include "GpuCompoundScene.h"
  2. #include "GpuRigidBodyDemo.h"
  3. #include "OpenGLWindow/ShapeData.h"
  4. #include "OpenGLWindow/GLInstancingRenderer.h"
  5. #include "Bullet3Common/b3Quaternion.h"
  6. #include "OpenGLWindow/b3gWindowInterface.h"
  7. #include "Bullet3OpenCL/BroadphaseCollision/b3GpuSapBroadphase.h"
  8. #include "../GpuDemoInternalData.h"
  9. #include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
  10. #include "OpenGLWindow/OpenGLInclude.h"
  11. #include "OpenGLWindow/GLInstanceRendererInternalData.h"
  12. #include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
  13. #include "Bullet3OpenCL/RigidBody/b3GpuRigidBodyPipeline.h"
  14. #include "Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.h"
  15. #include "Bullet3Collision/NarrowPhaseCollision/b3Config.h"
  16. #include "GpuRigidBodyDemoInternalData.h"
  17. #include "Bullet3Common/b3Transform.h"
  18. #include "OpenGLWindow/GLInstanceGraphicsShape.h"
  19. #define NUM_COMPOUND_CHILDREN_X 4
  20. #define NUM_COMPOUND_CHILDREN_Y 4
  21. #define NUM_COMPOUND_CHILDREN_Z 4
  22. void GpuCompoundScene::setupScene(const ConstructionInfo& ci)
  23. {
  24. createStaticEnvironment(ci);
  25. int strideInBytes = 9*sizeof(float);
  26. int numVertices = sizeof(cube_vertices)/strideInBytes;
  27. int numIndices = sizeof(cube_indices)/sizeof(int);
  28. float scaling[4] = {1,1,1,1};
  29. GLInstanceVertex* cubeVerts = (GLInstanceVertex*)&cube_vertices[0];
  30. int stride2 = sizeof(GLInstanceVertex);
  31. b3Assert(stride2 == strideInBytes);
  32. int index=0;
  33. int colIndex = -1;
  34. b3AlignedObjectArray<GLInstanceVertex> vertexArray;
  35. b3AlignedObjectArray<int> indexArray;
  36. {
  37. int childColIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  38. /* b3Vector3 childPositions[3] = {
  39. b3Vector3(0,-2,0),
  40. b3Vector3(0,0,0),
  41. b3Vector3(0,0,2)
  42. };
  43. */
  44. b3AlignedObjectArray<b3GpuChildShape> childShapes;
  45. for (int x=0;x<NUM_COMPOUND_CHILDREN_X;x++)
  46. for (int y=0;y<NUM_COMPOUND_CHILDREN_Y;y++)
  47. for (int z=0;z<NUM_COMPOUND_CHILDREN_Z;z++)
  48. {
  49. int blax = x!=0 ?1 : 0;
  50. int blay = y!=0 ?1 : 0;
  51. int blaz = z!=0 ?1 : 0;
  52. int bla=blax+blay+blaz;
  53. if (bla!=1)
  54. continue;
  55. //for now, only support polyhedral child shapes
  56. b3GpuChildShape child;
  57. child.m_shapeIndex = childColIndex;
  58. b3Vector3 pos=b3MakeVector3((x-NUM_COMPOUND_CHILDREN_X/2.f)*2,(y-NUM_COMPOUND_CHILDREN_X/2.f)*2,(z-NUM_COMPOUND_CHILDREN_X/2.f)*2);//childPositions[i];
  59. b3Quaternion orn(0,0,0,1);
  60. for (int v=0;v<4;v++)
  61. {
  62. child.m_childPosition[v] = pos[v];
  63. child.m_childOrientation[v] = orn[v];
  64. }
  65. childShapes.push_back(child);
  66. b3Transform tr;
  67. tr.setIdentity();
  68. tr.setOrigin(pos);
  69. tr.setRotation(orn);
  70. int baseIndex = vertexArray.size();
  71. for (int j=0;j<numIndices;j++)
  72. indexArray.push_back(cube_indices[j]+baseIndex);
  73. //add transformed graphics vertices and indices
  74. for (int v=0;v<numVertices;v++)
  75. {
  76. GLInstanceVertex vert = cubeVerts[v];
  77. b3Vector3 vertPos=b3MakeVector3(vert.xyzw[0],vert.xyzw[1],vert.xyzw[2]);
  78. b3Vector3 newPos = tr*vertPos;
  79. vert.xyzw[0] = newPos[0];
  80. vert.xyzw[1] = newPos[1];
  81. vert.xyzw[2] = newPos[2];
  82. vert.xyzw[3] = 0.f;
  83. vertexArray.push_back(vert);
  84. }
  85. }
  86. colIndex= m_data->m_np->registerCompoundShape(&childShapes);
  87. }
  88. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  89. int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size());
  90. b3Vector4 colors[4] =
  91. {
  92. b3MakeVector4(1,0,0,1),
  93. b3MakeVector4(0,1,0,1),
  94. b3MakeVector4(0,0,1,1),
  95. b3MakeVector4(0,1,1,1),
  96. };
  97. int curColor = 0;
  98. for (int i=0;i<ci.arraySizeX;i++)
  99. {
  100. for (int j=0;j<ci.arraySizeY;j++)
  101. {
  102. for (int k=0;k<ci.arraySizeZ;k++)
  103. {
  104. float mass = 1;//j==0? 0.f : 1.f;
  105. b3Vector3 position=b3MakeVector3((i-ci.arraySizeX/2.)*ci.gapX,35+j*3*ci.gapY,(k-ci.arraySizeZ/2.f)*ci.gapZ);
  106. //b3Quaternion orn(0,0,0,1);
  107. b3Quaternion orn(b3MakeVector3(1,0,0),0.7);
  108. b3Vector4 color = colors[curColor];
  109. curColor++;
  110. curColor&=3;
  111. b3Vector4 scaling=b3MakeVector4(1,1,1,1);
  112. int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  113. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
  114. index++;
  115. }
  116. }
  117. }
  118. m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
  119. float camPos[4]={0,0,0};//65.5,4.5,65.5,0};
  120. //float camPos[4]={1,12.5,1.5,0};
  121. m_instancingRenderer->setCameraTargetPosition(camPos);
  122. m_instancingRenderer->setCameraDistance(320);
  123. }
  124. void GpuCompoundScene::createStaticEnvironment(const ConstructionInfo& ci)
  125. {
  126. int strideInBytes = 9*sizeof(float);
  127. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  128. int group=1;
  129. int mask=1;
  130. int index=0;
  131. int colIndex = 0;
  132. {
  133. if (1)
  134. {
  135. float radius = 41;
  136. int prevGraphicsShapeIndex = -1;
  137. {
  138. if (radius>=100)
  139. {
  140. int numVertices = sizeof(detailed_sphere_vertices)/strideInBytes;
  141. int numIndices = sizeof(detailed_sphere_indices)/sizeof(int);
  142. prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&detailed_sphere_vertices[0],numVertices,detailed_sphere_indices,numIndices);
  143. } else
  144. {
  145. bool usePointSprites = false;
  146. if (usePointSprites)
  147. {
  148. int numVertices = sizeof(point_sphere_vertices)/strideInBytes;
  149. int numIndices = sizeof(point_sphere_indices)/sizeof(int);
  150. prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&point_sphere_vertices[0],numVertices,point_sphere_indices,numIndices,B3_GL_POINTS);
  151. } else
  152. {
  153. if (radius>=10)
  154. {
  155. int numVertices = sizeof(medium_sphere_vertices)/strideInBytes;
  156. int numIndices = sizeof(medium_sphere_indices)/sizeof(int);
  157. prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&medium_sphere_vertices[0],numVertices,medium_sphere_indices,numIndices);
  158. } else
  159. {
  160. int numVertices = sizeof(low_sphere_vertices)/strideInBytes;
  161. int numIndices = sizeof(low_sphere_indices)/sizeof(int);
  162. prevGraphicsShapeIndex = ci.m_instancingRenderer->registerShape(&low_sphere_vertices[0],numVertices,low_sphere_indices,numIndices);
  163. }
  164. }
  165. }
  166. }
  167. b3Vector4 colors[4] =
  168. {
  169. b3MakeVector4(1,0,0,1),
  170. b3MakeVector4(0,1,0,1),
  171. b3MakeVector4(0,1,1,1),
  172. b3MakeVector4(1,1,0,1),
  173. };
  174. int curColor = 1;
  175. //int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  176. int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  177. float mass = 0.f;
  178. //b3Vector3 position((j&1)+i*2.2,1+j*2.,(j&1)+k*2.2);
  179. b3Vector3 position=b3MakeVector3(0,-41,0);
  180. b3Quaternion orn(0,0,0,1);
  181. b3Vector4 color = colors[curColor];
  182. curColor++;
  183. curColor&=3;
  184. b3Vector4 scaling=b3MakeVector4(radius,radius,radius,1);
  185. int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
  186. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
  187. index++;
  188. }
  189. }
  190. }
  191. void GpuCompoundPlaneScene::createStaticEnvironment(const ConstructionInfo& ci)
  192. {
  193. int index=0;
  194. b3Vector3 normal=b3MakeVector3(0,1,0);
  195. float constant=0.f;
  196. int strideInBytes = 9*sizeof(float);
  197. int numVertices = sizeof(cube_vertices)/strideInBytes;
  198. int numIndices = sizeof(cube_indices)/sizeof(int);
  199. b3Vector4 scaling=b3MakeVector4(400,1.,400,1);
  200. //int colIndex = m_data->m_np->registerPlaneShape(normal,constant);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  201. int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  202. b3Vector3 position=b3MakeVector3(0,0,0);
  203. b3Quaternion orn(0,0,0,1);
  204. // b3Quaternion orn(b3Vector3(1,0,0),0.3);
  205. b3Vector4 color=b3MakeVector4(0,0,1,1);
  206. int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  207. int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  208. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false);
  209. }