demo_trimesh.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001-2003 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. // TriMesh test by Erwin de Vries
  23. #include <ode/ode.h>
  24. #include <drawstuff/drawstuff.h>
  25. #include "texturepath.h"
  26. #ifdef _MSC_VER
  27. #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
  28. #endif
  29. // select correct drawing functions
  30. #ifdef dDOUBLE
  31. #define dsDrawBox dsDrawBoxD
  32. #define dsDrawSphere dsDrawSphereD
  33. #define dsDrawCylinder dsDrawCylinderD
  34. #define dsDrawCapsule dsDrawCapsuleD
  35. #define dsDrawLine dsDrawLineD
  36. #define dsDrawTriangle dsDrawTriangleD
  37. #endif
  38. // some constants
  39. #define NUM 200 // max number of objects
  40. #define DENSITY (5.0) // density of all objects
  41. #define GPB 3 // maximum number of geometries per body
  42. #define MAX_CONTACTS 40 // maximum number of contact points per body
  43. // dynamics and collision objects
  44. struct MyObject {
  45. dBodyID body; // the body
  46. dGeomID geom[GPB]; // geometries representing this body
  47. };
  48. static int num=0; // number of objects in simulation
  49. static int nextobj=0; // next object to recycle if num==NUM
  50. static dWorldID world;
  51. static dSpaceID space;
  52. static MyObject obj[NUM];
  53. static dJointGroupID contactgroup;
  54. static int selected = -1; // selected object
  55. static int show_aabb = 0; // show geom AABBs?
  56. static int show_contacts = 0; // show contact points?
  57. static int random_pos = 1; // drop objects from random position?
  58. #define VertexCount 5
  59. #define IndexCount 12
  60. static dVector3 Size;
  61. static float Vertices[VertexCount][3];
  62. static dTriIndex Indices[IndexCount];
  63. static dGeomID TriMesh;
  64. static dGeomID Ray;
  65. // this is called by dSpaceCollide when two objects in space are
  66. // potentially colliding.
  67. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  68. {
  69. int i;
  70. // if (o1->body && o2->body) return;
  71. // exit without doing anything if the two bodies are connected by a joint
  72. dBodyID b1 = dGeomGetBody(o1);
  73. dBodyID b2 = dGeomGetBody(o2);
  74. if (b1 && b2 && dAreConnectedExcluding (b1,b2,dJointTypeContact)) return;
  75. dContact contact[MAX_CONTACTS]; // up to MAX_CONTACTS contacts per box-box
  76. for (i=0; i<MAX_CONTACTS; i++) {
  77. contact[i].surface.mode = dContactBounce | dContactSoftCFM;
  78. contact[i].surface.mu = dInfinity;
  79. contact[i].surface.mu2 = 0;
  80. contact[i].surface.bounce = 0.1;
  81. contact[i].surface.bounce_vel = 0.1;
  82. contact[i].surface.soft_cfm = 0.01;
  83. }
  84. if (int numc = dCollide (o1,o2,MAX_CONTACTS,&contact[0].geom,
  85. sizeof(dContact))) {
  86. dMatrix3 RI;
  87. dRSetIdentity (RI);
  88. const dReal ss[3] = {0.02,0.02,0.02};
  89. for (i=0; i<numc; i++) {
  90. if (dGeomGetClass(o1) == dRayClass || dGeomGetClass(o2) == dRayClass){
  91. dMatrix3 Rotation;
  92. dRSetIdentity(Rotation);
  93. dsDrawSphere(contact[i].geom.pos, Rotation, REAL(0.01));
  94. dVector3 End;
  95. End[0] = contact[i].geom.pos[0] + (contact[i].geom.normal[0] * contact[i].geom.depth);
  96. End[1] = contact[i].geom.pos[1] + (contact[i].geom.normal[1] * contact[i].geom.depth);
  97. End[2] = contact[i].geom.pos[2] + (contact[i].geom.normal[2] * contact[i].geom.depth);
  98. End[3] = contact[i].geom.pos[3] + (contact[i].geom.normal[3] * contact[i].geom.depth);
  99. dsDrawLine(contact[i].geom.pos, End);
  100. continue;
  101. }
  102. dJointID c = dJointCreateContact (world,contactgroup,contact+i);
  103. dJointAttach (c,b1,b2);
  104. if (show_contacts) dsDrawBox (contact[i].geom.pos,RI,ss);
  105. }
  106. }
  107. }
  108. // start simulation - set viewpoint
  109. static void start()
  110. {
  111. dAllocateODEDataForThread(dAllocateMaskAll);
  112. static float xyz[3] = {2.1640f,-1.3079f,1.7600f};
  113. static float hpr[3] = {125.5000f,-17.0000f,0.0000f};
  114. dsSetViewpoint (xyz,hpr);
  115. printf ("To drop another object, press:\n");
  116. printf (" b for box.\n");
  117. printf (" s for sphere.\n");
  118. printf (" c for cylinder.\n");
  119. printf (" x for a composite object.\n");
  120. printf ("To select an object, press space.\n");
  121. printf ("To disable the selected object, press d.\n");
  122. printf ("To enable the selected object, press e.\n");
  123. printf ("To toggle showing the geom AABBs, press a.\n");
  124. printf ("To toggle showing the contact points, press t.\n");
  125. printf ("To toggle dropping from random position/orientation, press r.\n");
  126. }
  127. char locase (char c)
  128. {
  129. if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
  130. else return c;
  131. }
  132. // called when a key pressed
  133. static void command (int cmd)
  134. {
  135. int i,j,k;
  136. dReal sides[3];
  137. dMass m;
  138. cmd = locase (cmd);
  139. if (cmd == 'b' || cmd == 's' || cmd == 'c' || cmd == 'x'
  140. /* || cmd == 'l' */) {
  141. if (num < NUM) {
  142. i = num;
  143. num++;
  144. }
  145. else {
  146. i = nextobj;
  147. nextobj++;
  148. if (nextobj >= num) nextobj = 0;
  149. // destroy the body and geoms for slot i
  150. dBodyDestroy (obj[i].body);
  151. for (k=0; k < GPB; k++) {
  152. if (obj[i].geom[k]) dGeomDestroy (obj[i].geom[k]);
  153. }
  154. memset (&obj[i],0,sizeof(obj[i]));
  155. }
  156. obj[i].body = dBodyCreate (world);
  157. for (k=0; k<3; k++) sides[k] = dRandReal()*0.5+0.1;
  158. dMatrix3 R;
  159. if (random_pos) {
  160. dBodySetPosition (obj[i].body,
  161. dRandReal()*2-1,dRandReal()*2-1,dRandReal()+1);
  162. dRFromAxisAndAngle (R,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
  163. dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
  164. }
  165. else {
  166. dReal maxheight = 0;
  167. for (k=0; k<num; k++) {
  168. const dReal *pos = dBodyGetPosition (obj[k].body);
  169. if (pos[2] > maxheight) maxheight = pos[2];
  170. }
  171. dBodySetPosition (obj[i].body, 0,0,maxheight+1);
  172. dRFromAxisAndAngle (R,0,0,1,dRandReal()*10.0-5.0);
  173. }
  174. dBodySetRotation (obj[i].body,R);
  175. dBodySetData (obj[i].body,(void*)(size_t)i);
  176. if (cmd == 'b') {
  177. dMassSetBox (&m,DENSITY,sides[0],sides[1],sides[2]);
  178. obj[i].geom[0] = dCreateBox (space,sides[0],sides[1],sides[2]);
  179. }
  180. else if (cmd == 'c') {
  181. sides[0] *= 0.5;
  182. dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
  183. obj[i].geom[0] = dCreateCapsule (space,sides[0],sides[1]);
  184. }
  185. /*
  186. // cylinder option not yet implemented
  187. else if (cmd == 'l') {
  188. sides[1] *= 0.5;
  189. dMassSetCapsule (&m,DENSITY,3,sides[0],sides[1]);
  190. obj[i].geom[0] = dCreateCylinder (space,sides[0],sides[1]);
  191. }
  192. */
  193. else if (cmd == 's') {
  194. sides[0] *= 0.5;
  195. dMassSetSphere (&m,DENSITY,sides[0]);
  196. obj[i].geom[0] = dCreateSphere (space,sides[0]);
  197. }
  198. else if (cmd == 'x') {
  199. dGeomID g2[GPB]; // encapsulated geometries
  200. dReal dpos[GPB][3]; // delta-positions for encapsulated geometries
  201. // start accumulating masses for the encapsulated geometries
  202. dMass m2;
  203. dMassSetZero (&m);
  204. // set random delta positions
  205. for (j=0; j<GPB; j++) {
  206. for (k=0; k<3; k++) dpos[j][k] = dRandReal()*0.3-0.15;
  207. }
  208. for (k=0; k<GPB; k++) {
  209. obj[i].geom[k] = dCreateGeomTransform (space);
  210. dGeomTransformSetCleanup (obj[i].geom[k],1);
  211. if (k==0) {
  212. dReal radius = dRandReal()*0.25+0.05;
  213. g2[k] = dCreateSphere (0,radius);
  214. dMassSetSphere (&m2,DENSITY,radius);
  215. }
  216. else if (k==1) {
  217. g2[k] = dCreateBox (0,sides[0],sides[1],sides[2]);
  218. dMassSetBox (&m2,DENSITY,sides[0],sides[1],sides[2]);
  219. }
  220. else {
  221. dReal radius = dRandReal()*0.1+0.05;
  222. dReal length = dRandReal()*1.0+0.1;
  223. g2[k] = dCreateCapsule (0,radius,length);
  224. dMassSetCapsule (&m2,DENSITY,3,radius,length);
  225. }
  226. dGeomTransformSetGeom (obj[i].geom[k],g2[k]);
  227. // set the transformation (adjust the mass too)
  228. dGeomSetPosition (g2[k],dpos[k][0],dpos[k][1],dpos[k][2]);
  229. dMassTranslate (&m2,dpos[k][0],dpos[k][1],dpos[k][2]);
  230. dMatrix3 Rtx;
  231. dRFromAxisAndAngle (Rtx,dRandReal()*2.0-1.0,dRandReal()*2.0-1.0,
  232. dRandReal()*2.0-1.0,dRandReal()*10.0-5.0);
  233. dGeomSetRotation (g2[k],Rtx);
  234. dMassRotate (&m2,Rtx);
  235. // add to the total mass
  236. dMassAdd (&m,&m2);
  237. }
  238. // move all encapsulated objects so that the center of mass is (0,0,0)
  239. for (k=0; k<2; k++) {
  240. dGeomSetPosition (g2[k],
  241. dpos[k][0]-m.c[0],
  242. dpos[k][1]-m.c[1],
  243. dpos[k][2]-m.c[2]);
  244. }
  245. dMassTranslate (&m,-m.c[0],-m.c[1],-m.c[2]);
  246. }
  247. for (k=0; k < GPB; k++) {
  248. if (obj[i].geom[k]) dGeomSetBody (obj[i].geom[k],obj[i].body);
  249. }
  250. dBodySetMass (obj[i].body,&m);
  251. }
  252. if (cmd == ' ') {
  253. selected++;
  254. if (selected >= num) selected = 0;
  255. if (selected < 0) selected = 0;
  256. }
  257. else if (cmd == 'd' && selected >= 0 && selected < num) {
  258. dBodyDisable (obj[selected].body);
  259. }
  260. else if (cmd == 'e' && selected >= 0 && selected < num) {
  261. dBodyEnable (obj[selected].body);
  262. }
  263. else if (cmd == 'a') {
  264. show_aabb ^= 1;
  265. }
  266. else if (cmd == 't') {
  267. show_contacts ^= 1;
  268. }
  269. else if (cmd == 'r') {
  270. random_pos ^= 1;
  271. }
  272. }
  273. // draw a geom
  274. void drawGeom (dGeomID g, const dReal *pos, const dReal *R, int show_aabb)
  275. {
  276. if (!g) return;
  277. if (!pos) pos = dGeomGetPosition (g);
  278. if (!R) R = dGeomGetRotation (g);
  279. int type = dGeomGetClass (g);
  280. if (type == dBoxClass) {
  281. dVector3 sides;
  282. dGeomBoxGetLengths (g,sides);
  283. dsDrawBox (pos,R,sides);
  284. }
  285. else if (type == dSphereClass) {
  286. dsDrawSphere (pos,R,dGeomSphereGetRadius (g));
  287. }
  288. else if (type == dCapsuleClass) {
  289. dReal radius,length;
  290. dGeomCapsuleGetParams (g,&radius,&length);
  291. dsDrawCapsule (pos,R,length,radius);
  292. }
  293. /*
  294. // cylinder option not yet implemented
  295. else if (type == dCylinderClass) {
  296. dReal radius,length;
  297. dGeomCylinderGetParams (g,&radius,&length);
  298. dsDrawCylinder (pos,R,length,radius);
  299. }
  300. */
  301. else if (type == dGeomTransformClass) {
  302. dGeomID g2 = dGeomTransformGetGeom (g);
  303. const dReal *pos2 = dGeomGetPosition (g2);
  304. const dReal *R2 = dGeomGetRotation (g2);
  305. dVector3 actual_pos;
  306. dMatrix3 actual_R;
  307. dMultiply0_331 (actual_pos,R,pos2);
  308. actual_pos[0] += pos[0];
  309. actual_pos[1] += pos[1];
  310. actual_pos[2] += pos[2];
  311. dMultiply0_333 (actual_R,R,R2);
  312. drawGeom (g2,actual_pos,actual_R,0);
  313. }
  314. if (show_aabb) {
  315. // draw the bounding box for this geom
  316. dReal aabb[6];
  317. dGeomGetAABB (g,aabb);
  318. dVector3 bbpos;
  319. for (int i=0; i<3; i++) bbpos[i] = 0.5*(aabb[i*2] + aabb[i*2+1]);
  320. dVector3 bbsides;
  321. for (int j=0; j<3; j++) bbsides[j] = aabb[j*2+1] - aabb[j*2];
  322. dMatrix3 RI;
  323. dRSetIdentity (RI);
  324. dsSetColorAlpha (1,0,0,0.5);
  325. dsDrawBox (bbpos,RI,bbsides);
  326. }
  327. }
  328. // simulation loop
  329. static void simLoop (int pause)
  330. {
  331. dsSetColor (0,0,2);
  332. dSpaceCollide (space,0,&nearCallback);
  333. if (!pause) dWorldStep (world,0.05);
  334. //if (!pause) dWorldStepFast (world,0.05, 1);
  335. // remove all contact joints
  336. dJointGroupEmpty (contactgroup);
  337. dsSetColor (1,1,0);
  338. dsSetTexture (DS_WOOD);
  339. for (int i=0; i<num; i++) {
  340. for (int j=0; j < GPB; j++) {
  341. if (i==selected) {
  342. dsSetColor (0,0.7,1);
  343. }
  344. else if (! dBodyIsEnabled (obj[i].body)) {
  345. dsSetColor (1,0,0);
  346. }
  347. else {
  348. dsSetColor (1,1,0);
  349. }
  350. drawGeom (obj[i].geom[j],0,0,show_aabb);
  351. }
  352. }
  353. /*{
  354. for (int i = 1; i < IndexCount; i++) {
  355. dsDrawLine(Vertices[Indices[i - 1]], Vertices[Indices[i]]);
  356. }
  357. }*/
  358. {const dReal* Pos = dGeomGetPosition(TriMesh);
  359. const dReal* Rot = dGeomGetRotation(TriMesh);
  360. {for (int i = 0; i < IndexCount / 3; i++){
  361. const float *p = Vertices[Indices[i * 3 + 0]];
  362. const dVector3 v0 = { p[0], p[1], p[2] };
  363. p = Vertices[Indices[i * 3 + 1]];
  364. const dVector3 v1 = { p[0], p[1], p[2] };
  365. p = Vertices[Indices[i * 3 + 2]];
  366. const dVector3 v2 = { p[0], p[1], p[2] };
  367. dsDrawTriangle(Pos, Rot, v0, v1, v2, 0);
  368. }}}
  369. if (Ray){
  370. dVector3 Origin, Direction;
  371. dGeomRayGet(Ray, Origin, Direction);
  372. dReal Length = dGeomRayGetLength(Ray);
  373. dVector3 End;
  374. End[0] = Origin[0] + (Direction[0] * Length);
  375. End[1] = Origin[1] + (Direction[1] * Length);
  376. End[2] = Origin[2] + (Direction[2] * Length);
  377. End[3] = Origin[3] + (Direction[3] * Length);
  378. dsDrawLine(Origin, End);
  379. }
  380. }
  381. int main (int argc, char **argv)
  382. {
  383. // setup pointers to drawstuff callback functions
  384. dsFunctions fn;
  385. fn.version = DS_VERSION;
  386. fn.start = &start;
  387. fn.step = &simLoop;
  388. fn.command = &command;
  389. fn.stop = 0;
  390. fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
  391. // create world
  392. dInitODE2(0);
  393. world = dWorldCreate();
  394. space = dSimpleSpaceCreate(0);
  395. contactgroup = dJointGroupCreate (0);
  396. dWorldSetGravity (world,0,0,-0.5);
  397. dWorldSetCFM (world,1e-5);
  398. //dCreatePlane (space,0,0,1,0);
  399. memset (obj,0,sizeof(obj));
  400. Size[0] = 5.0f;
  401. Size[1] = 5.0f;
  402. Size[2] = 2.5f;
  403. Vertices[0][0] = -Size[0];
  404. Vertices[0][1] = -Size[1];
  405. Vertices[0][2] = Size[2];
  406. Vertices[1][0] = Size[0];
  407. Vertices[1][1] = -Size[1];
  408. Vertices[1][2] = Size[2];
  409. Vertices[2][0] = Size[0];
  410. Vertices[2][1] = Size[1];
  411. Vertices[2][2] = Size[2];
  412. Vertices[3][0] = -Size[0];
  413. Vertices[3][1] = Size[1];
  414. Vertices[3][2] = Size[2];
  415. Vertices[4][0] = 0;
  416. Vertices[4][1] = 0;
  417. Vertices[4][2] = 0;
  418. Indices[0] = 0;
  419. Indices[1] = 1;
  420. Indices[2] = 4;
  421. Indices[3] = 1;
  422. Indices[4] = 2;
  423. Indices[5] = 4;
  424. Indices[6] = 2;
  425. Indices[7] = 3;
  426. Indices[8] = 4;
  427. Indices[9] = 3;
  428. Indices[10] = 0;
  429. Indices[11] = 4;
  430. dTriMeshDataID Data = dGeomTriMeshDataCreate();
  431. //dGeomTriMeshDataBuildSimple(Data, (dReal*)Vertices, VertexCount, Indices, IndexCount);
  432. dGeomTriMeshDataBuildSingle(Data, Vertices[0], 3 * sizeof(float), VertexCount, &Indices[0], IndexCount, 3 * sizeof(dTriIndex));
  433. TriMesh = dCreateTriMesh(space, Data, 0, 0, 0);
  434. //dGeomSetPosition(TriMesh, 0, 0, 1.0);
  435. Ray = dCreateRay(space, 0.9);
  436. dVector3 Origin, Direction;
  437. Origin[0] = 0.0;
  438. Origin[1] = 0;
  439. Origin[2] = 0.5;
  440. Origin[3] = 0;
  441. Direction[0] = 0;
  442. Direction[1] = 1.1f;
  443. Direction[2] = -1;
  444. Direction[3] = 0;
  445. dNormalize3(Direction);
  446. dGeomRaySet(Ray, Origin[0], Origin[1], Origin[2], Direction[0], Direction[1], Direction[2]);
  447. dThreadingImplementationID threading = dThreadingAllocateMultiThreadedImplementation();
  448. dThreadingThreadPoolID pool = dThreadingAllocateThreadPool(4, 0, dAllocateFlagBasicData, NULL);
  449. dThreadingThreadPoolServeMultiThreadedImplementation(pool, threading);
  450. // dWorldSetStepIslandsProcessingMaxThreadCount(world, 1);
  451. dWorldSetStepThreadingImplementation(world, dThreadingImplementationGetFunctions(threading), threading);
  452. // run simulation
  453. dsSimulationLoop (argc,argv,352,288,&fn);
  454. dThreadingImplementationShutdownProcessing(threading);
  455. dThreadingFreeThreadPool(pool);
  456. dWorldSetStepThreadingImplementation(world, NULL, NULL);
  457. dThreadingFreeImplementation(threading);
  458. dJointGroupDestroy (contactgroup);
  459. dSpaceDestroy (space);
  460. dWorldDestroy (world);
  461. dCloseODE();
  462. return 0;
  463. }