~ubuntu-branches/ubuntu/intrepid/blender/intrepid-updates

« back to all changes in this revision

Viewing changes to extern/bullet/Demos/Raytracer/Raytracer.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2008-08-08 02:45:40 UTC
  • mfrom: (12.1.14 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080808024540-kkjp7ekfivzhuw3l
Tags: 2.46+dfsg-4
* Fix python syntax warning in import_dxf.py, which led to nasty output
  in installation/upgrade logs during byte-compilation, using a patch
  provided by the script author (Closes: #492280):
   - debian/patches/45_fix_python_syntax_warning

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
* Copyright (c) 2005 Erwin Coumans <www.erwincoumans.com>
3
 
*
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.
10
 
*/
11
 
 
12
 
 
13
 
 
14
 
/*
15
 
Raytracer uses the Convex Raycast to visualize the Collision Shapes/Minkowski Sum.
16
 
Very basic raytracer, rendering into a texture.
17
 
*/
18
 
 
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
24
 
#include <windows.h>
25
 
#endif
26
 
#include <GL/glut.h>
27
 
#include "GlutStuff.h"
28
 
 
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"
34
 
 
35
 
 
36
 
 
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"
45
 
 
46
 
 
47
 
 
48
 
#include "RenderTexture.h"
49
 
 
50
 
VoronoiSimplexSolver    simplexSolver;
51
 
 
52
 
float yaw=0.f,pitch=0.f,roll=0.f;
53
 
const int maxNumObjects = 4;
54
 
const int numObjects = 4;
55
 
 
56
 
/// simplex contains the vertices, and some extra code to draw and debug
57
 
GL_Simplex1to4  simplex;
58
 
 
59
 
ConvexShape*    shapePtr[maxNumObjects];
60
 
SimdTransform transforms[maxNumObjects];
61
 
 
62
 
RenderTexture*  raytracePicture = 0;
63
 
 
64
 
int screenWidth = 128;
65
 
int screenHeight = 128;
66
 
GLuint glTextureId;
67
 
 
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);
72
 
 
73
 
MinkowskiSumShape myMink(&myCylinder,&myBox);
74
 
 
75
 
 
76
 
///
77
 
///
78
 
///
79
 
int main(int argc,char** argv)
80
 
{
81
 
        raytracePicture = new RenderTexture(screenWidth,screenHeight);
82
 
 
83
 
        myBox.SetMargin(0.02f);
84
 
        myCone.SetMargin(0.2f);
85
 
 
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));
91
 
        
92
 
        
93
 
        /// convex hull of 5 spheres
94
 
#define NUM_SPHERES 5
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)
102
 
        };
103
 
        SimdScalar radi[NUM_SPHERES] = { 0.35f,0.35f,0.45f,0.40f,0.40f };
104
 
 
105
 
        MultiSphereShape multiSphereShape(inertiaHalfExtents,positions,radi,NUM_SPHERES);
106
 
 
107
 
        ConvexHullShape convexHullShape(positions,3);
108
 
 
109
 
 
110
 
        //choose shape
111
 
        shapePtr[0] = &myCone;
112
 
        shapePtr[1] =&simplex;
113
 
        shapePtr[2] =&convexHullShape;
114
 
        shapePtr[3] =&myMink;//myBox;
115
 
 
116
 
        simplex.SetMargin(0.3f);
117
 
 
118
 
        setCameraDistance(6.f);
119
 
 
120
 
        return glutmain(argc, argv,screenWidth,screenHeight,"Minkowski-Sum Raytracer Demo");
121
 
}
122
 
 
123
 
//to be implemented by the demo
124
 
 
125
 
void clientMoveAndDisplay()
126
 
{
127
 
 
128
 
        clientDisplay();
129
 
}
130
 
 
131
 
int once = 1;
132
 
extern float eye[3];
133
 
 
134
 
void clientDisplay(void) 
135
 
{
136
 
 
137
 
        for (int i=0;i<numObjects;i++)
138
 
        {
139
 
                transforms[i].setIdentity();
140
 
                SimdVector3     pos(-3.5f+i*2.5f,0.f,0.f);
141
 
                transforms[i].setOrigin( pos );
142
 
                SimdQuaternion orn;
143
 
                if (i < 2)
144
 
                {
145
 
                        orn.setEuler(yaw,pitch,roll);
146
 
                        transforms[i].setRotation(orn);
147
 
                }
148
 
        }
149
 
        myMink.SetTransformA(SimdTransform(transforms[0].getRotation()));
150
 
 
151
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
152
 
        glDisable(GL_LIGHTING);
153
 
        if (once)
154
 
        {
155
 
                glGenTextures(1, &glTextureId);
156
 
                glBindTexture(GL_TEXTURE_2D,glTextureId );
157
 
                once = 0;
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);
163
 
        }
