You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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