Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

953 wiersze
34 KiB

  1. //
  2. // Lol Engine - EasyMesh tutorial
  3. //
  4. // Copyright: (c) 2011-2013 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, -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::zero, vec3::one));
  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::mone, vec3::one) + 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 = length(m_camera->m_position) + max(local_min_max[0].z, local_min_max[1].z);
  461. vec2 screen_offset = vec2(0.f, -(screen_min_max[1].y + screen_min_max[0].y) * .5f);
  462. m_screen_offset = damp(m_screen_offset, screen_offset, .9f, seconds);
  463. if (cam_factor > 0.f)
  464. {
  465. vec2 new_screen_scale = m_camera->GetScreenScale();
  466. float zoom_in = 1.f + lol::max(0.f, m_zoom_mesh);
  467. float zoom_out = 1.f + lol::max(0.f, -m_zoom_mesh);
  468. m_camera->SetScreenScale(max(vec2(0.001f), ((new_screen_scale * zoom_in) / (screen_ratio * zoom_out * SCREEN_LIMIT))));
  469. m_camera->SetFov(m_fov_mesh);
  470. m_camera->SetScreenInfos(damp(m_camera->GetScreenSize(), max(1.f, screen_ratio * zoom_out), 1.2f, seconds));
  471. vec3 posz = ((mat4::rotate(m_rot_mesh.y, vec3::axis_y) * mat4::rotate(-m_rot_mesh.x, vec3::axis_x) * vec4::axis_z)).xyz;
  472. vec3 newpos = posz * damp(length(m_camera->m_position), z_dist * 1.2f, .1f, seconds);
  473. m_camera->SetView(newpos, vec3(0.f), vec3::axis_y);
  474. }
  475. //--
  476. //Message Service
  477. //--
  478. String mesh("");
  479. int u = 1;
  480. while (u-- > 0 && MessageService::FetchFirst(MessageBucket::AppIn, mesh))
  481. {
  482. int o = 1;
  483. while (o-- > 0)
  484. {
  485. SceneSetup* new_ssetup = new SceneSetup();
  486. if (new_ssetup->Compile(mesh.C()) && new_ssetup->m_lights.Count())
  487. {
  488. //Store current light datas, in World
  489. Array<LightData> light_datas;
  490. for (int i = 0; i < m_ssetup->m_lights.Count(); ++i)
  491. light_datas << LightData(m_ssetup->m_lights[i]->GetPosition(), m_ssetup->m_lights[i]->GetColor());
  492. if (m_ssetup)
  493. delete(m_ssetup);
  494. m_ssetup = new_ssetup;
  495. m_ssetup->Startup();
  496. //Restore all light datas so blend can occur
  497. mat4 light_mat = m_mat * inverse(mat4(quat::fromeuler_xyz(vec3::zero)));
  498. for (int i = 0; i < m_ssetup->m_lights.Count(); ++i)
  499. {
  500. //Store local dst in current m_ld
  501. LightData ltmp = LightData(m_ssetup->m_lights[i]->GetPosition(), m_ssetup->m_lights[i]->GetColor());
  502. if (i < m_light_datas.Count())
  503. m_light_datas[i] = ltmp;
  504. else
  505. m_light_datas << ltmp;
  506. vec3 loc = vec3::zero;
  507. vec4 col = vec4::zero;
  508. if (i < light_datas.Count())
  509. {
  510. loc = light_datas[i].m_pos;
  511. col = light_datas[i].m_col;
  512. }
  513. //Restore old light datas in new lights
  514. m_ssetup->m_lights[i]->SetPosition(loc);
  515. m_ssetup->m_lights[i]->SetColor(col);
  516. }
  517. }
  518. else
  519. {
  520. m_ssetup->m_custom_cmd += new_ssetup->m_custom_cmd;
  521. delete(new_ssetup);
  522. }
  523. }
  524. }
  525. //Check the custom cmd even if we don't have new messages.
  526. int o = 1;
  527. while (o-- > 0)
  528. {
  529. for (int i = 0; m_ssetup && i < m_ssetup->m_custom_cmd.Count(); ++i)
  530. {
  531. if (m_ssetup->m_custom_cmd[i].m1 == "setmesh")
  532. {
  533. //Create a new mesh
  534. EasyMesh* em = new EasyMesh();
  535. if (em->Compile(m_ssetup->m_custom_cmd[i].m2.C(), false))
  536. {
  537. em->BD()->Cmdi() = 0;
  538. if (m_mesh_id == m_meshes.Count() - 1)
  539. m_mesh_id++;
  540. m_meshes.Push(em, nullptr);
  541. }
  542. else
  543. delete(em);
  544. }
  545. }
  546. }
  547. m_ssetup->m_custom_cmd.Empty();
  548. #endif //ALL_FEATURES
  549. #if NACL_EM
  550. /*
  551. if (m_stream_update_time > .0f)
  552. {
  553. m_stream_update_time = -1.f;
  554. MessageService::Send(MessageBucket::AppIn,
  555. " addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1) \
  556. addlight 0.0 position (8 2 6) color #ffff \
  557. custom setmesh \"[sc#f8f ab 1]\"");
  558. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1]");
  559. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1 splt 4 twy 90]");
  560. // MessageService::Send(MessageBucket::AppIn, "[sc#8ff afcb 1 1 1 0]");
  561. // MessageService::Send(MessageBucket::AppIn, "[sc#ff8 afcb 1 1 1 0]");
  562. }
  563. */
  564. #elif WIN32
  565. //--
  566. //File management
  567. //--
  568. m_stream_update_time += seconds;
  569. if (m_stream_update_time > m_stream_update_timer)
  570. {
  571. m_stream_update_time = 0.f;
  572. File f;
  573. f.Open(m_file_name.C(), FileAccess::Read);
  574. String cmd = f.ReadString();
  575. f.Close();
  576. if (cmd.Count()
  577. && (!m_cmdlist.Count() || cmd != m_cmdlist.Last()))
  578. {
  579. m_cmdlist << cmd;
  580. MessageService::Send(MessageBucket::AppIn, cmd);
  581. }
  582. }
  583. #endif //WINDOWS
  584. }
  585. virtual void TickDraw(float seconds)
  586. {
  587. WorldEntity::TickDraw(seconds);
  588. if (!m_init || !m_first_tick)
  589. return;
  590. //TODO : This should probably be "standard LoL behaviour"
  591. #if NO_NACL_EM_INPUT
  592. {
  593. if (KeyReleased(KEY_F2))
  594. Video::SetDebugRenderMode((Video::GetDebugRenderMode() + 1) % DebugRenderMode::Max);
  595. else if (KeyReleased(KEY_F1))
  596. Video::SetDebugRenderMode((Video::GetDebugRenderMode() + DebugRenderMode::Max - 1) % DebugRenderMode::Max);
  597. }
  598. #endif //NO_NACL_EM_INPUT
  599. #if NO_NACL_EM && WITH_TEXTURE
  600. if (!m_default_texture)
  601. {
  602. m_texture_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinymvtexture));
  603. m_texture_uni = m_texture_shader->GetUniformLocation("u_Texture");
  604. m_default_texture = Tiler::Register("data/test-texture.png", ivec2::zero, ivec2(0,1));
  605. }
  606. else if (m_texture && m_default_texture)
  607. m_texture_shader->SetUniform(m_texture_uni, m_default_texture->GetTexture(), 0);
  608. #endif //NO_NACL_EM
  609. g_renderer->SetClearColor(m_ssetup->m_clear_color);
  610. for (int i = 0; i < m_gizmos.Count(); ++i)
  611. {
  612. if (m_gizmos[i]->GetMeshState() == MeshRender::NeedConvert)
  613. m_gizmos[i]->MeshConvert();
  614. else
  615. break;
  616. }
  617. if (m_build_timer > .0f)
  618. {
  619. if (m_build_time < .0f)
  620. {
  621. m_build_time = m_build_timer;
  622. for (int i = 0; i < m_meshes.Count(); ++i)
  623. {
  624. if (m_meshes[i].m1 && m_meshes[i].m1->BD()->Cmdi() < m_meshes[i].m1->BD()->CmdStack().GetCmdNb())
  625. {
  626. EasyMesh* tmp = m_meshes[i].m1;
  627. EasyMesh* newtmp = new EasyMesh(*tmp);
  628. int ii = 1;
  629. #if 1
  630. bool stop = false;
  631. while (!stop)
  632. {
  633. int cmdi = newtmp->BD()->Cmdi() + ii;
  634. if (cmdi < newtmp->BD()->CmdStack().GetCmdNb())
  635. {
  636. switch (newtmp->BD()->CmdStack().GetCmd(cmdi))
  637. {
  638. case EasyMeshCmdType::LoopStart:
  639. case EasyMeshCmdType::LoopEnd:
  640. case EasyMeshCmdType::OpenBrace:
  641. case EasyMeshCmdType::CloseBrace:
  642. case EasyMeshCmdType::ScaleWinding:
  643. case EasyMeshCmdType::QuadWeighting:
  644. case EasyMeshCmdType::PostBuildNormal:
  645. case EasyMeshCmdType::PreventVertCleanup:
  646. case EasyMeshCmdType::SetColorA:
  647. case EasyMeshCmdType::SetColorB:
  648. {
  649. ii++;
  650. break;
  651. }
  652. default:
  653. {
  654. stop = true;
  655. break;
  656. }
  657. }
  658. }
  659. else
  660. stop = true;
  661. }
  662. #endif
  663. newtmp->BD()->CmdExecNb() = ii;
  664. newtmp->ExecuteCmdStack(false);
  665. m_meshes[i].m1 = newtmp;
  666. delete(tmp);
  667. }
  668. }
  669. }
  670. m_build_time -= seconds;
  671. }
  672. vec3 x = vec3(1.f,0.f,0.f);
  673. vec3 y = vec3(0.f,1.f,0.f);
  674. mat4 save_proj = m_camera->GetProjection();
  675. //Y object Offset
  676. mat4 mat_obj_offset = mat4::translate(x * m_screen_offset.x + y * m_screen_offset.y) *
  677. //Mesh Pos Offset
  678. mat4::translate((x * m_pos_mesh.x * RATIO_HW + y * m_pos_mesh.y) * 2.f * (1.f + .5f * m_zoom_mesh / SCREEN_LIMIT));
  679. //Align right meshes
  680. mat4 mat_align = mat4::translate(x - x * RATIO_HW);
  681. mat4 mat_gizmo = mat_obj_offset * mat_align * save_proj;
  682. for (int i = 0; i < m_meshes.Count(); i++)
  683. {
  684. {
  685. if (m_meshes[i].m1->GetMeshState() == MeshRender::NeedConvert)
  686. {
  687. #if WITH_TEXTURE
  688. m_meshes[i].m1->MeshConvert(new DefaultShaderData(((1 << VertexUsage::Position) | (1 << VertexUsage::Normal) |
  689. (1 << VertexUsage::Color) | (1 << VertexUsage::TexCoord)),
  690. m_texture_shader, true));
  691. #else
  692. m_meshes[i].m1->MeshConvert();
  693. #endif //WITH_TEXTURE
  694. }
  695. #if ALL_FEATURES
  696. float j = -(float)(m_meshes.Count() - (i + 1)) + (-m_mesh_id1 + (float)(m_meshes.Count() - 1));
  697. if (m_mesh_id1 - m_render_max[0] > (float)i && m_mesh_id1 - m_render_max[1] < (float)i &&
  698. m_meshes[i].m1->GetMeshState() > MeshRender::NeedConvert)
  699. {
  700. float a_j = lol::abs(j);
  701. float i_trans = (a_j * a_j * m_hist_scale_mesh.x + a_j * m_hist_scale_mesh.x) * .5f;
  702. float i_scale = clamp(1.f - (m_hist_scale_mesh.y * (m_mesh_id1 - (float)i)), 0.f, 1.f);
  703. //Mesh count offset
  704. mat4 mat_count_offset = mat4::translate(x * RATIO_HW * 2.f * (j + i_trans));
  705. //Mesh count scale
  706. mat4 mat_count_scale = mat4::scale(vec3(vec2(i_scale), 1.f));
  707. //Camera projection
  708. mat4 new_proj = mat_obj_offset * mat_count_offset * mat_align * mat_count_scale * save_proj;
  709. m_camera->SetProjection(new_proj);
  710. m_meshes[i].m1->Render(m_mat);
  711. g_renderer->Clear(ClearMask::Depth);
  712. }
  713. m_camera->SetProjection(save_proj);
  714. #else
  715. m_meshes[i].m1->Render(m_mat);
  716. #endif //ALL_FEATURES
  717. }
  718. }
  719. //Scene setup update
  720. if (m_ssetup)
  721. {
  722. m_camera->SetProjection(mat_gizmo);
  723. if (m_ssetup->m_show_gizmo)
  724. m_gizmos[GZ_Editor]->Render(m_mat);
  725. if (m_ssetup->m_show_lights)
  726. {
  727. for (int k = 0; k < m_ssetup->m_lights.Count(); ++k)
  728. {
  729. Light* ltmp = m_ssetup->m_lights[k];
  730. mat4 world = mat4::translate(ltmp->GetPosition());
  731. mat4 local = mat4::translate((inverse(m_mat) * world).v3.xyz);
  732. //dir light
  733. if (ltmp->GetType() == LightType::Directional)
  734. {
  735. m_gizmos[GZ_LightPos]->Render(m_mat * inverse(local));
  736. m_gizmos[GZ_LightDir]->Render(inverse(world) * inverse(mat4::lookat(vec3::zero, -ltmp->GetPosition(), vec3::axis_y)));
  737. }
  738. else //point light
  739. {
  740. m_gizmos[GZ_LightPos]->Render(m_mat * local);
  741. }
  742. }
  743. }
  744. m_camera->SetProjection(save_proj);
  745. }
  746. #if 0 //Debug normal draw
  747. for (int i = m_meshes.Count() - 1; 0 <= i && i < m_meshes.Count(); i++)
  748. {
  749. for (int j = 0; j < m_meshes[i].m1->m_indices.Count(); j += 3)
  750. {
  751. VertexData v[3] = { m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j ]],
  752. m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j+1]],
  753. m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j+2]]
  754. };
  755. for (int k = 0; k < 3; k++)
  756. Debug::DrawLine((m_mat * mat4::translate(v[k].m_coord)).v3.xyz,
  757. (m_mat * mat4::translate(v[(k+1)%3].m_coord)).v3.xyz, vec4(vec3((v[k].m_coord.z + 1.f)*.5f),1.f));
  758. }
  759. for (int j = 0; j < m_meshes[i].m1->m_vert.Count(); j++)
  760. {
  761. VertexData &v = m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j]];
  762. Debug::DrawLine((m_mat * mat4::translate(v.m_coord)).v3.xyz,
  763. (m_mat * mat4::translate(v.m_coord)).v3.xyz +
  764. (m_mat * vec4(v.m_normal * 5.f, 0.f)).xyz, vec4(lol::abs(v.m_normal), 1.f));
  765. }
  766. }
  767. #endif
  768. }
  769. private:
  770. SceneSetup* m_ssetup;
  771. Array<LightData> m_light_datas;
  772. Controller* m_controller;
  773. short m_input_usage;
  774. mat4 m_mat;
  775. mat4 m_mat_prev;
  776. bool m_init;
  777. bool m_first_tick;
  778. //Camera Setup
  779. Camera * m_camera;
  780. float m_reset_timer;
  781. float m_fov;
  782. float m_fov_mesh;
  783. float m_fov_speed;
  784. float m_zoom;
  785. float m_zoom_mesh;
  786. float m_zoom_speed;
  787. vec2 m_rot;
  788. vec2 m_rot_mesh;
  789. vec2 m_rot_speed;
  790. vec2 m_pos;
  791. vec2 m_pos_mesh;
  792. vec2 m_pos_speed;
  793. vec2 m_hist_scale;
  794. vec2 m_hist_scale_mesh;
  795. vec2 m_hist_scale_speed;
  796. vec2 m_screen_offset;
  797. //Mesh update timer
  798. float m_build_timer;
  799. float m_build_time;
  800. //Mesh infos
  801. vec2 m_render_max;
  802. int m_mesh_render;
  803. int m_mesh_id;
  804. float m_mesh_id1;
  805. Array<EasyMesh*, EasyMesh*> m_meshes;
  806. Array<EasyMesh*> m_gizmos;
  807. //File data
  808. String m_file_name;
  809. Array<String> m_cmdlist;
  810. float m_stream_update_time;
  811. float m_stream_update_timer;
  812. //misc datas
  813. Shader * m_texture_shader;
  814. TileSet * m_default_texture;
  815. Texture * m_texture;
  816. ShaderUniform m_texture_uni;
  817. };
  818. //The basic main :
  819. int main(int argc, char **argv)
  820. {
  821. System::Init(argc, argv);
  822. Application app("MeshViewer", ivec2((int)DEFAULT_WIDTH, (int)DEFAULT_HEIGHT), 60.0f);
  823. if (argc > 1)
  824. new MeshViewer(argv[1]);
  825. else
  826. new MeshViewer();
  827. app.Run();
  828. return EXIT_SUCCESS;
  829. }