dstest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 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. #include <stdio.h>
  23. #include <math.h>
  24. #include <drawstuff/drawstuff.h>
  25. #ifndef M_PI
  26. #define M_PI (3.14159265358979323846)
  27. #endif
  28. void start()
  29. {
  30. // adjust the starting viewpoint a bit
  31. float xyz[3],hpr[3];
  32. dsGetViewpoint (xyz,hpr);
  33. hpr[0] += 7;
  34. dsSetViewpoint (xyz,hpr);
  35. }
  36. void simLoop (int pause)
  37. {
  38. float pos[3];
  39. float R[12];
  40. static float a = 0;
  41. if (!pause) a += 0.02f;
  42. if (a > (2*M_PI)) a -= (float) (2*M_PI);
  43. float ca = (float) cos(a);
  44. float sa = (float) sin(a);
  45. dsSetTexture (DS_WOOD);
  46. float b = (a > M_PI) ? (2*(a-(float)M_PI)) : a*2;
  47. pos[0] = -0.3f;
  48. pos[1] = 0;
  49. pos[2] = (float) (0.1f*(2*M_PI*b - b*b) + 0.65f);
  50. R[0] = ca; R[1] = 0; R[2] = -sa;
  51. R[4] = 0; R[5] = 1; R[6] = 0;
  52. R[8] = sa; R[9] = 0; R[10] = ca;
  53. dsSetColor (1,0.8f,0.6f);
  54. dsDrawSphere (pos,R,0.3f);
  55. dsSetTexture (DS_NONE);
  56. pos[0] = -0.2f;
  57. pos[1] = 0.8f;
  58. pos[2] = 0.4f;
  59. R[0] = ca; R[1] = -sa; R[2] = 0;
  60. R[4] = sa; R[5] = ca; R[6] = 0;
  61. R[8] = 0; R[9] = 0; R[10] = 1;
  62. float sides[3] = {0.1f,0.4f,0.8f};
  63. dsSetColor (0.6f,0.6f,1);
  64. dsDrawBox (pos,R,sides);
  65. dsSetTexture (DS_WOOD);
  66. float r = 0.3f; // cylinder radius
  67. float d = (float)cos(a*2) * 0.4f;
  68. float cd = (float)cos(-d/r);
  69. float sd = (float)sin(-d/r);
  70. pos[0] = -0.2f;
  71. pos[1] = -1 + d;
  72. pos[2] = 0.3f;
  73. R[0] = 0; R[1] = 0; R[2] = -1;
  74. R[4] = -sd; R[5] = cd; R[6] = 0;
  75. R[8] = cd; R[9] = sd; R[10] = 0;
  76. dsSetColor (0.4f,1,1);
  77. dsDrawCylinder (pos,R,0.8f,r);
  78. pos[0] = 0;
  79. pos[1] = 0;
  80. pos[2] = 0.2f;
  81. R[0] = 0; R[1] = sa; R[2] = -ca;
  82. R[4] = 0; R[5] = ca; R[6] = sa;
  83. R[8] = 1; R[9] = 0; R[10] = 0;
  84. dsSetColor (1,0.9f,0.2f);
  85. dsDrawCappedCylinder (pos,R,0.8f,0.2f);
  86. }
  87. void command (int cmd)
  88. {
  89. dsPrint ("received command %d (`%c')\n",cmd,cmd);
  90. }
  91. int main (int argc, char **argv)
  92. {
  93. // setup pointers to callback functions
  94. dsFunctions fn;
  95. fn.version = DS_VERSION;
  96. fn.start = &start;
  97. fn.step = &simLoop;
  98. fn.command = command;
  99. fn.stop = 0;
  100. fn.path_to_textures = 0; // uses default
  101. // run simulation
  102. dsSimulationLoop (argc,argv,400,400,&fn);
  103. return 0;
  104. }