ConcaveScene.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. #include "ConcaveScene.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"../../Wavefront/tiny_obj_loader.h"
  18. #include "Bullet3Common/b3Transform.h"
  19. #include "Bullet3Collision/NarrowPhaseCollision/b3ConvexUtility.h"
  20. #include "Bullet3AppSupport/gwenUserInterface.h"
  21. #include "OpenGLWindow/GLInstanceGraphicsShape.h"
  22. #define CONCAVE_GAPX 14
  23. #define CONCAVE_GAPY 5
  24. #define CONCAVE_GAPZ 14
  25. GLInstanceGraphicsShape* createGraphicsShapeFromWavefrontObj(std::vector<tinyobj::shape_t>& shapes)
  26. {
  27. b3AlignedObjectArray<GLInstanceVertex>* vertices = new b3AlignedObjectArray<GLInstanceVertex>;
  28. {
  29. // int numVertices = obj->vertexCount;
  30. // int numIndices = 0;
  31. b3AlignedObjectArray<int>* indicesPtr = new b3AlignedObjectArray<int>;
  32. for (int s=0;s<shapes.size();s++)
  33. {
  34. tinyobj::shape_t& shape = shapes[s];
  35. int faceCount = shape.mesh.indices.size();
  36. for (int f=0;f<faceCount;f+=3)
  37. {
  38. //b3Vector3 normal(face.m_plane[0],face.m_plane[1],face.m_plane[2]);
  39. if (1)
  40. {
  41. b3Vector3 normal=b3MakeVector3(0,1,0);
  42. int vtxBaseIndex = vertices->size();
  43. indicesPtr->push_back(vtxBaseIndex);
  44. indicesPtr->push_back(vtxBaseIndex+1);
  45. indicesPtr->push_back(vtxBaseIndex+2);
  46. GLInstanceVertex vtx0;
  47. vtx0.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f]*3+0];
  48. vtx0.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f]*3+1];
  49. vtx0.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f]*3+2];
  50. vtx0.xyzw[3] = 0.f;
  51. vtx0.uv[0] = 0.5f;//shape.mesh.positions[shape.mesh.indices[f]*3+2];?
  52. vtx0.uv[1] = 0.5f;
  53. GLInstanceVertex vtx1;
  54. vtx1.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f+1]*3+0];
  55. vtx1.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f+1]*3+1];
  56. vtx1.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f+1]*3+2];
  57. vtx1.xyzw[3]= 0.f;
  58. vtx1.uv[0] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[0];
  59. vtx1.uv[1] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[1];
  60. GLInstanceVertex vtx2;
  61. vtx2.xyzw[0] = shape.mesh.positions[shape.mesh.indices[f+2]*3+0];
  62. vtx2.xyzw[1] = shape.mesh.positions[shape.mesh.indices[f+2]*3+1];
  63. vtx2.xyzw[2] = shape.mesh.positions[shape.mesh.indices[f+2]*3+2];
  64. vtx2.xyzw[3] = 0.f;
  65. vtx2.uv[0] = 0.5f;
  66. vtx2.uv[1] = 0.5f;
  67. b3Vector3 v0=b3MakeVector3(vtx0.xyzw[0],vtx0.xyzw[1],vtx0.xyzw[2]);
  68. b3Vector3 v1=b3MakeVector3(vtx1.xyzw[0],vtx1.xyzw[1],vtx1.xyzw[2]);
  69. b3Vector3 v2=b3MakeVector3(vtx2.xyzw[0],vtx2.xyzw[1],vtx2.xyzw[2]);
  70. normal = (v1-v0).cross(v2-v0);
  71. normal.normalize();
  72. vtx0.normal[0] = normal[0];
  73. vtx0.normal[1] = normal[1];
  74. vtx0.normal[2] = normal[2];
  75. vtx1.normal[0] = normal[0];
  76. vtx1.normal[1] = normal[1];
  77. vtx1.normal[2] = normal[2];
  78. vtx2.normal[0] = normal[0];
  79. vtx2.normal[1] = normal[1];
  80. vtx2.normal[2] = normal[2];
  81. vertices->push_back(vtx0);
  82. vertices->push_back(vtx1);
  83. vertices->push_back(vtx2);
  84. }
  85. }
  86. }
  87. GLInstanceGraphicsShape* gfxShape = new GLInstanceGraphicsShape;
  88. gfxShape->m_vertices = vertices;
  89. gfxShape->m_numvertices = vertices->size();
  90. gfxShape->m_indices = indicesPtr;
  91. gfxShape->m_numIndices = indicesPtr->size();
  92. for (int i=0;i<4;i++)
  93. gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices
  94. return gfxShape;
  95. }
  96. }
  97. void ConcaveScene::createConcaveMesh(const ConstructionInfo& ci, const char* fileName, const b3Vector3& shift, const b3Vector3& scaling)
  98. {
  99. char relativeFileName[1024];
  100. const char* prefix[]={"./data/","../data/","../../data/","../../../data/","../../../../data/"};
  101. int prefixIndex=-1;
  102. {
  103. int numPrefixes = sizeof(prefix)/sizeof(char*);
  104. for (int i=0;i<numPrefixes;i++)
  105. {
  106. FILE* f = 0;
  107. sprintf(relativeFileName,"%s%s",prefix[i],fileName);
  108. f = fopen(relativeFileName,"r");
  109. if (f)
  110. {
  111. fclose(f);
  112. prefixIndex = i;
  113. break;
  114. }
  115. }
  116. }
  117. if (prefixIndex<0)
  118. return;
  119. int index=10;
  120. {
  121. std::vector<tinyobj::shape_t> shapes;
  122. std::string err = tinyobj::LoadObj(shapes, relativeFileName, prefix[prefixIndex]);
  123. GLInstanceGraphicsShape* shape = createGraphicsShapeFromWavefrontObj(shapes);
  124. b3AlignedObjectArray<b3Vector3> verts;
  125. for (int i=0;i<shape->m_numvertices;i++)
  126. {
  127. for (int j=0;j<3;j++)
  128. shape->m_vertices->at(i).xyzw[j] += shift[j];
  129. b3Vector3 vtx=b3MakeVector3(shape->m_vertices->at(i).xyzw[0],
  130. shape->m_vertices->at(i).xyzw[1],
  131. shape->m_vertices->at(i).xyzw[2]);
  132. verts.push_back(vtx*scaling);
  133. }
  134. int colIndex = m_data->m_np->registerConcaveMesh(&verts,shape->m_indices,b3MakeVector3(1,1,1));
  135. {
  136. int strideInBytes = 9*sizeof(float);
  137. int numVertices = sizeof(cube_vertices)/strideInBytes;
  138. int numIndices = sizeof(cube_indices)/sizeof(int);
  139. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  140. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  141. int shapeId = ci.m_instancingRenderer->registerShape(&shape->m_vertices->at(0).xyzw[0], shape->m_numvertices, &shape->m_indices->at(0), shape->m_numIndices);
  142. b3Quaternion orn(0,0,0,1);
  143. b3Vector4 color=b3MakeVector4(0.3,0.3,1,1.f);//0.5);//1.f
  144. {
  145. float mass = 0.f;
  146. b3Vector3 position=b3MakeVector3(0,0,0);
  147. int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  148. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
  149. index++;
  150. }
  151. delete shape->m_indices;
  152. delete shape->m_vertices;
  153. delete shape;
  154. }
  155. }
  156. }
  157. void ConcaveScene::setupScene(const ConstructionInfo& ci)
  158. {
  159. if (1)
  160. {
  161. //char* fileName = "slopedPlane100.obj";
  162. //char* fileName = "plane100.obj";
  163. // char* fileName = "plane100.obj";
  164. //char* fileName = "teddy.obj";//"plane.obj";
  165. // char* fileName = "sponza_closed.obj";//"plane.obj";
  166. //char* fileName = "leoTest1.obj";
  167. const char* fileName = "samurai_monastry.obj";
  168. // char* fileName = "teddy2_VHACD_CHs.obj";
  169. b3Vector3 shift1=b3MakeVector3(0,0,0);//0,230,80);//150,-100,-120);
  170. b3Vector4 scaling=b3MakeVector4(10,10,10,1);
  171. // createConcaveMesh(ci,"plane100.obj",shift1,scaling);
  172. //createConcaveMesh(ci,"plane100.obj",shift,scaling);
  173. // b3Vector3 shift2(0,0,0);//0,230,80);//150,-100,-120);
  174. // createConcaveMesh(ci,"teddy.obj",shift2,scaling);
  175. // b3Vector3 shift3(130,-150,-75);//0,230,80);//150,-100,-120);
  176. // createConcaveMesh(ci,"leoTest1.obj",shift3,scaling);
  177. createConcaveMesh(ci,fileName,shift1,scaling);
  178. } else
  179. {
  180. int strideInBytes = 9*sizeof(float);
  181. int numVertices = sizeof(cube_vertices)/strideInBytes;
  182. int numIndices = sizeof(cube_indices)/sizeof(int);
  183. int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  184. int group=1;
  185. int mask=1;
  186. int index=0;
  187. {
  188. b3Vector4 scaling=b3MakeVector4(400,1.,400,1);
  189. int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  190. b3Vector3 position=b3MakeVector3(0,-2,0);
  191. b3Quaternion orn(0,0,0,1);
  192. b3Vector4 color=b3MakeVector4(0,0,1,1);
  193. int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  194. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(0.f,position,orn,colIndex,index,false);
  195. }
  196. }
  197. createDynamicObjects(ci);
  198. m_data->m_rigidBodyPipeline->writeAllInstancesToGpu();
  199. float camPos[4]={0,0,0,0};//65.5,4.5,65.5,0};
  200. //float camPos[4]={1,12.5,1.5,0};
  201. m_instancingRenderer->setCameraPitch(45);
  202. m_instancingRenderer->setCameraTargetPosition(camPos);
  203. m_instancingRenderer->setCameraDistance(355);
  204. char msg[1024];
  205. int numInstances = m_data->m_rigidBodyPipeline->getNumBodies();
  206. sprintf(msg,"Num objects = %d",numInstances);
  207. if (ci.m_gui)
  208. ci.m_gui->setStatusBarMessage(msg,true);
  209. }
  210. void ConcaveScene::createDynamicObjects(const ConstructionInfo& ci)
  211. {
  212. int strideInBytes = 9*sizeof(float);
  213. int numVertices = sizeof(cube_vertices)/strideInBytes;
  214. int numIndices = sizeof(cube_indices)/sizeof(int);
  215. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  216. int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  217. int group=1;
  218. int mask=1;
  219. int index=0;
  220. if (1)
  221. {
  222. int curColor = 0;
  223. b3Vector4 colors[4] =
  224. {
  225. b3MakeVector4(1,1,1,1),
  226. b3MakeVector4(1,1,0.3,1),
  227. b3MakeVector4(0.3,1,1,1),
  228. b3MakeVector4(0.3,0.3,1,1),
  229. };
  230. b3ConvexUtility* utilPtr = new b3ConvexUtility();
  231. b3Vector4 scaling=b3MakeVector4(1,1,1,1);
  232. {
  233. b3AlignedObjectArray<b3Vector3> verts;
  234. unsigned char* vts = (unsigned char*) cube_vertices;
  235. for (int i=0;i<numVertices;i++)
  236. {
  237. float* vertex = (float*) &vts[i*strideInBytes];
  238. verts.push_back(b3MakeVector3(vertex[0]*scaling[0],vertex[1]*scaling[1],vertex[2]*scaling[2]));
  239. }
  240. bool merge = true;
  241. if (numVertices)
  242. {
  243. utilPtr->initializePolyhedralFeatures(&verts[0],verts.size(),merge);
  244. }
  245. }
  246. // int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  247. int colIndex=-1;
  248. if (ci.m_useInstancedCollisionShapes)
  249. colIndex = m_data->m_np->registerConvexHullShape(utilPtr);
  250. for (int i=0;i<ci.arraySizeX;i++)
  251. {
  252. for (int j=0;j<ci.arraySizeY;j++)
  253. {
  254. for (int k=0;k<ci.arraySizeZ;k++)
  255. {
  256. if (!ci.m_useInstancedCollisionShapes)
  257. colIndex = m_data->m_np->registerConvexHullShape(utilPtr);
  258. float mass = 1;
  259. //b3Vector3 position(-2*ci.gapX+i*ci.gapX,25+j*ci.gapY,-2*ci.gapZ+k*ci.gapZ);
  260. b3Vector3 position=b3MakeVector3(-(ci.arraySizeX/2)*CONCAVE_GAPX+i*CONCAVE_GAPX,
  261. 23+j*CONCAVE_GAPY,
  262. -(ci.arraySizeZ/2)*CONCAVE_GAPZ+k*CONCAVE_GAPZ);
  263. b3Quaternion orn(0,0,0,1);
  264. b3Vector4 color = colors[curColor];
  265. curColor++;
  266. curColor&=3;
  267. int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  268. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
  269. index++;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. void ConcaveCompoundScene::setupScene(const ConstructionInfo& ci)
  276. {
  277. ConcaveScene::setupScene(ci);
  278. float camPos[4]={0,50,0,0};//65.5,4.5,65.5,0};
  279. //float camPos[4]={1,12.5,1.5,0};
  280. m_instancingRenderer->setCameraPitch(45);
  281. m_instancingRenderer->setCameraTargetPosition(camPos);
  282. m_instancingRenderer->setCameraDistance(40);
  283. }
  284. void ConcaveCompound2Scene::createDynamicObjects(const ConstructionInfo& ci)
  285. {
  286. const char* fileName = "teddy2_VHACD_CHs.obj";
  287. //char* fileName = "cube_offset.obj";
  288. b3Vector3 shift=b3MakeVector3(0,0,0);//0,230,80);//150,-100,-120);
  289. b3Vector4 scaling=b3MakeVector4(1,1,1,1);
  290. const char* prefix[]={"./data/","../data/","../../data/","../../../data/","../../../../data/"};
  291. int prefixIndex=-1;
  292. char relativeFileName[1024];
  293. {
  294. int numPrefixes = sizeof(prefix)/sizeof(char*);
  295. for (int i=0;i<numPrefixes;i++)
  296. {
  297. sprintf(relativeFileName,"%s%s",prefix[i],fileName);
  298. FILE* f = 0;
  299. f = fopen(relativeFileName,"r");
  300. if (f)
  301. {
  302. prefixIndex = i;
  303. fclose(f);
  304. break;
  305. }
  306. }
  307. }
  308. if (prefixIndex<0)
  309. return;
  310. std::vector<tinyobj::shape_t> shapes;
  311. std::string err = tinyobj::LoadObj(shapes, relativeFileName, prefix[prefixIndex]);
  312. if (shapes.size()>0)
  313. {
  314. int strideInBytes = 9*sizeof(float);
  315. b3AlignedObjectArray<GLInstanceVertex> vertexArray;
  316. b3AlignedObjectArray<int> indexArray;
  317. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  318. int group=1;
  319. int mask=1;
  320. int index=0;
  321. int colIndex = 0;
  322. b3AlignedObjectArray<GLInstanceVertex> vertices;
  323. int stride2 = sizeof(GLInstanceVertex);
  324. b3Assert(stride2 == strideInBytes);
  325. {
  326. b3AlignedObjectArray<b3GpuChildShape> childShapes;
  327. int numChildShapes = shapes.size();
  328. for (int i=0;i<numChildShapes;i++)
  329. // int i=4;
  330. {
  331. tinyobj::shape_t& shape = shapes[i];
  332. int numVertices = shape.mesh.positions.size()/3;
  333. int numFaces = shape.mesh.indices.size()/3;
  334. //for now, only support polyhedral child shapes
  335. b3GpuChildShape child;
  336. b3Vector3 pos=b3MakeVector3(0,0,0);
  337. b3Quaternion orn(0,0,0,1);
  338. for (int v=0;v<4;v++)
  339. {
  340. child.m_childPosition[v] = pos[v];
  341. child.m_childOrientation[v] = orn[v];
  342. }
  343. b3Transform tr;
  344. tr.setIdentity();
  345. tr.setOrigin(pos);
  346. tr.setRotation(orn);
  347. int baseIndex = vertexArray.size();
  348. for (int f=0;f<numFaces;f++)
  349. {
  350. for (int i=0;i<3;i++)
  351. {
  352. indexArray.push_back(baseIndex+shape.mesh.indices[f*3+i]);
  353. }
  354. }
  355. b3Vector3 center=b3MakeVector3(0,0,0);
  356. b3AlignedObjectArray<GLInstanceVertex> tmpVertices;
  357. //add transformed graphics vertices and indices
  358. b3Vector3 myScaling=b3MakeVector3(50,50,50);//300,300,300);
  359. for (int v=0;v<numVertices;v++)
  360. {
  361. GLInstanceVertex vert;
  362. vert.uv[0] = 0.5f;
  363. vert.uv[1] = 0.5f;
  364. vert.normal[0]=0.f;
  365. vert.normal[1]=1.f;
  366. vert.normal[2]=0.f;
  367. b3Vector3 vertPos;
  368. vertPos[0] = shape.mesh.positions[v*3+0]*myScaling[0];
  369. vertPos[1] = shape.mesh.positions[v*3+1]*myScaling[1];
  370. vertPos[2] = shape.mesh.positions[v*3+2]*myScaling[2];
  371. vertPos[3] =0.f;
  372. center+=vertPos;
  373. }
  374. center/=numVertices;
  375. for (int v=0;v<numVertices;v++)
  376. {
  377. GLInstanceVertex vert;
  378. vert.uv[0] = 0.5f;
  379. vert.uv[1] = 0.5f;
  380. vert.normal[0]=0.f;
  381. vert.normal[1]=1.f;
  382. vert.normal[2]=0.f;
  383. b3Vector3 vertPos;
  384. vertPos[0] = shape.mesh.positions[v*3+0]*myScaling[0];
  385. vertPos[1] = shape.mesh.positions[v*3+1]*myScaling[1];
  386. vertPos[2] = shape.mesh.positions[v*3+2]*myScaling[2];
  387. vertPos[3] =0.f;
  388. // vertPos-=center;
  389. vert.xyzw[0] = vertPos[0];
  390. vert.xyzw[1] = vertPos[1];
  391. vert.xyzw[2] = vertPos[2];
  392. tmpVertices.push_back(vert);
  393. b3Vector3 newPos = tr*vertPos;
  394. vert.xyzw[0] = newPos[0];
  395. vert.xyzw[1] = newPos[1];
  396. vert.xyzw[2] = newPos[2];
  397. vert.xyzw[3] = 0.f;
  398. vertexArray.push_back(vert);
  399. }
  400. int childColIndex = m_data->m_np->registerConvexHullShape(&tmpVertices[0].xyzw[0],strideInBytes,numVertices, scaling);
  401. child.m_shapeIndex = childColIndex;
  402. childShapes.push_back(child);
  403. colIndex = childColIndex;
  404. }
  405. colIndex= m_data->m_np->registerCompoundShape(&childShapes);
  406. }
  407. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  408. int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size());
  409. b3Vector4 colors[4] =
  410. {
  411. b3MakeVector4(1,0,0,1),
  412. b3MakeVector4(0,1,0,1),
  413. b3MakeVector4(0,0,1,1),
  414. b3MakeVector4(0,1,1,1),
  415. };
  416. int curColor = 0;
  417. for (int i=0;i<1;i++)//ci.arraySizeX;i++)
  418. {
  419. for (int j=0;j<4;j++)
  420. {
  421. // for (int k=0;k<ci.arraySizeZ;k++)
  422. int k=0;
  423. {
  424. float mass = 1;//j==0? 0.f : 1.f;
  425. //b3Vector3 position(i*10*ci.gapX,j*ci.gapY,k*10*ci.gapZ);
  426. b3Vector3 position=b3MakeVector3(i*10*ci.gapX,10+j*10*ci.gapY,k*10*ci.gapZ);
  427. // b3Quaternion orn(0,0,0,1);
  428. b3Quaternion orn(b3MakeVector3(0,0,1),1.8);
  429. b3Vector4 color = colors[curColor];
  430. curColor++;
  431. curColor&=3;
  432. b3Vector4 scaling=b3MakeVector4(1,1,1,1);
  433. int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  434. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
  435. index++;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. void ConcaveCompoundScene::createDynamicObjects(const ConstructionInfo& ci)
  442. {
  443. int strideInBytes = 9*sizeof(float);
  444. int numVertices = sizeof(cube_vertices)/strideInBytes;
  445. int numIndices = sizeof(cube_indices)/sizeof(int);
  446. b3AlignedObjectArray<GLInstanceVertex> vertexArray;
  447. b3AlignedObjectArray<int> indexArray;
  448. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  449. int group=1;
  450. int mask=1;
  451. int index=0;
  452. float scaling[4] = {1,1,1,1};
  453. int colIndex = 0;
  454. GLInstanceVertex* cubeVerts = (GLInstanceVertex*)&cube_vertices[0];
  455. int stride2 = sizeof(GLInstanceVertex);
  456. b3Assert(stride2 == strideInBytes);
  457. {
  458. int childColIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  459. b3Vector3 childPositions[3] = {
  460. b3MakeVector3(0,-2,0),
  461. b3MakeVector3(0,0,0),
  462. b3MakeVector3(0,0,2)
  463. };
  464. b3AlignedObjectArray<b3GpuChildShape> childShapes;
  465. int numChildShapes = 3;
  466. for (int i=0;i<numChildShapes;i++)
  467. {
  468. //for now, only support polyhedral child shapes
  469. b3GpuChildShape child;
  470. child.m_shapeIndex = childColIndex;
  471. b3Vector3 pos = childPositions[i];
  472. b3Quaternion orn(0,0,0,1);
  473. for (int v=0;v<4;v++)
  474. {
  475. child.m_childPosition[v] = pos[v];
  476. child.m_childOrientation[v] = orn[v];
  477. }
  478. childShapes.push_back(child);
  479. b3Transform tr;
  480. tr.setIdentity();
  481. tr.setOrigin(pos);
  482. tr.setRotation(orn);
  483. int baseIndex = vertexArray.size();
  484. for (int j=0;j<numIndices;j++)
  485. indexArray.push_back(cube_indices[j]+baseIndex);
  486. //add transformed graphics vertices and indices
  487. for (int v=0;v<numVertices;v++)
  488. {
  489. GLInstanceVertex vert = cubeVerts[v];
  490. b3Vector3 vertPos=b3MakeVector3(vert.xyzw[0],vert.xyzw[1],vert.xyzw[2]);
  491. b3Vector3 newPos = tr*vertPos;
  492. vert.xyzw[0] = newPos[0];
  493. vert.xyzw[1] = newPos[1];
  494. vert.xyzw[2] = newPos[2];
  495. vert.xyzw[3] = 0.f;
  496. vertexArray.push_back(vert);
  497. }
  498. }
  499. colIndex= m_data->m_np->registerCompoundShape(&childShapes);
  500. }
  501. //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices);
  502. int shapeId = ci.m_instancingRenderer->registerShape(&vertexArray[0].xyzw[0],vertexArray.size(),&indexArray[0],indexArray.size());
  503. b3Vector4 colors[4] =
  504. {
  505. b3MakeVector4(1,0,0,1),
  506. b3MakeVector4(0,1,0,1),
  507. b3MakeVector4(0,0,1,1),
  508. b3MakeVector4(0,1,1,1),
  509. };
  510. int curColor = 0;
  511. for (int i=0;i<ci.arraySizeX;i++)
  512. {
  513. for (int j=0;j<ci.arraySizeY;j++)
  514. {
  515. for (int k=0;k<ci.arraySizeZ;k++)
  516. {
  517. float mass = 1;//j==0? 0.f : 1.f;
  518. b3Vector3 position=b3MakeVector3((-ci.arraySizeX/2+i)*ci.gapX,50+j*ci.gapY,(-ci.arraySizeZ/2+k)*ci.gapZ);
  519. //b3Quaternion orn(0,0,0,1);
  520. b3Quaternion orn(b3MakeVector3(1,0,0),0.7);
  521. b3Vector4 color = colors[curColor];
  522. curColor++;
  523. curColor&=3;
  524. b3Vector4 scaling=b3MakeVector4(1,1,1,1);
  525. int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling);
  526. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
  527. index++;
  528. }
  529. }
  530. }
  531. }
  532. void ConcaveSphereScene::setupScene(const ConstructionInfo& ci)
  533. {
  534. ConcaveScene::setupScene(ci);
  535. float camPos[4]={0,50,0,0};//65.5,4.5,65.5,0};
  536. //float camPos[4]={1,12.5,1.5,0};
  537. m_instancingRenderer->setCameraPitch(45);
  538. m_instancingRenderer->setCameraTargetPosition(camPos);
  539. m_instancingRenderer->setCameraDistance(40);
  540. }
  541. void ConcaveSphereScene::createDynamicObjects(const ConstructionInfo& ci)
  542. {
  543. b3Vector4 colors[4] =
  544. {
  545. b3MakeVector4(1,0,0,1),
  546. b3MakeVector4(0,1,0,1),
  547. b3MakeVector4(0,1,1,1),
  548. b3MakeVector4(1,1,0,1),
  549. };
  550. int index=0;
  551. int curColor = 0;
  552. float radius = 1;
  553. //int colIndex = m_data->m_np->registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  554. int colIndex = m_data->m_np->registerSphereShape(radius);//>registerConvexHullShape(&cube_vertices[0],strideInBytes,numVertices, scaling);
  555. int prevGraphicsShapeIndex = registerGraphicsSphereShape(ci,radius,false);
  556. for (int i=0;i<ci.arraySizeX;i++)
  557. {
  558. for (int j=0;j<ci.arraySizeY;j++)
  559. {
  560. for (int k=0;k<ci.arraySizeZ;k++)
  561. {
  562. float mass = 1.f;
  563. b3Vector3 position=b3MakeVector3(-(ci.arraySizeX/2)*8+i*8,50+j*8,-(ci.arraySizeZ/2)*8+k*8);
  564. //b3Vector3 position(0,-41,0);//0,0,0);//i*radius*3,-41+j*radius*3,k*radius*3);
  565. b3Quaternion orn(0,0,0,1);
  566. b3Vector4 color = colors[curColor];
  567. curColor++;
  568. curColor&=3;
  569. b3Vector4 scaling=b3MakeVector4(radius,radius,radius,1);
  570. int id = ci.m_instancingRenderer->registerGraphicsInstance(prevGraphicsShapeIndex,position,orn,color,scaling);
  571. int pid = m_data->m_rigidBodyPipeline->registerPhysicsInstance(mass,position,orn,colIndex,index,false);
  572. index++;
  573. }
  574. }
  575. }
  576. }