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.
 
 
 

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