Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

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