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.

meshviewer.cpp 17 KiB

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