Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

1122 rader
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. Debug::DrawSetupColor(Color::white);
  327. Debug::DrawSetupSegment(1.f);
  328. Debug::DrawGrid(vec3::zero, vec3::axis_x, vec3::axis_y, vec3::axis_z, 10.f);
  329. /** OLD STUFF **/
  330. //Draw(seconds);
  331. }
  332. //The basic main --------------------------------------------------------------
  333. int main(int argc, char **argv)
  334. {
  335. sys::init(argc, argv);
  336. Application app("MeshViewer", ivec2((int)DEFAULT_WIDTH, (int)DEFAULT_HEIGHT), 60.0f);
  337. if (argc > 1)
  338. new MeshViewer(argv[1]);
  339. else
  340. new MeshViewer();
  341. ////DEBUG TEST
  342. //SceneDisplay* display = new ApplicationDisplay("newDisplay", ivec2(800, 600));
  343. //SceneDisplay::Add(display);
  344. //Scene::GetScene(Scene::GetCount() - 1).SetDisplay(display);
  345. app.Run();
  346. return EXIT_SUCCESS;
  347. }
  348. //-------------------------------------------------------------------------
  349. //OLD ---------------------------------------------------------------------
  350. //-------------------------------------------------------------------------
  351. #if HAS_INPUT
  352. bool MeshViewer::KeyReleased(MVKeyboardList index) { return (HAS_KBOARD && m_controller->WasKeyReleasedThisFrame(index)); }
  353. bool MeshViewer::KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->WasKeyPressedThisFrame(index)); }
  354. bool MeshViewer::KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->IsKeyPressed(index)); }
  355. bool MeshViewer::KeyReleased(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->WasKeyReleasedThisFrame(index)); }
  356. bool MeshViewer::KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->WasKeyPressedThisFrame(index)); }
  357. bool MeshViewer::KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->IsKeyPressed(index)); }
  358. float MeshViewer::AxisValue(MVMouseAxisList index) { return (HAS_MOUSE) ? (m_controller->GetAxisValue(index)) : (0.f); }
  359. #endif //HAS_INPUT
  360. void MeshViewer::Prepare()
  361. {
  362. // Message Service
  363. MessageService::Setup();
  364. //Compile ref meshes
  365. m_gizmos << new EasyMesh();
  366. 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]");
  367. m_gizmos << new EasyMesh();
  368. m_gizmos.last()->Compile("[sc#666 acap 1 .5 .5 ty -.5 sc#fff asph 2 1]");
  369. m_gizmos << new EasyMesh();
  370. 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]");
  371. // Mesh Setup
  372. m_render_max = vec2(-.9f, 4.1f);
  373. m_mesh_render = 0;
  374. m_mesh_id = 0;
  375. m_mesh_id1 = 0.f;
  376. m_default_texture = nullptr;
  377. m_texture_shader = nullptr;
  378. m_texture = nullptr;
  379. //Camera Setup
  380. m_reset_timer = -1.f;
  381. m_fov = radians(-100.f);
  382. m_fov_mesh = 0.f;
  383. m_fov_speed = 0.f;
  384. m_zoom = 0.f;
  385. m_zoom_mesh = 0.f;
  386. m_zoom_speed = 0.f;
  387. m_rot = vec2(/*45.f*/0.f, -45.f);
  388. m_rot_mesh = vec2::zero;
  389. m_rot_speed = vec2::zero;
  390. m_pos = vec2::zero;
  391. m_pos_mesh = vec2::zero;
  392. m_pos_speed = vec2::zero;
  393. m_screen_offset = vec2::zero;
  394. m_hist_scale = vec2(.13f, .03f);
  395. m_hist_scale_mesh = vec2(.0f);
  396. m_hist_scale_speed = vec2(.0f);
  397. m_mat_prev = mat4(quat::fromeuler_xyz(vec3::zero));
  398. m_mat = mat4::translate(vec3(0.f));//mat4(quat::fromeuler_xyz(vec3(m_rot_mesh, .0f)));
  399. m_build_timer = 0.1f;
  400. m_build_time = -1.f;
  401. //stream update
  402. m_stream_update_time = 2.0f;
  403. m_stream_update_timer = 1.0f;
  404. m_init = true;
  405. m_input_usage = 0;
  406. #if HAS_INPUT
  407. /* Register an input controller for the keyboard */
  408. m_controller = new Controller("Default");
  409. m_controller->SetInputCount(MAX_KEYS, MAX_AXIS);
  410. if (InputDevice::Get(g_name_mouse.C()))
  411. {
  412. m_input_usage |= (1 << IPT_MV_MOUSE);
  413. m_controller->GetKey(MSE_CAM_ROT).BindMouse("Left");
  414. m_controller->GetKey(MSE_CAM_POS).BindMouse("Right");
  415. m_controller->GetKey(MSE_CAM_FOV).BindMouse("Middle");
  416. m_controller->GetKey(MSE_FOCUS).BindMouse("InScreen");
  417. m_controller->GetAxis(MSEX_CAM_Y).BindMouse("Y");
  418. m_controller->GetAxis(MSEX_CAM_X).BindMouse("X");
  419. }
  420. if (InputDevice::Get(g_name_keyboard.C()))
  421. {
  422. m_input_usage |= (1 << IPT_MV_KBOARD);
  423. //Camera keyboard rotation
  424. m_controller->GetKey(KEY_CAM_UP).BindKeyboard("Up");
  425. m_controller->GetKey(KEY_CAM_DOWN).BindKeyboard("Down");
  426. m_controller->GetKey(KEY_CAM_LEFT).BindKeyboard("Left");
  427. m_controller->GetKey(KEY_CAM_RIGHT).BindKeyboard("Right");
  428. //Camera keyboard position switch
  429. m_controller->GetKey(KEY_CAM_POS).BindKeyboard("LeftShift");
  430. m_controller->GetKey(KEY_CAM_FOV).BindKeyboard("LeftCtrl");
  431. //Camera unzoom switch
  432. m_controller->GetKey(KEY_CAM_RESET).BindKeyboard("Space");
  433. //Mesh change
  434. m_controller->GetKey(KEY_MESH_NEXT).BindKeyboard("PageUp");
  435. m_controller->GetKey(KEY_MESH_PREV).BindKeyboard("PageDown");
  436. //Base setup
  437. m_controller->GetKey(KEY_F1).BindKeyboard("F1");
  438. m_controller->GetKey(KEY_F2).BindKeyboard("F2");
  439. m_controller->GetKey(KEY_F3).BindKeyboard("F3");
  440. m_controller->GetKey(KEY_F4).BindKeyboard("F4");
  441. m_controller->GetKey(KEY_F5).BindKeyboard("F5");
  442. m_controller->GetKey(KEY_ESC).BindKeyboard("Escape");
  443. }
  444. #endif //HAS_INPUT
  445. m_camera = new Camera();
  446. m_camera->SetView(vec3(0.f, 0.f, 10.f), vec3::zero, vec3::axis_y);
  447. m_camera->SetProjection(0.f, .0001f, 2000.f, WIDTH * SCREEN_W, RATIO_HW);
  448. m_camera->UseShift(true);
  449. Scene& scene = Scene::GetScene();
  450. scene.PushCamera(m_camera);
  451. //Lights setup
  452. m_ssetup = new SceneSetup();
  453. #if NO_SC_SETUP
  454. m_ssetup->m_lights << new Light();
  455. m_ssetup->m_lights.last()->SetPosition(vec4(4.f, -1.f, -4.f, 0.f));
  456. m_ssetup->m_lights.last()->SetColor(vec4(.0f, .2f, .5f, 1.f));
  457. Ticker::Ref(m_ssetup->m_lights.last());
  458. m_ssetup->m_lights << new Light();
  459. m_ssetup->m_lights.last()->SetPosition(vec4(8.f, 2.f, 6.f, 0.f));
  460. m_ssetup->m_lights.last()->SetColor(vec4(1.f));
  461. Ticker::Ref(m_ssetup->m_lights.last());
  462. EasyMesh* em = new EasyMesh();
  463. if (em->Compile("sc#fff ab 1"))
  464. {
  465. if (m_mesh_id == m_meshes.count() - 1)
  466. m_mesh_id++;
  467. m_meshes.push(em, nullptr);
  468. }
  469. #else
  470. //TOUKY CHANGE THAT
  471. /*
  472. m_ssetup->Compile("addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1) "
  473. "addlight 0.0 position (8 2 6) color #ffff "
  474. "showgizmo true ");
  475. */
  476. m_ssetup->Startup();
  477. #endif //NO_SC_SETUP
  478. for (int i = 0; i < m_ssetup->m_lights.count(); ++i)
  479. {
  480. m_light_datas << LightData(m_ssetup->m_lights[i]->GetPosition().xyz, m_ssetup->m_lights[i]->GetColor());
  481. m_ssetup->m_lights[i]->SetPosition(vec3::zero);
  482. m_ssetup->m_lights[i]->SetColor(vec4::zero);
  483. }
  484. }
  485. void MeshViewer::Unprepare()
  486. {
  487. Scene& scene = Scene::GetScene();
  488. if (m_camera) scene.PopCamera(m_camera);
  489. if (m_ssetup) delete m_ssetup;
  490. MessageService::Destroy();
  491. //Register all entities
  492. for (Entity* entity : m_entities)
  493. Ticker::Unref(entity);
  494. m_controller = nullptr;
  495. m_camera = nullptr;
  496. m_ssetup = nullptr;
  497. /** ----- Init is needed ----- **/
  498. m_init = false;
  499. }
  500. void MeshViewer::Update(float seconds)
  501. {
  502. //TODO : This should probably be "standard LoL behaviour"
  503. #if HAS_INPUT
  504. {
  505. //Shutdown logic
  506. if (KeyReleased(KEY_ESC))
  507. Ticker::Shutdown();
  508. }
  509. #endif //HAS_INPUT
  510. //Compute render mesh count
  511. float a_j = lol::abs(m_render_max[1]);
  512. float i_m = m_hist_scale_mesh.x;
  513. float i_trans = a_j - ((a_j * a_j * i_m * i_m + a_j * i_m) * .5f);
  514. m_render_max[1] = a_j * ((RATIO_WH * 1.f) / ((i_trans != 0.f)?(i_trans):(RATIO_WH))) - RATIO_HW * .3f;
  515. //Mesh Change
  516. #if HAS_INPUT
  517. m_mesh_id = clamp(m_mesh_id + ((int)KeyPressed(KEY_MESH_PREV) - (int)KeyPressed(KEY_MESH_NEXT)), 0, m_meshes.count() - 1);
  518. #endif //HAS_INPUT
  519. m_mesh_id1 = damp(m_mesh_id1, (float)m_mesh_id, .2f, seconds);
  520. #if ALL_FEATURES
  521. //Update light position & damping
  522. for (int i = 0; i < m_ssetup->m_lights.count(); ++i)
  523. {
  524. vec3 pos = (m_mat * inverse(m_mat_prev) * vec4(m_ssetup->m_lights[i]->GetPosition(), 1.f)).xyz;
  525. vec3 tgt = (m_mat * vec4(m_light_datas[i].m_pos, 1.f)).xyz;
  526. vec3 new_pos = damp(pos, tgt, .3f, seconds);
  527. vec4 new_col = damp(m_ssetup->m_lights[i]->GetColor(), m_light_datas[i].m_col, .3f, seconds);
  528. m_ssetup->m_lights[i]->SetPosition(new_pos);
  529. m_ssetup->m_lights[i]->SetColor(new_col);
  530. }
  531. //Camera update
  532. bool is_pos = false;
  533. bool is_fov = false;
  534. bool is_hsc = false;
  535. vec2 tmpv = vec2::zero;
  536. #if HAS_INPUT
  537. is_pos = KeyDown(KEY_CAM_POS) || KeyDown(MSE_CAM_POS);
  538. is_fov = KeyDown(KEY_CAM_FOV) || KeyDown(MSE_CAM_FOV);
  539. if (KeyDown(MSE_FOCUS) && (KeyDown(MSE_CAM_ROT) || KeyDown(MSE_CAM_POS) || KeyDown(MSE_CAM_FOV)))
  540. {
  541. tmpv += vec2(AxisValue(MSEX_CAM_Y), AxisValue(MSEX_CAM_X));
  542. if (KeyDown(MSE_CAM_ROT))
  543. tmpv *= vec2(1.f, 1.f) * 6.f;
  544. if (KeyDown(MSE_CAM_POS))
  545. tmpv *= vec2(1.f, -1.f) * 3.f;
  546. if (KeyDown(MSE_CAM_FOV))
  547. tmpv = vec2(tmpv.y * 4.f, tmpv.x * 6.f);
  548. }
  549. tmpv += vec2((float)KeyDown(KEY_CAM_UP ) - (float)KeyDown(KEY_CAM_DOWN),
  550. (float)KeyDown(KEY_CAM_RIGHT) - (float)KeyDown(KEY_CAM_LEFT));
  551. #endif //HAS_INPUT
  552. //Base data
  553. vec2 rot = (!is_pos && !is_fov)?(tmpv):(vec2(.0f)); rot = vec2(rot.x, -rot.y);
  554. vec2 pos = ( is_pos && !is_fov)?(tmpv):(vec2(.0f)); pos = -vec2(pos.y, pos.x);
  555. vec2 fov = (!is_pos && is_fov )?(tmpv):(vec2(.0f)); fov = vec2(-fov.x, fov.y);
  556. vec2 hsc = (is_hsc)?(vec2(0.f)):(vec2(0.f));
  557. //speed
  558. m_rot_speed = damp(m_rot_speed, rot * ROT_SPEED, .2f, seconds);
  559. float pos_factor = 1.f / (1.f + m_zoom_mesh * .5f);
  560. m_pos_speed = damp(m_pos_speed, pos * POS_SPEED * pos_factor, .2f, seconds);
  561. float fov_factor = 1.f + lol::pow((m_fov_mesh / FOV_CLAMP) * 1.f, 2.f);
  562. m_fov_speed = damp(m_fov_speed, fov.x * FOV_SPEED * fov_factor, .2f, seconds);
  563. float zom_factor = 1.f + lol::pow((m_zoom_mesh / ZOM_CLAMP) * 1.f, 2.f);
  564. m_zoom_speed = damp(m_zoom_speed, fov.y * ZOM_SPEED * zom_factor, .2f, seconds);
  565. m_hist_scale_speed = damp(m_hist_scale_speed, hsc * HST_SPEED, .2f, seconds);
  566. m_rot += m_rot_speed * seconds;
  567. #if HAS_INPUT
  568. if (m_reset_timer >= 0.f)
  569. m_reset_timer -= seconds;
  570. if (KeyPressed(KEY_CAM_RESET))
  571. {
  572. if (m_reset_timer >= 0.f)
  573. {
  574. m_pos = vec2(0.f);
  575. m_zoom = 0.f;
  576. }
  577. else
  578. m_reset_timer = RESET_TIMER;
  579. }
  580. //Transform update
  581. if (!KeyDown(KEY_CAM_RESET))
  582. {
  583. m_pos += m_pos_speed * seconds;
  584. m_fov += m_fov_speed * seconds;
  585. m_zoom += m_zoom_speed * seconds;
  586. m_hist_scale += m_hist_scale_speed * seconds;
  587. }
  588. #endif //HAS_INPUT
  589. //clamp
  590. vec2 rot_mesh = vec2(SmoothClamp(m_rot.x, -ROT_CLAMP, ROT_CLAMP, ROT_CLAMP * .1f), m_rot.y);
  591. vec2 pos_mesh = vec2(SmoothClamp(m_pos.x, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f),
  592. SmoothClamp(m_pos.y, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f));
  593. float fov_mesh = SmoothClamp(m_fov, 0.f, FOV_CLAMP, FOV_CLAMP * .1f);
  594. float zoom_mesh = SmoothClamp(m_zoom, -ZOM_CLAMP, ZOM_CLAMP, ZOM_CLAMP * .1f);
  595. vec2 hist_scale_mesh = vec2(SmoothClamp(m_hist_scale.x, 0.f, HST_CLAMP, HST_CLAMP * .1f),
  596. SmoothClamp(m_hist_scale.y, 0.f, HST_CLAMP, HST_CLAMP * .1f));
  597. #if HAS_INPUT
  598. if (KeyDown(KEY_CAM_RESET) && m_reset_timer < 0.f)
  599. {
  600. pos_mesh = vec2::zero;
  601. zoom_mesh = 0.f;
  602. }
  603. #endif //HAS_INPUT
  604. m_rot_mesh = vec2(damp(m_rot_mesh.x, rot_mesh.x, .2f, seconds), damp(m_rot_mesh.y, rot_mesh.y, .2f, seconds));
  605. m_pos_mesh = vec2(damp(m_pos_mesh.x, pos_mesh.x, .2f, seconds), damp(m_pos_mesh.y, pos_mesh.y, .2f, seconds));
  606. m_fov_mesh = damp(m_fov_mesh, fov_mesh, .2f, seconds);
  607. m_zoom_mesh = damp(m_zoom_mesh, zoom_mesh, .2f, seconds);
  608. m_hist_scale_mesh = damp(m_hist_scale_mesh, hist_scale_mesh, .2f, seconds);
  609. //Mesh mat calculation
  610. m_mat_prev = m_mat;
  611. m_mat = mat4::translate(vec3(0.f));
  612. //Target List Setup
  613. TargetCamera tc;
  614. if (m_meshes.count() && m_mesh_id >= 0)
  615. for (int i = 0; i < m_meshes[m_mesh_id].m1->GetVertexCount(); i++)
  616. tc.AddTarget((m_mat * mat4::translate(m_meshes[m_mesh_id].m1->GetVertexLocation(i)))[3].xyz);
  617. tc.AddTarget(box3(vec3(0.f), vec3(1.f)));
  618. for (int k = 0; k < m_ssetup->m_lights.count() && m_ssetup->m_show_lights; ++k)
  619. {
  620. vec3 light_pos = m_ssetup->m_lights[k]->GetPosition();
  621. mat4 world_cam = m_camera->GetView();
  622. light_pos = (inverse(world_cam) * vec4((world_cam * vec4(light_pos, 1.0f)).xyz * vec3::axis_z, 1.0f)).xyz;
  623. tc.AddTarget(box3(vec3(-1.f), vec3(1.f)) + light_pos *
  624. ((m_ssetup->m_lights[k]->GetType() == LightType::Directional)?(-1.f):(1.f)));
  625. }
  626. //--
  627. //Update mesh screen location - Get the Min/Max needed
  628. //--
  629. vec2 cam_center(0.f);
  630. float cam_factor = .0f;
  631. vec3 local_min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
  632. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  633. mat4 world_cam = m_camera->GetView();
  634. mat4 cam_screen = m_camera->GetProjection();
  635. //target on-screen computation
  636. for (int i = 0; i < tc.m_targets.count(); i++)
  637. {
  638. vec3 obj_loc = tc.m_targets[i];
  639. {
  640. //Debug::DrawBox(obj_loc - vec3(4.f), obj_loc + vec3(4.f), vec4(1.f, 0.f, 0.f, 1.f));
  641. mat4 target_mx = mat4::translate(obj_loc);
  642. vec3 vpos;
  643. //Get location in cam coordinates
  644. target_mx = world_cam * target_mx;
  645. vpos = target_mx[3].xyz;
  646. local_min_max[0] = min(vpos.xyz, local_min_max[0]);
  647. local_min_max[1] = max(vpos.xyz, local_min_max[1]);
  648. //Get location in screen coordinates
  649. target_mx = cam_screen * target_mx;
  650. vpos = (target_mx[3] / target_mx[3].w).xyz;
  651. screen_min_max[0] = min(screen_min_max[0], vpos.xy * vec2(RATIO_WH, 1.f));
  652. screen_min_max[1] = max(screen_min_max[1], vpos.xy * vec2(RATIO_WH, 1.f));
  653. //Build Barycenter
  654. cam_center += vpos.xy;
  655. cam_factor += 1.f;
  656. }
  657. }
  658. float screen_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
  659. max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
  660. float z_dist = //m_camera->m_target_distance
  661. length(m_camera->m_position)
  662. + max(local_min_max[0].z, local_min_max[1].z);
  663. vec2 screen_offset = vec2(0.f, -(screen_min_max[1].y + screen_min_max[0].y) * .5f);
  664. m_screen_offset = damp(m_screen_offset, screen_offset, .9f, seconds);
  665. float forced_zoom = m_zoom_mesh;
  666. if (cam_factor > 0.f)
  667. {
  668. vec2 old_sscale = m_camera->GetScreenScale();
  669. float old_ssize = m_camera->GetScreenSize();
  670. float zoom_in = 1.f + lol::max(0.f, forced_zoom);
  671. float zoom_out = 1.f + lol::max(0.f, -forced_zoom);
  672. m_camera->SetScreenScale(max(vec2(0.001f), ((old_sscale * zoom_in) / (screen_ratio * zoom_out * SCREEN_LIMIT))));
  673. m_camera->SetFov(m_fov_mesh);
  674. m_camera->SetScreenInfos(damp(old_ssize, max(1.f, screen_ratio * zoom_out), 1.2f, seconds));
  675. vec3 posz = ((mat4::rotate(m_rot_mesh.y, vec3::axis_y) * mat4::rotate(-m_rot_mesh.x, vec3::axis_x) * vec4::axis_z)).xyz;
  676. vec3 newpos = posz * damp(length(m_camera->m_position), z_dist * 1.2f, .1f, seconds);
  677. m_camera->SetView(newpos, vec3(0.f), vec3::axis_y);
  678. }
  679. //--
  680. //Message Service
  681. //--
  682. String mesh("");
  683. int u = 1;
  684. while (u-- > 0 && MessageService::FetchFirst(MessageBucket::AppIn, mesh))
  685. {
  686. int o = 1;
  687. while (o-- > 0)
  688. {
  689. SceneSetup* new_ssetup = new SceneSetup();
  690. if (false) //new_ssetup->Compile(mesh.C()) && new_ssetup->m_lights.count())
  691. {
  692. //Store current light datas, in World
  693. array<LightData> light_datas;
  694. for (int i = 0; i < m_ssetup->m_lights.count(); ++i)
  695. light_datas << LightData(m_ssetup->m_lights[i]->GetPosition(), m_ssetup->m_lights[i]->GetColor());
  696. if (m_ssetup)
  697. delete m_ssetup;
  698. m_ssetup = new_ssetup;
  699. m_ssetup->Startup();
  700. //Restore all light datas so blend can occur
  701. mat4 light_mat = m_mat * inverse(mat4(quat::fromeuler_xyz(vec3::zero)));
  702. for (int i = 0; i < m_ssetup->m_lights.count(); ++i)
  703. {
  704. //Store local dst in current m_ld
  705. LightData ltmp = LightData(m_ssetup->m_lights[i]->GetPosition(), m_ssetup->m_lights[i]->GetColor());
  706. if (i < m_light_datas.count())
  707. m_light_datas[i] = ltmp;
  708. else
  709. m_light_datas << ltmp;
  710. vec3 loc = vec3::zero;
  711. vec4 col = vec4::zero;
  712. if (i < light_datas.count())
  713. {
  714. loc = light_datas[i].m_pos;
  715. col = light_datas[i].m_col;
  716. }
  717. //Restore old light datas in new lights
  718. m_ssetup->m_lights[i]->SetPosition(loc);
  719. m_ssetup->m_lights[i]->SetColor(col);
  720. }
  721. }
  722. else
  723. {
  724. m_ssetup->m_custom_cmd += new_ssetup->m_custom_cmd;
  725. delete new_ssetup;
  726. }
  727. }
  728. }
  729. //Check the custom cmd even if we don't have new messages.
  730. int o = 1;
  731. while (o-- > 0)
  732. {
  733. for (int i = 0; m_ssetup && i < m_ssetup->m_custom_cmd.count(); ++i)
  734. {
  735. if (m_ssetup->m_custom_cmd[i].m1 == "setmesh")
  736. {
  737. //Create a new mesh
  738. EasyMesh* em = new EasyMesh();
  739. if (em->Compile(m_ssetup->m_custom_cmd[i].m2.C(), false))
  740. {
  741. em->BD()->Cmdi() = 0;
  742. if (m_mesh_id == m_meshes.count() - 1)
  743. m_mesh_id++;
  744. m_meshes.push(em, nullptr);
  745. }
  746. else
  747. delete em;
  748. }
  749. }
  750. }
  751. m_ssetup->m_custom_cmd.empty();
  752. #endif //ALL_FEATURES
  753. #if HAS_WEB
  754. /*
  755. if (m_stream_update_time > .0f)
  756. {
  757. m_stream_update_time = -1.f;
  758. MessageService::Send(MessageBucket::AppIn,
  759. " addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1) \
  760. addlight 0.0 position (8 2 6) color #ffff \
  761. custom setmesh \"[sc#f8f ab 1]\"");
  762. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1]");
  763. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1 splt 4 twy 90]");
  764. // MessageService::Send(MessageBucket::AppIn, "[sc#8ff afcb 1 1 1 0]");
  765. // MessageService::Send(MessageBucket::AppIn, "[sc#ff8 afcb 1 1 1 0]");
  766. }
  767. */
  768. #elif defined(_WIN32)
  769. //--
  770. //File management
  771. //--
  772. m_stream_update_time += seconds;
  773. if (m_stream_update_time > m_stream_update_timer)
  774. {
  775. m_stream_update_time = 0.f;
  776. File f;
  777. f.Open(m_file_name.C(), FileAccess::Read);
  778. String cmd = f.ReadString();
  779. f.Close();
  780. if (cmd.count()
  781. && (!m_cmdlist.count() || cmd != m_cmdlist.last()))
  782. {
  783. m_cmdlist << cmd;
  784. MessageService::Send(MessageBucket::AppIn, cmd);
  785. }
  786. }
  787. #endif //WINDOWS
  788. }
  789. void MeshViewer::Draw(float seconds, Scene &scene)
  790. {
  791. if (!m_init || !m_first_tick)
  792. return;
  793. //TODO : This should probably be "standard LoL behaviour"
  794. #if HAS_INPUT
  795. {
  796. if (KeyReleased(KEY_F2))
  797. Video::SetDebugRenderMode((Video::GetDebugRenderMode() + 1) % DebugRenderMode::Max);
  798. else if (KeyReleased(KEY_F1))
  799. Video::SetDebugRenderMode((Video::GetDebugRenderMode() + DebugRenderMode::Max - 1) % DebugRenderMode::Max);
  800. }
  801. #endif //HAS_INPUT
  802. #if !HAS_WEB && WITH_TEXTURE
  803. if (!m_default_texture)
  804. {
  805. m_texture_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinymvtexture));
  806. m_texture_uni = m_texture_shader->GetUniformLocation("u_Texture");
  807. m_default_texture = Tiler::Register("data/test-texture.png", ivec2::zero, ivec2(0,1));
  808. }
  809. else if (m_texture && m_default_texture)
  810. m_texture_shader->SetUniform(m_texture_uni, m_default_texture->GetTexture(), 0);
  811. #endif //!HAS_WEB && WITH_TEXTURE
  812. Renderer::Get()->SetClearColor(m_ssetup->m_clear_color);
  813. for (int i = 0; i < m_gizmos.count(); ++i)
  814. {
  815. if (m_gizmos[i]->GetMeshState() == MeshRender::NeedConvert)
  816. m_gizmos[i]->MeshConvert();
  817. else
  818. break;
  819. }
  820. if (m_build_timer > .0f)
  821. {
  822. if (m_build_time < .0f)
  823. {
  824. m_build_time = m_build_timer;
  825. for (int i = 0; i < m_meshes.count(); ++i)
  826. {
  827. if (m_meshes[i].m1 && m_meshes[i].m1->BD()->Cmdi() < m_meshes[i].m1->BD()->CmdStack().GetCmdNb())
  828. {
  829. EasyMesh* tmp = m_meshes[i].m1;
  830. EasyMesh* newtmp = new EasyMesh(*tmp);
  831. int ii = 1;
  832. #if 1
  833. bool stop = false;
  834. while (!stop)
  835. {
  836. int cmdi = newtmp->BD()->Cmdi() + ii;
  837. if (cmdi < newtmp->BD()->CmdStack().GetCmdNb())
  838. {
  839. switch (newtmp->BD()->CmdStack().GetCmd(cmdi))
  840. {
  841. case EasyMeshCmdType::LoopStart:
  842. case EasyMeshCmdType::LoopEnd:
  843. case EasyMeshCmdType::OpenBrace:
  844. case EasyMeshCmdType::CloseBrace:
  845. case EasyMeshCmdType::ScaleWinding:
  846. case EasyMeshCmdType::QuadWeighting:
  847. case EasyMeshCmdType::PostBuildNormal:
  848. case EasyMeshCmdType::PreventVertCleanup:
  849. case EasyMeshCmdType::SetColorA:
  850. case EasyMeshCmdType::SetColorB:
  851. {
  852. ii++;
  853. break;
  854. }
  855. default:
  856. {
  857. stop = true;
  858. break;
  859. }
  860. }
  861. }
  862. else
  863. stop = true;
  864. }
  865. #endif
  866. newtmp->BD()->CmdExecNb() = ii;
  867. newtmp->ExecuteCmdStack(false);
  868. m_meshes[i].m1 = newtmp;
  869. delete tmp;
  870. }
  871. }
  872. }
  873. m_build_time -= seconds;
  874. }
  875. #define NORMAL_USAGE 1
  876. #if NORMAL_USAGE
  877. vec3 x = vec3(1.f,0.f,0.f);
  878. vec3 y = vec3(0.f,1.f,0.f);
  879. mat4 save_proj = m_camera->GetProjection();
  880. //Y object Offset
  881. mat4 mat_obj_offset = mat4::translate(x * m_screen_offset.x + y * m_screen_offset.y) *
  882. //Mesh Pos Offset
  883. mat4::translate((x * m_pos_mesh.x * RATIO_HW + y * m_pos_mesh.y) * 2.f * (1.f + .5f * m_zoom_mesh / SCREEN_LIMIT));
  884. //Align right meshes
  885. mat4 mat_align = mat4::translate(x - x * RATIO_HW);
  886. mat4 mat_gizmo = mat_obj_offset * mat_align * save_proj;
  887. for (int i = 0; i < m_meshes.count(); i++)
  888. {
  889. {
  890. if (m_meshes[i].m1->GetMeshState() == MeshRender::NeedConvert)
  891. {
  892. #if WITH_TEXTURE
  893. m_meshes[i].m1->MeshConvert(new DefaultShaderData(((1 << VertexUsage::Position) | (1 << VertexUsage::Normal) |
  894. (1 << VertexUsage::Color) | (1 << VertexUsage::TexCoord)),
  895. m_texture_shader, true));
  896. #else
  897. m_meshes[i].m1->MeshConvert();
  898. #endif //WITH_TEXTURE
  899. }
  900. #if ALL_FEATURES
  901. float j = -(float)(m_meshes.count() - (i + 1)) + (-m_mesh_id1 + (float)(m_meshes.count() - 1));
  902. if (m_mesh_id1 - m_render_max[0] > (float)i && m_mesh_id1 - m_render_max[1] < (float)i &&
  903. m_meshes[i].m1->GetMeshState() > MeshRender::NeedConvert)
  904. {
  905. float a_j = lol::abs(j);
  906. float i_trans = (a_j * a_j * m_hist_scale_mesh.x + a_j * m_hist_scale_mesh.x) * .5f;
  907. float i_scale = clamp(1.f - (m_hist_scale_mesh.y * (m_mesh_id1 - (float)i)), 0.f, 1.f);
  908. //Mesh count offset
  909. mat4 mat_count_offset = mat4::translate(x * RATIO_HW * 2.f * (j + i_trans));
  910. //Mesh count scale
  911. mat4 mat_count_scale = mat4::scale(vec3(vec2(i_scale), 1.f));
  912. //Camera projection
  913. mat4 new_proj = mat_obj_offset * mat_count_offset * mat_align * mat_count_scale * save_proj;
  914. m_camera->SetProjection(new_proj);
  915. m_meshes[i].m1->Render(scene, m_mat);
  916. Renderer::Get()->Clear(ClearMask::Depth);
  917. }
  918. m_camera->SetProjection(save_proj);
  919. #else
  920. m_meshes[i].m1->Render(scene, m_mat);
  921. #endif //ALL_FEATURES
  922. }
  923. }
  924. //Scene setup update
  925. if (m_ssetup)
  926. {
  927. m_camera->SetProjection(mat_gizmo);
  928. if (m_ssetup->m_show_gizmo)
  929. m_gizmos[GZ_Editor]->Render(scene, m_mat);
  930. if (m_ssetup->m_show_lights)
  931. {
  932. for (int k = 0; k < m_ssetup->m_lights.count(); ++k)
  933. {
  934. Light* ltmp = m_ssetup->m_lights[k];
  935. mat4 world = mat4::translate(ltmp->GetPosition());
  936. mat4 local = mat4::translate((inverse(m_mat) * world)[3].xyz);
  937. //dir light
  938. if (ltmp->GetType() == LightType::Directional)
  939. {
  940. m_gizmos[GZ_LightPos]->Render(scene, m_mat * inverse(local));
  941. m_gizmos[GZ_LightDir]->Render(scene, inverse(world) * inverse(mat4::lookat(vec3::zero, -ltmp->GetPosition(), vec3::axis_y)));
  942. }
  943. else //point light
  944. {
  945. m_gizmos[GZ_LightPos]->Render(scene, m_mat * local);
  946. }
  947. }
  948. }
  949. m_camera->SetProjection(save_proj);
  950. }
  951. #endif //NORMAL_USAGE
  952. #if 0 //Debug normal draw
  953. for (int i = m_meshes.count() - 1; 0 <= i && i < m_meshes.count(); i++)
  954. {
  955. for (int j = 0; j < m_meshes[i].m1->m_indices.count(); j += 3)
  956. {
  957. VertexData v[3] = { m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j ]],
  958. m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j+1]],
  959. m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j+2]]
  960. };
  961. for (int k = 0; k < 3; k++)
  962. Debug::DrawLine((m_mat * mat4::translate(v[k].m_coord))[3].xyz,
  963. (m_mat * mat4::translate(v[(k+1)%3].m_coord))[3].xyz, vec4(vec3((v[k].m_coord.z + 1.f)*.5f),1.f));
  964. }
  965. for (int j = 0; j < m_meshes[i].m1->m_vert.count(); j++)
  966. {
  967. VertexData &v = m_meshes[i].m1->m_vert[m_meshes[i].m1->m_indices[j]];
  968. Debug::DrawLine((m_mat * mat4::translate(v.m_coord))[3].xyz,
  969. (m_mat * mat4::translate(v.m_coord))[3].xyz +
  970. (m_mat * vec4(v.m_normal * 5.f, 0.f)).xyz, vec4(lol::abs(v.m_normal), 1.f));
  971. }
  972. }
  973. #endif
  974. }