Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

meshviewer.cpp 29 KiB

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