main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. #define TPE_LOG puts
  2. #include "tinyphysicsengine.h"
  3. #include <SDL2/SDL.h>
  4. #include <math.h>
  5. #define S3L_RESOLUTION_X 640
  6. #define S3L_RESOLUTION_Y 480
  7. #define S3L_PIXEL_FUNCTION drawPixel
  8. #define S3L_Z_BUFFER 1
  9. #include "small3dlib.h"
  10. #define PIXELS_SIZE (S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 4)
  11. #define FPS 30
  12. #define MSPF (1000 / (FPS))
  13. TPE_World world;
  14. TPE_Body bodies[128];
  15. S3L_Unit cubeVertices[] = { S3L_CUBE_VERTICES(1024) };
  16. S3L_Index cubeTriangles[] = { S3L_CUBE_TRIANGLES };
  17. S3L_Model3D cube;
  18. uint8_t pixels[PIXELS_SIZE];
  19. uint8_t red = 100;
  20. TPE_Vec3 environmentDistance(TPE_Vec3 p, TPE_Unit distLim)
  21. {
  22. TPE_Vec3 r, rMin = p;
  23. TPE_Unit d, dMin = 10000000;
  24. #define testShape(c) \
  25. r = c; if (p.x == r.x && p.y == r.y && p.z == r.z) return p; \
  26. d = TPE_DISTANCE(p,r); if (d < dMin) { dMin = d; rMin = r; }
  27. /*
  28. testShape( TPE_envHalfPlane(p,TPE_vec3(-1000,0,0),TPE_vec3(20,20,0)) )
  29. testShape( TPE_envHalfPlane(p,TPE_vec3(+1000,0,0),TPE_vec3(-20,20,0)) )
  30. */
  31. testShape( TPE_envAABoxInside(p,TPE_vec3(0,0,0),TPE_vec3(16000,6000,16000)) )
  32. testShape( TPE_envSphere(p,TPE_vec3(2000,-3500,2000),2000) )
  33. testShape( TPE_envSphere(p,TPE_vec3(-2000,-3500,0),2000) )
  34. #undef testShape
  35. return rMin;
  36. }
  37. void drawPixel(S3L_PixelInfo *p)
  38. {
  39. uint32_t index = (p->y * S3L_RESOLUTION_X + p->x) * 4;
  40. pixels[index + 1] = p->triangleIndex * 16;
  41. pixels[index + 2] = 255 - p->triangleIndex * 16;
  42. pixels[index + 3] = red;
  43. }
  44. void draw2DPoint(int x, int y, int r, int g, int b)
  45. {
  46. if (x < 1 || x > S3L_RESOLUTION_X - 3 ||
  47. y < 1 || y > S3L_RESOLUTION_Y - 3)
  48. return;
  49. uint32_t index = ((y - 1) * S3L_RESOLUTION_X + x) * 4;
  50. #define d pixels[index] = 0; pixels[index + 1] = b; pixels[index + 2] = g; pixels[index + 3] = r;
  51. d
  52. index += S3L_RESOLUTION_X * 4 - 4;
  53. d
  54. index += 4;
  55. d
  56. index += 4;
  57. d
  58. index += S3L_RESOLUTION_X * 4 - 4;
  59. d
  60. #undef d
  61. }
  62. void debugDrawPixel(uint16_t x, uint16_t y, uint8_t color)
  63. {
  64. draw2DPoint(x,y,255 - color * 64,color * 128,color * 64);
  65. }
  66. void drawLine(int x1, int y1, int x2, int y2, int r, int g, int b)
  67. {
  68. #define STEPS 20
  69. float dx = (x2 - x1) / ((float) STEPS);
  70. float dy = (y2 - y1) / ((float) STEPS);
  71. for (int i = 0; i < STEPS; ++i)
  72. draw2DPoint(x1 + dx * i, y1 + dy * i,r,g,b);
  73. #undef STEPS
  74. }
  75. S3L_Scene sphereScene;
  76. void draw3DLine(int x1, int y1, int z1, int x2, int y2, int z2)
  77. {
  78. S3L_Vec4 p1, p2, r1, r2;
  79. S3L_vec4Set(&p1,x1,y1,z1,0);
  80. S3L_vec4Set(&p2,x2,y2,z2,0);
  81. S3L_project3DPointToScreen(p1,sphereScene.camera,&r1);
  82. S3L_project3DPointToScreen(p2,sphereScene.camera,&r2);
  83. if (r1.z > 0 && r2.z > 0)
  84. drawLine(r1.x,r1.y,r2.x,r2.y,200,100,200);
  85. }
  86. void drawSphere(S3L_Unit x, S3L_Unit y, S3L_Unit z, S3L_Unit r)
  87. {
  88. sphereScene.models[0].transform.translation.x = x;
  89. sphereScene.models[0].transform.translation.y = y;
  90. sphereScene.models[0].transform.translation.z = z;
  91. sphereScene.models[0].transform.scale.x = r;
  92. sphereScene.models[0].transform.scale.y = r;
  93. sphereScene.models[0].transform.scale.z = r;
  94. S3L_drawScene(sphereScene);
  95. }
  96. void drawBody(TPE_Body *body, uint8_t color)
  97. {
  98. red = color;
  99. for (int i = 0; i < body->jointCount; ++i)
  100. drawSphere(
  101. body->joints[i].position.x,
  102. body->joints[i].position.y,
  103. body->joints[i].position.z,
  104. TPE_JOINT_SIZE(body->joints[i]));
  105. for (int i = 0; i < body->connectionCount; ++i)
  106. {
  107. S3L_Vec4 p1, p2, r1, r2;
  108. S3L_vec4Set(&p1,
  109. body->joints[body->connections[i].joint1].position.x,
  110. body->joints[body->connections[i].joint1].position.y,
  111. body->joints[body->connections[i].joint1].position.z,0);
  112. S3L_vec4Set(&p2,
  113. body->joints[body->connections[i].joint2].position.x,
  114. body->joints[body->connections[i].joint2].position.y,
  115. body->joints[body->connections[i].joint2].position.z,0);
  116. S3L_project3DPointToScreen(p1,sphereScene.camera,&r1);
  117. S3L_project3DPointToScreen(p2,sphereScene.camera,&r2);
  118. if (r1.z > 0 && r2.z > 0)
  119. drawLine(r1.x,r1.y,r2.x,r2.y,100,200,300);
  120. }
  121. }
  122. #define SPHERE_VERTEX_COUNT 42
  123. const S3L_Unit sphereVertices[SPHERE_VERTEX_COUNT * 3] = {
  124. 0, -512, 0, // 0
  125. 370, -228, -269, // 3
  126. -141, -228, -435, // 6
  127. -457, -228, 0, // 9
  128. -141, -228, 435, // 12
  129. 370, -228, 269, // 15
  130. 141, 228, -435, // 18
  131. -370, 228, -269, // 21
  132. -370, 228, 269, // 24
  133. 141, 228, 435, // 27
  134. 457, 228, 0, // 30
  135. 0, 512, 0, // 33
  136. -83, -435, -255, // 36
  137. 217, -435, -158, // 39
  138. 134, -269, -414, // 42
  139. 435, -269, 0, // 45
  140. 217, -435, 158, // 48
  141. -269, -435, 0, // 51
  142. -352, -269, -255, // 54
  143. -83, -435, 255, // 57
  144. -352, -269, 255, // 60
  145. 134, -269, 414, // 63
  146. 486, 0, -158, // 66
  147. 486, 0, 158, // 69
  148. 0, 0, -512, // 72
  149. 300, 0, -414, // 75
  150. -486, 0, -158, // 78
  151. -300, 0, -414, // 81
  152. -300, 0, 414, // 84
  153. -486, 0, 158, // 87
  154. 300, 0, 414, // 90
  155. 0, 0, 512, // 93
  156. 352, 269, -255, // 96
  157. -134, 269, -414, // 99
  158. -435, 269, 0, // 102
  159. -134, 269, 414, // 105
  160. 352, 269, 255, // 108
  161. 83, 435, -255, // 111
  162. 269, 435, 0, // 114
  163. -217, 435, -158, // 117
  164. -217, 435, 158, // 120
  165. 83, 435, 255 // 123
  166. }; // sphereVertices
  167. #define SPHERE_TRIANGLE_COUNT 80
  168. const S3L_Index sphereTriangleIndices[SPHERE_TRIANGLE_COUNT * 3] = {
  169. 0, 13, 12, // 0
  170. 1, 13, 15, // 3
  171. 0, 12, 17, // 6
  172. 0, 17, 19, // 9
  173. 0, 19, 16, // 12
  174. 1, 15, 22, // 15
  175. 2, 14, 24, // 18
  176. 3, 18, 26, // 21
  177. 4, 20, 28, // 24
  178. 5, 21, 30, // 27
  179. 1, 22, 25, // 30
  180. 2, 24, 27, // 33
  181. 3, 26, 29, // 36
  182. 4, 28, 31, // 39
  183. 5, 30, 23, // 42
  184. 6, 32, 37, // 45
  185. 7, 33, 39, // 48
  186. 8, 34, 40, // 51
  187. 9, 35, 41, // 54
  188. 10, 36, 38, // 57
  189. 38, 41, 11, // 60
  190. 38, 36, 41, // 63
  191. 36, 9, 41, // 66
  192. 41, 40, 11, // 69
  193. 41, 35, 40, // 72
  194. 35, 8, 40, // 75
  195. 40, 39, 11, // 78
  196. 40, 34, 39, // 81
  197. 34, 7, 39, // 84
  198. 39, 37, 11, // 87
  199. 39, 33, 37, // 90
  200. 33, 6, 37, // 93
  201. 37, 38, 11, // 96
  202. 37, 32, 38, // 99
  203. 32, 10, 38, // 102
  204. 23, 36, 10, // 105
  205. 23, 30, 36, // 108
  206. 30, 9, 36, // 111
  207. 31, 35, 9, // 114
  208. 31, 28, 35, // 117
  209. 28, 8, 35, // 120
  210. 29, 34, 8, // 123
  211. 29, 26, 34, // 126
  212. 26, 7, 34, // 129
  213. 27, 33, 7, // 132
  214. 27, 24, 33, // 135
  215. 24, 6, 33, // 138
  216. 25, 32, 6, // 141
  217. 25, 22, 32, // 144
  218. 22, 10, 32, // 147
  219. 30, 31, 9, // 150
  220. 30, 21, 31, // 153
  221. 21, 4, 31, // 156
  222. 28, 29, 8, // 159
  223. 28, 20, 29, // 162
  224. 20, 3, 29, // 165
  225. 26, 27, 7, // 168
  226. 26, 18, 27, // 171
  227. 18, 2, 27, // 174
  228. 24, 25, 6, // 177
  229. 24, 14, 25, // 180
  230. 14, 1, 25, // 183
  231. 22, 23, 10, // 186
  232. 22, 15, 23, // 189
  233. 15, 5, 23, // 192
  234. 16, 21, 5, // 195
  235. 16, 19, 21, // 198
  236. 19, 4, 21, // 201
  237. 19, 20, 4, // 204
  238. 19, 17, 20, // 207
  239. 17, 3, 20, // 210
  240. 17, 18, 3, // 213
  241. 17, 12, 18, // 216
  242. 12, 2, 18, // 219
  243. 15, 16, 5, // 222
  244. 15, 13, 16, // 225
  245. 13, 0, 16, // 228
  246. 12, 14, 2, // 231
  247. 12, 13, 14, // 234
  248. 13, 1, 14 // 237
  249. }; // sphereTriangleIndices
  250. int main(void)
  251. {
  252. SDL_Window *window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN);
  253. SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
  254. SDL_Texture *textureSDL = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y);
  255. SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
  256. SDL_Event event;
  257. int running = 1;
  258. S3L_Model3D sphereModel;
  259. S3L_model3DInit(sphereVertices,SPHERE_VERTEX_COUNT,sphereTriangleIndices,
  260. SPHERE_TRIANGLE_COUNT,&sphereModel);
  261. S3L_model3DInit(cubeVertices,S3L_CUBE_VERTEX_COUNT,cubeTriangles,
  262. S3L_CUBE_TRIANGLE_COUNT,&cube);
  263. S3L_sceneInit(&sphereModel,1,&sphereScene);
  264. sphereScene.camera.transform.translation.z = -512 -512 -512;
  265. int frame = 0;
  266. TPE_Joint joints[100];
  267. TPE_Connection connections[100];
  268. #define MASS 200
  269. switch (1)
  270. {
  271. case 0:
  272. TPE_make2Line(joints,connections,1500,512);
  273. TPE_bodyInit(bodies,joints,2,connections,1,MASS);
  274. break;
  275. case 1:
  276. TPE_makeBox(joints,connections,1000,1000,1000,500);
  277. // TPE_bodyInit(bodies,joints,8,connections,16,MASS);
  278. TPE_bodyInit(bodies,joints,1,connections,0,MASS);
  279. break;
  280. case 2:
  281. TPE_makeCenterRect(joints,connections,1300,1000,512);
  282. TPE_bodyInit(bodies,joints,5,connections,8,MASS);
  283. break;
  284. case 3:
  285. TPE_makeCenterBox(joints,connections,1000,1000,1000,512);
  286. joints[8].sizeDivided *= 3;
  287. joints[8].sizeDivided /= 2;
  288. TPE_bodyInit(bodies,joints,9,connections,18,MASS);
  289. break;
  290. case 4:
  291. TPE_makeTriangle(joints,connections,2000,512);
  292. TPE_bodyInit(bodies,joints,3,connections,3,MASS);
  293. break;
  294. case 5:
  295. TPE_make2Line(joints,connections,1500,200);
  296. TPE_bodyInit(bodies,joints,1,connections,0,MASS);
  297. break;
  298. default: break;
  299. }
  300. //TPE_make2Line(joints + 20,connections + 20,1000,200);
  301. //TPE_bodyInit(bodies + 1,joints + 20,1,connections + 20,0,MASS);
  302. TPE_makeBox(joints + 20,connections + 20,1000,1000,1000,500);
  303. TPE_bodyInit(bodies + 1,joints + 20,8,connections + 20,16,100);
  304. //bodies[0].flags |= TPE_BODY_FLAG_SOFT;
  305. //bodies[0].elasticity = 256;
  306. TPE_worldInit(&world,bodies,2,environmentDistance);
  307. #define FR 256
  308. world.bodies[0].friction = FR;
  309. world.bodies[1].friction = FR;
  310. TPE_bodyMove(world.bodies,TPE_vec3(500,-200,400));
  311. TPE_bodyMove(&world.bodies[1],TPE_vec3(-500,800,400));
  312. TPE_bodyStop(world.bodies);
  313. TPE_bodyStop(world.bodies + 1);
  314. TPE_bodyAccelerate(world.bodies,TPE_vec3(-50,0,0));
  315. TPE_bodyAccelerate(world.bodies + 1,TPE_vec3(50,0,0));
  316. //TPE_bodyRotate(world.bodies,TPE_vec3(0,0,200));
  317. //-------
  318. int time;
  319. while (running)
  320. {
  321. time = SDL_GetTicks();
  322. for (uint32_t i = 0; i < PIXELS_SIZE; ++i)
  323. pixels[i] = 0;
  324. S3L_newFrame();
  325. //TPE_bodiesResolveCollision(world.bodies,world.bodies + 1);
  326. for (int i = 0; i < world.bodyCount; ++i)
  327. {
  328. if (!(world.bodies[i].flags & TPE_BODY_FLAG_DEACTIVATED))
  329. TPE_bodyAccelerate(world.bodies + i,TPE_vec3(0,-6,0));
  330. }
  331. TPE_worldStep(&world);
  332. while (SDL_PollEvent(&event))
  333. {
  334. if (event.type == SDL_QUIT)
  335. running = 0;
  336. else if (event.type == SDL_KEYDOWN)
  337. {
  338. if (event.key.keysym.scancode == SDL_SCANCODE_Q || event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
  339. running = 0;
  340. }
  341. }
  342. const uint8_t *state = SDL_GetKeyboardState(NULL);
  343. S3L_Vec4 camF, camR;
  344. #define SHIFT_STEP 50
  345. #define ROT_STEP 5
  346. S3L_rotationToDirections(sphereScene.camera.transform.rotation,SHIFT_STEP,&camF,&camR,0);
  347. TPE_Vec3 forw = TPE_vec3Minus(
  348. bodies[0].joints[2].position,
  349. bodies[0].joints[0].position);
  350. TPE_Vec3 righ = TPE_vec3Minus(
  351. bodies[0].joints[0].position,
  352. bodies[0].joints[1].position);
  353. /*
  354. forw = TPE_vec3Plus(forw,
  355. TPE_vec3Minus(
  356. bodies[0].joints[6].position,
  357. bodies[0].joints[4].position)
  358. );
  359. righ = TPE_vec3Plus(righ,
  360. TPE_vec3Minus(
  361. bodies[0].joints[4].position,
  362. bodies[0].joints[5].position)
  363. );
  364. */
  365. TPE_Vec3 rrrr = TPE_orientationFromVecs(forw,righ);
  366. cube.transform.rotation.x = rrrr.x;
  367. cube.transform.rotation.y = rrrr.y;
  368. cube.transform.rotation.z = rrrr.z;
  369. cube.transform.scale.x = 600;
  370. cube.transform.scale.y = cube.transform.scale.x;
  371. cube.transform.scale.z = cube.transform.scale.x;
  372. TPE_Vec3 ppp = TPE_bodyGetCenter(&bodies[0]);
  373. cube.transform.translation.x = ppp.x;
  374. cube.transform.translation.y = ppp.y;
  375. cube.transform.translation.z = ppp.z;
  376. TPE_Vec3 camPos = TPE_vec3(
  377. sphereScene.camera.transform.translation.x,
  378. sphereScene.camera.transform.translation.y,
  379. sphereScene.camera.transform.translation.z);
  380. TPE_Vec3 camRot = TPE_vec3(
  381. sphereScene.camera.transform.rotation.x,
  382. sphereScene.camera.transform.rotation.y,
  383. sphereScene.camera.transform.rotation.z);
  384. //drawEnv(TPE_vec3(-100,-100,-100),100,5);
  385. //for (int i = 0; i < world.bodyCount; ++i)
  386. // drawBody(&(world.bodies[i]),100 * i);
  387. sphereScene.models = &cube;
  388. S3L_newFrame();
  389. //S3L_drawScene(sphereScene);
  390. sphereScene.models = &sphereModel;
  391. TPE_worldDebugDraw(&world,debugDrawPixel,
  392. camPos,camRot,TPE_vec3(S3L_RESOLUTION_X,S3L_RESOLUTION_Y,sphereScene.camera.focalLength));
  393. /*
  394. draw3DLine(0,0,0,forw.x,forw.y,forw.z);
  395. draw3DLine(0,0,0,righ.x,righ.y,righ.z);
  396. */
  397. SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
  398. if (state[SDL_SCANCODE_LSHIFT])
  399. {
  400. if (state[SDL_SCANCODE_UP])
  401. S3L_vec3Add(&sphereScene.camera.transform.translation,camF);
  402. else if (state[SDL_SCANCODE_DOWN])
  403. S3L_vec3Sub(&sphereScene.camera.transform.translation,camF);
  404. else if (state[SDL_SCANCODE_LEFT])
  405. S3L_vec3Sub(&sphereScene.camera.transform.translation,camR);
  406. else if (state[SDL_SCANCODE_RIGHT])
  407. S3L_vec3Add(&sphereScene.camera.transform.translation,camR);
  408. }
  409. else
  410. {
  411. if (state[SDL_SCANCODE_UP])
  412. sphereScene.camera.transform.rotation.x += ROT_STEP;
  413. else if (state[SDL_SCANCODE_DOWN])
  414. sphereScene.camera.transform.rotation.x -= ROT_STEP;
  415. else if (state[SDL_SCANCODE_LEFT])
  416. sphereScene.camera.transform.rotation.y += ROT_STEP;
  417. else if (state[SDL_SCANCODE_RIGHT])
  418. sphereScene.camera.transform.rotation.y -= ROT_STEP;
  419. else if (state[SDL_SCANCODE_K])
  420. sphereScene.camera.transform.rotation.z += ROT_STEP;
  421. else if (state[SDL_SCANCODE_L])
  422. sphereScene.camera.transform.rotation.z -= ROT_STEP;
  423. }
  424. if (state[SDL_SCANCODE_M])
  425. {
  426. TPE_bodyWake(world.bodies);
  427. TPE_bodySpin(bodies,TPE_vec3(50,40,100));
  428. TPE_bodyAccelerate(bodies,TPE_vec3(
  429. 500,
  430. 500,
  431. 30));
  432. }
  433. #define S 10
  434. if (state[SDL_SCANCODE_J])
  435. TPE_bodyAccelerate(bodies,TPE_vec3(S,0,0));
  436. else if (state[SDL_SCANCODE_G])
  437. TPE_bodyAccelerate(bodies,TPE_vec3(-S,0,0));
  438. if (state[SDL_SCANCODE_Y])
  439. TPE_bodyAccelerate(bodies,TPE_vec3(0,0,S));
  440. else if (state[SDL_SCANCODE_H])
  441. TPE_bodyAccelerate(bodies,TPE_vec3(0,0,-S));
  442. #undef S
  443. #define SHIFT_STEP 50
  444. if (state[SDL_SCANCODE_P])
  445. sphereScene.camera.transform.translation.y += SHIFT_STEP;
  446. else if (state[SDL_SCANCODE_O])
  447. sphereScene.camera.transform.translation.y -= SHIFT_STEP;
  448. #undef SHIFT_STEP
  449. SDL_RenderClear(renderer);
  450. SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
  451. SDL_RenderPresent(renderer);
  452. time = time + MSPF - SDL_GetTicks();
  453. if (time > 1)
  454. usleep(time * 1000);
  455. frame++;
  456. }
  457. return 0;
  458. }