You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

731 lines
24 KiB

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