Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

961 рядки
35 KiB

  1. //
  2. // Lol Engine - EasyMesh tutorial
  3. //
  4. // Copyright: (c) 2011-2014 Sam Hocevar <sam@hocevar.net>
  5. // (c) 2012-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #if defined HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <cfloat> /* for FLT_MAX */
  15. #include "core.h"
  16. #include "scenesetup.h"
  17. using namespace std;
  18. using namespace lol;
  19. static int const TEXTURE_WIDTH = 256;
  20. #define NO_NACL_EM (!__native_client__ && !EMSCRIPTEN)
  21. #define NACL_EM (__native_client__ || EMSCRIPTEN)
  22. #define NO_NACL_EM_INPUT (1)
  23. #define R_M 1.f
  24. #if NACL_EM
  25. #define DEFAULT_WIDTH (800.f * R_M)
  26. #define DEFAULT_HEIGHT (400.f * R_M)
  27. #else
  28. #define DEFAULT_WIDTH (1200.f * R_M)
  29. #define DEFAULT_HEIGHT (400.f * R_M)
  30. #endif //NACL_EM
  31. #define WIDTH ((float)Video::GetSize().x)
  32. #define HEIGHT ((float)Video::GetSize().y)
  33. #define SCREEN_W (10.f / WIDTH)
  34. #define RATIO_HW (HEIGHT / WIDTH)
  35. #define RATIO_WH (WIDTH / HEIGHT)
  36. #define SCREEN_LIMIT 1.4f
  37. #define RESET_TIMER .2f
  38. #define ROT_SPEED vec2(50.f)
  39. #define ROT_CLAMP 89.f
  40. #define POS_SPEED vec2(1.2f)
  41. #define POS_CLAMP 1.f
  42. #define FOV_SPEED 20.f
  43. #define FOV_CLAMP 120.f
  44. #define ZOM_SPEED 3.f
  45. #define ZOM_CLAMP 20.f
  46. #define HST_SPEED .5f
  47. #define HST_CLAMP 1.f
  48. #define WITH_TEXTURE 0
  49. #define HAS_KBOARD (m_input_usage & (1<<IPT_MV_KBOARD))
  50. #define HAS_MOUSE (m_input_usage & (1<<IPT_MV_MOUSE))
  51. LOLFX_RESOURCE_DECLARE(shinyfur);
  52. LOLFX_RESOURCE_DECLARE(shinymvtexture);
  53. enum
  54. {
  55. IPT_MV_KBOARD = 0,
  56. IPT_MV_MOUSE,
  57. INPUT_MAX
  58. };
  59. enum MVKeyboardList
  60. {
  61. KEY_CAM_RESET = 0,
  62. KEY_CAM_POS,
  63. KEY_CAM_FOV,
  64. KEY_CAM_UP,
  65. KEY_CAM_DOWN,
  66. KEY_CAM_LEFT,
  67. KEY_CAM_RIGHT,
  68. KEY_MESH_NEXT,
  69. KEY_MESH_PREV,
  70. KEY_F1,
  71. KEY_F2,
  72. KEY_F3,
  73. KEY_F4,
  74. KEY_F5,
  75. KEY_ESC,
  76. KEY_MAX
  77. };
  78. enum MVMouseKeyList
  79. {
  80. MSE_CAM_ROT = KEY_MAX,
  81. MSE_CAM_POS,
  82. MSE_CAM_FOV,
  83. MSE_FOCUS,
  84. MSE_MAX
  85. };
  86. enum MVMouseAxisList
  87. {
  88. MSEX_CAM_Y = 0,
  89. MSEX_CAM_X,
  90. MSEX_MAX
  91. };
  92. #define MAX_KEYS MSE_MAX
  93. #define MAX_AXIS MSEX_MAX
  94. #define ALL_FEATURES 1
  95. #define NO_SC_SETUP 0
  96. enum GizmoType
  97. {
  98. GZ_Editor = 0,
  99. GZ_LightPos,
  100. GZ_LightDir,
  101. GZ_MAX
  102. };
  103. struct LightData
  104. {
  105. LightData(vec3 pos, vec4 col)
  106. {
  107. m_pos = pos;
  108. m_col = col;
  109. }
  110. vec3 m_pos;
  111. vec4 m_col;
  112. };
  113. class TargetCamera
  114. {
  115. public:
  116. void EmptyTargets() { m_targets.Empty(); }
  117. void AddTarget(vec3 new_target) { m_targets << new_target; }
  118. //This considers the box usage A to B as top-left to bottom-right
  119. void AddTarget(box3 new_target)
  120. {
  121. vec3 base_off = .5f * (new_target.B - new_target.A);
  122. vec3 base_pos = new_target.A + base_off;
  123. int pass = 0;
  124. while (pass < 3)
  125. {
  126. int mask = 3 - max(0, pass - 1);
  127. while (mask-- > 0)
  128. {
  129. ivec3 A((pass == 1 || (pass == 2 && mask == 1))?(1):(0));
  130. ivec3 B((pass == 2)?(1):(0)); B[mask] = 1;
  131. vec3 offset = vec3(ivec3((int)(!A.x != !B.x), (int)(!A.y != !B.y), (int)(!A.z != !B.z)));
  132. AddTarget(base_pos + offset * base_off * 2.f - base_off);
  133. }
  134. pass++;
  135. }
  136. }
  137. Array<vec3> m_targets;
  138. };
  139. class MeshViewer : public WorldEntity
  140. {
  141. public:
  142. MeshViewer(char const *file_name = "data/mesh-buffer.txt")
  143. : m_file_name(file_name)
  144. {
  145. m_init = false;
  146. m_first_tick = false;
  147. // Message Service
  148. MessageService::Setup();
  149. m_ssetup = nullptr;
  150. m_camera = nullptr;
  151. m_controller = nullptr;
  152. //Compile ref meshes
  153. m_gizmos << new EasyMesh();
  154. m_gizmos.Last()->Compile("[sc#0f0 ac 3 .5 .4 0 ty .25 [ad 3 .4 sy -1] ty .5 ac 3 1 .075 ty .5 dup[rz 90 ry 90 scv#00f dup[ry 90 scv#f00]]][sc#fff ab .1]");
  155. m_gizmos << new EasyMesh();
  156. m_gizmos.Last()->Compile("[sc#666 acap 1 .5 .5 ty -.5 sc#fff asph 2 1]");
  157. m_gizmos << new EasyMesh();
  158. m_gizmos.Last()->Compile("[sc#fff ac 3 .5 .4 0 ty .25 [ad 3 .4 sy -1] ty .5 ac 3 1 .1 ty .5 [ad 3 .1 sy -1] ty 1 rz 90 ry 90]");
  159. // Mesh Setup
  160. m_render_max = vec2(-.9f, 4.1f);
  161. m_mesh_render = 0;
  162. m_mesh_id = 0;
  163. m_mesh_id1 = 0.f;
  164. m_default_texture = nullptr;
  165. m_texture_shader = nullptr;
  166. m_texture = nullptr;
  167. //Camera Setup
  168. m_reset_timer = -1.f;
  169. m_fov = -100.f;
  170. m_fov_mesh = 0.f;
  171. m_fov_speed = 0.f;
  172. m_zoom = 0.f;
  173. m_zoom_mesh = 0.f;
  174. m_zoom_speed = 0.f;
  175. m_rot = vec2(/*45.f*/0.f, -45.f);
  176. m_rot_mesh = vec2::zero;
  177. m_rot_speed = vec2::zero;
  178. m_pos = vec2::zero;
  179. m_pos_mesh = vec2::zero;
  180. m_pos_speed = vec2::zero;
  181. m_screen_offset = vec2::zero;
  182. m_hist_scale = vec2(.13f, .03f);
  183. m_hist_scale_mesh = vec2(.0f);
  184. m_hist_scale_speed = vec2(.0f);
  185. m_mat_prev = mat4(quat::fromeuler_xyz(vec3::zero));
  186. m_mat = mat4::translate(vec3(0.f));//mat4(quat::fromeuler_xyz(vec3(m_rot_mesh, .0f)));
  187. m_build_timer = 0.1f;
  188. m_build_time = -1.f;
  189. //stream update
  190. m_stream_update_time = 2.0f;
  191. m_stream_update_timer = 1.0f;
  192. }
  193. ~MeshViewer()
  194. {
  195. if (m_camera)
  196. g_scene->PopCamera(m_camera);
  197. if (m_ssetup)
  198. delete(m_ssetup);
  199. MessageService::Destroy();
  200. m_controller = nullptr;
  201. m_camera = nullptr;
  202. m_ssetup = nullptr;
  203. }
  204. #if NO_NACL_EM_INPUT
  205. bool KeyReleased(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsReleased()); }
  206. bool KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsPressed()); }
  207. bool KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsDown()); }
  208. bool KeyReleased(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsReleased()); }
  209. bool KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsPressed()); }
  210. bool KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsDown()); }
  211. float AxisValue(MVMouseAxisList index) { return (HAS_MOUSE)?(m_controller->GetAxis(index).GetValue()):(0.f); }
  212. #endif //NO_NACL_EM_INPUT
  213. void Init()
  214. {
  215. m_init = true;
  216. m_input_usage = 0;
  217. #if NO_NACL_EM_INPUT
  218. /* Register an input controller for the keyboard */
  219. m_controller = new Controller("Default", MAX_KEYS, MAX_AXIS);
  220. if (InputDevice::Get(g_name_mouse.C()))
  221. {
  222. m_input_usage |= (1<<IPT_MV_MOUSE);
  223. m_controller->GetKey(MSE_CAM_ROT).BindMouse("Left");
  224. m_controller->GetKey(MSE_CAM_POS).BindMouse("Right");
  225. m_controller->GetKey(MSE_CAM_FOV).BindMouse("Middle");
  226. m_controller->GetKey(MSE_FOCUS).BindMouse("InScreen");
  227. m_controller->GetAxis(MSEX_CAM_Y).BindMouse("Y");
  228. m_controller->GetAxis(MSEX_CAM_X).BindMouse("X");
  229. }
  230. if (InputDevice::Get(g_name_keyboard.C()))
  231. {
  232. m_input_usage |= (1<<IPT_MV_KBOARD);
  233. //Camera keyboard rotation
  234. m_controller->GetKey(KEY_CAM_UP ).BindKeyboard("Up");
  235. m_controller->GetKey(KEY_CAM_DOWN ).BindKeyboard("Down");
  236. m_controller->GetKey(KEY_CAM_LEFT ).BindKeyboard("Left");
  237. m_controller->GetKey(KEY_CAM_RIGHT).BindKeyboard("Right");
  238. //Camera keyboard position switch
  239. m_controller->GetKey(KEY_CAM_POS ).BindKeyboard("LeftShift");
  240. m_controller->GetKey(KEY_CAM_FOV ).BindKeyboard("LeftCtrl");
  241. //Camera unzoom switch
  242. m_controller->GetKey(KEY_CAM_RESET).BindKeyboard("Space");
  243. //Mesh change
  244. m_controller->GetKey(KEY_MESH_NEXT).BindKeyboard("PageUp");
  245. m_controller->GetKey(KEY_MESH_PREV).BindKeyboard("PageDown");
  246. //Base setup
  247. m_controller->GetKey(KEY_F1).BindKeyboard("F1");
  248. m_controller->GetKey(KEY_F2).BindKeyboard("F2");
  249. m_controller->GetKey(KEY_F3).BindKeyboard("F3");
  250. m_controller->GetKey(KEY_F4).BindKeyboard("F4");
  251. m_controller->GetKey(KEY_F5).BindKeyboard("F5");
  252. m_controller->GetKey(KEY_ESC).BindKeyboard("Escape");
  253. }
  254. #endif //NO_NACL_EM_INPUT
  255. m_camera = new Camera();
  256. m_camera->SetView(vec3(0.f, 0.f, 10.f), vec3::zero, vec3::axis_y);
  257. m_camera->SetProjection(0.f, .0001f, 2000.f, WIDTH * SCREEN_W, RATIO_HW);
  258. m_camera->UseShift(true);
  259. g_scene->PushCamera(m_camera);
  260. //Lights setup
  261. m_ssetup = new SceneSetup();
  262. #if NO_SC_SETUP
  263. m_ssetup->m_lights << new Light();
  264. m_ssetup->m_lights.Last()->SetPosition(vec4(4.f, -1.f, -4.f, 0.f));
  265. m_ssetup->m_lights.Last()->SetColor(vec4(.0f, .2f, .5f, 1.f));
  266. Ticker::Ref(m_ssetup->m_lights.Last());
  267. m_ssetup->m_lights << new Light();
  268. m_ssetup->m_lights.Last()->SetPosition(vec4(8.f, 2.f, 6.f, 0.f));
  269. m_ssetup->m_lights.Last()->SetColor(vec4(1.f));
  270. Ticker::Ref(m_ssetup->m_lights.Last());
  271. EasyMesh* em = new EasyMesh();
  272. if (em->Compile("sc#fff ab 1"))
  273. {
  274. if (m_mesh_id == m_meshes.Count() - 1)
  275. m_mesh_id++;
  276. m_meshes.Push(em, nullptr);
  277. }
  278. #else
  279. m_ssetup->Compile("addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1) "
  280. "addlight 0.0 position (8 2 6) color #ffff "
  281. "showgizmo true ");
  282. m_ssetup->Startup();
  283. #endif //NO_SC_SETUP
  284. for (int i = 0; i < m_ssetup->m_lights.Count(); ++i)
  285. {
  286. m_light_datas << LightData(m_ssetup->m_lights[i]->GetPosition().xyz, m_ssetup->m_lights[i]->GetColor());
  287. m_ssetup->m_lights[i]->SetPosition(vec3::zero);
  288. m_ssetup->m_lights[i]->SetColor(vec4::zero);
  289. }
  290. }
  291. virtual void TickGame(float seconds)
  292. {
  293. WorldEntity::TickGame(seconds);
  294. if (!m_init && g_scene)
  295. {
  296. Init();
  297. return;
  298. }
  299. if (!m_init)
  300. return;
  301. m_first_tick = true;
  302. //TODO : This should probably be "standard LoL behaviour"
  303. #if NO_NACL_EM_INPUT
  304. {
  305. //Shutdown logic
  306. if (KeyReleased(KEY_ESC))
  307. Ticker::Shutdown();
  308. }
  309. #endif //NO_NACL_EM_INPUT
  310. //Compute render mesh count
  311. float a_j = lol::abs(m_render_max[1]);
  312. float i_m = m_hist_scale_mesh.x;
  313. float i_trans = a_j - ((a_j * a_j * i_m * i_m + a_j * i_m) * .5f);
  314. m_render_max[1] = a_j * ((RATIO_WH * 1.f) / ((i_trans != 0.f)?(i_trans):(RATIO_WH))) - RATIO_HW * .3f;
  315. //Mesh Change
  316. #if NO_NACL_EM_INPUT
  317. m_mesh_id = clamp(m_mesh_id + ((int)KeyPressed(KEY_MESH_PREV) - (int)KeyPressed(KEY_MESH_NEXT)), 0, m_meshes.Count() - 1);
  318. #endif //NO_NACL_EM_INPUT
  319. m_mesh_id1 = damp(m_mesh_id1, (float)m_mesh_id, .2f, seconds);
  320. #if ALL_FEATURES
  321. //Update light position & damping
  322. for (int i = 0; i < m_ssetup->m_lights.Count(); ++i)
  323. {
  324. vec3 pos = (m_mat * inverse(m_mat_prev) * vec4(m_ssetup->m_lights[i]->GetPosition(), 1.f)).xyz;
  325. vec3 tgt = (m_mat * vec4(m_light_datas[i].m_pos, 1.f)).xyz;
  326. vec3 new_pos = damp(pos, tgt, .3f, seconds);
  327. vec4 new_col = damp(m_ssetup->m_lights[i]->GetColor(), m_light_datas[i].m_col, .3f, seconds);
  328. m_ssetup->m_lights[i]->SetPosition(new_pos);
  329. m_ssetup->m_lights[i]->SetColor(new_col);
  330. }
  331. //Camera update
  332. bool is_pos = false;
  333. bool is_fov = false;
  334. bool is_hsc = false;
  335. vec2 tmpv = vec2::zero;
  336. #if NO_NACL_EM_INPUT
  337. is_pos = KeyDown(KEY_CAM_POS) || KeyDown(MSE_CAM_POS);
  338. is_fov = KeyDown(KEY_CAM_FOV) || KeyDown(MSE_CAM_FOV);
  339. if (KeyDown(MSE_FOCUS) && (KeyDown(MSE_CAM_ROT) || KeyDown(MSE_CAM_POS) || KeyDown(MSE_CAM_FOV)))
  340. {
  341. tmpv += vec2(AxisValue(MSEX_CAM_Y), AxisValue(MSEX_CAM_X));
  342. if (KeyDown(MSE_CAM_ROT))
  343. tmpv *= vec2(1.f, 1.f) * 6.f;
  344. if (KeyDown(MSE_CAM_POS))
  345. tmpv *= vec2(1.f, -1.f) * 3.f;
  346. if (KeyDown(MSE_CAM_FOV))
  347. tmpv = vec2(tmpv.y * 4.f, tmpv.x * 6.f);
  348. }
  349. tmpv += vec2((float)KeyDown(KEY_CAM_UP ) - (float)KeyDown(KEY_CAM_DOWN),
  350. (float)KeyDown(KEY_CAM_RIGHT) - (float)KeyDown(KEY_CAM_LEFT));
  351. #endif //NO_NACL_EM_INPUT
  352. //Base data
  353. vec2 rot = (!is_pos && !is_fov)?(tmpv):(vec2(.0f)); rot = vec2(rot.x, -rot.y);
  354. vec2 pos = ( is_pos && !is_fov)?(tmpv):(vec2(.0f)); pos = -vec2(pos.y, pos.x);
  355. vec2 fov = (!is_pos && is_fov )?(tmpv):(vec2(.0f)); fov = vec2(-fov.x, fov.y);
  356. vec2 hsc = (is_hsc)?(vec2(0.f)):(vec2(0.f));
  357. //speed
  358. m_rot_speed = damp(m_rot_speed, rot * ROT_SPEED, .2f, seconds);
  359. float pos_factor = 1.f / (1.f + m_zoom_mesh * .5f);
  360. m_pos_speed = damp(m_pos_speed, pos * POS_SPEED * pos_factor, .2f, seconds);
  361. float fov_factor = 1.f + lol::pow((m_fov_mesh / FOV_CLAMP) * 1.f, 2.f);
  362. m_fov_speed = damp(m_fov_speed, fov.x * FOV_SPEED * fov_factor, .2f, seconds);
  363. float zom_factor = 1.f + lol::pow((m_zoom_mesh / ZOM_CLAMP) * 1.f, 2.f);
  364. m_zoom_speed = damp(m_zoom_speed, fov.y * ZOM_SPEED * zom_factor, .2f, seconds);
  365. m_hist_scale_speed = damp(m_hist_scale_speed, hsc * HST_SPEED, .2f, seconds);
  366. m_rot += m_rot_speed * seconds;
  367. #if NO_NACL_EM_INPUT
  368. if (m_reset_timer >= 0.f)
  369. m_reset_timer -= seconds;
  370. if (KeyPressed(KEY_CAM_RESET))
  371. {
  372. if (m_reset_timer >= 0.f)
  373. {
  374. m_pos = vec2(0.f);
  375. m_zoom = 0.f;
  376. }
  377. else
  378. m_reset_timer = RESET_TIMER;
  379. }
  380. //Transform update
  381. if (!KeyDown(KEY_CAM_RESET))
  382. {
  383. m_pos += m_pos_speed * seconds;
  384. m_fov += m_fov_speed * seconds;
  385. m_zoom += m_zoom_speed * seconds;
  386. m_hist_scale += m_hist_scale_speed * seconds;
  387. }
  388. #endif //NO_NACL_EM_INPUT
  389. //clamp
  390. vec2 rot_mesh = vec2(SmoothClamp(m_rot.x, -ROT_CLAMP, ROT_CLAMP, ROT_CLAMP * .1f), m_rot.y);
  391. vec2 pos_mesh = vec2(SmoothClamp(m_pos.x, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f),
  392. SmoothClamp(m_pos.y, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f));
  393. float fov_mesh = SmoothClamp(m_fov, 0.f, FOV_CLAMP, FOV_CLAMP * .1f);
  394. float zoom_mesh = SmoothClamp(m_zoom, -ZOM_CLAMP, ZOM_CLAMP, ZOM_CLAMP * .1f);
  395. vec2 hist_scale_mesh = vec2(SmoothClamp(m_hist_scale.x, 0.f, HST_CLAMP, HST_CLAMP * .1f),
  396. SmoothClamp(m_hist_scale.y, 0.f, HST_CLAMP, HST_CLAMP * .1f));
  397. #if NO_NACL_EM_INPUT
  398. if (KeyDown(KEY_CAM_RESET) && m_reset_timer < 0.f)
  399. {
  400. pos_mesh = vec2::zero;
  401. zoom_mesh = 0.f;
  402. }
  403. #endif //NO_NACL_EM_INPUT
  404. m_rot_mesh = vec2(damp(m_rot_mesh.x, rot_mesh.x, .2f, seconds), damp(m_rot_mesh.y, rot_mesh.y, .2f, seconds));
  405. m_pos_mesh = vec2(damp(m_pos_mesh.x, pos_mesh.x, .2f, seconds), damp(m_pos_mesh.y, pos_mesh.y, .2f, seconds));
  406. m_fov_mesh = damp(m_fov_mesh, fov_mesh, .2f, seconds);
  407. m_zoom_mesh = damp(m_zoom_mesh, zoom_mesh, .2f, seconds);
  408. m_hist_scale_mesh = damp(m_hist_scale_mesh, hist_scale_mesh, .2f, seconds);
  409. //Mesh mat calculation
  410. m_mat_prev = m_mat;
  411. m_mat = mat4::translate(vec3(0.f));
  412. //Target List Setup
  413. TargetCamera tc;
  414. if (m_meshes.Count() && m_mesh_id >= 0)
  415. for (int i = 0; i < m_meshes[m_mesh_id].m1->GetVertexCount(); i++)
  416. tc.AddTarget((m_mat * mat4::translate(m_meshes[m_mesh_id].m1->GetVertexLocation(i))).v3.xyz);
  417. tc.AddTarget(box3(vec3(0.f), vec3(1.f)));
  418. for (int k = 0; k < m_ssetup->m_lights.Count() && m_ssetup->m_show_lights; ++k)
  419. {
  420. vec3 light_pos = m_ssetup->m_lights[k]->GetPosition();
  421. mat4 world_cam = m_camera->GetView();
  422. light_pos = (inverse(world_cam) * vec4((world_cam * vec4(light_pos, 1.0f)).xyz * vec3::axis_z, 1.0f)).xyz;
  423. tc.AddTarget(box3(vec3(-1.f), vec3(1.f)) + light_pos *
  424. ((m_ssetup->m_lights[k]->GetType() == LightType::Directional)?(-1.f):(1.f)));
  425. }
  426. //--
  427. //Update mesh screen location - Get the Min/Max needed
  428. //--
  429. vec2 cam_center(0.f);
  430. float cam_factor = .0f;
  431. vec3 local_min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
  432. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  433. mat4 world_cam = m_camera->GetView();
  434. mat4 cam_screen = m_camera->GetProjection();
  435. //target on-screen computation
  436. for (int i = 0; i < tc.m_targets.Count(); i++)
  437. {
  438. vec3 obj_loc = tc.m_targets[i];
  439. {
  440. //Debug::DrawBox(obj_loc - vec3(4.f), obj_loc + vec3(4.f), vec4(1.f, 0.f, 0.f, 1.f));
  441. mat4 target_mx = mat4::translate(obj_loc);
  442. vec3 vpos;
  443. //Get location in cam coordinates
  444. target_mx = world_cam * target_mx;
  445. vpos = target_mx.v3.xyz;
  446. local_min_max[0] = min(vpos.xyz, local_min_max[0]);
  447. local_min_max[1] = max(vpos.xyz, local_min_max[1]);
  448. //Get location in screen coordinates
  449. target_mx = cam_screen * target_mx;
  450. vpos = (target_mx.v3 / target_mx.v3.w).xyz;
  451. screen_min_max[0] = min(screen_min_max[0], vpos.xy * vec2(RATIO_WH, 1.f));
  452. screen_min_max[1] = max(screen_min_max[1], vpos.xy * vec2(RATIO_WH, 1.f));
  453. //Build Barycenter
  454. cam_center += vpos.xy;
  455. cam_factor += 1.f;
  456. }
  457. }
  458. float screen_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
  459. max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
  460. float z_dist = //m_camera->m_target_distance
  461. length(m_camera->m_position)
  462. + max(local_min_max[0].z, local_min_max[1].z);
  463. vec2 screen_offset = vec2(0.f, -(screen_min_max[1].y + screen_min_max[0].y) * .5f);
  464. m_screen_offset = damp(m_screen_offset, screen_offset, .9f, seconds);
  465. float forced_zoom = m_zoom_mesh;
  466. if (cam_factor > 0.f)
  467. {
  468. vec2 old_sscale = m_camera->GetScreenScale();
  469. float old_ssize = m_camera->GetScreenSize();
  470. float zoom_in = 1.f + lol::max(0.f, forced_zoom);
  471. float zoom_out = 1.f + lol::max(0.f, -forced_zoom);
  472. m_camera->SetScreenScale(max(vec2(0.001f), ((old_sscale * zoom_in) / (screen_ratio * zoom_out * SCREEN_LIMIT))));
  473. m_camera->SetFov(m_fov_mesh);
  474. m_camera->SetScreenInfos(damp(old_ssize, max(1.f, screen_ratio * zoom_out), 1.2f, seconds));
  475. vec3 posz = ((mat4::rotate(m_rot_mesh.y, vec3::axis_y) * mat4::rotate(-m_rot_mesh.x, vec3::axis_x) * vec4::axis_z)).xyz;
  476. vec3 newpos = posz * damp(length(m_camera->m_position), z_dist * 1.2f, .1f, seconds);
  477. m_camera->SetView(newpos, vec3(0.f), vec3::axis_y);
  478. }
  479. //--
  480. //Message Service
  481. //--
  482. String mesh("");
  483. int u = 1;
  484. while (u-- > 0 && MessageService::FetchFirst(MessageBucket::AppIn, mesh))
  485. {
  486. int o = 1;
  487. while (o-- > 0)
  488. {
  489. SceneSetup* new_ssetup = new SceneSetup();
  490. if (new_ssetup->Compile(mesh.C()) && new_ssetup->m_lights.Count())
  491. {
  492. //Store current light datas, in World
  493. Array<LightData> light_datas;
  494. for (int i = 0; i < m_ssetup->m_lights.Count(); ++i)
  495. light_datas << LightData(m_ssetup->m_lights[i]->GetPosition(), m_ssetup->m_lights[i]->GetColor());
  496. if (m_ssetup)
  497. delete(m_ssetup);
  498. m_ssetup = new_ssetup;
  499. m_ssetup->Startup();
  500. //Restore all light datas so blend can occur
  501. mat4 light_mat = m_mat * inverse(mat4(quat::fromeuler_xyz(vec3::zero)));
  502. for (int i = 0; i < m_ssetup->m_lights.Count(); ++i)
  503. {
  504. //Store local dst in current m_ld
  505. LightData ltmp = LightData(m_ssetup->m_lights[i]->GetPosition(), m_ssetup->m_lights[i]->GetColor());
  506. if (i < m_light_datas.Count())
  507. m_light_datas[i] = ltmp;
  508. else
  509. m_light_datas << ltmp;
  510. vec3 loc = vec3::zero;
  511. vec4 col = vec4::zero;
  512. if (i < light_datas.Count())
  513. {
  514. loc = light_datas[i].m_pos;
  515. col = light_datas[i].m_col;
  516. }
  517. //Restore old light datas in new lights
  518. m_ssetup->m_lights[i]->SetPosition(loc);
  519. m_ssetup->m_lights[i]->SetColor(col);
  520. }
  521. }
  522. else
  523. {
  524. m_ssetup->m_custom_cmd += new_ssetup->m_custom_cmd;
  525. delete(new_ssetup);
  526. }
  527. }
  528. }
  529. //Check the custom cmd even if we don't have new messages.
  530. int o = 1;
  531. while (o-- > 0)
  532. {
  533. for (int i = 0; m_ssetup && i < m_ssetup->m_custom_cmd.Count(); ++i)
  534. {
  535. if (m_ssetup->m_custom_cmd[i].m1 == "setmesh")
  536. {
  537. //Create a new mesh
  538. EasyMesh* em = new EasyMesh();
  539. if (em->Compile(m_ssetup->m_custom_cmd[i].m2.C(), false))
  540. {
  541. em->BD()->Cmdi() = 0;
  542. if (m_mesh_id == m_meshes.Count() - 1)
  543. m_mesh_id++;
  544. m_meshes.Push(em, nullptr);
  545. }
  546. else
  547. delete(em);
  548. }
  549. }
  550. }
  551. m_ssetup->m_custom_cmd.Empty();
  552. #endif //ALL_FEATURES
  553. #if NACL_EM
  554. /*
  555. if (m_stream_update_time > .0f)
  556. {
  557. m_stream_update_time = -1.f;
  558. MessageService::Send(MessageBucket::AppIn,
  559. " addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1) \
  560. addlight 0.0 position (8 2 6) color #ffff \
  561. custom setmesh \"[sc#f8f ab 1]\"");
  562. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1]");
  563. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1 splt 4 twy 90]");
  564. // MessageService::Send(MessageBucket::AppIn, "[sc#8ff afcb 1 1 1 0]");
  565. // MessageService::Send(MessageBucket::AppIn, "[sc#ff8 afcb 1 1 1 0]");
  566. }
  567. */
  568. #elif defined(_WIN32)
  569. //--
  570. //File management
  571. //--
  572. m_stream_update_time += seconds;
  573. if (m_stream_update_time > m_stream_update_timer)
  574. {
  575. m_stream_update_time = 0.f;
  576. File f;
  577. f.Open(m_file_name.C(), FileAccess::Read);
  578. String cmd = f.ReadString();
  579. f.Close();
  580. if (cmd.Count()
  581. && (!m_cmdlist.Count() || cmd != m_cmdlist.Last()))
  582. {
  583. m_cmdlist << cmd;
  584. MessageService::Send(MessageBucket::AppIn, cmd);
  585. }
  586. }
  587. #endif //WINDOWS
  588. }
  589. virtual void TickDraw(float seconds)
  590. {
  591. WorldEntity::TickDraw(seconds);
  592. if (!m_init || !m_first_tick)
  593. return;
  594. //TODO : This should probably be "standard LoL behaviour"
  595. #if NO_NACL_EM_INPUT
  596. {
  597. if (KeyReleased(KEY_F2))
  598. Video::SetDebugRenderMode((Video::GetDebugRenderMode() + 1) % DebugRenderMode::Max);
  599. else if (KeyReleased(KEY_F1))
  600. Video::SetDebugRenderMode((Video::GetDebugRenderMode() + DebugRenderMode::Max - 1) % DebugRenderMode::Max);
  601. }
  602. #endif //NO_NACL_EM_INPUT
  603. #if NO_NACL_EM && WITH_TEXTURE
  604. if (!m_default_texture)
  605. {
  606. m_texture_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinymvtexture));
  607. m_texture_uni = m_texture_shader->GetUniformLocation("u_Texture");
  608. m_default_texture = Tiler::Register("data/test-texture.png", ivec2::zero, ivec2(0,1));
  609. }
  610. else if (m_texture && m_default_texture)
  611. m_texture_shader->SetUniform(m_texture_uni, m_default_texture->GetTexture(), 0);
  612. #endif //NO_NACL_EM
  613. g_renderer->SetClearColor(m_ssetup->m_clear_color);
  614. for (int i = 0; i < m_gizmos.Count(); ++i)
  615. {
  616. if (m_gizmos[i]->GetMeshState() == MeshRender::NeedConvert)
  617. m_gizmos[i]->MeshConvert();
  618. else
  619. break;
  620. }
  621. if (m_build_timer > .0f)
  622. {
  623. if (m_build_time < .0f)
  624. {
  625. m_build_time = m_build_timer;
  626. for (int i = 0; i < m_meshes.Count(); ++i)
  627. {
  628. if (m_meshes[i].m1 && m_meshes[i].m1->BD()->Cmdi() < m_meshes[i].m1->BD()->CmdStack().GetCmdNb())
  629. {
  630. EasyMesh* tmp = m_meshes[i].m1;
  631. EasyMesh* newtmp = new EasyMesh(*tmp);
  632. int ii = 1;
  633. #if 1
  634. bool stop = false;
  635. while (!stop)
  636. {
  637. int cmdi = newtmp->BD()->Cmdi() + ii;
  638. if (cmdi < newtmp->BD()->CmdStack().GetCmdNb())
  639. {
  640. switch (newtmp->BD()->CmdStack().GetCmd(cmdi))
  641. {
  642. case EasyMeshCmdType::LoopStart:
  643. case EasyMeshCmdType::LoopEnd:
  644. case EasyMeshCmdType::OpenBrace:
  645. case EasyMeshCmdType::CloseBrace:
  646. case EasyMeshCmdType::ScaleWinding:
  647. case EasyMeshCmdType::QuadWeighting:
  648. case EasyMeshCmdType::PostBuildNormal:
  649. case EasyMeshCmdType::PreventVertCleanup:
  650. case EasyMeshCmdType::SetColorA:
  651. case EasyMeshCmdType::SetColorB:
  652. {
  653. ii++;
  654. break;
  655. }
  656. default:
  657. {
  658. stop = true;
  659. break;
  660. }
  661. }
  662. }
  663. else
  664. stop = true;
  665. }
  666. #endif
  667. newtmp->BD()->CmdExecNb() = ii;
  668. newtmp->ExecuteCmdStack(false);
  669. m_meshes[i].m1 = newtmp;
  670. delete(tmp);
  671. }
  672. }
  673. }
  674. m_build_time -= seconds;
  675. }
  676. #define NORMAL_USAGE 1
  677. #if NORMAL_USAGE
  678. vec3 x = vec3(1.f,0.f,0.f);
  679. vec3 y = vec3(0.f,1.f,0.f);
  680. mat4 save_proj = m_camera->GetProjection();
  681. //Y object Offset
  682. mat4 mat_obj_offset = mat4::translate(x * m_screen_offset.x + y * m_screen_offset.y) *
  683. //Mesh Pos Offset
  684. mat4::translate((x * m_pos_mesh.x * RATIO_HW + y * m_pos_mesh.y) * 2.f * (1.f + .5f * m_zoom_mesh / SCREEN_LIMIT));
  685. //Align right meshes
  686. mat4 mat_align = mat4::translate(x - x * RATIO_HW);
  687. mat4 mat_gizmo = mat_obj_offset * mat_align * save_proj;
  688. for (int i = 0; i < m_meshes.Count(); i++)
  689. {
  690. {
  691. if (m_meshes[i].m1->GetMeshState() == MeshRender::NeedConvert)
  692. {
  693. #if WITH_TEXTURE
  694. m_meshes[i].m1->MeshConvert(new DefaultShaderData(((1 << VertexUsage::Position) | (1 << VertexUsage::Normal) |
  695. (1 << VertexUsage::Color) | (1 << VertexUsage::TexCoord)),
  696. m_texture_shader, true));
  697. #else
  698. m_meshes[i].m1->MeshConvert();
  699. #endif //WITH_TEXTURE
  700. }
  701. #if ALL_FEATURES
  702. float j = -(float)(m_meshes.Count() - (i + 1)) + (-m_mesh_id1 + (float)(m_meshes.Count() - 1));
  703. if (m_mesh_id1 - m_render_max[0] > (float)i && m_mesh_id1 - m_render_max[1] < (float)i &&
  704. m_meshes[i].m1->GetMeshState() > MeshRender::NeedConvert)
  705. {
  706. float a_j = lol::abs(j);
  707. float i_trans = (a_j * a_j * m_hist_scale_mesh.x + a_j * m_hist_scale_mesh.x) * .5f;
  708. float i_scale = clamp(1.f - (m_hist_scale_mesh.y * (m_mesh_id1 - (float)i)), 0.f, 1.f);
  709. //Mesh count offset
  710. mat4 mat_count_offset = mat4::translate(x * RATIO_HW * 2.f * (j + i_trans));
  711. //Mesh count scale
  712. mat4 mat_count_scale = mat4::scale(vec3(vec2(i_scale), 1.f));
  713. //Camera projection
  714. mat4 new_proj = mat_obj_offset * mat_count_offset * mat_align * mat_count_scale * save_proj;
  715. m_camera->SetProjection(new_proj);
  716. g_scene->AddPrimitive(*m_meshes[i].m1, m_mat);
  717. g_renderer->Clear(ClearMask::Depth);
  718. }
  719. m_camera->SetProjection(save_proj);
  720. #else
  721. g_scene->AddPrimitive(*m_meshes[i].m1, m_mat);
  722. #endif //ALL_FEATURES
  723. }
  724. }
  725. //Scene setup update
  726. if (m_ssetup)
  727. {
  728. m_camera->SetProjection(mat_gizmo);
  729. if (m_ssetup->m_show_gizmo)
  730. g_scene->AddPrimitive(*m_gizmos[GZ_Editor], m_mat);
  731. if (m_ssetup->m_show_lights)
  732. {
  733. for (int k = 0; k < m_ssetup->m_lights.Count(); ++k)
  734. {
  735. Light* ltmp = m_ssetup->m_lights[k];
  736. mat4 world = mat4::translate(ltmp->GetPosition());
  737. mat4 local = mat4::translate((inverse(m_mat) * world).v3.xyz);
  738. //dir light
  739. if (ltmp->GetType() == LightType::Directional)
  740. {
  741. g_scene->AddPrimitive(*m_gizmos[GZ_LightPos], m_mat * inverse(local));
  742. g_scene->AddPrimitive(*m_gizmos[GZ_LightDir], inverse(world) * inverse(mat4::lookat(vec3::zero, -ltmp->GetPosition(), vec3::axis_y)));
  743. }
  744. else //point light
  745. {
  746. g_scene->AddPrimitive(*m_gizmos[GZ_LightPos], m_mat * local);
  747. }
  748. }
  749. }
  750. m_camera->SetProjection(save_proj);
  751. }
  752. #endif //NORMAL_USAGE
  753. #if 0 //Debug normal draw
  754. for (int i = m_meshes.Count() - 1; 0 <= i && i < m_meshes.Count(); i++)
  755. {
  756. for (int j = 0; j < m_meshes[i].m1->m_indices.Count(); j += 3)
  757. {
  758. VertexData v[3] = { m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j ]],
  759. m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j+1]],
  760. m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j+2]]
  761. };
  762. for (int k = 0; k < 3; k++)
  763. Debug::DrawLine((m_mat * mat4::translate(v[k].m_coord)).v3.xyz,
  764. (m_mat * mat4::translate(v[(k+1)%3].m_coord)).v3.xyz, vec4(vec3((v[k].m_coord.z + 1.f)*.5f),1.f));
  765. }
  766. for (int j = 0; j < m_meshes[i].m1->m_vert.Count(); j++)
  767. {
  768. VertexData &v = m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j]];
  769. Debug::DrawLine((m_mat * mat4::translate(v.m_coord)).v3.xyz,
  770. (m_mat * mat4::translate(v.m_coord)).v3.xyz +
  771. (m_mat * vec4(v.m_normal * 5.f, 0.f)).xyz, vec4(lol::abs(v.m_normal), 1.f));
  772. }
  773. }
  774. #endif
  775. }
  776. private:
  777. SceneSetup* m_ssetup;
  778. Array<LightData> m_light_datas;
  779. Controller* m_controller;
  780. short m_input_usage;
  781. mat4 m_mat;
  782. mat4 m_mat_prev;
  783. bool m_init;
  784. bool m_first_tick;
  785. //Camera Setup
  786. Camera * m_camera;
  787. float m_reset_timer;
  788. float m_fov;
  789. float m_fov_mesh;
  790. float m_fov_speed;
  791. float m_zoom;
  792. float m_zoom_mesh;
  793. float m_zoom_speed;
  794. vec2 m_rot;
  795. vec2 m_rot_mesh;
  796. vec2 m_rot_speed;
  797. vec2 m_pos;
  798. vec2 m_pos_mesh;
  799. vec2 m_pos_speed;
  800. vec2 m_hist_scale;
  801. vec2 m_hist_scale_mesh;
  802. vec2 m_hist_scale_speed;
  803. vec2 m_screen_offset;
  804. //Mesh update timer
  805. float m_build_timer;
  806. float m_build_time;
  807. //Mesh infos
  808. vec2 m_render_max;
  809. int m_mesh_render;
  810. int m_mesh_id;
  811. float m_mesh_id1;
  812. Array<EasyMesh*, EasyMesh*> m_meshes;
  813. Array<EasyMesh*> m_gizmos;
  814. //File data
  815. String m_file_name;
  816. Array<String> m_cmdlist;
  817. float m_stream_update_time;
  818. float m_stream_update_timer;
  819. //misc datas
  820. Shader * m_texture_shader;
  821. TileSet * m_default_texture;
  822. Texture * m_texture;
  823. ShaderUniform m_texture_uni;
  824. };
  825. //The basic main :
  826. int main(int argc, char **argv)
  827. {
  828. System::Init(argc, argv);
  829. Application app("MeshViewer", ivec2((int)DEFAULT_WIDTH, (int)DEFAULT_HEIGHT), 60.0f);
  830. if (argc > 1)
  831. new MeshViewer(argv[1]);
  832. else
  833. new MeshViewer();
  834. app.Run();
  835. return EXIT_SUCCESS;
  836. }