您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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