164
 
 
165
 
 
166
 
 
167
 
        glDisable(GL_TEXTURE_2D);
168
 
        glDisable(GL_BLEND);
169
 
 
170
 
#define RAYTRACER
171
 
#ifdef RAYTRACER
172
 
 
173
 
 
174
 
 
175
 
 
176
 
 
177
 
 
178
 
        SimdVector4 rgba(1.f,0.f,0.f,0.5f);
179
 
 
180
 
        float top = 1.f;
181
 
        float bottom = -1.f;
182
 
        float nearPlane = 1.f;
183
 
 
184
 
        float tanFov = (top-bottom)*0.5f / nearPlane;
185
 
 
186
 
        float fov = 2.0 * atanf (tanFov);
187
 
 
188
 
 
189
 
        SimdVector3     rayFrom(eye[0],eye[1],eye[2]);
190
 
        SimdVector3 rayForward = -rayFrom;
191
 
        rayForward.normalize();
192
 
        float farPlane = 600.f;
193
 
        rayForward*= farPlane;
194
 
 
195
 
        SimdVector3 rightOffset;
196
 
        SimdVector3 vertical(0.f,1.f,0.f);
197
 
        SimdVector3 hor;
198
 
        hor = rayForward.cross(vertical);
199
 
        hor.normalize();
200
 
        vertical = hor.cross(rayForward);
201
 
        vertical.normalize();
202
 
 
203
 
        float tanfov = tanf(0.5f*fov);
204
 
 
205
 
        hor *= 2.f * farPlane * tanfov;
206
 
        vertical *= 2.f * farPlane * tanfov;
207
 
 
208
 
        SimdVector3 rayToCenter = rayFrom + rayForward;
209
 
 
210
 
        SimdVector3 dHor = hor * 1.f/float(screenWidth);
211
 
        SimdVector3 dVert = vertical * 1.f/float(screenHeight);
212
 
 
213
 
        SimdTransform rayFromTrans;
214
 
        rayFromTrans.setIdentity();
215
 
        rayFromTrans.setOrigin(rayFrom);
216
 
 
217
 
        SimdTransform rayFromLocal;
218
 
        SimdTransform   rayToLocal;
219
 
 
220
 
 
221
 
        SphereShape pointShape(0.0f);
222
 
 
223
 
 
224
 
        ///clear texture
225
 
        for (int x=0;x<screenWidth;x++)
226
 
        {
227
 
                for (int y=0;y<screenHeight;y++)
228
 
                {
229
 
                        SimdVector4 rgba(0.f,0.f,0.f,0.f);
230
 
                        raytracePicture->SetPixel(x,y,rgba);
231
 
                }
232
 
        }
233
 
        
234
 
 
235
 
        ConvexCast::CastResult rayResult;
236
 
        SimdTransform rayToTrans;
237
 
        rayToTrans.setIdentity();
238
 
        SimdVector3 rayTo;
239
 
        for (int x=0;x<screenWidth;x++)
240
 
        {
241
 
                for (int y=0;y<screenHeight;y++)
242
 
                {
243
 
                        rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
244
 
                        rayTo += x * dHor;
245
 
                        rayTo -= y * dVert;
246
 
                        rayToTrans.setOrigin(rayTo);
247
 
                        for (int s=0;s<numObjects;s++)
248
 
                        {
249
 
                        //      rayFromLocal = transforms[s].inverse()* rayFromTrans;
250
 
                        //      rayToLocal = transforms[s].inverse()* rayToTrans;
251
 
 
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);
256
 
                                
257
 
                                //      BU_Simplex1to4  ptShape(SimdVector3(0,0,0));//algebraic needs features, doesnt use 'supporting vertex'
258
 
                                //      BU_CollisionPair convexCaster(&ptShape,shapePtr[0]);
259
 
 
260
 
 
261
 
                                //reset previous result
262
 
                                rayResult.m_fraction = 1.f;
263
 
 
264
 
 
265
 
                                if (convexCaster.calcTimeOfImpact(rayFromTrans,rayToTrans,transforms[s],transforms[s],rayResult))
266
 
                                {
267
 
                                        //float fog = 1.f - 0.1f * rayResult.m_fraction;
268
 
                                        rayResult.m_normal.normalize();
269
 
 
270
 
                                        SimdVector3 worldNormal;
271
 
                                        worldNormal = transforms[s].getBasis() *rayResult.m_normal;
272
 
 
273
 
                                        float light = worldNormal.dot(SimdVector3(0.4f,-1.f,-0.4f));
274
 
                                        if (light < 0.2f)
275
 
                                                light = 0.2f;
276
 
                                        if (light > 1.f)
277
 
                                                light = 1.f;
278
 
 
279
 
                                        rgba = SimdVector4(light,light,light,1.f);
280
 
                                        raytracePicture->SetPixel(x,y,rgba);
281
 
                                } else
282
 
                                {
283
 
                                        //clear is already done
284
 
                                        //rgba = SimdVector4(0.f,0.f,0.f,0.f);
285
 
                                        //raytracePicture->SetPixel(x,y,rgba);
286
 
 
287
 
                                }
288
 
 
289
 
                                
290
 
                        }
291
 
                }
