Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

700 Zeilen
24 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. #include "scenesetup.h"
  17. using namespace std;
  18. using namespace lol;
  19. static int const TEXTURE_WIDTH = 256;
  20. #define R_M 1.f
  21. #define DEFAULT_WIDTH (770.f * R_M)
  22. #define DEFAULT_HEIGHT (200.f * R_M)
  23. #define WIDTH ((float)Video::GetSize().x)
  24. #define HEIGHT ((float)Video::GetSize().y)
  25. #define SCREEN_W (10.f / WIDTH)
  26. #define SCREEN_LIMIT 1.1f
  27. #define RATIO_HW (HEIGHT / WIDTH)
  28. #define RATIO_WH (WIDTH / HEIGHT)
  29. #define RESET_TIMER .2f
  30. #define ROT_SPEED vec2(50.f)
  31. #define ROT_CLAMP 89.f
  32. #define POS_SPEED vec2(1.2f)
  33. #define POS_CLAMP 1.f
  34. #define FOV_SPEED 20.f
  35. #define FOV_CLAMP 120.f
  36. #define ZOM_SPEED 3.f
  37. #define ZOM_CLAMP 20.f
  38. #define HST_SPEED .5f
  39. #define HST_CLAMP 1.f
  40. #define WITH_TEXTURE 0
  41. #define NO_NACL_EM (!__native_client__ && !EMSCRIPTEN)
  42. #define NACL_EM (__native_client__ || EMSCRIPTEN)
  43. #define HAS_KBOARD (m_input_usage & (1<<IPT_MV_KBOARD))
  44. #define HAS_MOUSE (m_input_usage & (1<<IPT_MV_MOUSE))
  45. LOLFX_RESOURCE_DECLARE(shinyfur);
  46. LOLFX_RESOURCE_DECLARE(shinymvtexture);
  47. enum
  48. {
  49. IPT_MV_KBOARD = 0,
  50. IPT_MV_MOUSE,
  51. INPUT_MAX
  52. };
  53. enum MVKeyboardList
  54. {
  55. KEY_CAM_RESET = 0,
  56. KEY_CAM_POS,
  57. KEY_CAM_FOV,
  58. KEY_CAM_UP,
  59. KEY_CAM_DOWN,
  60. KEY_CAM_LEFT,
  61. KEY_CAM_RIGHT,
  62. KEY_MESH_NEXT,
  63. KEY_MESH_PREV,
  64. KEY_F1,
  65. KEY_F2,
  66. KEY_F3,
  67. KEY_F4,
  68. KEY_F5,
  69. KEY_ESC,
  70. KEY_MAX
  71. };
  72. enum MVMouseKeyList
  73. {
  74. MSE_CAM_ROT = KEY_MAX,
  75. MSE_CAM_POS,
  76. MSE_CAM_FOV,
  77. MSE_MAX
  78. };
  79. enum MVMouseAxisList
  80. {
  81. MSEX_CAM_Y = 0,
  82. MSEX_CAM_X,
  83. MSEX_MAX
  84. };
  85. #define MAX_KEYS MSE_MAX
  86. #define MAX_AXIS MSEX_MAX
  87. enum MessageType
  88. {
  89. MSG_IN,
  90. MSG_OUT,
  91. MSG_MAX
  92. };
  93. class MeshViewer : public WorldEntity
  94. {
  95. public:
  96. MeshViewer(char const *file_name = "data/mesh-buffer.txt")
  97. : m_file_name(file_name)
  98. {
  99. m_init = false;
  100. m_first_tick = false;
  101. // Message Service
  102. MessageService::Setup();
  103. m_ssetup = nullptr;
  104. m_camera = nullptr;
  105. m_controller = nullptr;
  106. // Mesh Setup
  107. m_render_max = vec2(-.9f, 6.1f);
  108. m_mesh_id = 0;
  109. m_mesh_id1 = 0.f;
  110. m_default_texture = nullptr;
  111. m_texture_shader = nullptr;
  112. m_texture = nullptr;
  113. //Camera Setup
  114. m_reset_timer = -1.f;
  115. m_fov = -100.f;
  116. m_fov_mesh = 0.f;
  117. m_fov_speed = 0.f;
  118. m_zoom = -100.f;
  119. m_zoom_mesh = 0.f;
  120. m_zoom_speed = 0.f;
  121. m_rot = vec2(45.f);
  122. m_rot_mesh = vec2::zero;
  123. m_rot_speed = vec2::zero;
  124. m_pos = vec2::zero;
  125. m_pos_mesh = vec2::zero;
  126. m_pos_speed = vec2::zero;
  127. m_screen_offset = vec2::zero;
  128. m_hist_scale = vec2(.13f, .03f);
  129. m_hist_scale_mesh = vec2(.0f);
  130. m_hist_scale_speed = vec2(.0f);
  131. m_mat_prev = mat4(quat::fromeuler_xyz(vec3::zero));
  132. m_mat = mat4(quat::fromeuler_xyz(vec3(m_rot_mesh, .0f)));
  133. //stream update
  134. m_stream_update_time = 2.0f;
  135. m_stream_update_timer = 1.0f;
  136. }
  137. ~MeshViewer()
  138. {
  139. if (m_camera)
  140. g_scene->PopCamera(m_camera);
  141. if (m_ssetup)
  142. delete(m_ssetup);
  143. MessageService::Destroy();
  144. m_controller = nullptr;
  145. m_camera = nullptr;
  146. m_ssetup = nullptr;
  147. }
  148. #if NO_NACL_EM
  149. bool KeyReleased(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsReleased()); }
  150. bool KeyPressed(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsPressed()); }
  151. bool KeyDown(MVKeyboardList index) { return (HAS_KBOARD && m_controller->GetKey(index).IsDown()); }
  152. bool KeyReleased(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsReleased()); }
  153. bool KeyPressed(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsPressed()); }
  154. bool KeyDown(MVMouseKeyList index) { return (HAS_MOUSE && m_controller->GetKey(index).IsDown()); }
  155. float AxisValue(MVMouseAxisList index) { return (HAS_MOUSE)?(m_controller->GetAxis(index).GetValue()):(0.f); }
  156. #endif //NO_NACL_EM
  157. void Init()
  158. {
  159. m_init = true;
  160. m_input_usage = 0;
  161. #if NO_NACL_EM
  162. /* Register an input controller for the keyboard */
  163. m_controller = new Controller("Default", MAX_KEYS, MAX_AXIS);
  164. if (InputDevice::Get("Mouse"))
  165. {
  166. m_input_usage |= (1<<IPT_MV_MOUSE);
  167. m_controller->GetKey(MSE_CAM_ROT).Bind("Mouse", "Left");
  168. m_controller->GetKey(MSE_CAM_POS).Bind("Mouse", "Right");
  169. m_controller->GetKey(MSE_CAM_FOV).Bind("Mouse", "Middle");
  170. m_controller->GetAxis(MSEX_CAM_Y).Bind("Mouse", "Y");
  171. m_controller->GetAxis(MSEX_CAM_X).Bind("Mouse", "X");
  172. }
  173. if (InputDevice::Get("Keyboard"))
  174. {
  175. m_input_usage |= (1<<IPT_MV_KBOARD);
  176. //Camera keyboard rotation
  177. m_controller->GetKey(KEY_CAM_UP ).Bind("Keyboard", "Up");
  178. m_controller->GetKey(KEY_CAM_DOWN ).Bind("Keyboard", "Down");
  179. m_controller->GetKey(KEY_CAM_LEFT ).Bind("Keyboard", "Left");
  180. m_controller->GetKey(KEY_CAM_RIGHT).Bind("Keyboard", "Right");
  181. //Camera keyboard position switch
  182. m_controller->GetKey(KEY_CAM_POS ).Bind("Keyboard", "LeftShift");
  183. m_controller->GetKey(KEY_CAM_FOV ).Bind("Keyboard", "LeftCtrl");
  184. //Camera unzoom switch
  185. m_controller->GetKey(KEY_CAM_RESET).Bind("Keyboard", "Space");
  186. //Mesh change
  187. m_controller->GetKey(KEY_MESH_NEXT).Bind("Keyboard", "PageUp");
  188. m_controller->GetKey(KEY_MESH_PREV).Bind("Keyboard", "PageDown");
  189. //Base setup
  190. m_controller->GetKey(KEY_F1).Bind("Keyboard", "F1");
  191. m_controller->GetKey(KEY_F2).Bind("Keyboard", "F2");
  192. m_controller->GetKey(KEY_F3).Bind("Keyboard", "F3");
  193. m_controller->GetKey(KEY_F4).Bind("Keyboard", "F4");
  194. m_controller->GetKey(KEY_F5).Bind("Keyboard", "F5");
  195. m_controller->GetKey(KEY_ESC).Bind("Keyboard", "Escape");
  196. }
  197. #endif //NO_NACL_EM
  198. m_camera = new Camera();
  199. m_camera->SetView(vec3(0.f, 0.f, 10.f), vec3(0.f, 0.f, 0.f), vec3(0.f, 1.f, 0.f));
  200. m_camera->SetProjection(0.f, .0001f, 2000.f, WIDTH * SCREEN_W, RATIO_HW);
  201. m_camera->UseShift(true);
  202. g_scene->PushCamera(m_camera);
  203. //Lights setup
  204. m_ssetup = new SceneSetup();
  205. #define MV_TEST 0
  206. #if MV_TEST
  207. m_ssetup->m_lights << new Light();
  208. m_ssetup->m_lights.Last()->SetPosition(vec4(4.f, -1.f, -4.f, 0.f));
  209. m_ssetup->m_lights.Last()->SetColor(vec4(.0f, .2f, .5f, 1.f));
  210. Ticker::Ref(m_ssetup->m_lights.Last());
  211. m_ssetup->m_lights << new Light();
  212. m_ssetup->m_lights.Last()->SetPosition(vec4(8.f, 2.f, 6.f, 0.f));
  213. m_ssetup->m_lights.Last()->SetColor(vec4(1.f));
  214. Ticker::Ref(m_ssetup->m_lights.Last());
  215. EasyMesh* em = new EasyMesh();
  216. if (em->Compile("sc#fff ab 1"))
  217. {
  218. if (m_mesh_id == m_meshes.Count() - 1)
  219. m_mesh_id++;
  220. m_meshes.Push(em);
  221. }
  222. #else
  223. m_ssetup->Compile(" addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1)"
  224. " addlight 0.0 position (8 2 6) color #ffff"
  225. " custom setmesh \"sc#fff ab 1\"");
  226. m_ssetup->Startup();
  227. #endif //MV_TEST
  228. }
  229. virtual void TickGame(float seconds)
  230. {
  231. WorldEntity::TickGame(seconds);
  232. if (!m_init && g_scene)
  233. {
  234. Init();
  235. return;
  236. }
  237. if (!m_init)
  238. return;
  239. m_first_tick = true;
  240. //TODO : This should probably be "standard LoL behaviour"
  241. #if NO_NACL_EM
  242. {
  243. //Shutdown logic
  244. if (KeyReleased(KEY_ESC))
  245. Ticker::Shutdown();
  246. }
  247. #endif //NO_NACL_EM
  248. //Mesh Change
  249. #if NO_NACL_EM
  250. m_mesh_id = clamp(m_mesh_id + ((int)KeyPressed(KEY_MESH_PREV) - (int)KeyPressed(KEY_MESH_NEXT)), 0, m_meshes.Count() - 1);
  251. #endif //NO_NACL_EM
  252. m_mesh_id1 = damp(m_mesh_id1, (float)m_mesh_id, .2f, seconds);
  253. //Update light position
  254. for (int i = 0; i < m_ssetup->m_lights.Count(); ++i)
  255. {
  256. vec4 v = m_ssetup->m_lights[i]->GetPosition();
  257. m_ssetup->m_lights[i]->SetPosition(vec4((m_mat * inverse(m_mat_prev) * vec4(v.xyz, 1.f)).xyz, v.w));
  258. }
  259. //Camera update
  260. bool is_pos = false;
  261. bool is_fov = false;
  262. bool is_hsc = false;
  263. vec2 tmp = vec2::zero;
  264. #if NO_NACL_EM
  265. is_pos = KeyDown(KEY_CAM_POS) || KeyDown(MSE_CAM_POS);
  266. is_fov = KeyDown(KEY_CAM_FOV) || KeyDown(MSE_CAM_FOV);
  267. if (KeyDown(MSE_CAM_ROT) || KeyDown(MSE_CAM_POS) || KeyDown(MSE_CAM_FOV))
  268. {
  269. tmp += vec2(AxisValue(MSEX_CAM_Y), AxisValue(MSEX_CAM_X));
  270. if (KeyDown(MSE_CAM_ROT))
  271. tmp *= 6.f;
  272. if (KeyDown(MSE_CAM_POS))
  273. tmp *= vec2(1.f, -1.f) * 3.f;
  274. if (KeyDown(MSE_CAM_FOV))
  275. tmp = vec2(tmp.y * 4.f, tmp.x * 6.f);
  276. }
  277. tmp += vec2((float)KeyDown(KEY_CAM_UP ) - (float)KeyDown(KEY_CAM_DOWN),
  278. (float)KeyDown(KEY_CAM_RIGHT) - (float)KeyDown(KEY_CAM_LEFT));
  279. #endif //NO_NACL_EM
  280. //Base data
  281. vec2 rot = (!is_pos && !is_fov)?(tmp):(vec2(.0f)); rot = vec2(rot.x, rot.y);
  282. vec2 pos = ( is_pos && !is_fov)?(tmp):(vec2(.0f)); pos = -vec2(pos.y, pos.x);
  283. vec2 fov = (!is_pos && is_fov )?(tmp):(vec2(.0f)); fov = vec2(-fov.x, fov.y);
  284. vec2 hsc = (is_hsc)?(vec2(0.f)):(vec2(0.f));
  285. //speed
  286. m_rot_speed = damp(m_rot_speed, rot * ROT_SPEED, .2f, seconds);
  287. float pos_factor = 1.f / (1.f + m_zoom_mesh * .5f);
  288. m_pos_speed = damp(m_pos_speed, pos * POS_SPEED * pos_factor, .2f, seconds);
  289. float fov_factor = 1.f + lol::pow((m_fov_mesh / FOV_CLAMP) * 1.f, 2.f);
  290. m_fov_speed = damp(m_fov_speed, fov.x * FOV_SPEED * fov_factor, .2f, seconds);
  291. float zom_factor = 1.f + lol::pow((m_zoom_mesh / ZOM_CLAMP) * 1.f, 2.f);
  292. m_zoom_speed = damp(m_zoom_speed, fov.y * ZOM_SPEED * zom_factor, .2f, seconds);
  293. m_hist_scale_speed = damp(m_hist_scale_speed, hsc * HST_SPEED, .2f, seconds);
  294. m_rot += m_rot_speed * seconds;
  295. #if NO_NACL_EM
  296. if (m_reset_timer >= 0.f)
  297. m_reset_timer -= seconds;
  298. if (KeyPressed(KEY_CAM_RESET))
  299. {
  300. if (m_reset_timer >= 0.f)
  301. {
  302. m_pos = vec2(0.f);
  303. m_zoom = -100.f;
  304. }
  305. else
  306. m_reset_timer = RESET_TIMER;
  307. }
  308. //Transform update
  309. if (!KeyDown(KEY_CAM_RESET))
  310. {
  311. m_pos += m_pos_speed * seconds;
  312. m_fov += m_fov_speed * seconds;
  313. m_zoom += m_zoom_speed * seconds;
  314. m_hist_scale += m_hist_scale_speed * seconds;
  315. }
  316. #endif //NO_NACL_EM
  317. //clamp
  318. vec2 rot_mesh = vec2(SmoothClamp(m_rot.x, -ROT_CLAMP, ROT_CLAMP, ROT_CLAMP * .1f), m_rot.y);
  319. vec2 pos_mesh = vec2(SmoothClamp(m_pos.x, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f),
  320. SmoothClamp(m_pos.y, -POS_CLAMP, POS_CLAMP, POS_CLAMP * .1f));
  321. float fov_mesh = SmoothClamp(m_fov, 0.f, FOV_CLAMP, FOV_CLAMP * .1f);
  322. float zoom_mesh = SmoothClamp(m_zoom, 0.f, ZOM_CLAMP, ZOM_CLAMP * .1f);
  323. vec2 hist_scale_mesh = vec2(SmoothClamp(m_hist_scale.x, 0.f, HST_CLAMP, HST_CLAMP * .1f),
  324. SmoothClamp(m_hist_scale.y, 0.f, HST_CLAMP, HST_CLAMP * .1f));
  325. #if NO_NACL_EM
  326. if (KeyDown(KEY_CAM_RESET) && m_reset_timer < 0.f)
  327. {
  328. pos_mesh = vec2::zero;
  329. zoom_mesh = 0.f;
  330. }
  331. #endif //NO_NACL_EM
  332. m_rot_mesh = vec2(damp(m_rot_mesh.x, rot_mesh.x, .2f, seconds), damp(m_rot_mesh.y, rot_mesh.y, .2f, seconds));
  333. m_pos_mesh = vec2(damp(m_pos_mesh.x, pos_mesh.x, .2f, seconds), damp(m_pos_mesh.y, pos_mesh.y, .2f, seconds));
  334. m_fov_mesh = damp(m_fov_mesh, fov_mesh, .2f, seconds);
  335. m_zoom_mesh = damp(m_zoom_mesh, zoom_mesh, .2f, seconds);
  336. m_hist_scale_mesh = damp(m_hist_scale_mesh, hist_scale_mesh, .2f, seconds);
  337. //Mesh mat calculation
  338. m_mat_prev = m_mat;
  339. m_mat = mat4(quat::fromeuler_xyz(vec3(m_rot_mesh, .0f)));
  340. //Target List Setup
  341. Array<vec3> target_list;
  342. if (m_meshes.Count() && m_mesh_id >= 0)
  343. for (int i = 0; i < m_meshes[m_mesh_id]->GetVertexCount(); i++)
  344. target_list << (m_mat * mat4::translate(m_meshes[m_mesh_id]->GetVertexLocation(i))).v3.xyz;
  345. //--
  346. //Update mesh screen location - Get the Min/Max needed
  347. //--
  348. vec2 cam_center(0.f);
  349. float cam_factor = .0f;
  350. vec3 local_min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
  351. vec2 screen_min_max[2] = { vec2(FLT_MAX), vec2(-FLT_MAX) };
  352. mat4 world_cam = m_camera->GetView();
  353. mat4 cam_screen = m_camera->GetProjection();
  354. //target on-screen computation
  355. for (int i = 0; i < target_list.Count(); i++)
  356. {
  357. vec3 obj_loc = target_list[i];
  358. {
  359. //Debug::DrawBox(obj_loc - vec3(4.f), obj_loc + vec3(4.f), vec4(1.f, 0.f, 0.f, 1.f));
  360. mat4 target_mx = mat4::translate(obj_loc);
  361. vec3 vpos;
  362. //Get location in cam coordinates
  363. target_mx = world_cam * target_mx;
  364. vpos = target_mx.v3.xyz;
  365. local_min_max[0] = min(vpos.xyz, local_min_max[0]);
  366. local_min_max[1] = max(vpos.xyz, local_min_max[1]);
  367. //Get location in screen coordinates
  368. target_mx = cam_screen * target_mx;
  369. vpos = (target_mx.v3 / target_mx.v3.w).xyz;
  370. screen_min_max[0] = min(screen_min_max[0], vpos.xy * vec2(RATIO_WH, 1.f));
  371. screen_min_max[1] = max(screen_min_max[1], vpos.xy * vec2(RATIO_WH, 1.f));
  372. //Build Barycenter
  373. cam_center += vpos.xy;
  374. cam_factor += 1.f;
  375. }
  376. }
  377. float screen_ratio = max(max(lol::abs(local_min_max[0].x), lol::abs(local_min_max[0].y)),
  378. max(lol::abs(local_min_max[1].x), lol::abs(local_min_max[1].y)));
  379. float scale_ratio = max(max(lol::abs(screen_min_max[0].x), lol::abs(screen_min_max[0].y)),
  380. max(lol::abs(screen_min_max[1].x), lol::abs(screen_min_max[1].y)));
  381. vec2 screen_offset = vec2(0.f, -(screen_min_max[1].y + screen_min_max[0].y) * .5f);
  382. m_screen_offset = damp(m_screen_offset, screen_offset, .9f, seconds);
  383. 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;
  384. if (cam_factor > 0.f)
  385. {
  386. vec2 new_screen_scale = m_camera->GetScreenScale();
  387. m_camera->SetScreenScale(max(vec2(0.001f), new_screen_scale * ((1.0f + m_zoom_mesh) / (scale_ratio * SCREEN_LIMIT))));
  388. m_camera->SetPosition(vec3(vec2::zero, damp(m_camera->m_position.z, z_pos + screen_ratio * 2.f, .1f, seconds)), true);
  389. m_camera->SetFov(m_fov_mesh);
  390. m_camera->SetScreenInfos(damp(m_camera->GetScreenSize(), max(1.f, screen_ratio), 1.2f, seconds));
  391. }
  392. //--
  393. //Message Service
  394. //--
  395. String mesh("");
  396. int u = 4;
  397. while (u-- > 0 && MessageService::FetchFirst(MessageBucket::AppIn, mesh))
  398. {
  399. int o = 1;
  400. while (o-- > 0)
  401. {
  402. SceneSetup* new_ssetup = new SceneSetup();
  403. if (new_ssetup->Compile(mesh.C()))
  404. {
  405. if (m_ssetup)
  406. delete(m_ssetup);
  407. m_ssetup = new_ssetup;
  408. m_ssetup->Startup();
  409. m_mat_prev = mat4(quat::fromeuler_xyz(vec3::zero));
  410. }
  411. else
  412. delete(new_ssetup);
  413. }
  414. }
  415. //Check the custom cmd even if we don't have new messages.
  416. for (int i = 0; m_ssetup && i < m_ssetup->m_custom_cmd.Count(); ++i)
  417. {
  418. if (m_ssetup->m_custom_cmd[i].m1 == "setmesh")
  419. {
  420. //Create a new mesh
  421. EasyMesh* em = new EasyMesh();
  422. if (em->Compile(m_ssetup->m_custom_cmd[i].m2.C()))
  423. {
  424. if (m_mesh_id == m_meshes.Count() - 1)
  425. m_mesh_id++;
  426. m_meshes.Push(em);
  427. }
  428. else
  429. delete(em);
  430. }
  431. }
  432. m_ssetup->m_custom_cmd.Empty();
  433. #if NACL_EM
  434. //if (m_stream_update_time > .0f)
  435. //{
  436. // m_stream_update_time = -1.f;
  437. // MessageService::Send(MessageBucket::AppIn,
  438. // " addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1) \
  439. // addlight 0.0 position (8 2 6) color #ffff \
  440. // custom setmesh \"[sc#f8f ab 1]\"");
  441. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1]");
  442. // MessageService::Send(MessageBucket::AppIn, "[sc#f8f ab 1 splt 4 twy 90]");
  443. // MessageService::Send(MessageBucket::AppIn, "[sc#8ff afcb 1 1 1 0]");
  444. // MessageService::Send(MessageBucket::AppIn, "[sc#ff8 afcb 1 1 1 0]");
  445. //}
  446. #elif WIN32 && 0
  447. //--
  448. //File management
  449. //--
  450. m_stream_update_time += seconds;
  451. if (m_stream_update_time > m_stream_update_timer)
  452. {
  453. m_stream_update_time = 0.f;
  454. File f;
  455. f.Open(m_file_name.C(), FileAccess::Read);
  456. String cmd = f.ReadString();
  457. f.Close();
  458. if (cmd.Count()
  459. && (!m_cmdlist.Count() || cmd != m_cmdlist.Last()))
  460. {
  461. m_cmdlist << cmd;
  462. /*
  463. cmd = String(" addlight 0.0 position (4 -1 -4) color (.0 .2 .5 1) \
  464. addlight 0.0 position (8 2 6) color #ffff \
  465. custom setmesh \"") + cmd + "\"";
  466. */
  467. MessageService::Send(MessageBucket::AppIn, cmd);
  468. }
  469. }
  470. #endif //WINDOWS
  471. }
  472. virtual void TickDraw(float seconds)
  473. {
  474. WorldEntity::TickDraw(seconds);
  475. if (!m_init || !m_first_tick)
  476. return;
  477. //TODO : This should probably be "standard LoL behaviour"
  478. #if NO_NACL_EM
  479. {
  480. if (KeyReleased(KEY_F1))
  481. Video::SetDebugRenderMode(DebugRenderMode::Default);
  482. if (KeyReleased(KEY_F2))
  483. Video::SetDebugRenderMode(DebugRenderMode::Wireframe);
  484. if (KeyReleased(KEY_F3))
  485. Video::SetDebugRenderMode(DebugRenderMode::Lighting);
  486. if (KeyReleased(KEY_F4))
  487. Video::SetDebugRenderMode(DebugRenderMode::Normal);
  488. if (KeyReleased(KEY_F5))
  489. Video::SetDebugRenderMode(DebugRenderMode::UV);
  490. }
  491. #endif //NO_NACL_EM
  492. #if NO_NACL_EM && WITH_TEXTURE
  493. if (!m_default_texture)
  494. {
  495. m_texture_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinymvtexture));
  496. m_texture_uni = m_texture_shader->GetUniformLocation("u_Texture");
  497. m_default_texture = Tiler::Register("data/test-texture.png", ivec2::zero, ivec2(0,1));
  498. }
  499. else if (m_texture && m_default_texture)
  500. m_texture_shader->SetUniform(m_texture_uni, m_default_texture->GetTexture(), 0);
  501. #endif //NO_NACL_EM
  502. g_renderer->SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  503. vec3 x = vec3(1.f,0.f,0.f);
  504. vec3 y = vec3(0.f,1.f,0.f);
  505. for (int i = 0; i < m_meshes.Count(); i++)
  506. {
  507. {
  508. if (m_meshes[i]->GetMeshState() == MeshRender::NeedConvert)
  509. {
  510. #if WITH_TEXTURE
  511. m_meshes[i]->MeshConvert(new DefaultShaderData(((1 << VertexUsage::Position) | (1 << VertexUsage::Normal) |
  512. (1 << VertexUsage::Color) | (1 << VertexUsage::TexCoord)),
  513. m_texture_shader, true));
  514. #else
  515. m_meshes[i]->MeshConvert();
  516. #endif //WITH_TEXTURE
  517. }
  518. mat4 save_proj = m_camera->GetProjection();
  519. float j = -(float)(m_meshes.Count() - (i + 1)) + (-m_mesh_id1 + (float)(m_meshes.Count() - 1));
  520. if (m_mesh_id1 - m_render_max[0] > (float)i && m_mesh_id1 - m_render_max[1] < (float)i &&
  521. m_meshes[i]->GetMeshState() > MeshRender::NeedConvert)
  522. {
  523. float a_j = lol::abs(j);
  524. float i_trans = (a_j * a_j * m_hist_scale_mesh.x + a_j * m_hist_scale_mesh.x) * .5f;
  525. float i_scale = clamp(1.f - (m_hist_scale_mesh.y * (m_mesh_id1 - (float)i)), 0.f, 1.f);
  526. mat4 new_proj =
  527. //Y object Offset
  528. mat4::translate(x * m_screen_offset.x + y * m_screen_offset.y) *
  529. //Mesh Pos Offset
  530. mat4::translate((x * m_pos_mesh.x * RATIO_HW + y * m_pos_mesh.y) * 2.f * (1.f + .5f * m_zoom_mesh / SCREEN_LIMIT)) *
  531. //Mesh count offset
  532. mat4::translate(x * RATIO_HW * 2.f * (j + i_trans)) *
  533. //Align right meshes
  534. mat4::translate(x - x * RATIO_HW) *
  535. //Mesh count scale
  536. mat4::scale(vec3(vec2(i_scale), 1.f)) *
  537. //Camera projection
  538. save_proj;
  539. m_camera->SetProjection(new_proj);
  540. //#if NO_NACL_EM
  541. m_meshes[i]->Render(m_mat);
  542. //#endif //NO_NACL_EM
  543. g_renderer->Clear(ClearMask::Depth);
  544. }
  545. m_camera->SetProjection(save_proj);
  546. }
  547. }
  548. }
  549. private:
  550. SceneSetup* m_ssetup;
  551. Controller* m_controller;
  552. short m_input_usage;
  553. mat4 m_mat;
  554. mat4 m_mat_prev;
  555. bool m_init;
  556. bool m_first_tick;
  557. //Camera Setup
  558. Camera * m_camera;
  559. float m_reset_timer;
  560. float m_fov;
  561. float m_fov_mesh;
  562. float m_fov_speed;
  563. float m_zoom;
  564. float m_zoom_mesh;
  565. float m_zoom_speed;
  566. vec2 m_rot;
  567. vec2 m_rot_mesh;
  568. vec2 m_rot_speed;
  569. vec2 m_pos;
  570. vec2 m_pos_mesh;
  571. vec2 m_pos_speed;
  572. vec2 m_hist_scale;
  573. vec2 m_hist_scale_mesh;
  574. vec2 m_hist_scale_speed;
  575. vec2 m_screen_offset;
  576. //Mesh infos
  577. vec2 m_render_max;
  578. int m_mesh_id;
  579. float m_mesh_id1;
  580. Array<EasyMesh*> m_meshes;
  581. //File data
  582. String m_file_name;
  583. Array<String> m_cmdlist;
  584. float m_stream_update_time;
  585. float m_stream_update_timer;
  586. //misc datas
  587. Shader * m_texture_shader;
  588. TileSet * m_default_texture;
  589. Texture * m_texture;
  590. ShaderUniform m_texture_uni;
  591. };
  592. //The basic main :
  593. int main(int argc, char **argv)
  594. {
  595. System::Init(argc, argv);
  596. Application app("MeshViewer", ivec2((int)DEFAULT_WIDTH, (int)DEFAULT_HEIGHT), 60.0f);
  597. if (argc > 1)
  598. new MeshViewer(argv[1]);
  599. else
  600. new MeshViewer();
  601. app.Run();
  602. return EXIT_SUCCESS;
  603. }