No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

1121 líneas
40 KiB

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