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.
 
 
 

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