2
* Copyright (c) 2005 Erwin Coumans <www.erwincoumans.com>
4
* Permission to use, copy, modify, distribute and sell this software
5
* and its documentation for any purpose is hereby granted without fee,
6
* provided that the above copyright notice appear in all copies.
7
* Erwin Coumans makes no representations about the suitability
8
* of this software for any purpose.
9
* It is provided "as is" without express or implied warranty.
15
Raytracer uses the Convex Raycast to visualize the Collision Shapes/Minkowski Sum.
16
Very basic raytracer, rendering into a texture.
19
#include "GL_Simplex1to4.h"
20
#include "SimdQuaternion.h"
21
#include "SimdTransform.h"
22
#include "GL_ShapeDrawer.h"
23
#ifdef WIN32 //needed for glut.h
27
#include "GlutStuff.h"
29
#include "NarrowPhaseCollision/VoronoiSimplexSolver.h"
30
#include "NarrowPhaseCollision/SubSimplexConvexCast.h"
31
#include "NarrowPhaseCollision/GjkConvexCast.h"
32
#include "NarrowPhaseCollision/ContinuousConvexCollision.h"
33
#include "NarrowPhaseCollision/BU_CollisionPair.h"
37
#include "CollisionShapes/SphereShape.h"
38
#include "CollisionShapes/MultiSphereShape.h"
39
#include "CollisionShapes/ConvexHullShape.h"
40
#include "CollisionShapes/BoxShape.h"
41
#include "CollisionShapes/Simplex1to4Shape.h"
42
#include "CollisionShapes/ConeShape.h"
43
#include "CollisionShapes/CylinderShape.h"
44
#include "CollisionShapes/MinkowskiSumShape.h"
48
#include "RenderTexture.h"
50
VoronoiSimplexSolver simplexSolver;
52
float yaw=0.f,pitch=0.f,roll=0.f;
53
const int maxNumObjects = 4;
54
const int numObjects = 4;
56
/// simplex contains the vertices, and some extra code to draw and debug
57
GL_Simplex1to4 simplex;
59
ConvexShape* shapePtr[maxNumObjects];
60
SimdTransform transforms[maxNumObjects];
62
RenderTexture* raytracePicture = 0;
64
int screenWidth = 128;
65
int screenHeight = 128;
68
SphereShape mySphere(1);
69
BoxShape myBox(SimdVector3(0.4f,0.4f,0.4f));
70
CylinderShape myCylinder(SimdVector3(0.3f,0.3f,0.3f));
71
ConeShape myCone(1,1);
73
MinkowskiSumShape myMink(&myCylinder,&myBox);
79
int main(int argc,char** argv)
81
raytracePicture = new RenderTexture(screenWidth,screenHeight);
83
myBox.SetMargin(0.02f);
84
myCone.SetMargin(0.2f);
86
simplex.SetSimplexSolver(&simplexSolver);
87
simplex.AddVertex(SimdPoint3(-1,0,-1));
88
simplex.AddVertex(SimdPoint3(1,0,-1));
89
simplex.AddVertex(SimdPoint3(0,0,1));
90
simplex.AddVertex(SimdPoint3(0,1,0));
93
/// convex hull of 5 spheres
95
SimdVector3 inertiaHalfExtents(10.f,10.f,10.f);
96
SimdVector3 positions[NUM_SPHERES] = {
97
SimdVector3(-1.2f, -0.3f, 0.f),
98
SimdVector3(0.8f, -0.3f, 0.f),
99
SimdVector3(0.5f, 0.6f, 0.f),
100
SimdVector3(-0.5f, 0.6f, 0.f),
101
SimdVector3(0.f, 0.f, 0.f)
103
SimdScalar radi[NUM_SPHERES] = { 0.35f,0.35f,0.45f,0.40f,0.40f };
105
MultiSphereShape multiSphereShape(inertiaHalfExtents,positions,radi,NUM_SPHERES);
107
ConvexHullShape convexHullShape(positions,3);
111
shapePtr[0] = &myCone;
112
shapePtr[1] =&simplex;
113
shapePtr[2] =&convexHullShape;
114
shapePtr[3] =&myMink;//myBox;
116
simplex.SetMargin(0.3f);
118
setCameraDistance(6.f);
120
return glutmain(argc, argv,screenWidth,screenHeight,"Minkowski-Sum Raytracer Demo");
123
//to be implemented by the demo
125
void clientMoveAndDisplay()
134
void clientDisplay(void)
137
for (int i=0;i<numObjects;i++)
139
transforms[i].setIdentity();
140
SimdVector3 pos(-3.5f+i*2.5f,0.f,0.f);
141
transforms[i].setOrigin( pos );
145
orn.setEuler(yaw,pitch,roll);
146
transforms[i].setRotation(orn);
149
myMink.SetTransformA(SimdTransform(transforms[0].getRotation()));
151
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
152
glDisable(GL_LIGHTING);
155
glGenTextures(1, &glTextureId);
156
glBindTexture(GL_TEXTURE_2D,glTextureId );
158
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
159
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
160
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
161
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
162
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
167
glDisable(GL_TEXTURE_2D);
178
SimdVector4 rgba(1.f,0.f,0.f,0.5f);
182
float nearPlane = 1.f;
184
float tanFov = (top-bottom)*0.5f / nearPlane;
186
float fov = 2.0 * atanf (tanFov);
189
SimdVector3 rayFrom(eye[0],eye[1],eye[2]);
190
SimdVector3 rayForward = -rayFrom;
191
rayForward.normalize();
192
float farPlane = 600.f;
193
rayForward*= farPlane;
195
SimdVector3 rightOffset;
196
SimdVector3 vertical(0.f,1.f,0.f);
198
hor = rayForward.cross(vertical);
200
vertical = hor.cross(rayForward);
201
vertical.normalize();
203
float tanfov = tanf(0.5f*fov);
205
hor *= 2.f * farPlane * tanfov;
206
vertical *= 2.f * farPlane * tanfov;
208
SimdVector3 rayToCenter = rayFrom + rayForward;
210
SimdVector3 dHor = hor * 1.f/float(screenWidth);
211
SimdVector3 dVert = vertical * 1.f/float(screenHeight);
213
SimdTransform rayFromTrans;
214
rayFromTrans.setIdentity();
215
rayFromTrans.setOrigin(rayFrom);
217
SimdTransform rayFromLocal;
218
SimdTransform rayToLocal;
221
SphereShape pointShape(0.0f);
225
for (int x=0;x<screenWidth;x++)
227
for (int y=0;y<screenHeight;y++)
229
SimdVector4 rgba(0.f,0.f,0.f,0.f);
230
raytracePicture->SetPixel(x,y,rgba);
235
ConvexCast::CastResult rayResult;
236
SimdTransform rayToTrans;
237
rayToTrans.setIdentity();
239
for (int x=0;x<screenWidth;x++)
241
for (int y=0;y<screenHeight;y++)
243
rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
246
rayToTrans.setOrigin(rayTo);
247
for (int s=0;s<numObjects;s++)
249
// rayFromLocal = transforms[s].inverse()* rayFromTrans;
250
// rayToLocal = transforms[s].inverse()* rayToTrans;
252
//choose the continuous collision detection method
253
SubsimplexConvexCast convexCaster(&pointShape,shapePtr[s],&simplexSolver);
254
//GjkConvexCast convexCaster(&pointShape,shapePtr[0],&simplexSolver);
255
//ContinuousConvexCollision convexCaster(&pointShape,shapePtr[0],&simplexSolver,0);
257
// BU_Simplex1to4 ptShape(SimdVector3(0,0,0));//algebraic needs features, doesnt use 'supporting vertex'
258
// BU_CollisionPair convexCaster(&ptShape,shapePtr[0]);
261
//reset previous result
262
rayResult.m_fraction = 1.f;
265
if (convexCaster.calcTimeOfImpact(rayFromTrans,rayToTrans,transforms[s],transforms[s],rayResult))
267
//float fog = 1.f - 0.1f * rayResult.m_fraction;
268
rayResult.m_normal.normalize();
270
SimdVector3 worldNormal;
271
worldNormal = transforms[s].getBasis() *rayResult.m_normal;
273
float light = worldNormal.dot(SimdVector3(0.4f,-1.f,-0.4f));
279
rgba = SimdVector4(light,light,light,1.f);
280
raytracePicture->SetPixel(x,y,rgba);
283
//clear is already done
284
//rgba = SimdVector4(0.f,0.f,0.f,0.f);
285
//raytracePicture->SetPixel(x,y,rgba);
298
extern BMF_FontData BMF_font_helv10;
300
raytracePicture->Printf("CCD RAYTRACER",&BMF_font_helv10);
302
sprintf(buffer,"%d RAYS / Frame",screenWidth*screenHeight*numObjects);
303
raytracePicture->Printf(buffer,&BMF_font_helv10,0,10);
308
glMatrixMode(GL_PROJECTION);
311
glFrustum(-1.0,1.0,-1.0,1.0,3,2020.0);
313
glMatrixMode(GL_MODELVIEW);
315
glLoadIdentity(); // Reset The Modelview Matrix
316
glTranslatef(0.0f,0.0f,-3.0f); // Move Into The Screen 5 Units
320
glEnable(GL_TEXTURE_2D);
321
glBindTexture(GL_TEXTURE_2D,glTextureId );
323
const unsigned char *ptr = raytracePicture->GetBuffer();
324
glTexImage2D(GL_TEXTURE_2D,
327
raytracePicture->GetWidth(),raytracePicture->GetHeight(),
335
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
336
glColor4f (1,1,1,1); // alpha=0.5=half visible
339
glTexCoord2f(0.0f, 0.0f);
341
glTexCoord2f(1.0f, 0.0f);
343
glTexCoord2f(1.0f, 1.0f);
345
glTexCoord2f(0.0f, 1.0f);
351
glMatrixMode(GL_MODELVIEW);
353
glMatrixMode(GL_PROJECTION);
355
glMatrixMode(GL_MODELVIEW);
359
glDisable(GL_TEXTURE_2D);
360
glDisable(GL_DEPTH_TEST);
362
GL_ShapeDrawer::DrawCoordSystem();
370
/// normal opengl rendering
374
for (i=0;i<numObjects;i++)
378
transA.getOpenGLMatrix( m );
380
GL_ShapeDrawer::DrawOpenGL(m,shapePtr[i],SimdVector3(1,1,1));
381
/// calculate closest point from simplex to the origin, and draw this vector
382
simplex.CalcClosest(m);
396
void clientResetScene()
400
void clientKeyboard(unsigned char key, int x, int y)
402
defaultKeyboard(key, x, y);
406
void clientMouseFunc(int button, int state, int x, int y)
410
void clientMotionFunc(int x,int y)