292
 
        }
293
 
 
294
 
#define TEST_PRINTF
295
 
#ifdef TEST_PRINTF
296
 
 
297
 
        
298
 
        extern BMF_FontData BMF_font_helv10;
299
 
        
300
 
        raytracePicture->Printf("CCD RAYTRACER",&BMF_font_helv10);
301
 
        char buffer[256];
302
 
        sprintf(buffer,"%d RAYS / Frame",screenWidth*screenHeight*numObjects);
303
 
        raytracePicture->Printf(buffer,&BMF_font_helv10,0,10);
304
 
        
305
 
 
306
 
#endif //TEST_PRINTF
307
 
 
308
 
        glMatrixMode(GL_PROJECTION);
309
 
        glPushMatrix();
310
 
        glLoadIdentity();
311
 
        glFrustum(-1.0,1.0,-1.0,1.0,3,2020.0);
312
 
 
313
 
        glMatrixMode(GL_MODELVIEW);
314
 
        glPushMatrix();
315
 
        glLoadIdentity();                                                                       // Reset The Modelview Matrix
316
 
        glTranslatef(0.0f,0.0f,-3.0f);                                          // Move Into The Screen 5 Units
317
 
 
318
 
 
319
 
 
320
 
        glEnable(GL_TEXTURE_2D);
321
 
        glBindTexture(GL_TEXTURE_2D,glTextureId );
322
 
 
323
 
        const unsigned char *ptr = raytracePicture->GetBuffer();
324
 
        glTexImage2D(GL_TEXTURE_2D, 
325
 
                0, 
326
 
                GL_RGBA, 
327
 
                raytracePicture->GetWidth(),raytracePicture->GetHeight(), 
328
 
                0, 
329
 
                GL_RGBA, 
330
 
                GL_UNSIGNED_BYTE, 
331
 
                ptr);
332
 
 
333
 
 
334
 
        glEnable (GL_BLEND);
335
 
        glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
336
 
        glColor4f (1,1,1,1); // alpha=0.5=half visible
337
 
 
338
 
        glBegin(GL_QUADS);
339
 
        glTexCoord2f(0.0f, 0.0f);
340
 
        glVertex2f(-1,1);
341
 
        glTexCoord2f(1.0f, 0.0f);
342
 
        glVertex2f(1,1);
343
 
        glTexCoord2f(1.0f, 1.0f);
344
 
        glVertex2f(1,-1);
345
 
        glTexCoord2f(0.0f, 1.0f);
346
 
        glVertex2f(-1,-1);
347
 
        glEnd();
348
 
 
349
 
 
350
 
 
351
 
        glMatrixMode(GL_MODELVIEW);
352
 
        glPopMatrix();
353
 
        glMatrixMode(GL_PROJECTION);
354
 
        glPopMatrix();
355
 
        glMatrixMode(GL_MODELVIEW);
356
 
 
357
 
#endif //RAYRACER
358
 
 
359
 
        glDisable(GL_TEXTURE_2D);
360
 
        glDisable(GL_DEPTH_TEST);
361
 
 
362
 
        GL_ShapeDrawer::DrawCoordSystem();
363
 
 
364
 
        glPushMatrix();
365
 
 
366
 
 
367
 
 
368
 
        
369
 
        /*
370
 
        /// normal opengl rendering
371
 
        float m[16];
372
 
        int i;
373
 
 
374
 
        for (i=0;i<numObjects;i++)
375
 
        {
376
 
 
377
 
 
378
 
                transA.getOpenGLMatrix( m );
379
 
                /// draw the simplex
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);
383
 
 
384
 
        }
385
 
        */
386
 
 
387
 
        glPopMatrix();
388
 
 
389
 
        pitch += 0.005f;
390
 
        yaw += 0.01f;
391
 
 
392
 
        glFlush();
393
 
        glutSwapBuffers();
394
 
}
395
 
 
396
 
void clientResetScene()
397
 
{
398
 
}
399
 
 
400
 
void clientKeyboard(unsigned char key, int x, int y)
401
 
{
402
 
        defaultKeyboard(key, x, y);
403
 
}
404
 
 
405
 
 
406
 
void clientMouseFunc(int button, int state, int x, int y)
407
 
{
408
 
 
409
 
}
410
 
void    clientMotionFunc(int x,int y)
411
 
{
412
 
}