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