Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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