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.
 
 
 

390 lines
14 KiB

  1. //
  2. // Lol Engine - EasyMesh tutorial
  3. //
  4. // Copyright: (c) 2011-2013 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 defined HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <cfloat> /* for FLT_MAX */
  15. #include "core.h"
  16. using namespace std;
  17. using namespace lol;
  18. #define IPT_CAM_RESET "Cam_Center"
  19. #define IPT_CAM_FORWARD "Cam_Forward"
  20. #define IPT_CAM_BACKWARD "Cam_Backward"
  21. #define IPT_CAM_ZOOM_OUT "Cam_Zoom_In"
  22. #define IPT_CAM_ZOOM_IN "Cam_Zoom_Out"
  23. #define IPT_MESH_UPDATE "Mesh_Update"
  24. #define IPT_MESH_RESET "Mesh_Reset"
  25. #define IPT_MESH_LEFT "Mesh_Left"
  26. #define IPT_MESH_RIGHT "Mesh_Right"
  27. #define IPT_MESH_UP "Mesh_Up"
  28. #define IPT_MESH_DOWN "Mesh_Down"
  29. #define IPT_MESH_SCALE_UP "Mesh_Scale_Up"
  30. #define IPT_MESH_SCALE_DOWN "Mesh_Scale_Down"
  31. #define IPT_MESH_OFFSET_UP "Mesh_Offset_Up"
  32. #define IPT_MESH_OFFSET_DOWN "Mesh_Offset_Down"
  33. #define IPT_MESH_ROT_LEFT "Mesh_Rot_Left"
  34. #define IPT_MESH_ROT_RIGHT "Mesh_Rot_Right"
  35. #define IPT_MESH_ROT_UP "Mesh_Rot_Up"
  36. #define IPT_MESH_ROT_DOWN "Mesh_Rot_Down"
  37. class MeshViewer : public WorldEntity
  38. {
  39. public:
  40. void SetFov(float new_fov=60.0f)
  41. {
  42. //FOV compensation doesn't work
  43. ivec2 video_size = Video::GetSize();
  44. float near = (float)-video_size.x - video_size.y;
  45. float far = (float)video_size.x + video_size.y;
  46. float t1 = tanf(new_fov / 2);
  47. float dist = (float)video_size.x / (2.0f * t1);
  48. //m_fov_compensation = mat4::translate(-0.5f * video_size.x, -0.5f * video_size.y, -dist);
  49. m_fov_compensation = mat4::translate(vec3(.0f));
  50. if (new_fov > 0.1f)
  51. m_camera->SetPerspective(new_fov, (float)video_size.x, (float)video_size.y, .1f, 1000.f);
  52. else
  53. m_camera->SetOrtho((float)video_size.x, (float)video_size.y, .1f, 1000.f);
  54. }
  55. MeshViewer(char const *file_name = "MeshViewerBuffer.txt")
  56. : m_file_name(file_name)
  57. {
  58. //Input setup
  59. Input::LinkActionToKey(IPT_CAM_RESET, Key::Return);
  60. Input::LinkActionToKey(IPT_CAM_FORWARD, Key::PageUp);
  61. Input::LinkActionToKey(IPT_CAM_BACKWARD, Key::PageDown);
  62. Input::LinkActionToKey(IPT_CAM_ZOOM_IN, Key::Home);
  63. Input::LinkActionToKey(IPT_CAM_ZOOM_OUT, Key::End);
  64. Input::LinkActionToKey(IPT_MESH_LEFT, Key::Left);
  65. Input::LinkActionToKey(IPT_MESH_RIGHT, Key::Right);
  66. Input::LinkActionToKey(IPT_MESH_UP, Key::Up);
  67. Input::LinkActionToKey(IPT_MESH_DOWN, Key::Down);
  68. Input::LinkActionToKey(IPT_MESH_UPDATE, Key::Space);
  69. Input::LinkActionToKey(IPT_MESH_RESET, Key::KP0);
  70. Input::LinkActionToKey(IPT_MESH_OFFSET_DOWN, Key::KP1);
  71. Input::LinkActionToKey(IPT_MESH_OFFSET_UP, Key::KP3);
  72. Input::LinkActionToKey(IPT_MESH_SCALE_DOWN, Key::KP7);
  73. Input::LinkActionToKey(IPT_MESH_SCALE_UP, Key::KP9);
  74. Input::LinkActionToKey(IPT_MESH_ROT_LEFT, Key::KP4);
  75. Input::LinkActionToKey(IPT_MESH_ROT_RIGHT, Key::KP6);
  76. Input::LinkActionToKey(IPT_MESH_ROT_UP, Key::KP8);
  77. Input::LinkActionToKey(IPT_MESH_ROT_DOWN, Key::KP5);
  78. m_angle = 0;
  79. //Camera Setup
  80. m_fov_zoom_damp = .0f;
  81. m_fov_damp = 60.0f;
  82. m_fov = 60.0f;
  83. m_camera = new Camera(vec3(0.f, 600.f, 0.f),
  84. vec3(0.f, 0.f, 0.f),
  85. vec3(0, 1, 0));
  86. SetFov(m_fov_damp);
  87. m_camera->SetTarget(vec3(0.f, 0.f, 0.f));
  88. m_camera->SetPosition(vec3(0.f, 0.f, 10.f));
  89. m_camera->ForceSceneUpdate();
  90. Ticker::Ref(m_camera);
  91. //Lights setup
  92. m_lights << new Light();
  93. m_lights.Last()->SetPosition(vec4(4.f, -1.f, -4.f, 0.f));
  94. m_lights.Last()->SetColor(vec4(.0f, .2f, .5f, 1.f));
  95. Ticker::Ref(m_lights.Last());
  96. m_lights << new Light();
  97. m_lights.Last()->SetPosition(vec4(8.f, 2.f, 6.f, 1.f));
  98. m_lights.Last()->SetColor(vec4(.5f, .3f, .0f, 1.f));
  99. Ticker::Ref(m_lights.Last());
  100. //Speed damp
  101. m_mesh_rotate_damp = vec2(.0f);
  102. m_mesh_screen_move_damp = vec2(.0f);
  103. m_mesh_move_damp = vec2(.0f);
  104. //Actual values
  105. SetDefaultMeshTransform();
  106. //Actual values damp
  107. m_mesh_rotation_damp = vec2(.0f);
  108. m_mesh_screen_offset_damp = vec2(.0f);
  109. m_mesh_offset_damp = vec2(.0f);
  110. m_mat = mat4::rotate(m_mesh_rotation.x, vec3(1, 0, 0)) *
  111. mat4::rotate(m_angle, vec3(0, 1, 0)) *
  112. mat4::rotate(m_mesh_rotation.y, vec3(0, 1, 0));
  113. m_stream_update_time = 2.0f;
  114. m_stream_update_timer = 1.0f;
  115. }
  116. ~MeshViewer()
  117. {
  118. Ticker::Unref(m_camera);
  119. for (int i = 0; i < m_lights.Count(); ++i)
  120. Ticker::Unref(m_lights[i]);
  121. }
  122. void SetDefaultMeshTransform()
  123. {
  124. m_mesh_rotation = vec2(25.0f, .0f);
  125. m_mesh_screen_offset = vec2(.54f, .0f);
  126. m_mesh_offset = vec2(-.64f, .07f);
  127. }
  128. virtual void TickGame(float seconds)
  129. {
  130. WorldEntity::TickGame(seconds);
  131. //TODO : This should probably be "standard LoL behaviour"
  132. {
  133. //Shutdown logic
  134. if (Input::WasReleased(Key::Escape))
  135. Ticker::Shutdown();
  136. }
  137. //Update Mesh BBox - Get the Min/Max needed
  138. vec3 min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
  139. int mesh_id = m_meshes.Count() - 1;
  140. for (; mesh_id >= 0; mesh_id--)
  141. if (m_meshes[mesh_id].m2)
  142. break;
  143. if (m_meshes.Count() && mesh_id >= 0)
  144. {
  145. for (int i = 0; i < m_meshes[mesh_id].m1.GetVertexCount(); i++)
  146. {
  147. vec3 vpos = m_meshes[mesh_id].m1.GetVertexLocation(i);
  148. min_max[0] = min(vpos.xyz, min_max[0]);
  149. min_max[1] = max(vpos.xyz, min_max[1]);
  150. }
  151. }
  152. else
  153. {
  154. min_max[0] = vec3(.0f);
  155. min_max[1] = vec3(.0f);
  156. }
  157. //[0] : center, [1] : size.
  158. vec3 BBox[2] = { vec3(.0f), vec3(.0f) };
  159. BBox[1] = min_max[1] - min_max[0];
  160. BBox[0] = min_max[0] + BBox[1] * .5f;
  161. vec3 BBox_mod = BBox[1];
  162. //--
  163. //Camera movement handling
  164. //--
  165. vec3 cam_move = BBox_mod * seconds *
  166. vec3(.0f, .0f, (float)(Input::GetStatus(IPT_CAM_BACKWARD) - Input::GetStatus(IPT_CAM_FORWARD)));
  167. if (Input::WasReleased(IPT_CAM_RESET))
  168. SetFov();
  169. float fov_zoom = (float)(Input::GetStatus(IPT_CAM_ZOOM_OUT) - Input::GetStatus(IPT_CAM_ZOOM_IN));
  170. m_fov_zoom_damp = damp(m_fov_zoom_damp, fov_zoom, (fov_zoom == .0f)?(.15f):(0.5f), seconds);
  171. m_fov = max(.0f, m_fov + seconds * 10.0f * m_fov_zoom_damp);
  172. m_fov_damp = damp(m_fov_damp, m_fov, .2f, seconds);
  173. SetFov(m_fov_damp);
  174. m_camera->SetPosition(m_camera->GetPosition() + cam_move);
  175. m_camera->SetTarget(m_camera->GetPosition() + vec3(0, 0, -5.0f));
  176. //--
  177. //Mesh movement handling
  178. //--
  179. if (Input::WasReleased(IPT_MESH_RESET))
  180. SetDefaultMeshTransform();
  181. vec2 new_move = vec2(.0f);
  182. new_move = vec2((float)(Input::GetStatus(IPT_MESH_RIGHT) - Input::GetStatus(IPT_MESH_LEFT)),
  183. (float)(Input::GetStatus(IPT_MESH_UP) - Input::GetStatus(IPT_MESH_DOWN)));
  184. m_mesh_screen_move_damp = vec2(damp(m_mesh_screen_move_damp.x, new_move.x, (new_move.x == .0f)?(.15f):(0.5f), seconds),
  185. damp(m_mesh_screen_move_damp.y, new_move.y, (new_move.y == .0f)?(.15f):(0.5f), seconds));
  186. new_move = vec2((float)(Input::GetStatus(IPT_MESH_OFFSET_UP) - Input::GetStatus(IPT_MESH_OFFSET_DOWN)),
  187. (float)(Input::GetStatus(IPT_MESH_SCALE_UP) - Input::GetStatus(IPT_MESH_SCALE_DOWN)));
  188. m_mesh_move_damp = vec2(damp(m_mesh_move_damp.x, new_move.x, (new_move.x == .0f)?(.15f):(0.5f), seconds),
  189. damp(m_mesh_move_damp.y, new_move.y, (new_move.y == .0f)?(.15f):(0.5f), seconds));
  190. new_move = vec2((float)(Input::GetStatus(IPT_MESH_ROT_UP) - Input::GetStatus(IPT_MESH_ROT_DOWN)),
  191. (float)(Input::GetStatus(IPT_MESH_ROT_RIGHT) - Input::GetStatus(IPT_MESH_ROT_LEFT)));
  192. m_mesh_rotate_damp = vec2(damp(m_mesh_rotate_damp.x, new_move.x, (new_move.x == .0f)?(.15f):(0.5f), seconds),
  193. damp(m_mesh_rotate_damp.y, new_move.y, (new_move.y == .0f)?(.15f):(0.5f), seconds));
  194. vec2 mesh_screen_move = seconds * 0.6f * m_mesh_screen_move_damp;
  195. vec2 mesh_offset_move = seconds * vec2(.6f, .2f) * m_mesh_move_damp;
  196. vec2 mesh_rotate = seconds * vec2(40.0f, 60.0f) * m_mesh_rotate_damp;
  197. //Add movement
  198. m_mesh_screen_offset += mesh_screen_move;
  199. m_mesh_offset += mesh_offset_move;
  200. m_mesh_rotation += mesh_rotate;
  201. //Compute damp
  202. m_mesh_rotation_damp = damp(m_mesh_rotation_damp, m_mesh_rotation, .2f, seconds);
  203. m_mesh_screen_offset_damp = damp(m_mesh_screen_offset_damp, m_mesh_screen_offset, .2f, seconds);
  204. m_mesh_offset_damp = damp(m_mesh_offset_damp, m_mesh_offset, .2f, seconds);
  205. //Clamp necessary
  206. m_mesh_rotation.x = clamp(m_mesh_rotation.x, -90.0f, 90.0f);
  207. m_mesh_offset.y = max(.0f, m_mesh_offset.y);
  208. //m_angle += seconds * 70.0f;
  209. m_mat = mat4::rotate(m_mesh_rotation.x, vec3(1, 0, 0)) *
  210. mat4::rotate(m_angle, vec3(0, 1, 0)) *
  211. mat4::rotate(m_mesh_rotation.y, vec3(0, 1, 0));
  212. //--
  213. //File management
  214. //--
  215. if (Input::WasReleased(IPT_MESH_UPDATE))
  216. m_stream_update_time = m_stream_update_timer + 1.0f;
  217. m_stream_update_time += seconds;
  218. if (m_stream_update_time > m_stream_update_timer)
  219. {
  220. m_stream_update_time = 0.f;
  221. File f;
  222. f.Open(m_file_name.C(), FileAccess::Read);
  223. String cmd = f.ReadString();
  224. f.Close();
  225. if (cmd.Count()
  226. && (!m_cmdlist.Count() || cmd != m_cmdlist.Last()))
  227. {
  228. m_cmdlist << cmd;
  229. //Create a new mesh
  230. m_meshes.Push(EasyMesh(), false, .0f, vec3(.0f));
  231. m_meshes.Last().m1.Compile(cmd.C());
  232. }
  233. }
  234. }
  235. virtual void TickDraw(float seconds)
  236. {
  237. WorldEntity::TickDraw(seconds);
  238. //TODO : This should probably be "standard LoL behaviour"
  239. {
  240. if (Input::WasReleased(Key::F1))
  241. Video::SetDebugRenderMode(DebugRenderMode::Default);
  242. if (Input::WasReleased(Key::F2))
  243. Video::SetDebugRenderMode(DebugRenderMode::Wireframe);
  244. if (Input::WasReleased(Key::F3))
  245. Video::SetDebugRenderMode(DebugRenderMode::Lighting);
  246. if (Input::WasReleased(Key::F4))
  247. Video::SetDebugRenderMode(DebugRenderMode::Normal);
  248. if (Input::WasReleased(Key::F5))
  249. Video::SetDebugRenderMode(DebugRenderMode::UV);
  250. }
  251. for (int i = 0; i < m_meshes.Count(); i++)
  252. {
  253. if (!m_meshes[i].m2)
  254. {
  255. m_meshes[i].m1.MeshConvert();
  256. m_meshes[i].m2 = true;
  257. }
  258. }
  259. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  260. mat4 default_proj = Scene::GetDefault()->GetProjMatrix();
  261. for (int i = 0; i < m_meshes.Count(); i++)
  262. {
  263. float new_scale = max(.0f, 1.0f - (m_mesh_offset_damp.y * (float)(m_meshes.Count() - (i + 1))));
  264. m_meshes[i].m3 = damp(m_meshes[i].m3, new_scale, .35f, seconds);
  265. if (m_meshes[i].m3 > .0f)
  266. {
  267. vec3 new_mesh_offset = vec3(.0f);
  268. for (int j = m_meshes.Count() - 1; j > i; j--)
  269. {
  270. float ofs_scale = max(.0f, 1.0f - (m_mesh_offset_damp.y * (float)(m_meshes.Count() - (j + 0))));
  271. new_mesh_offset = new_mesh_offset + vec3(m_meshes[j].m3 * ofs_scale * ofs_scale * m_mesh_offset_damp.x, .0f, .0f);
  272. }
  273. m_meshes[i].m4 = damp(m_meshes[i].m4, new_mesh_offset, .35f, seconds);
  274. Scene::GetDefault()->SetProjMatrix(mat4::translate(m_meshes[i].m4) *
  275. mat4::translate(vec3(m_mesh_screen_offset_damp, .0f)) *
  276. mat4::scale(vec3(vec2(m_meshes[i].m3), 1.0f)) *
  277. default_proj *
  278. m_fov_compensation);
  279. m_meshes[i].m1.Render(m_mat);
  280. Video::Clear(ClearMask::Depth);
  281. }
  282. }
  283. Scene::GetDefault()->SetProjMatrix(default_proj);
  284. }
  285. private:
  286. float m_angle;
  287. mat4 m_mat;
  288. //Mesh infos
  289. //Move damping
  290. vec2 m_mesh_rotate_damp;
  291. vec2 m_mesh_screen_move_damp;
  292. vec2 m_mesh_move_damp;
  293. //Move transform damping
  294. vec2 m_mesh_rotation_damp;
  295. vec2 m_mesh_screen_offset_damp;
  296. vec2 m_mesh_offset_damp;
  297. vec2 m_mesh_rotation; //Meshes rotation
  298. vec2 m_mesh_screen_offset;//Meshes screen offset
  299. vec2 m_mesh_offset; //Mesh Offset after first mesh (x: offset, y: scale)
  300. //File data
  301. String m_file_name;
  302. Array<String> m_cmdlist;
  303. float m_stream_update_time;
  304. float m_stream_update_timer;
  305. //misc datas
  306. Array<EasyMesh, bool, float, vec3> m_meshes;
  307. Array<Light *> m_lights;
  308. Camera * m_camera;
  309. float m_fov;
  310. float m_fov_damp;
  311. float m_fov_zoom_damp;
  312. mat4 m_fov_compensation;
  313. };
  314. //The basic main :
  315. int main(int argc, char **argv)
  316. {
  317. System::Init(argc, argv);
  318. Application app("MeshViewer", ivec2(1600, 600), 60.0f);
  319. if (argc > 1)
  320. new MeshViewer(argv[1]);
  321. else
  322. new MeshViewer();
  323. app.Run();
  324. return EXIT_SUCCESS;
  325. }