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.
 
 
 

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