1132 righe
40 KiB

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