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