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.

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