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.
 
 
 

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