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.
 
 
 

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