344 line
7.9 KiB

  1. //
  2. // Lol Engine — EasyMesh tutorial
  3. //
  4. // Copyright © 2009—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. // © 2012—2018 Sam Hocevar <sam@hocevar.net>
  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. #pragma once
  14. #include <string>
  15. namespace lol
  16. {
  17. enum
  18. {
  19. IPT_MV_KBOARD = 0,
  20. IPT_MV_MOUSE,
  21. INPUT_MAX
  22. };
  23. enum MVKeyboardList
  24. {
  25. KEY_CAM_RESET = 0,
  26. KEY_CAM_POS,
  27. KEY_CAM_FOV,
  28. KEY_CAM_UP,
  29. KEY_CAM_DOWN,
  30. KEY_CAM_LEFT,
  31. KEY_CAM_RIGHT,
  32. KEY_MESH_NEXT,
  33. KEY_MESH_PREV,
  34. KEY_F1,
  35. KEY_F2,
  36. KEY_F3,
  37. KEY_F4,
  38. KEY_F5,
  39. KEY_ESC,
  40. KEY_MAX
  41. };
  42. enum MVMouseKeyList
  43. {
  44. MSE_CAM_ROT = KEY_MAX,
  45. MSE_CAM_POS,
  46. MSE_CAM_FOV,
  47. MSE_FOCUS,
  48. MSE_MAX
  49. };
  50. enum MVMouseAxisList
  51. {
  52. MSEX_CAM_Y = 0,
  53. MSEX_CAM_X,
  54. MSEX_MAX
  55. };
  56. #define MAX_KEYS MSE_MAX
  57. #define MAX_AXIS MSEX_MAX
  58. //MeshViewerInput ---------------------------------------------------------
  59. struct MeshViewerKeyInputBase : public StructSafeEnum
  60. {
  61. enum Type
  62. {
  63. KBD_BEG = 0,
  64. Exit = KBD_BEG,
  65. KBD_END,
  66. MSE_BEG = KBD_END,
  67. LeftClick = MSE_BEG,
  68. RightClick,
  69. MiddleClick,
  70. Focus,
  71. MSE_END,
  72. MAX = MSE_END,
  73. };
  74. protected:
  75. virtual bool BuildEnumMap(map<int64_t, std::string>& enum_map)
  76. {
  77. enum_map[Exit] = g_name_key_Escape;
  78. enum_map[LeftClick] = g_name_mouse_key_left;
  79. enum_map[RightClick] = g_name_mouse_key_right;
  80. enum_map[MiddleClick] = g_name_mouse_key_middle;
  81. enum_map[Focus] = g_name_mouse_key_in_screen;
  82. return true;
  83. }
  84. };
  85. typedef SafeEnum<MeshViewerKeyInputBase> MeshViewerKeyInput;
  86. #define ALL_FEATURES 1
  87. #define NO_SC_SETUP 0
  88. enum GizmoType
  89. {
  90. GZ_Editor = 0,
  91. GZ_LightPos,
  92. GZ_LightDir,
  93. GZ_MAX
  94. };
  95. struct LightData
  96. {
  97. LightData(vec3 pos, vec4 col)
  98. {
  99. m_pos = pos;
  100. m_col = col;
  101. }
  102. vec3 m_pos;
  103. vec4 m_col;
  104. };
  105. //ViewerObject ----------------------------------------------------------------
  106. class ViewerObject
  107. {
  108. public:
  109. ViewerObject() { }
  110. ViewerObject(std::string const& name) : m_name(name) { }
  111. virtual ~ViewerObject() { }
  112. virtual void TickDraw(float seconds, Scene &scene) { }
  113. std::string GetName() { return m_name; }
  114. protected:
  115. std::string m_name;
  116. };
  117. //EasyMeshViewerObject --------------------------------------------------------
  118. class EasyMeshViewerObject : public ViewerObject
  119. {
  120. typedef ViewerObject super;
  121. public:
  122. EasyMeshViewerObject()
  123. : ViewerObject() { }
  124. EasyMeshViewerObject(std::string const& name, EasyMesh const& mesh)
  125. : ViewerObject(name)
  126. {
  127. Init(name, mesh);
  128. }
  129. virtual ~EasyMeshViewerObject() { }
  130. virtual void TickDraw(float seconds, Scene &scene);
  131. void Init(std::string const& name, EasyMesh const& mesh)
  132. {
  133. m_name = name;
  134. m_mesh = mesh;
  135. }
  136. protected:
  137. EasyMesh m_mesh;
  138. };
  139. //MeshViewerLoadJob -----------------------------------------------------------
  140. class MeshViewerLoadJob : public ThreadJob
  141. {
  142. friend class BaseThreadManager;
  143. typedef ThreadJob super;
  144. public:
  145. inline MeshViewerLoadJob() : ThreadJob() { }
  146. inline MeshViewerLoadJob(std::string const& path)
  147. : ThreadJob(ThreadJobType::WORK_TODO), m_path(path) { }
  148. virtual ~MeshViewerLoadJob() { }
  149. virtual void RetrieveResult(class MeshViewer* app) { }
  150. protected:
  151. virtual bool DoWork() { return super::DoWork(); }
  152. protected:
  153. std::string m_path;
  154. };
  155. //EasyMeshLoadJob -------------------------------------------------------------
  156. class EasyMeshLoadJob : public MeshViewerLoadJob
  157. {
  158. friend class BaseThreadManager;
  159. typedef EasyMeshLoadJob super;
  160. public:
  161. inline EasyMeshLoadJob() : MeshViewerLoadJob() { }
  162. inline EasyMeshLoadJob(std::string const& path)
  163. : MeshViewerLoadJob(path) { }
  164. virtual ~EasyMeshLoadJob() { }
  165. static MeshViewerLoadJob* GetInstance(std::string const& path);
  166. virtual void RetrieveResult(class MeshViewer* app);
  167. protected:
  168. static bool Check(std::string const& path) { return path.find(".easymesh") != std::string::npos; }
  169. virtual bool DoWork();
  170. protected:
  171. EasyMeshLuaLoader m_loader;
  172. array<EasyMeshViewerObject*> m_meshes;
  173. };
  174. //MeshViewer ------------------------------------------------------------------
  175. class MeshViewer : public WorldEntity
  176. {
  177. typedef WorldEntity super;
  178. public:
  179. MeshViewer(char const *file_name = "../data/meshviewer.easymesh.lua");
  180. ~MeshViewer();
  181. void Start();
  182. void Stop();
  183. void UpdateSceneSetup(bool only_destroy = false);
  184. MeshViewerLoadJob* GetLoadJob(std::string const& path);
  185. void AddViewerObj(ViewerObject* obj) { m_objs << obj; }
  186. virtual void TickGame(float seconds);
  187. virtual void TickDraw(float seconds, Scene &scene);
  188. bool KeyReleased(MVKeyboardList index);
  189. bool KeyPressed(MVKeyboardList index);
  190. bool KeyDown(MVKeyboardList index);
  191. bool KeyReleased(MVMouseKeyList index);
  192. bool KeyPressed(MVMouseKeyList index);
  193. bool KeyDown(MVMouseKeyList index);
  194. float AxisValue(MVMouseAxisList index);
  195. void Prepare();
  196. void Unprepare();
  197. void Update(float seconds);
  198. void Draw(float seconds, Scene &scene);
  199. private:
  200. //Main stuff --------------------------------------------------------------
  201. bool m_init = false;
  202. bool m_first_tick = false;
  203. InputProfile m_profile;
  204. Camera* m_camera = nullptr;
  205. Text* m_text = nullptr;
  206. //ImGui stuff
  207. bool m_menu_cam_useage = true;
  208. float m_menu_cam_fov = radians(40.f);
  209. vec3 m_menu_cam_pos = vec3(20.f, 45.f, 45.f);
  210. int m_menu_mesh_idx = 0;
  211. array<char const *> m_menu_mesh_names_char;
  212. array<std::string> m_menu_mesh_names_str;
  213. //Scene setup data
  214. SceneSetupLuaLoader m_ssetup_loader;
  215. FileUpdateStatus *m_ssetup_file_status = nullptr;
  216. std::string m_ssetup_file_name;
  217. std::string m_ssetup_name;
  218. map<std::string, SceneSetup*> m_ssetups;
  219. //File data
  220. std::string m_file_name;
  221. FileUpdateStatus* m_file_status;
  222. //Object data
  223. array<ViewerObject*> m_objs;
  224. //Entities listing
  225. array<Entity*> m_entities;
  226. //Input
  227. Controller* m_controller = nullptr;
  228. //Thread stuff
  229. FileUpdateTester* m_file_check = nullptr;
  230. DefaultThreadManager* m_file_loader = nullptr;
  231. //OLD ---------------------------------------------------------------------
  232. SceneSetup *m_ssetup = nullptr;
  233. array<LightData> m_light_datas;
  234. short m_input_usage;
  235. mat4 m_mat;
  236. mat4 m_mat_prev;
  237. //Camera Setup
  238. float m_reset_timer;
  239. float m_fov;
  240. float m_fov_mesh;
  241. float m_fov_speed;
  242. float m_zoom;
  243. float m_zoom_mesh;
  244. float m_zoom_speed;
  245. vec2 m_rot;
  246. vec2 m_rot_mesh;
  247. vec2 m_rot_speed;
  248. vec2 m_pos;
  249. vec2 m_pos_mesh;
  250. vec2 m_pos_speed;
  251. vec2 m_hist_scale;
  252. vec2 m_hist_scale_mesh;
  253. vec2 m_hist_scale_speed;
  254. vec2 m_screen_offset;
  255. //Mesh update timer
  256. float m_build_timer;
  257. float m_build_time;
  258. //Mesh infos
  259. vec2 m_render_max;
  260. int m_mesh_render;
  261. int m_mesh_id;
  262. float m_mesh_id1;
  263. array<EasyMesh*, EasyMesh*> m_meshes;
  264. array<EasyMesh*> m_gizmos;
  265. //File data
  266. array<std::string> m_cmdlist;
  267. float m_stream_update_time;
  268. float m_stream_update_timer;
  269. //misc datas
  270. Shader * m_texture_shader;
  271. TileSet * m_default_texture;
  272. Texture * m_texture;
  273. ShaderUniform m_texture_uni;
  274. };
  275. } /* namespace lol */