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