Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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