Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

513 řádky
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. #define R_M 2.f
  20. #define WIDTH (770.f * R_M)
  21. #define HEIGHT (200.f * R_M)
  22. #define SCREEN_W (10.f / WIDTH)
  23. #define SCREEN_LIMIT 1.1f
  24. #define RATIO_HW (HEIGHT / WIDTH)
  25. #define RATIO_WH (WIDTH / HEIGHT)
  26. #define ROT_SPEED vec2(50.f)
  27. #define ROT_CLAMP 89.f
  28. #define POS_SPEED vec2(1.2f)
  29. #define POS_CLAMP 1.f
  30. #define FOV_SPEED 20.f
  31. #define FOV_CLAMP 120.f
  32. #define ZOM_SPEED 3.f
  33. #define ZOM_CLAMP 20.f
  34. #define WITH_TEXTURE 0
  35. LOLFX_RESOURCE_DECLARE(shinyfur);
  36. LOLFX_RESOURCE_DECLARE(shinymvtexture);
  37. enum
  38. {
  39. KEY_CAM_RESET,
  40. KEY_CAM_POS,
  41. KEY_CAM_FOV,
  42. KEY_CAM_UP,
  43. KEY_CAM_DOWN,
  44. KEY_CAM_LEFT,
  45. KEY_CAM_RIGHT,
  46. KEY_MESH_NEXT,
  47. KEY_MESH_PREV,
  48. KEY_F1,
  49. KEY_F2,
  50. KEY_F3,
  51. KEY_F4,
  52. KEY_F5,
  53. KEY_ESC,
  54. KEY_MAX,
  55. };
  56. enum MessageType
  57. {
  58. MSG_IN,
  59. MSG_OUT,
  60. MSG_MAX
  61. };
  62. class MeshViewer : public WorldEntity
  63. {
  64. public:
  65. MeshViewer(char const *file_name = "data/mesh-buffer.txt")
  66. : m_file_name(file_name)
  67. {
  68. #if !__native_client__
  69. /* Register an input controller for the keyboard */
  70. m_controller = new Controller("Default", KEY_MAX, 0);
  71. //Camera keyboard rotation
  72. m_controller->GetKey(KEY_CAM_UP ).Bind("Keyboard", "Up");
  73. m_controller->GetKey(KEY_CAM_DOWN ).Bind("Keyboard", "Down");
  74. m_controller->GetKey(KEY_CAM_LEFT ).Bind("Keyboard", "Left");
  75. m_controller->GetKey(KEY_CAM_RIGHT).Bind("Keyboard", "Right");
  76. //Camera keyboard position switch
  77. m_controller->GetKey(KEY_CAM_POS ).Bind("Keyboard", "LeftShift");
  78. m_controller->GetKey(KEY_CAM_FOV ).Bind("Keyboard", "LeftCtrl");
  79. //Camera unzoom switch
  80. m_controller->GetKey(KEY_CAM_RESET).Bind("Keyboard", "Space");
  81. //Mesh change
  82. m_controller->GetKey(KEY_MESH_NEXT).Bind("Keyboard", "PageUp");
  83. m_controller->GetKey(KEY_MESH_PREV).Bind("Keyboard", "PageDown");
  84. //Base setup
  85. m_controller->GetKey(KEY_F1).Bind("Keyboard", "F1");
  86. m_controller->GetKey(KEY_F2).Bind("Keyboard", "F2");
  87. m_controller->GetKey(KEY_F3).Bind("Keyboard", "F3");
  88. m_controller->GetKey(KEY_F4).Bind("Keyboard", "F4");
  89. m_controller->GetKey(KEY_F5).Bind("Keyboard", "F5");
  90. m_controller->GetKey(KEY_ESC).Bind("Keyboard", "Escape");
  91. #endif //!__native_client__
  92. // Message Service
  93. MessageService::Setup(MSG_MAX);
  94. // Mesh Setup
  95. m_mesh_id = 0;
  96. m_mesh_id1 = 0.f;
  97. m_default_texture = NULL;
  98. //Camera Setup
  99. m_fov = -100.f;
  100. m_fov_mesh = 0.f;
  101. m_fov_speed = 0.f;
  102. m_zoom = -100.f;
  103. m_zoom_mesh = 0.f;
  104. m_zoom_speed = 0.f;
  105. m_rot = vec2(0.f);
  106. m_rot_mesh = vec2(0.f);
  107. m_rot_speed = vec2(0.f);
  108. m_pos = vec2(0.f);
  109. m_pos_mesh = vec2(0.f);
  110. m_pos_speed = vec2(0.f);
  111. m_screen_offset = vec2(0.f);
  112. m_camera = new Camera();
  113. m_camera->SetView(vec3(0.f, 0.f, 10.f), vec3(0.f, 0.f, 0.f), vec3(0.f, 1.f, 0.f));
  114. m_camera->SetProjection(0.f, .0001f, 2000.f, WIDTH * SCREEN_W, RATIO_HW);
  115. //m_camera->SetScreenScale(vec2(10.f));
  116. m_camera->UseShift(true);
  117. g_scene->PushCamera(m_camera);
  118. //Lights setup
  119. m_lights << new Light();
  120. m_lights.Last()->SetPosition(vec4(4.f, -1.f, -4.f, 1.f));
  121. m_lights.Last()->SetColor(vec4(.0f, .2f, .5f, 1.f));
  122. Ticker::Ref(m_lights.Last());
  123. m_lights << new Light();
  124. m_lights.Last()->SetPosition(vec4(8.f, 2.f, 6.f, 1.f));
  125. m_lights.Last()->SetColor(vec4(1.f, 1.f, 1.f, 1.f));
  126. Ticker::Ref(m_lights.Last());
  127. //stream update
  128. m_stream_update_time = 2.0f;
  129. m_stream_update_timer = 1.0f;
  130. }
  131. ~MeshViewer()
  132. {
  133. g_scene->PopCamera(m_camera);
  134. for (int i = 0; i < m_lights.Count(); ++i)
  135. Ticker::Unref(m_lights[i]);
  136. MessageService::Destroy();
  137. }
  138. virtual void TickGame(float seconds)
  139. {
  140. WorldEntity::TickGame(seconds);
  141. //TODO : This should probably be "standard LoL behaviour"
  142. #if !__native_client__
  143. {
  144. //Shutdown logic
  145. if (m_controller->GetKey(KEY_ESC).IsReleased())
  146. Ticker::Shutdown();
  147. }
  148. #endif //!__native_client__
  149. //Mesh Change
  150. #if !__native_client__
  151. m_mesh_id = clamp(m_mesh_id + ((int)m_controller->GetKey(KEY_MESH_PREV).IsPressed() - (int)m_controller->GetKey(KEY_MESH_NEXT).IsPressed()), 0, m_meshes.Count() - 1);
  152. #endif //!__native_client__
  153. m_mesh_id1 = damp(m_mesh_id1, (float)m_mesh_id, .2f, seconds);
  154. //Camera update
  155. bool is_pos = false;
  156. bool is_fov = false;
  157. vec2 tmp = vec2(0.f);
  158. #if !__native_client__
  159. is_pos = m_controller->GetKey(KEY_CAM_POS).IsDown();
  160. is_fov = m_controller->GetKey(KEY_CAM_FOV).IsDown();
  161. vec2 tmp = vec2((float)m_controller->GetKey(KEY_CAM_UP ).IsDown() - (float)m_controller->GetKey(KEY_CAM_DOWN).IsDown(),
  162. ((float)m_controller->GetKey(KEY_CAM_RIGHT ).IsDown() - (float)m_controller->GetKey(KEY_CAM_LEFT).IsDown()));
  163. #endif //!__native_client__
  164. //Base data
  165. vec2 rot = (!is_pos && !is_fov)?(tmp):(vec2(.0f)); rot = vec2(rot.x, rot.y);
  166. vec2 pos = (is_pos && !is_fov)?(tmp):(vec2(.0f)); pos = -vec2(pos.y, pos.x);
  167. vec2 fov = (!is_pos && is_fov)?(tmp):(vec2(.0f)); fov = vec2(-fov.x, fov.y);
  168. //speed
  169. m_rot_speed = damp(m_rot_speed, rot * ROT_SPEED, .2f, seconds);
  170. float pos_factor = 1.f / (1.f + m_zoom_mesh * .5f);
  171. m_pos_speed = damp(m_pos_speed, pos * POS_SPEED * pos_factor, .2f, seconds);
  172. float fov_factor = 1.f + lol::pow((m_fov_mesh / FOV_CLAMP) * 1.f, 2.f);
  173. m_fov_speed = damp(m_fov_speed, fov.x * FOV_SPEED * fov_factor, .2f, seconds);
  174. float zom_factor = 1.f + lol::pow((m_zoom_mesh / ZOM_CLAMP) * 1.f, 2.f);
  175. m_zoom_speed = damp(m_zoom_speed, fov.y * ZOM_SPEED * zom_factor, .2f, seconds);
  176. m_rot += m_rot_speed * seconds;
  177. #if !__native_client__
  178. //Transform update
  179. if (!m_controller->GetKey(KEY_CAM_RESET).IsDown())
  180. {
  181. m_pos += m_pos_speed * seconds;
  182. m_fov += m_fov_speed * seconds;
  183. m_zoom += m_zoom_speed * seconds;
  184. }
  185. #endif //!__native_client__
  186. //clamp
  187. vec2 rot_mesh = vec2(SmoothClamp(m_rot.x, -ROT_CLAMP, ROT_CLAMP, ROT_CLAMP * .1f), m_rot.y);
  188. vec2 pos_mesh = vec2(SmoothClamp(m_pos.x, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f),
  189. SmoothClamp(m_pos.y, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f));
  190. float fov_mesh = SmoothClamp(m_fov, 0.f, FOV_CLAMP, FOV_CLAMP * .1f);
  191. float zoom_mesh = SmoothClamp(m_zoom, 0.f, ZOM_CLAMP, ZOM_CLAMP * .1f);
  192. #if !__native_client__
  193. if (m_controller->GetKey(KEY_CAM_RESET).IsDown())
  194. {
  195. pos_mesh = vec2(0.f);
  196. zoom_mesh = 0.f;
  197. }
  198. #endif //!__native_client__
  199. m_rot_mesh = vec2(damp(m_rot_mesh.x, rot_mesh.x, .2f, seconds), damp(m_rot_mesh.y, rot_mesh.y, .2f, seconds));
  200. m_pos_mesh = vec2(damp(m_pos_mesh.x, pos_mesh.x, .2f, seconds), damp(m_pos_mesh.y, pos_mesh.y, .2f, seconds));
  201. m_fov_mesh = damp(m_fov_mesh, fov_mesh, .2f, seconds);
  202. m_zoom_mesh = damp(m_zoom_mesh, zoom_mesh, .2f, seconds);
  203. //Mesh mat calculation
  204. m_mat = mat4(quat::fromeuler_xyz(vec3(m_rot_mesh, .0f)));
  205. //Target List Setup
  206. Array<vec3> target_list;
  207. if (m_meshes.Count() && m_mesh_id >= 0)
  208. for (int i = 0; i < m_meshes[m_mesh_id].m1.GetVertexCount(); i++)
  209. target_list << (m_mat * mat4::translate(m_meshes[m_mesh_id].m1.GetVertexLocation(i))).v3.xyz;
  210. //--
  211. //Update mesh screen location - Get the Min/Max needed
  212. //--
  213. vec2 cam_center(0.f);
  214. float cam_factor = .0f;
  215. vec3 local_min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
  216. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  217. mat4 world_cam = m_camera->GetView();
  218. mat4 cam_screen = m_camera->GetProjection();
  219. //target on-screen computation
  220. for (int i = 0; i < target_list.Count(); i++)
  221. {
  222. vec3 obj_loc = target_list[i];
  223. {
  224. //Debug::DrawBox(obj_loc - vec3(4.f), obj_loc + vec3(4.f), vec4(1.f, 0.f, 0.f, 1.f));
  225. mat4 target_mx = mat4::translate(obj_loc);
  226. vec3 vpos;
  227. //Get location in cam coordinates
  228. target_mx = world_cam * target_mx;
  229. vpos = target_mx.v3.xyz;
  230. local_min_max[0] = min(vpos.xyz, local_min_max[0]);
  231. local_min_max[1] = max(vpos.xyz, local_min_max[1]);
  232. //Get location in screen coordinates
  233. target_mx = cam_screen * target_mx;
  234. vpos = (target_mx.v3 / target_mx.v3.w).xyz;
  235. screen_min_max[0] = min(screen_min_max[0], vpos.xy * vec2(RATIO_WH, 1.f));
  236. screen_min_max[1] = max(screen_min_max[1], vpos.xy * vec2(RATIO_WH, 1.f));
  237. //Build Barycenter
  238. cam_center += vpos.xy;
  239. cam_factor += 1.f;
  240. }
  241. }
  242. float screen_ratio = max(max(lol::abs(local_min_max[0].x), lol::abs(local_min_max[0].y)),
  243. max(lol::abs(local_min_max[1].x), lol::abs(local_min_max[1].y)));
  244. float scale_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
  245. max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
  246. vec2 screen_offset = vec2(0.f, -(screen_min_max[1].y + screen_min_max[0].y) * .5f);
  247. m_screen_offset = damp(m_screen_offset, screen_offset, .9f, seconds);
  248. float z_pos = (inverse(world_cam) * mat4::translate(vec3(0.f, 0.f, max(local_min_max[0].z, local_min_max[1].z)))).v3.z;
  249. if (cam_factor > 0.f)
  250. {
  251. vec2 new_screen_scale = m_camera->GetScreenScale();
  252. m_camera->SetScreenScale(max(vec2(0.001f), new_screen_scale * ((1.0f + m_zoom_mesh) / (scale_ratio * SCREEN_LIMIT))));
  253. m_camera->m_position.z = damp(m_camera->m_position.z, z_pos + screen_ratio * 2.f, .1f, seconds);
  254. m_camera->SetFov(m_fov_mesh);
  255. m_camera->SetScreenInfos(damp(m_camera->GetScreenSize(), max(1.f, screen_ratio), 1.2f, seconds));
  256. }
  257. //--
  258. //Message Service
  259. //--
  260. String mesh("");
  261. int u = 4;
  262. while (u-- > 0 && MessageService::FetchFirst(MSG_IN, mesh))
  263. {
  264. int o = 1;
  265. while (o-- > 0)
  266. {
  267. if (m_mesh_id == m_meshes.Count() - 1)
  268. m_mesh_id++;
  269. //Create a new mesh
  270. m_meshes.Push(EasyMesh(), false);
  271. if (!m_meshes.Last().m1.Compile(mesh.C()))
  272. m_meshes.Pop();
  273. //else
  274. // m_meshes.Last().m1.ComputeTexCoord(0.2f, 2);
  275. }
  276. }
  277. #if __native_client__
  278. if (m_stream_update_time > .0f)
  279. {
  280. m_stream_update_time = -1.f;
  281. // MessageService::Send(MSG_IN, "[sc#f8f afcb 1 1 1 0]");
  282. // MessageService::Send(MSG_IN, "[sc#8ff afcb 1 1 1 0]");
  283. // MessageService::Send(MSG_IN, "[sc#ff8 afcb 1 1 1 0]");
  284. }
  285. #elif WIN32
  286. //--
  287. //File management
  288. //--
  289. m_stream_update_time += seconds;
  290. if (m_stream_update_time > m_stream_update_timer)
  291. {
  292. m_stream_update_time = 0.f;
  293. File f;
  294. f.Open(m_file_name.C(), FileAccess::Read);
  295. String cmd = f.ReadString();
  296. f.Close();
  297. for (int i = 0; i < cmd.Count() - 1; i++)
  298. {
  299. if (cmd[i] == '/' && cmd[i + 1] == '/')
  300. {
  301. int j = i;
  302. for (; j < cmd.Count(); j++)
  303. {
  304. if (cmd[j] == '\r' || cmd[j] == '\n')
  305. break;
  306. }
  307. String new_cmd = cmd.Sub(0, i);
  308. if (j < cmd.Count())
  309. new_cmd += cmd.Sub(j, cmd.Count() - j);
  310. cmd = new_cmd;
  311. i--;
  312. }
  313. }
  314. if (cmd.Count()
  315. && (!m_cmdlist.Count() || cmd != m_cmdlist.Last()))
  316. {
  317. m_cmdlist << cmd;
  318. MessageService::Send(MSG_IN, cmd);
  319. }
  320. }
  321. #endif //WINDOWS
  322. }
  323. virtual void TickDraw(float seconds)
  324. {
  325. WorldEntity::TickDraw(seconds);
  326. //TODO : This should probably be "standard LoL behaviour"
  327. #if !__native_client__
  328. {
  329. if (m_controller->GetKey(KEY_F1).IsReleased())
  330. Video::SetDebugRenderMode(DebugRenderMode::Default);
  331. if (m_controller->GetKey(KEY_F2).IsReleased())
  332. Video::SetDebugRenderMode(DebugRenderMode::Wireframe);
  333. if (m_controller->GetKey(KEY_F3).IsReleased())
  334. Video::SetDebugRenderMode(DebugRenderMode::Lighting);
  335. if (m_controller->GetKey(KEY_F4).IsReleased())
  336. Video::SetDebugRenderMode(DebugRenderMode::Normal);
  337. if (m_controller->GetKey(KEY_F5).IsReleased())
  338. Video::SetDebugRenderMode(DebugRenderMode::UV);
  339. }
  340. #endif //!__native_client__
  341. #if !__native_client__
  342. if (!m_default_texture)
  343. {
  344. m_texture_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinymvtexture));
  345. m_texture_uni = m_texture_shader->GetUniformLocation("u_Texture");
  346. m_default_texture = Tiler::Register("data/test-texture.png", ivec2(0), ivec2(0,1));
  347. }
  348. else if (m_texture && m_default_texture)
  349. m_texture_shader->SetUniform(m_texture_uni, m_default_texture->GetTexture(), 0);
  350. #endif //!__native_client__
  351. for (int i = 0; i < m_meshes.Count(); i++)
  352. {
  353. if (!m_meshes[i].m2)
  354. {
  355. #if WITH_TEXTURE
  356. m_meshes[i].m1.MeshConvert(new DefaultShaderData(((1 << VertexUsage::Position) | (1 << VertexUsage::Normal) |
  357. (1 << VertexUsage::Color) | (1 << VertexUsage::TexCoord)),
  358. m_texture_shader, true));
  359. #else
  360. m_meshes[i].m1.MeshConvert();
  361. #endif
  362. m_meshes[i].m2 = true;
  363. }
  364. }
  365. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  366. vec3 x = vec3(1.f,0.f,0.f);
  367. vec3 y = vec3(0.f,1.f,0.f);
  368. for (int i = 0; i < m_meshes.Count(); i++)
  369. {
  370. mat4 save_proj = m_camera->GetProjection();
  371. float j = -(float)(m_meshes.Count() - (i + 1)) + (-m_mesh_id1 + (float)(m_meshes.Count() - 1));
  372. if (m_meshes[i].m2)
  373. {
  374. mat4 new_proj =
  375. //Y object Offset
  376. mat4::translate(x * m_screen_offset.x + y * m_screen_offset.y) *
  377. //Mesh Pos Offset
  378. mat4::translate((x * m_pos_mesh.x * RATIO_HW + y * m_pos_mesh.y) * 2.f * (1.f + .5f * m_zoom_mesh / SCREEN_LIMIT)) *
  379. //Mesh count offset
  380. mat4::translate(x * RATIO_HW * 2.f * j) *
  381. //Align right meshes
  382. mat4::translate(x - x * RATIO_HW) *
  383. //Mesh count scale
  384. //mat4::scale(1.f - .2f * j * (1.f / (float)m_meshes.Count())) *
  385. //Camera projection
  386. save_proj;
  387. m_camera->SetProjection(new_proj);
  388. m_meshes[i].m1.Render(m_mat);
  389. g_renderer->Clear(ClearMask::Depth);
  390. }
  391. m_camera->SetProjection(save_proj);
  392. }
  393. }
  394. private:
  395. Array<Light *> m_lights;
  396. Controller *m_controller;
  397. mat4 m_mat;
  398. //Camera Setup
  399. Camera *m_camera;
  400. float m_fov;
  401. float m_fov_mesh;
  402. float m_fov_speed;
  403. float m_zoom;
  404. float m_zoom_mesh;
  405. float m_zoom_speed;
  406. vec2 m_rot;
  407. vec2 m_rot_mesh;
  408. vec2 m_rot_speed;
  409. vec2 m_pos;
  410. vec2 m_pos_mesh;
  411. vec2 m_pos_speed;
  412. vec2 m_screen_offset;
  413. //Mesh infos
  414. int m_mesh_id;
  415. float m_mesh_id1;
  416. Array<EasyMesh, bool> m_meshes;
  417. //File data
  418. String m_file_name;
  419. Array<String> m_cmdlist;
  420. float m_stream_update_time;
  421. float m_stream_update_timer;
  422. //misc datas
  423. Shader * m_texture_shader;
  424. TileSet * m_default_texture;
  425. Texture * m_texture;
  426. ShaderUniform m_texture_uni;
  427. Image * m_image;
  428. };
  429. //The basic main :
  430. int main(int argc, char **argv)
  431. {
  432. System::Init(argc, argv);
  433. Application app("MeshViewer", ivec2((int)WIDTH, (int)HEIGHT), 60.0f);
  434. if (argc > 1)
  435. new MeshViewer(argv[1]);
  436. else
  437. new MeshViewer();
  438. app.Run();
  439. return EXIT_SUCCESS;
  440. }