You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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