1 
2 #include "RaytracerSetup.h"
3 
4 #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
5 #include "Bullet3Common/b3Quaternion.h"
6 #include "Bullet3Common/b3AlignedObjectArray.h"
7 #include "../CommonInterfaces/CommonRenderInterface.h"
8 
9 #include "../CommonInterfaces/Common2dCanvasInterface.h"
10 //#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
11 #include "BulletCollision/NarrowPhaseCollision/btSubSimplexConvexCast.h"
12 //#include "BulletCollision/NarrowPhaseCollision/btGjkConvexCast.h"
13 //#include "BulletCollision/NarrowPhaseCollision/btContinuousConvexCollision.h"
14 #include "../CommonInterfaces/CommonExampleInterface.h"
15 #include "LinearMath/btAlignedObjectArray.h"
16 #include "btBulletCollisionCommon.h"
17 #include "../CommonInterfaces/CommonGUIHelperInterface.h"
18 
19 struct RaytracerPhysicsSetup : public CommonExampleInterface
20 {
21 	struct CommonGraphicsApp* m_app;
22 	struct RaytracerInternalData* m_internalData;
23 
24 	RaytracerPhysicsSetup(struct CommonGraphicsApp* app);
25 
26 	virtual ~RaytracerPhysicsSetup();
27 
28 	virtual void initPhysics();
29 
30 	virtual void exitPhysics();
31 
32 	virtual void stepSimulation(float deltaTime);
33 
34 	virtual void physicsDebugDraw(int debugFlags);
35 
36 	virtual void syncPhysicsToGraphics(struct GraphicsPhysicsBridge& gfxBridge);
37 
38 	///worldRaytest performs a ray versus all objects in a collision world, returning true is a hit is found (filling in worldNormal and worldHitPoint)
39 	bool worldRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint);
40 
41 	///singleObjectRaytest performs a ray versus one collision shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
42 	bool singleObjectRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint);
43 
44 	///lowlevelRaytest performs a ray versus convex shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
45 	bool lowlevelRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint);
46 
47 	virtual bool mouseMoveCallback(float x, float y);
48 
49 	virtual bool mouseButtonCallback(int button, int state, float x, float y);
50 
51 	virtual bool keyboardCallback(int key, int state);
52 
renderSceneRaytracerPhysicsSetup53 	virtual void renderScene()
54 	{
55 	}
56 };
57 
58 struct RaytracerInternalData
59 {
60 	int m_canvasIndex;
61 	struct Common2dCanvasInterface* m_canvas;
62 
63 	int m_width;
64 	int m_height;
65 
66 	btAlignedObjectArray<btConvexShape*> m_shapePtr;
67 	btAlignedObjectArray<btTransform> m_transforms;
68 	btVoronoiSimplexSolver m_simplexSolver;
69 	btScalar m_pitch;
70 	btScalar m_roll;
71 	btScalar m_yaw;
72 
RaytracerInternalDataRaytracerInternalData73 	RaytracerInternalData()
74 		: m_canvasIndex(-1),
75 		  m_canvas(0),
76 #ifdef _DEBUG
77 		  m_width(64),
78 		  m_height(64),
79 #else
80 		  m_width(128),
81 		  m_height(128),
82 #endif
83 		  m_pitch(0),
84 		  m_roll(0),
85 		  m_yaw(0)
86 	{
87 		btConeShape* cone = new btConeShape(1, 1);
88 		btSphereShape* sphere = new btSphereShape(1);
89 		btBoxShape* box = new btBoxShape(btVector3(1, 1, 1));
90 		m_shapePtr.push_back(cone);
91 		m_shapePtr.push_back(sphere);
92 		m_shapePtr.push_back(box);
93 
94 		updateTransforms();
95 	}
updateTransformsRaytracerInternalData96 	void updateTransforms()
97 	{
98 		int numObjects = m_shapePtr.size();
99 		m_transforms.resize(numObjects);
100 		for (int i = 0; i < numObjects; i++)
101 		{
102 			m_transforms[i].setIdentity();
103 			btVector3 pos(0.f, 0.f, -(2.5 * numObjects * 0.5) + i * 2.5f);
104 			m_transforms[i].setIdentity();
105 			m_transforms[i].setOrigin(pos);
106 			btQuaternion orn;
107 			if (i < 2)
108 			{
109 				orn.setEuler(m_yaw, m_pitch, m_roll);
110 				m_transforms[i].setRotation(orn);
111 			}
112 		}
113 		m_pitch += 0.005f;
114 		m_yaw += 0.01f;
115 	}
116 };
117 
RaytracerPhysicsSetup(struct CommonGraphicsApp * app)118 RaytracerPhysicsSetup::RaytracerPhysicsSetup(struct CommonGraphicsApp* app)
119 {
120 	m_app = app;
121 	m_internalData = new RaytracerInternalData;
122 }
123 
~RaytracerPhysicsSetup()124 RaytracerPhysicsSetup::~RaytracerPhysicsSetup()
125 {
126 	delete m_internalData;
127 }
128 
initPhysics()129 void RaytracerPhysicsSetup::initPhysics()
130 {
131 	//request a visual bitma/texture we can render to
132 
133 	m_internalData->m_canvas = m_app->m_2dCanvasInterface;
134 
135 	if (m_internalData->m_canvas)
136 	{
137 		m_internalData->m_canvasIndex = m_internalData->m_canvas->createCanvas("raytracer", m_internalData->m_width, m_internalData->m_height, 15, 55);
138 		for (int i = 0; i < m_internalData->m_width; i++)
139 		{
140 			for (int j = 0; j < m_internalData->m_height; j++)
141 			{
142 				unsigned char red = 255;
143 				unsigned char green = 255;
144 				unsigned char blue = 255;
145 				unsigned char alpha = 255;
146 				m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex, i, j, red, green, blue, alpha);
147 			}
148 		}
149 		m_internalData->m_canvas->refreshImageData(m_internalData->m_canvasIndex);
150 
151 		//int bitmapId = gfxBridge.createRenderBitmap(width,height);
152 	}
153 }
154 
155 ///worldRaytest performs a ray versus all objects in a collision world, returning true is a hit is found (filling in worldNormal and worldHitPoint)
worldRaytest(const btVector3 & rayFrom,const btVector3 & rayTo,btVector3 & worldNormal,btVector3 & worldHitPoint)156 bool RaytracerPhysicsSetup::worldRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint)
157 {
158 	return false;
159 }
160 
161 ///singleObjectRaytest performs a ray versus one collision shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
singleObjectRaytest(const btVector3 & rayFrom,const btVector3 & rayTo,btVector3 & worldNormal,btVector3 & worldHitPoint)162 bool RaytracerPhysicsSetup::singleObjectRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint)
163 {
164 	return false;
165 }
166 
167 ///lowlevelRaytest performs a ray versus convex shape, returning true is a hit is found (filling in worldNormal and worldHitPoint)
lowlevelRaytest(const btVector3 & rayFrom,const btVector3 & rayTo,btVector3 & worldNormal,btVector3 & worldHitPoint)168 bool RaytracerPhysicsSetup::lowlevelRaytest(const btVector3& rayFrom, const btVector3& rayTo, btVector3& worldNormal, btVector3& worldHitPoint)
169 {
170 	btScalar closestHitResults = 1.f;
171 
172 	bool hasHit = false;
173 	btConvexCast::CastResult rayResult;
174 	btSphereShape pointShape(0.0f);
175 	btTransform rayFromTrans;
176 	btTransform rayToTrans;
177 
178 	rayFromTrans.setIdentity();
179 	rayFromTrans.setOrigin(rayFrom);
180 	rayToTrans.setIdentity();
181 	rayToTrans.setOrigin(rayTo);
182 
183 	int numObjects = m_internalData->m_shapePtr.size();
184 
185 	for (int s = 0; s < numObjects; s++)
186 	{
187 		//do some culling, ray versus aabb
188 		btVector3 aabbMin, aabbMax;
189 		m_internalData->m_shapePtr[s]->getAabb(m_internalData->m_transforms[s], aabbMin, aabbMax);
190 		btScalar hitLambda = 1.f;
191 		btVector3 hitNormal;
192 		btCollisionObject tmpObj;
193 		tmpObj.setWorldTransform(m_internalData->m_transforms[s]);
194 
195 		if (btRayAabb(rayFrom, rayTo, aabbMin, aabbMax, hitLambda, hitNormal))
196 		{
197 			//reset previous result
198 
199 			//choose the continuous collision detection method
200 			btSubsimplexConvexCast convexCaster(&pointShape, m_internalData->m_shapePtr[s], &m_internalData->m_simplexSolver);
201 			//btGjkConvexCast convexCaster(&pointShape,shapePtr[s],&simplexSolver);
202 			//btContinuousConvexCollision convexCaster(&pointShape,shapePtr[s],&simplexSolver,0);
203 
204 			if (convexCaster.calcTimeOfImpact(rayFromTrans, rayToTrans, m_internalData->m_transforms[s], m_internalData->m_transforms[s], rayResult))
205 			{
206 				if (rayResult.m_fraction < closestHitResults)
207 				{
208 					closestHitResults = rayResult.m_fraction;
209 
210 					worldNormal = m_internalData->m_transforms[s].getBasis() * rayResult.m_normal;
211 					worldNormal.normalize();
212 					hasHit = true;
213 				}
214 			}
215 		}
216 	}
217 
218 	return hasHit;
219 }
220 
exitPhysics()221 void RaytracerPhysicsSetup::exitPhysics()
222 {
223 	if (m_internalData->m_canvas && m_internalData->m_canvasIndex >= 0)
224 	{
225 		m_internalData->m_canvas->destroyCanvas(m_internalData->m_canvasIndex);
226 	}
227 }
228 
stepSimulation(float deltaTime)229 void RaytracerPhysicsSetup::stepSimulation(float deltaTime)
230 {
231 	m_internalData->updateTransforms();
232 
233 	float top = 1.f;
234 	float bottom = -1.f;
235 	float nearPlane = 1.f;
236 
237 	float tanFov = (top - bottom) * 0.5f / nearPlane;
238 
239 	float fov = 2.0 * atanf(tanFov);
240 
241 	btVector3 cameraPosition(5, 0, 0);
242 	btVector3 cameraTargetPosition(0, 0, 0);
243 
244 	if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
245 	{
246 		m_app->m_renderer->getActiveCamera()->getCameraPosition(cameraPosition);
247 		m_app->m_renderer->getActiveCamera()->getCameraTargetPosition(cameraTargetPosition);
248 	}
249 
250 	btVector3 rayFrom = cameraPosition;
251 	btVector3 rayForward = cameraTargetPosition - cameraPosition;
252 
253 	rayForward.normalize();
254 	float farPlane = 600.f;
255 	rayForward *= farPlane;
256 
257 	btVector3 rightOffset;
258 	btVector3 vertical(0.f, 1.f, 0.f);
259 	btVector3 hor;
260 	hor = rayForward.cross(vertical);
261 	hor.normalize();
262 	vertical = hor.cross(rayForward);
263 	vertical.normalize();
264 
265 	float tanfov = tanf(0.5f * fov);
266 
267 	hor *= 2.f * farPlane * tanfov;
268 	vertical *= 2.f * farPlane * tanfov;
269 
270 	btVector3 rayToCenter = rayFrom + rayForward;
271 
272 	btVector3 dHor = hor * 1.f / float(m_internalData->m_width);
273 	btVector3 dVert = vertical * 1.f / float(m_internalData->m_height);
274 
275 	//	int	mode = 0;
276 	int x, y;
277 
278 	for (x = 0; x < m_internalData->m_width; x++)
279 	{
280 		for (y = 0; y < m_internalData->m_height; y++)
281 		{
282 			btVector4 rgba(0, 0, 0, 0);
283 			btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
284 			rayTo += x * dHor;
285 			rayTo -= y * dVert;
286 			btVector3 worldNormal(0, 0, 0);
287 			btVector3 worldPoint(0, 0, 0);
288 
289 			bool hasHit = false;
290 			int mode = 0;
291 			switch (mode)
292 			{
293 				case 0:
294 					hasHit = lowlevelRaytest(rayFrom, rayTo, worldNormal, worldPoint);
295 					break;
296 				case 1:
297 					hasHit = singleObjectRaytest(rayFrom, rayTo, worldNormal, worldPoint);
298 					break;
299 				case 2:
300 					hasHit = worldRaytest(rayFrom, rayTo, worldNormal, worldPoint);
301 					break;
302 				default:
303 				{
304 				}
305 			}
306 
307 			if (hasHit)
308 			{
309 				float lightVec0 = worldNormal.dot(btVector3(0, -1, -1));  //0.4f,-1.f,-0.4f));
310 				float lightVec1 = worldNormal.dot(btVector3(-1, 0, -1));  //-0.4f,-1.f,-0.4f));
311 
312 				rgba = btVector4(lightVec0, lightVec1, 0, 1.f);
313 				rgba.setMin(btVector3(1, 1, 1));
314 				rgba.setMax(btVector3(0.2, 0.2, 0.2));
315 				rgba[3] = 1.f;
316 				unsigned char red = rgba[0] * 255;
317 				unsigned char green = rgba[1] * 255;
318 				unsigned char blue = rgba[2] * 255;
319 				unsigned char alpha = 255;
320 				m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex, x, y, red, green, blue, alpha);
321 			}
322 			else
323 			{
324 				//	btVector4 rgba = raytracePicture->getPixel(x,y);
325 			}
326 			if (!rgba.length2())
327 			{
328 				m_internalData->m_canvas->setPixel(m_internalData->m_canvasIndex, x, y, 255, 0, 0, 255);
329 			}
330 		}
331 	}
332 	m_internalData->m_canvas->refreshImageData(m_internalData->m_canvasIndex);
333 }
334 
physicsDebugDraw(int debugDrawFlags)335 void RaytracerPhysicsSetup::physicsDebugDraw(int debugDrawFlags)
336 {
337 }
338 
mouseMoveCallback(float x,float y)339 bool RaytracerPhysicsSetup::mouseMoveCallback(float x, float y)
340 {
341 	return false;
342 }
343 
mouseButtonCallback(int button,int state,float x,float y)344 bool RaytracerPhysicsSetup::mouseButtonCallback(int button, int state, float x, float y)
345 {
346 	return false;
347 }
348 
keyboardCallback(int key,int state)349 bool RaytracerPhysicsSetup::keyboardCallback(int key, int state)
350 {
351 	return false;
352 }
353 
syncPhysicsToGraphics(GraphicsPhysicsBridge & gfxBridge)354 void RaytracerPhysicsSetup::syncPhysicsToGraphics(GraphicsPhysicsBridge& gfxBridge)
355 {
356 }
357 
RayTracerCreateFunc(struct CommonExampleOptions & options)358 CommonExampleInterface* RayTracerCreateFunc(struct CommonExampleOptions& options)
359 {
360 	return new RaytracerPhysicsSetup(options.m_guiHelper->getAppInterface());
361 }
362