Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

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