1124 righe
40 KiB

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