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.

meshviewer.cpp 17 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. LOLFX_RESOURCE_DECLARE(shinyfur);
  20. LOLFX_RESOURCE_DECLARE(shinymvtexture);
  21. #define IPT_CAM_RESET "Cam_Center"
  22. #define IPT_CAM_FORWARD "Cam_Forward"
  23. #define IPT_CAM_BACKWARD "Cam_Backward"
  24. #define IPT_CAM_ZOOM_OUT "Cam_Zoom_In"
  25. #define IPT_CAM_ZOOM_IN "Cam_Zoom_Out"
  26. #define IPT_MESH_UPDATE "Mesh_Update"
  27. #define IPT_MESH_RESET "Mesh_Reset"
  28. #define IPT_MESH_LEFT "Mesh_Left"
  29. #define IPT_MESH_RIGHT "Mesh_Right"
  30. #define IPT_MESH_UP "Mesh_Up"
  31. #define IPT_MESH_DOWN "Mesh_Down"
  32. #define IPT_MESH_SCALE_UP "Mesh_Scale_Up"
  33. #define IPT_MESH_SCALE_DOWN "Mesh_Scale_Down"
  34. #define IPT_MESH_OFFSET_UP "Mesh_Offset_Up"
  35. #define IPT_MESH_OFFSET_DOWN "Mesh_Offset_Down"
  36. #define IPT_MESH_ROT_LEFT "Mesh_Rot_Left"
  37. #define IPT_MESH_ROT_RIGHT "Mesh_Rot_Right"
  38. #define IPT_MESH_ROT_UP "Mesh_Rot_Up"
  39. #define IPT_MESH_ROT_DOWN "Mesh_Rot_Down"
  40. #define WITH_FUR 0
  41. class MeshViewer : public WorldEntity
  42. {
  43. public:
  44. void SetFov(float new_fov=60.0f)
  45. {
  46. //FOV compensation doesn't work
  47. ivec2 video_size = Video::GetSize();
  48. float near = (float)-video_size.x - video_size.y;
  49. float far = (float)video_size.x + video_size.y;
  50. float t1 = tanf(new_fov / 2);
  51. float dist = (float)video_size.x / (2.0f * t1);
  52. //m_fov_compensation = mat4::translate(-0.5f * video_size.x, -0.5f * video_size.y, -dist);
  53. m_fov_compensation = mat4::translate(vec3(.0f));
  54. if (new_fov > 0.1f)
  55. Scene::GetDefault()->GetCamera()->SetProjection(mat4::perspective(new_fov, (float)video_size.x, (float)video_size.y, .1f, 1000.f));
  56. else
  57. Scene::GetDefault()->GetCamera()->SetProjection(mat4::ortho((float)video_size.x, (float)video_size.y, .1f, 1000.f));
  58. }
  59. MeshViewer(char const *file_name = "data/mesh-buffer.txt")
  60. : m_file_name(file_name)
  61. {
  62. //Input setup
  63. Input::LinkActionToKey(IPT_CAM_RESET, Key::Return);
  64. Input::LinkActionToKey(IPT_CAM_FORWARD, Key::PageUp);
  65. Input::LinkActionToKey(IPT_CAM_BACKWARD, Key::PageDown);
  66. Input::LinkActionToKey(IPT_CAM_ZOOM_IN, Key::Home);
  67. Input::LinkActionToKey(IPT_CAM_ZOOM_OUT, Key::End);
  68. Input::LinkActionToKey(IPT_MESH_LEFT, Key::Left);
  69. Input::LinkActionToKey(IPT_MESH_RIGHT, Key::Right);
  70. Input::LinkActionToKey(IPT_MESH_UP, Key::Up);
  71. Input::LinkActionToKey(IPT_MESH_DOWN, Key::Down);
  72. Input::LinkActionToKey(IPT_MESH_UPDATE, Key::Space);
  73. Input::LinkActionToKey(IPT_MESH_RESET, Key::KP0);
  74. Input::LinkActionToKey(IPT_MESH_OFFSET_DOWN, Key::KP1);
  75. Input::LinkActionToKey(IPT_MESH_OFFSET_UP, Key::KP3);
  76. Input::LinkActionToKey(IPT_MESH_SCALE_DOWN, Key::KP7);
  77. Input::LinkActionToKey(IPT_MESH_SCALE_UP, Key::KP9);
  78. Input::LinkActionToKey(IPT_MESH_ROT_LEFT, Key::KP4);
  79. Input::LinkActionToKey(IPT_MESH_ROT_RIGHT, Key::KP6);
  80. Input::LinkActionToKey(IPT_MESH_ROT_UP, Key::KP8);
  81. Input::LinkActionToKey(IPT_MESH_ROT_DOWN, Key::KP5);
  82. m_angle = 0;
  83. m_default_texture = NULL;
  84. //Camera Setup
  85. m_fov_zoom_damp = .0f;
  86. m_fov_damp = 60.0f;
  87. m_fov = 60.0f;
  88. m_camera = new Camera();
  89. SetFov(m_fov_damp);
  90. m_camera->SetView(vec3(0.f, 0.f, 10.f),
  91. vec3(0.f, 0.f, 0.f),
  92. vec3(0.f, 1.f, 0.f));
  93. Scene::GetDefault()->PushCamera(m_camera);
  94. //Lights setup
  95. m_lights << new Light();
  96. m_lights.Last()->SetPosition(vec4(4.f, -1.f, -4.f, 0.f));
  97. m_lights.Last()->SetColor(vec4(.0f, .2f, .5f, 1.f));
  98. Ticker::Ref(m_lights.Last());
  99. m_lights << new Light();
  100. m_lights.Last()->SetPosition(vec4(8.f, 2.f, 6.f, 1.f));
  101. m_lights.Last()->SetColor(vec4(.5f, .3f, .0f, 1.f));
  102. Ticker::Ref(m_lights.Last());
  103. //Speed damp
  104. m_mesh_rotate_damp = vec2(.0f);
  105. m_mesh_screen_move_damp = vec2(.0f);
  106. m_mesh_move_damp = vec2(.0f);
  107. //Actual values
  108. SetDefaultMeshTransform();
  109. //Actual values damp
  110. m_mesh_rotation_damp = vec2(.0f);
  111. m_mesh_screen_offset_damp = vec2(.0f);
  112. m_mesh_offset_damp = vec2(.0f);
  113. m_mat = mat4::rotate(m_mesh_rotation.x, vec3(1, 0, 0)) *
  114. mat4::rotate(m_angle, vec3(0, 1, 0)) *
  115. mat4::rotate(m_mesh_rotation.y, vec3(0, 1, 0));
  116. m_stream_update_time = 2.0f;
  117. m_stream_update_timer = 1.0f;
  118. }
  119. ~MeshViewer()
  120. {
  121. Scene::GetDefault()->PopCamera(m_camera);
  122. for (int i = 0; i < m_lights.Count(); ++i)
  123. Ticker::Unref(m_lights[i]);
  124. }
  125. void SetDefaultMeshTransform()
  126. {
  127. m_mesh_rotation = vec2(25.0f, .0f);
  128. m_mesh_screen_offset = vec2(.54f, .0f);
  129. m_mesh_offset = vec2(-.64f, .07f);
  130. }
  131. virtual void TickGame(float seconds)
  132. {
  133. WorldEntity::TickGame(seconds);
  134. //TODO : This should probably be "standard LoL behaviour"
  135. {
  136. //Shutdown logic
  137. if (Input::WasReleased(Key::Escape))
  138. Ticker::Shutdown();
  139. }
  140. //Update Mesh BBox - Get the Min/Max needed
  141. vec3 min_max[2] = { vec3(FLT_MAX), vec3(-FLT_MAX) };
  142. int mesh_id = m_meshes.Count() - 1;
  143. for (; mesh_id >= 0; mesh_id--)
  144. if (m_meshes[mesh_id].m2)
  145. break;
  146. if (m_meshes.Count() && mesh_id >= 0)
  147. {
  148. for (int i = 0; i < m_meshes[mesh_id].m1.GetVertexCount(); i++)
  149. {
  150. vec3 vpos = m_meshes[mesh_id].m1.GetVertexLocation(i);
  151. min_max[0] = min(vpos.xyz, min_max[0]);
  152. min_max[1] = max(vpos.xyz, min_max[1]);
  153. }
  154. }
  155. else
  156. {
  157. min_max[0] = vec3(.0f);
  158. min_max[1] = vec3(.0f);
  159. }
  160. //[0] : center, [1] : size.
  161. vec3 BBox[2] = { vec3(.0f), vec3(.0f) };
  162. BBox[1] = min_max[1] - min_max[0];
  163. BBox[0] = min_max[0] + BBox[1] * .5f;
  164. vec3 BBox_mod = BBox[1];
  165. //--
  166. //Camera movement handling
  167. //--
  168. vec3 cam_move = BBox_mod * seconds *
  169. vec3(.0f, .0f, (float)(Input::GetStatus(IPT_CAM_BACKWARD) - Input::GetStatus(IPT_CAM_FORWARD)));
  170. if (Input::WasReleased(IPT_CAM_RESET))
  171. SetFov();
  172. float fov_zoom = (float)(Input::GetStatus(IPT_CAM_ZOOM_OUT) - Input::GetStatus(IPT_CAM_ZOOM_IN));
  173. m_fov_zoom_damp = damp(m_fov_zoom_damp, fov_zoom, (fov_zoom == .0f)?(.15f):(0.5f), seconds);
  174. m_fov = max(.0f, m_fov + seconds * 10.0f * m_fov_zoom_damp);
  175. m_fov_damp = damp(m_fov_damp, m_fov, .2f, seconds);
  176. SetFov(m_fov_damp);
  177. vec3 campos = Scene::GetDefault()->GetCamera()->GetPosition();
  178. Scene::GetDefault()->GetCamera()->SetView(campos + cam_move, quat(1.f));
  179. //--
  180. //Mesh movement handling
  181. //--
  182. if (Input::WasReleased(IPT_MESH_RESET))
  183. SetDefaultMeshTransform();
  184. vec2 new_move = vec2(.0f);
  185. new_move = vec2((float)(Input::GetStatus(IPT_MESH_RIGHT) - Input::GetStatus(IPT_MESH_LEFT)),
  186. (float)(Input::GetStatus(IPT_MESH_UP) - Input::GetStatus(IPT_MESH_DOWN)));
  187. m_mesh_screen_move_damp = vec2(damp(m_mesh_screen_move_damp.x, new_move.x, (new_move.x == .0f)?(.15f):(0.5f), seconds),
  188. damp(m_mesh_screen_move_damp.y, new_move.y, (new_move.y == .0f)?(.15f):(0.5f), seconds));
  189. new_move = vec2((float)(Input::GetStatus(IPT_MESH_OFFSET_UP) - Input::GetStatus(IPT_MESH_OFFSET_DOWN)),
  190. (float)(Input::GetStatus(IPT_MESH_SCALE_UP) - Input::GetStatus(IPT_MESH_SCALE_DOWN)));
  191. m_mesh_move_damp = vec2(damp(m_mesh_move_damp.x, new_move.x, (new_move.x == .0f)?(.15f):(0.5f), seconds),
  192. damp(m_mesh_move_damp.y, new_move.y, (new_move.y == .0f)?(.15f):(0.5f), seconds));
  193. new_move = vec2((float)(Input::GetStatus(IPT_MESH_ROT_UP) - Input::GetStatus(IPT_MESH_ROT_DOWN)),
  194. (float)(Input::GetStatus(IPT_MESH_ROT_RIGHT) - Input::GetStatus(IPT_MESH_ROT_LEFT)));
  195. m_mesh_rotate_damp = vec2(damp(m_mesh_rotate_damp.x, new_move.x, (new_move.x == .0f)?(.15f):(0.5f), seconds),
  196. damp(m_mesh_rotate_damp.y, new_move.y, (new_move.y == .0f)?(.15f):(0.5f), seconds));
  197. vec2 mesh_screen_move = seconds * 0.6f * m_mesh_screen_move_damp;
  198. vec2 mesh_offset_move = seconds * vec2(.6f, .2f) * m_mesh_move_damp;
  199. vec2 mesh_rotate = seconds * vec2(40.0f, 60.0f) * m_mesh_rotate_damp;
  200. //Add movement
  201. m_mesh_screen_offset += mesh_screen_move;
  202. m_mesh_offset += mesh_offset_move;
  203. m_mesh_rotation += mesh_rotate;
  204. //Compute damp
  205. m_mesh_rotation_damp = damp(m_mesh_rotation_damp, m_mesh_rotation, .2f, seconds);
  206. m_mesh_screen_offset_damp = damp(m_mesh_screen_offset_damp, m_mesh_screen_offset, .2f, seconds);
  207. m_mesh_offset_damp = damp(m_mesh_offset_damp, m_mesh_offset, .2f, seconds);
  208. //Clamp necessary
  209. m_mesh_rotation.x = clamp(m_mesh_rotation.x, -90.0f, 90.0f);
  210. m_mesh_offset.y = max(.0f, m_mesh_offset.y);
  211. //m_angle += seconds * 70.0f;
  212. m_mat = mat4::rotate(m_mesh_rotation.x, vec3(1, 0, 0)) *
  213. mat4::rotate(m_angle, vec3(0, 1, 0)) *
  214. mat4::rotate(m_mesh_rotation.y, vec3(0, 1, 0));
  215. //--
  216. //File management
  217. //--
  218. if (Input::WasReleased(IPT_MESH_UPDATE))
  219. m_stream_update_time = m_stream_update_timer + 1.0f;
  220. m_stream_update_time += seconds;
  221. if (m_stream_update_time > m_stream_update_timer)
  222. {
  223. m_stream_update_time = 0.f;
  224. File f;
  225. f.Open(m_file_name.C(), FileAccess::Read);
  226. String cmd = f.ReadString();
  227. f.Close();
  228. for (int i = 0; i < cmd.Count() - 1; i++)
  229. {
  230. if (cmd[i] == '/' && cmd[i + 1] == '/')
  231. {
  232. int j = i;
  233. for (; j < cmd.Count(); j++)
  234. {
  235. if (cmd[j] == '\r' || cmd[j] == '\n')
  236. break;
  237. }
  238. String new_cmd = cmd.Sub(0, i);
  239. if (j < cmd.Count())
  240. new_cmd += cmd.Sub(j, cmd.Count() - j);
  241. cmd = new_cmd;
  242. i--;
  243. }
  244. }
  245. if (cmd.Count()
  246. && (!m_cmdlist.Count() || cmd != m_cmdlist.Last()))
  247. {
  248. m_cmdlist << cmd;
  249. //Create a new mesh
  250. m_meshes.Push(EasyMesh(), false, .0f, vec3(.0f));
  251. if (!m_meshes.Last().m1.Compile(cmd.C()))
  252. m_meshes.Pop();
  253. //else
  254. // m_meshes.Last().m1.ComputeTexCoord(0.2f, 2);
  255. }
  256. }
  257. }
  258. virtual void TickDraw(float seconds)
  259. {
  260. WorldEntity::TickDraw(seconds);
  261. //TODO : This should probably be "standard LoL behaviour"
  262. {
  263. if (Input::WasReleased(Key::F1))
  264. Video::SetDebugRenderMode(DebugRenderMode::Default);
  265. if (Input::WasReleased(Key::F2))
  266. Video::SetDebugRenderMode(DebugRenderMode::Wireframe);
  267. if (Input::WasReleased(Key::F3))
  268. Video::SetDebugRenderMode(DebugRenderMode::Lighting);
  269. if (Input::WasReleased(Key::F4))
  270. Video::SetDebugRenderMode(DebugRenderMode::Normal);
  271. if (Input::WasReleased(Key::F5))
  272. Video::SetDebugRenderMode(DebugRenderMode::UV);
  273. }
  274. if (!m_default_texture)
  275. {
  276. m_texture_shader = Shader::Create(LOLFX_RESOURCE_NAME(shinymvtexture));
  277. m_texture_uni = m_texture_shader->GetUniformLocation("u_Texture");
  278. //m_image = new Image("data/test-texture.png");
  279. m_default_texture = Tiler::Register("data/test-texture.png", ivec2(0), ivec2(0,1));
  280. //ivec2 size = m_image->GetSize();
  281. //// m_image->GetFormat()
  282. //m_texture = new Texture(m_image->GetSize(), PixelFormat::ABGR_8);
  283. //m_texture->SetData(m_image->GetData());
  284. // PixelFormat::ABGR_8
  285. }
  286. else if (m_texture && m_default_texture)
  287. m_texture_shader->SetUniform(m_texture_uni, m_default_texture->GetTexture(), 0);
  288. for (int i = 0; i < m_meshes.Count(); i++)
  289. {
  290. if (!m_meshes[i].m2)
  291. {
  292. //Fur support
  293. #if WITH_FUR
  294. m_meshes[i].m1.MeshConvert(Shader::Create(LOLFX_RESOURCE_NAME(shinyfur)));
  295. #else
  296. //m_meshes[i].m1.MeshConvert(m_texture_shader);
  297. //m_meshes[i].m1.MeshConvert();
  298. m_meshes[i].m1.MeshConvert(new DefaultShaderData(((1 << VertexUsage::Position) | (1 << VertexUsage::Normal) |
  299. (1 << VertexUsage::Color) | (1 << VertexUsage::TexCoord)),
  300. m_texture_shader, true));
  301. #endif
  302. m_meshes[i].m2 = true;
  303. }
  304. }
  305. Video::SetClearColor(vec4(0.0f, 0.0f, 0.0f, 1.0f));
  306. mat4 default_proj = Scene::GetDefault()->GetCamera()->GetProjection();
  307. for (int i = 0; i < m_meshes.Count(); i++)
  308. {
  309. float new_scale = max(.0f, 1.0f - (m_mesh_offset_damp.y * (float)(m_meshes.Count() - (i + 1))));
  310. m_meshes[i].m3 = damp(m_meshes[i].m3, new_scale, .35f, seconds);
  311. if (m_meshes[i].m3 > .0f)
  312. {
  313. vec3 new_mesh_offset = vec3(.0f);
  314. for (int j = m_meshes.Count() - 1; j > i; j--)
  315. {
  316. float ofs_scale = max(.0f, 1.0f - (m_mesh_offset_damp.y * (float)(m_meshes.Count() - (j + 0))));
  317. new_mesh_offset = new_mesh_offset + vec3(m_meshes[j].m3 * ofs_scale * ofs_scale * m_mesh_offset_damp.x, .0f, .0f);
  318. }
  319. m_meshes[i].m4 = damp(m_meshes[i].m4, new_mesh_offset, .35f, seconds);
  320. Scene::GetDefault()->GetCamera()->SetProjection(mat4::translate(m_meshes[i].m4) *
  321. mat4::translate(vec3(m_mesh_screen_offset_damp, .0f)) *
  322. mat4::scale(vec3(vec2(m_meshes[i].m3), 1.0f)) *
  323. default_proj *
  324. m_fov_compensation);
  325. #if WITH_FUR
  326. for (int j=0; j < 40; j++)
  327. m_meshes[i].m1.Render(m_mat, 0.1 * j);
  328. #else
  329. m_meshes[i].m1.Render(m_mat);
  330. #endif
  331. Video::Clear(ClearMask::Depth);
  332. }
  333. }
  334. Scene::GetDefault()->GetCamera()->SetProjection(default_proj);
  335. }
  336. private:
  337. Camera * m_camera;
  338. float m_angle;
  339. mat4 m_mat;
  340. //Mesh infos
  341. //Move damping
  342. vec2 m_mesh_rotate_damp;
  343. vec2 m_mesh_screen_move_damp;
  344. vec2 m_mesh_move_damp;
  345. //Move transform damping
  346. vec2 m_mesh_rotation_damp;
  347. vec2 m_mesh_screen_offset_damp;
  348. vec2 m_mesh_offset_damp;
  349. vec2 m_mesh_rotation; //Meshes rotation
  350. vec2 m_mesh_screen_offset;//Meshes screen offset
  351. vec2 m_mesh_offset; //Mesh Offset after first mesh (x: offset, y: scale)
  352. //File data
  353. String m_file_name;
  354. Array<String> m_cmdlist;
  355. float m_stream_update_time;
  356. float m_stream_update_timer;
  357. //misc datas
  358. Array<EasyMesh, bool, float, vec3> m_meshes;
  359. Array<Light *> m_lights;
  360. Shader * m_texture_shader;
  361. TileSet * m_default_texture;
  362. Texture * m_texture;
  363. ShaderUniform m_texture_uni;
  364. Image * m_image;
  365. float m_fov;
  366. float m_fov_damp;
  367. float m_fov_zoom_damp;
  368. mat4 m_fov_compensation;
  369. };
  370. //The basic main :
  371. int main(int argc, char **argv)
  372. {
  373. System::Init(argc, argv);
  374. Application app("MeshViewer", ivec2(1600, 600), 60.0f);
  375. if (argc > 1)
  376. new MeshViewer(argv[1]);
  377. else
  378. new MeshViewer();
  379. app.Run();
  380. return EXIT_SUCCESS;
  381. }