Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

btphystest.cpp 21 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. //
  2. // BtPhysTest
  3. //
  4. // Copyright: (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
  6. //
  7. #if defined HAVE_CONFIG_H
  8. # include "config.h"
  9. #endif
  10. #include "core.h"
  11. #include "loldebug.h"
  12. using namespace lol;
  13. #ifndef HAVE_PHYS_USE_BULLET
  14. #define HAVE_PHYS_USE_BULLET
  15. #endif /* HAVE_PHYS_USE_BULLET */
  16. #include "physics/lolphysics.h"
  17. #include "physics/easyphysics.h"
  18. #define CAT_MODE 0
  19. #define OBJ_SIZE 2.f
  20. #include "physicobject.h"
  21. #include "btphystest.h"
  22. using namespace lol::phys;
  23. #define CUBE_HALF_EXTENTS .5f
  24. #define EXTRA_HEIGHT 1.f
  25. #define BASE_TIME 2.f
  26. #define ZERO_TIME (BASE_TIME + rand(-BASE_TIME * .4f, BASE_TIME * .4f))
  27. #define ZERO_SPEED 3.5f
  28. #define JUMP_HEIGHT 30.f
  29. #define JUMP_STRAFE .5f
  30. int gNumObjects = 64;
  31. #if CAT_MODE
  32. #define USE_WALL 1
  33. #define USE_BODIES 1
  34. #else
  35. #define USE_WALL 1
  36. #define USE_PLATFORM 1
  37. #define USE_ROPE 0
  38. #define USE_BODIES 1
  39. #define USE_ROTATION 0
  40. #define USE_CHARACTER 0
  41. #define USE_STAIRS 0
  42. #endif
  43. LOLFX_RESOURCE_DECLARE(front_camera_sprite);
  44. BtPhysTest::BtPhysTest(bool editor)
  45. {
  46. m_loop_value = .0f;
  47. g_renderer->SetAlphaFunc(AlphaFunc::Greater, 0.0);
  48. #if CAT_MODE
  49. /* cat datas setup */
  50. m_cat_texture = Tiler::Register("data/CatsSheet.png", ivec2(0), ivec2(0,1));
  51. #endif //CAT_MODE
  52. /* Register an input controller for the keyboard */
  53. m_controller = new Controller(KEY_MAX, 0);
  54. m_controller->GetKey(KEY_MOVE_FORWARD).Bind("Keyboard", "Up");
  55. m_controller->GetKey(KEY_MOVE_BACK).Bind("Keyboard", "Down");
  56. m_controller->GetKey(KEY_MOVE_LEFT).Bind("Keyboard", "Left");
  57. m_controller->GetKey(KEY_MOVE_RIGHT).Bind("Keyboard", "Right");
  58. m_controller->GetKey(KEY_MOVE_JUMP).Bind("Keyboard", "Space");
  59. m_controller->GetKey(KEY_MOVE_UP).Bind("Keyboard", "PageUp");
  60. m_controller->GetKey(KEY_MOVE_DOWN).Bind("Keyboard", "PageDown");
  61. m_controller->GetKey(KEY_QUIT).Bind("Keyboard", "Escape");
  62. /* Create a camera that matches the settings of XNA BtPhysTest */
  63. m_camera = new Camera();
  64. #if CAT_MODE
  65. m_camera->SetView(vec3(70.f, 50.f, 0.f),
  66. vec3(0.f, 0.f, 0.f),
  67. vec3(0, 1, 0));
  68. m_camera->SetProjection(mat4::perspective(60.f, 1280.f, 960.f, .1f, 1000.f));
  69. #else
  70. m_camera->SetView(vec3(50.f, 50.f, 0.f),
  71. vec3(0.f, 0.f, 0.f),
  72. vec3(0, 1, 0));
  73. m_camera->SetProjection(mat4::perspective(45.f, 1280.f, 960.f, .1f, 1000.f));
  74. #endif
  75. g_scene->PushCamera(m_camera);
  76. m_ready = false;
  77. m_simulation = new Simulation();
  78. m_simulation->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
  79. m_simulation->Init();
  80. vec3 NewGravity = vec3(.0f, -10.0f, .0f);
  81. m_simulation->SetGravity(NewGravity);
  82. m_simulation->SetContinuousDetection(true);
  83. m_simulation->SetTimestep(1.f / 120.f);
  84. Ticker::Ref(m_simulation);
  85. /* Add a white directional light */
  86. m_light1 = new Light();
  87. m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
  88. m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
  89. Ticker::Ref(m_light1);
  90. /* Add an orangeish point light */
  91. m_light2 = new Light();
  92. m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
  93. m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
  94. Ticker::Ref(m_light2);
  95. float offset = 29.5f;
  96. vec3 pos_offset = vec3(.0f, 30.f, .0f);
  97. #if USE_STAIRS
  98. {
  99. vec3 new_offset = vec3(1.0f, .125f, .0f);
  100. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  101. vec3 NewPosition = pos_offset + vec3(5.0f, -29.f, 15.0f);
  102. {
  103. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 30.f);
  104. NewPosition += vec3(4.0f, .0f, -4.0f);
  105. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  106. Ticker::Ref(NewPhyobj);
  107. m_stairs_list << NewPhyobj;
  108. }
  109. {
  110. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 40.f);
  111. NewPosition += vec3(4.0f, .0f, -4.0f);
  112. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  113. Ticker::Ref(NewPhyobj);
  114. m_stairs_list << NewPhyobj;
  115. }
  116. NewPosition = pos_offset + vec3(5.0f, -29.5f, 15.0f);
  117. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  118. for (int i=0; i < 15; i++)
  119. {
  120. NewPosition += new_offset;
  121. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
  122. Ticker::Ref(NewPhyobj);
  123. m_stairs_list << NewPhyobj;
  124. }
  125. }
  126. #endif //USE_STAIRS
  127. #if USE_WALL
  128. {
  129. for (int i=0; i < 6; i++)
  130. {
  131. vec3 NewPosition = vec3(.0f);
  132. quat NewRotation = quat(1.f);
  133. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
  134. int idx = i/2;
  135. NewPosition = pos_offset;
  136. NewPosition[idx] += offset;
  137. offset *= -1.f;
  138. if (idx != 1)
  139. {
  140. vec3 NewAxis = vec3(.0f);
  141. NewAxis[2 - idx] = 1;
  142. NewRotation = quat::rotate(90.f, NewAxis);
  143. }
  144. NewPhyobj->SetTransform(NewPosition, NewRotation);
  145. Ticker::Ref(NewPhyobj);
  146. m_ground_list << NewPhyobj;
  147. }
  148. }
  149. #endif //USE_WALL
  150. PhysicsObject* BasePhyobj = NULL;
  151. #if USE_PLATFORM
  152. {
  153. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  154. vec3 NewPosition = pos_offset + vec3(5.0f, -25.0f, -15.0f);
  155. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  156. m_platform_list << NewPhyobj;
  157. Ticker::Ref(NewPhyobj);
  158. NewPosition = pos_offset + vec3(-15.0f, -25.0f, 5.0f);
  159. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  160. BasePhyobj = NewPhyobj;
  161. m_platform_list << NewPhyobj;
  162. Ticker::Ref(NewPhyobj);
  163. NewRotation = quat::fromeuler_xyz(0.f, 0.f, 90.f);
  164. NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f);
  165. NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  166. NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  167. m_platform_list << NewPhyobj;
  168. Ticker::Ref(NewPhyobj);
  169. //NewPosition += vec3(-0.0f, .0f, .0f);
  170. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  171. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, false);
  172. //m_platform_list << NewPhyobj;
  173. //Ticker::Ref(NewPhyobj);
  174. //NewPosition += vec3(-2.0f, .0f, .0f);
  175. //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
  176. //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), false, false);
  177. //m_platform_list << NewPhyobj;
  178. //Ticker::Ref(NewPhyobj);
  179. }
  180. #endif //USE_PLATFORM
  181. #if USE_CHARACTER
  182. {
  183. quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
  184. vec3 NewPosition = pos_offset + vec3(-5.0f, -10.0f, 15.0f);
  185. PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2);
  186. m_character_list << NewPhyobj;
  187. Ticker::Ref(NewPhyobj);
  188. //NewPhyobj->GetCharacter()->AttachTo(BasePhyobj->GetPhysic(), true, true);
  189. }
  190. #endif //USE_CHARACTER
  191. #if USE_BODIES
  192. {
  193. for (int x=0; x < 6; x++)
  194. {
  195. for (int y=0; y < 2; y++)
  196. {
  197. for (int z=0; z < 5; z++)
  198. {
  199. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  200. vec3(-20.f, 15.f, -20.f) +
  201. vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z));
  202. m_physobj_list.Push(new_physobj, ZERO_TIME);
  203. Ticker::Ref(new_physobj);
  204. }
  205. }
  206. }
  207. }
  208. #endif //USE_BODIES
  209. #if USE_ROPE
  210. {
  211. Array<PhysicsObject*> RopeElements;
  212. for (int i = 0; i < 14; i++)
  213. {
  214. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
  215. vec3(0.f, 15.f, -20.f) +
  216. vec3(0.f, 0.f, 2.f * (float)i), 1);
  217. RopeElements << new_physobj;
  218. m_physobj_list.Push(new_physobj, ZERO_TIME);
  219. Ticker::Ref(new_physobj);
  220. if (RopeElements.Count() > 1)
  221. {
  222. EasyConstraint* new_constraint = new EasyConstraint();
  223. vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform().v3.xyz -
  224. RopeElements[i - 1]->GetPhysic()->GetTransform().v3.xyz);
  225. new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B));
  226. new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B));
  227. new_constraint->InitConstraintToPoint2Point();
  228. new_constraint->DisableCollisionBetweenObjs(true);
  229. new_constraint->AddToSimulation(m_simulation);
  230. m_constraint_list << new_constraint;
  231. }
  232. }
  233. }
  234. #endif //USE_ROPE
  235. }
  236. void BtPhysTest::TickGame(float seconds)
  237. {
  238. WorldEntity::TickGame(seconds);
  239. if (m_controller->GetKey(KEY_QUIT).IsReleased())
  240. Ticker::Shutdown();
  241. m_loop_value += seconds;
  242. if (m_loop_value > F_PI * 2.0f)
  243. m_loop_value -= F_PI * 2.0f;
  244. vec3 GroundBarycenter = vec3(.0f);
  245. vec3 PhysObjBarycenter = vec3(.0f);
  246. float factor = .0f;
  247. #if CAT_MODE
  248. #if USE_BODIES
  249. vec3 cam_center(0.f);
  250. float cam_factor = .0f;
  251. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  252. vec3 cam_min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
  253. mat4 world_cam = g_scene->GetCamera()->GetView();
  254. mat4 cam_screen = g_scene->GetCamera()->GetProjection();
  255. for (int i = 0; i < m_physobj_list.Count(); i++)
  256. {
  257. PhysicsObject* PhysObj = m_physobj_list[i].m1;
  258. float &Timer = m_physobj_list[i].m2;
  259. vec3 obj_loc = PhysObj->GetPhysic()->GetTransform().v3.xyz;
  260. cam_center += obj_loc;
  261. cam_factor += 1.f;
  262. mat4 LocalPos = mat4::translate(obj_loc);
  263. vec3 vpos;
  264. LocalPos = world_cam * LocalPos;
  265. vpos = LocalPos.v3.xyz;
  266. cam_min_max[0] = min(vpos.xyz, cam_min_max[0]);
  267. cam_min_max[1] = max(vpos.xyz, cam_min_max[1]);
  268. LocalPos = cam_screen * LocalPos;
  269. vpos = (LocalPos.v3 / LocalPos.v3.w).xyz;
  270. screen_min_max[0] = min(vpos.xy, screen_min_max[0]);
  271. screen_min_max[1] = max(vpos.xy, screen_min_max[1]);
  272. //if (length(PhysObj->GetPhysic()->GetLinearVelocity()) < ZERO_SPEED)
  273. if (lol::abs(PhysObj->GetPhysic()->GetLinearVelocity().y) < ZERO_SPEED)
  274. Timer -= seconds;
  275. if (Timer < .0f)
  276. {
  277. PhysObj->GetPhysic()->AddImpulse(JUMP_HEIGHT *
  278. vec3(JUMP_STRAFE, 1.f, JUMP_STRAFE) *
  279. vec3(rand(-1.f, 1.f), 1.0f, rand(-1.f, 1.f)) *
  280. PhysObj->GetPhysic()->GetMass());
  281. Timer = ZERO_TIME;
  282. }
  283. }
  284. vec3 min_max_diff = (cam_min_max[1] - cam_min_max[0]);
  285. float screen_size = max(max(lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)),
  286. max( lol::abs(min_max_diff.x), lol::abs(min_max_diff.y)));
  287. float fov_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
  288. max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
  289. //m_camera->SetProjection(mat4::perspective(30.f * fov_ratio * 1.1f, 1280.f, 960.f, .1f, 1000.f));
  290. m_camera->SetView((mat4::rotate(10.f * seconds, vec3(.0f, 1.f, .0f)) * vec4(m_camera->GetPosition(), 1.0f)).xyz,
  291. //vec3(70.f, 30.f, 0.f),
  292. cam_center / cam_factor, vec3(0, 1, 0));
  293. #endif //USE_BODIES
  294. #endif //CAT_MODE
  295. #if USE_WALL
  296. {
  297. for (int i = 0; i < m_ground_list.Count(); i++)
  298. {
  299. PhysicsObject* PhysObj = m_ground_list[i];
  300. mat4 GroundMat = PhysObj->GetTransform();
  301. GroundBarycenter += GroundMat.v3.xyz;
  302. factor += 1.f;
  303. }
  304. GroundBarycenter /= factor;
  305. for (int i = 0; i < m_ground_list.Count(); i++)
  306. {
  307. PhysicsObject* PhysObj = m_ground_list[i];
  308. mat4 GroundMat = PhysObj->GetTransform();
  309. vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter;
  310. vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
  311. if (dot(normalize(CenterToCam - CenterToGround),
  312. normalize(CenterToGround)) > 0.f)
  313. PhysObj->SetRender(false);
  314. else
  315. PhysObj->SetRender(true);
  316. }
  317. }
  318. #endif //USE_WALL
  319. #if USE_ROTATION
  320. {
  321. for (int i = 0; i < m_ground_list.Count(); i++)
  322. {
  323. PhysicsObject* PhysObj = m_ground_list[i];
  324. mat4 GroundMat = PhysObj->GetTransform();
  325. mat4 CenterMx = mat4::translate(GroundBarycenter);
  326. GroundMat = inverse(CenterMx) * GroundMat;
  327. GroundMat = CenterMx *
  328. mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds))
  329. * GroundMat;
  330. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  331. }
  332. }
  333. #endif //USE_ROTATION
  334. #if USE_PLATFORM
  335. {
  336. for (int i = 0; i < m_platform_list.Count(); i++)
  337. {
  338. PhysicsObject* PhysObj = m_platform_list[i];
  339. mat4 GroundMat = PhysObj->GetTransform();
  340. if (i == 0)
  341. {
  342. GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(20.f, .0f, .0f) * seconds));
  343. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  344. }
  345. else if (i == 1)
  346. {
  347. GroundMat =
  348. mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
  349. mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * 20.f, .0f)));
  350. PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
  351. }
  352. }
  353. }
  354. #endif //USE_PLATFORM
  355. #if USE_CHARACTER
  356. {
  357. for (int i = 0; i < m_character_list.Count(); i++)
  358. {
  359. PhysicsObject* PhysObj = m_character_list[i];
  360. EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
  361. mat4 CtlrMx = Character->GetTransform();
  362. vec3 movement(0.f);
  363. movement.z = (m_controller->GetKey(KEY_MOVE_RIGHT).IsDown() ? 1.f : 0.f)
  364. - (m_controller->GetKey(KEY_MOVE_LEFT).IsDown() ? 1.f : 0.f);
  365. movement.x = (m_controller->GetKey(KEY_MOVE_FORWARD).IsDown() ? 1.f : 0.f)
  366. - (m_controller->GetKey(KEY_MOVE_BACK).IsDown() ? 1.f : 0.f);
  367. movement.y = (m_controller->GetKey(KEY_MOVE_UP).IsDown() ? 1.f : 0.f)
  368. - (m_controller->GetKey(KEY_MOVE_DOWN).IsDown() ? 1.f : 0.f);
  369. vec3 CharMove = movement * seconds * vec3(4.f, 10.f, 4.f);
  370. if (m_controller->GetKey(KEY_MOVE_JUMP).IsReleased())
  371. Character->Jump();
  372. Character->SetMovementForFrame(CharMove);
  373. RayCastResult HitResult;
  374. if (m_simulation->RayHits(HitResult, ERT_Closest, Character->GetTransform().v3.xyz, (Character->GetTransform().v3.xyz + vec3(.0f, -1.f, .0f)), Character))
  375. Character->AttachTo(HitResult.m_collider_list[0], true, true);
  376. else
  377. Character->AttachTo(NULL);
  378. }
  379. }
  380. #endif //USE_CHARACTER
  381. #if USE_CHARACTER
  382. {
  383. PhysObjBarycenter = vec3(.0f);
  384. factor = .0f;
  385. for (int i = 0; i < m_character_list.Count(); i++)
  386. {
  387. PhysicsObject* PhysObj = m_character_list[i];
  388. mat4 GroundMat = PhysObj->GetTransform();
  389. PhysObjBarycenter += GroundMat.v3.xyz;
  390. factor += 1.f;
  391. }
  392. PhysObjBarycenter /= factor;
  393. #if 0
  394. m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
  395. vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 5.0f, .0f);
  396. m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 20.0f);
  397. #endif
  398. }
  399. #else
  400. {
  401. PhysObjBarycenter = vec3(.0f);
  402. for (int i = 0; i < m_physobj_list.Count(); i++)
  403. {
  404. PhysicsObject* PhysObj = m_physobj_list[i].m1;
  405. mat4 GroundMat = PhysObj->GetTransform();
  406. PhysObjBarycenter += GroundMat.v3.xyz;
  407. factor += 1.f;
  408. }
  409. PhysObjBarycenter /= factor;
  410. #if 0
  411. m_camera->SetTarget(PhysObjBarycenter);
  412. m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
  413. #endif
  414. }
  415. #endif //USE_CHARACTER
  416. }
  417. void BtPhysTest::TickDraw(float seconds)
  418. {
  419. WorldEntity::TickDraw(seconds);
  420. if (!m_ready)
  421. {
  422. #if CAT_MODE
  423. /* cat datas setup */
  424. m_cat_shader = Shader::Create(LOLFX_RESOURCE_NAME(front_camera_sprite));
  425. #if USE_BODIES
  426. for (int i = 0; i < m_physobj_list.Count(); i++)
  427. {
  428. PhysicsObject* PhysObj = m_physobj_list[i].m1;
  429. m_cat_sdata = new CatShaderData(((1 << VertexUsage::Position) |
  430. (1 << VertexUsage::Color) |
  431. (1 << VertexUsage::TexCoord) |
  432. (1 << VertexUsage::TexCoordExt)),
  433. m_cat_shader);
  434. m_cat_sdata->m_shader_texture = m_cat_texture->GetTexture();
  435. PhysObj->SetCustomShaderData(m_cat_sdata);
  436. }
  437. #endif //USE_BODIES
  438. #endif //CAT_MODE
  439. /* FIXME: this object never cleans up */
  440. m_ready = true;
  441. }
  442. //Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  443. }
  444. BtPhysTest::~BtPhysTest()
  445. {
  446. g_scene->PopCamera(m_camera);
  447. Ticker::Unref(m_light1);
  448. Ticker::Unref(m_light2);
  449. #if CAT_MODE
  450. /* cat datas setup */
  451. delete(m_cat_sdata);
  452. Shader::Destroy(m_cat_shader);
  453. Tiler::Deregister(m_cat_texture);
  454. #endif //CAT_MODE
  455. while (m_constraint_list.Count())
  456. {
  457. EasyConstraint* CurPop = m_constraint_list.Last();
  458. m_constraint_list.Pop();
  459. CurPop->RemoveFromSimulation(m_simulation);
  460. delete CurPop;
  461. }
  462. while (m_ground_list.Count())
  463. {
  464. PhysicsObject* CurPop = m_ground_list.Last();
  465. m_ground_list.Pop();
  466. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  467. Ticker::Unref(CurPop);
  468. }
  469. while (m_stairs_list.Count())
  470. {
  471. PhysicsObject* CurPop = m_stairs_list.Last();
  472. m_stairs_list.Pop();
  473. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  474. Ticker::Unref(CurPop);
  475. }
  476. while (m_character_list.Count())
  477. {
  478. PhysicsObject* CurPop = m_character_list.Last();
  479. m_character_list.Pop();
  480. CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
  481. Ticker::Unref(CurPop);
  482. }
  483. while (m_platform_list.Count())
  484. {
  485. PhysicsObject* CurPop = m_platform_list.Last();
  486. m_platform_list.Pop();
  487. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  488. Ticker::Unref(CurPop);
  489. }
  490. while (m_physobj_list.Count())
  491. {
  492. PhysicsObject* CurPop = m_physobj_list.Last().m1;
  493. m_physobj_list.Pop();
  494. CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
  495. Ticker::Unref(CurPop);
  496. }
  497. Ticker::Unref(m_simulation);
  498. }
  499. //-----------------------------------------------------------------------------
  500. // CShaderData
  501. //-----------------------------------------------------------------------------
  502. CatShaderData::CatShaderData(uint32_t vert_decl_flags, Shader* shader)
  503. : GpuShaderData(vert_decl_flags, shader, DebugRenderMode::Default)
  504. {
  505. SetupDefaultData();
  506. }
  507. //-----------------------------------------------------------------------------
  508. void CatShaderData::SetupDefaultData()
  509. {
  510. AddUniform("in_model_view");
  511. AddUniform("in_normal_mat");
  512. AddUniform("in_proj");
  513. AddUniform("in_texture");
  514. }
  515. //-----------------------------------------------------------------------------
  516. void CatShaderData::SetupShaderDatas(mat4 const &model)
  517. {
  518. mat4 proj = g_scene->GetCamera()->GetProjection();
  519. mat4 view = g_scene->GetCamera()->GetView();
  520. mat4 modelview = view * model;
  521. mat3 normalmat = transpose(inverse(mat3(view)));
  522. m_shader->SetUniform(*GetUniform("in_model_view"), modelview);
  523. m_shader->SetUniform(*GetUniform("in_normal_mat"), normalmat);
  524. m_shader->SetUniform(*GetUniform("in_proj"), proj);
  525. }
  526. int main(int argc, char **argv)
  527. {
  528. System::Init(argc, argv);
  529. Application app("BtPhysTest", ivec2(1280, 960), 60.0f);
  530. new BtPhysTest(argc > 1);
  531. app.ShowPointer(false);
  532. app.Run();
  533. return EXIT_SUCCESS;
  534. }