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.
 
 
 

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