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.
 
 
 

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