Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

430 lignes
15 KiB

  1. //
  2. // Lol Engine — Voronoi diagram tutorial
  3. //
  4. // Copyright © 2011—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include "loldebug.h"
  17. using namespace lol;
  18. LOLFX_RESOURCE_DECLARE(12_voronoi);
  19. LOLFX_RESOURCE_DECLARE(12_voronoi_setup);
  20. LOLFX_RESOURCE_DECLARE(12_voronoi_distance);
  21. LOLFX_RESOURCE_DECLARE(12_distance);
  22. LOLFX_RESOURCE_DECLARE(12_texture_to_screen);
  23. enum
  24. {
  25. KEY_ESC,
  26. KEY_PUSH,
  27. KEY_POP,
  28. KEY_F1,
  29. KEY_F2,
  30. KEY_F3,
  31. KEY_MAX
  32. };
  33. enum FboType
  34. {
  35. SrcVoronoiFbo,
  36. VoronoiFbo,
  37. DistanceVoronoiFbo,
  38. DistanceFbo,
  39. MaxFboType
  40. };
  41. class Voronoi : public WorldEntity
  42. {
  43. public:
  44. Voronoi()
  45. {
  46. m_vertices << vec2( 1.0, 1.0);
  47. m_vertices << vec2(-1.0, -1.0);
  48. m_vertices << vec2( 1.0, -1.0);
  49. m_vertices << vec2(-1.0, -1.0);
  50. m_vertices << vec2( 1.0, 1.0);
  51. m_vertices << vec2(-1.0, 1.0);
  52. m_ready = false;
  53. m_cur_fbo = 0;
  54. m_time = .0f;
  55. m_timer = -1.0f;
  56. mode = 0;
  57. m_controller = new Controller("Default");
  58. m_controller->SetInputCount(KEY_MAX, 0);
  59. m_controller->GetKey(KEY_ESC).Bind("Keyboard", "Escape");
  60. m_controller->GetKey(KEY_PUSH).Bind("Keyboard", "p");
  61. m_controller->GetKey(KEY_POP).Bind("Keyboard", "o");
  62. m_controller->GetKey(KEY_F1).Bind("Keyboard", "F1");
  63. m_controller->GetKey(KEY_F2).Bind("Keyboard", "F2");
  64. m_controller->GetKey(KEY_F3).Bind("Keyboard", "F3");
  65. }
  66. virtual void TickGame(float seconds)
  67. {
  68. WorldEntity::TickGame(seconds);
  69. {
  70. //Shutdown logic
  71. if (m_controller->WasKeyReleasedThisFrame(KEY_ESC))
  72. Ticker::Shutdown();
  73. }
  74. m_time += seconds;
  75. m_hotspot = 0.4f * vec3((float)lol::sin(m_time * 4.0) + (float)lol::cos(m_time * 5.3),
  76. (float)lol::sin(m_time * 5.7) + (float)lol::cos(m_time * 4.4),
  77. (float)lol::sin(m_time * 5.0));
  78. m_color = 0.25f * vec3(1.1f + (float)lol::sin(m_time * 2.5 + 1.0),
  79. 1.1f + (float)lol::sin(m_time * 2.8 + 1.3),
  80. 1.1f + (float)lol::sin(m_time * 2.7));
  81. /* Saturate dot color */
  82. float x = std::max(m_color.x, std::max(m_color.y, m_color.z));
  83. m_color /= x;
  84. }
  85. virtual void TickDraw(float seconds, Scene &scene)
  86. {
  87. WorldEntity::TickDraw(seconds, scene);
  88. if (!m_ready)
  89. {
  90. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  91. m_vbo = new VertexBuffer(m_vertices.bytes());
  92. void *vertices = m_vbo->Lock(0, 0);
  93. memcpy(vertices, &m_vertices[0], m_vertices.bytes());
  94. m_vbo->Unlock();
  95. m_screen_shader = Shader::Create(LOLFX_RESOURCE_NAME(12_texture_to_screen));
  96. m_screen_coord = m_screen_shader->GetAttribLocation(VertexUsage::Position, 0);
  97. m_screen_texture = m_screen_shader->GetUniformLocation("in_texture");
  98. for (int i = 0; i < MaxFboType; ++i)
  99. {
  100. m_fbos.push(new Framebuffer(Video::GetSize()), 0, array<ShaderUniform>(), array<ShaderAttrib>() );
  101. if (i == SrcVoronoiFbo)
  102. {
  103. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_voronoi_setup));
  104. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_texture");
  105. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_source_point");
  106. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_screen_res");
  107. m_fbos[i].m4 << m_fbos[i].m2->GetAttribLocation(VertexUsage::Position, 0);
  108. }
  109. else if (i == VoronoiFbo)
  110. {
  111. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_voronoi));
  112. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_texture");
  113. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_step");
  114. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_screen_res");
  115. m_fbos[i].m4 << m_fbos[i].m2->GetAttribLocation(VertexUsage::Position, 0);
  116. }
  117. else if (i == DistanceVoronoiFbo)
  118. {
  119. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_voronoi_distance));
  120. }
  121. else if (i == DistanceFbo)
  122. {
  123. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_distance));
  124. }
  125. m_fbos.last().m1->Bind();
  126. {
  127. RenderContext rc;
  128. rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  129. rc.SetClearDepth(1.f);
  130. Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
  131. }
  132. m_fbos.last().m1->Unbind();
  133. }
  134. temp_buffer = new Framebuffer(Video::GetSize());
  135. temp_buffer->Bind();
  136. {
  137. RenderContext rc;
  138. rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  139. rc.SetClearDepth(1.f);
  140. Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
  141. }
  142. temp_buffer->Unbind();
  143. m_ready = true;
  144. /* FIXME: this object never cleans up */
  145. //SRC SETUP
  146. m_cur_fbo = VoronoiFbo;
  147. }
  148. {
  149. //Shutdown logic
  150. if (m_controller->WasKeyReleasedThisFrame(KEY_POP))
  151. voronoi_points.pop();
  152. else if (m_controller->WasKeyReleasedThisFrame(KEY_PUSH))
  153. voronoi_points.push(vec3(rand<float>(512.f), rand<float>(512.f), .0f),
  154. vec2(64.f + rand<float>(64.f), 64.f + rand<float>(64.f)));
  155. else if (m_controller->WasKeyReleasedThisFrame(KEY_F1))
  156. m_cur_fbo = SrcVoronoiFbo;
  157. else if (m_controller->WasKeyReleasedThisFrame(KEY_F2))
  158. m_cur_fbo = VoronoiFbo;
  159. else if (m_controller->WasKeyReleasedThisFrame(KEY_F3))
  160. {
  161. voronoi_points.empty();
  162. if (mode == 0)
  163. {
  164. int i = 4;
  165. while (i-- > 0)
  166. voronoi_points.push(vec3(rand<float>(512.f), rand<float>(512.f), .0f),
  167. vec2(64.f + rand<float>(64.f), 64.f + rand<float>(64.f))
  168. //vec2::zero
  169. );
  170. mode = 1;
  171. }
  172. else
  173. {
  174. mode = 0;
  175. }
  176. }
  177. }
  178. if (mode == 0)
  179. {
  180. voronoi_points.empty();
  181. int maxi = 6;
  182. for (int i = 0; i < maxi; ++i)
  183. {
  184. float mi = (float)maxi;
  185. float j = (float)i;
  186. float f_time = (float)m_time;
  187. voronoi_points.push(vec3(256.f) + 196.f * vec3(lol::cos( f_time + j * 2.f * F_PI / mi), lol::sin( f_time + j * 2.f * F_PI / mi), .0f), vec2(.0f));
  188. voronoi_points.push(vec3(256.f) + 128.f * vec3(lol::cos(-f_time + j * 2.f * F_PI / mi), lol::sin(-f_time + j * 2.f * F_PI / mi), .0f), vec2(.0f));
  189. voronoi_points.push(vec3(256.f) + 64.f * vec3(lol::cos( f_time + j * 2.f * F_PI / mi), lol::sin( f_time + j * 2.f * F_PI / mi), .0f), vec2(.0f));
  190. voronoi_points.push(vec3(256.f) + 32.f * vec3(lol::cos(-f_time + j * 2.f * F_PI / mi), lol::sin(-f_time + j * 2.f * F_PI / mi), .0f), vec2(.0f));
  191. }
  192. voronoi_points.push(vec3(256.f), vec2(0.f));
  193. }
  194. temp_buffer->Bind();
  195. {
  196. RenderContext rc;
  197. rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  198. rc.SetClearDepth(1.f);
  199. Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
  200. }
  201. temp_buffer->Unbind();
  202. {
  203. vec2 limit(1.f, 511.f);
  204. //SRC SETUP
  205. for (int j = 0; j < voronoi_points.count(); ++j)
  206. {
  207. voronoi_points[j].m1 = vec3(voronoi_points[j].m1.xy + voronoi_points[j].m2 * seconds, voronoi_points[j].m1.z);
  208. if (voronoi_points[j].m1.x >= limit.y || voronoi_points[j].m1.x <= limit.x)
  209. {
  210. voronoi_points[j].m2.x *= -1.f;
  211. voronoi_points[j].m1.x = clamp(voronoi_points[j].m1.x, limit.x, limit.y);
  212. }
  213. if (voronoi_points[j].m1.y >= limit.y || voronoi_points[j].m1.y <= limit.x)
  214. {
  215. voronoi_points[j].m2.y *= -1.f;
  216. voronoi_points[j].m1.y = clamp(voronoi_points[j].m1.y, limit.x, limit.y);
  217. }
  218. voronoi_points[j].m1.z = ((float)j + 1) / ((float)voronoi_points.count());
  219. }
  220. int f = SrcVoronoiFbo;
  221. m_fbos[f].m1->Bind();
  222. {
  223. RenderContext rc;
  224. rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  225. rc.SetClearDepth(1.f);
  226. Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
  227. }
  228. m_fbos[f].m1->Unbind();
  229. int buf = voronoi_points.count() % 2;
  230. for (int j = 0; j < voronoi_points.count(); ++j)
  231. {
  232. Framebuffer *dst_buf;
  233. Framebuffer *src_buf;
  234. if (buf)
  235. {
  236. dst_buf = m_fbos[f].m1;
  237. src_buf = temp_buffer;
  238. }
  239. else
  240. {
  241. src_buf = m_fbos[f].m1;
  242. dst_buf = temp_buffer;
  243. }
  244. dst_buf->Bind();
  245. /* FIXME: we should just disable depth test in the shader */
  246. Renderer::Get()->Clear(ClearMask::Depth);
  247. m_fbos[f].m2->Bind();
  248. int i = 0;
  249. m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], src_buf->GetTextureUniform(), 0); //"in_texture"
  250. m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], voronoi_points[j].m1); //"in_source_point"
  251. m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], vec2(512.f, 512.f)); //"in_screen_res"
  252. m_vdecl->SetStream(m_vbo, m_fbos[f].m4.last());
  253. m_vdecl->Bind();
  254. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  255. m_vdecl->Unbind();
  256. m_fbos[f].m2->Unbind();
  257. dst_buf->Unbind();
  258. buf = 1 - buf;
  259. }
  260. }
  261. Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
  262. //FRAME BUFFER DRAW
  263. m_timer -= seconds;
  264. if (m_timer < .0f && m_cur_fbo != SrcVoronoiFbo)
  265. {
  266. //m_timer = 1.0f;
  267. m_fbos[m_cur_fbo].m1->Bind();
  268. {
  269. RenderContext rc;
  270. rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  271. rc.SetClearDepth(1.f);
  272. Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
  273. }
  274. m_fbos[m_cur_fbo].m1->Unbind();
  275. ivec2 curres = ivec2(512, 512) / 2;
  276. int buf = 0;
  277. while (1)
  278. {
  279. Framebuffer *dst_buf;
  280. Framebuffer *src_buf;
  281. Shader *shader;
  282. if (curres == ivec2::zero)
  283. shader = m_screen_shader;
  284. else
  285. shader = m_fbos[m_cur_fbo].m2;
  286. if (curres.x == 256)
  287. src_buf = m_fbos[SrcVoronoiFbo].m1;
  288. else if (buf)
  289. src_buf = m_fbos[m_cur_fbo].m1;
  290. else
  291. src_buf = temp_buffer;
  292. if (buf)
  293. dst_buf = temp_buffer;
  294. else
  295. dst_buf = m_fbos[m_cur_fbo].m1;
  296. dst_buf->Bind();
  297. /* FIXME: we should just disable depth test in the shader */
  298. Renderer::Get()->Clear(ClearMask::Depth);
  299. shader->Bind();
  300. //08_FBO ??
  301. #if _XBOX
  302. /* FIXME: the Xbox enforces full EDRAM clears on each frame, so
  303. * we cannot expect the render target contents to be preserved.
  304. * This code snippet should be moved inside the Framebuffer class. */
  305. //m_fbos[m_cur_fbo].m2->SetUniform(m_uni_flag, 1.f);
  306. //m_fbos[m_cur_fbo].m2->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0);
  307. //m_vdecl->SetStream(m_vbo, m_fbos[m_cur_fbo].m4.last());
  308. //m_vdecl->Bind();
  309. //m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  310. //m_vdecl->Unbind();
  311. #endif
  312. int i = 0;
  313. if (curres == ivec2::zero)
  314. m_screen_shader->SetUniform(m_screen_texture, src_buf->GetTextureUniform(), 0);
  315. else if (m_cur_fbo == VoronoiFbo)
  316. {
  317. shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], src_buf->GetTextureUniform(), 0); //"in_texture"
  318. shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], ((float)curres.x) / 512.f); //"in_step"
  319. shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], vec2(512.f, 512.f)); //"in_screen_res"
  320. }
  321. m_vdecl->SetStream(m_vbo, m_fbos[m_cur_fbo].m4.last());
  322. m_vdecl->Bind();
  323. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  324. m_vdecl->Unbind();
  325. m_fbos[m_cur_fbo].m2->Unbind();
  326. dst_buf->Unbind();
  327. if (curres == ivec2::zero)
  328. break;
  329. if (curres == ivec2(1))
  330. {
  331. if (buf == 1)
  332. curres = ivec2::zero;
  333. else
  334. break;
  335. }
  336. buf = 1 - buf;
  337. curres /= 2;
  338. }
  339. }
  340. //SCREEN DRAW
  341. m_screen_shader->Bind();
  342. m_screen_shader->SetUniform(m_screen_texture, m_fbos[m_cur_fbo].m1->GetTextureUniform(), 0);
  343. m_vdecl->SetStream(m_vbo, m_screen_coord);
  344. m_vdecl->Bind();
  345. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  346. m_vdecl->Unbind();
  347. m_screen_shader->Unbind();
  348. }
  349. private:
  350. Controller* m_controller;
  351. array<vec3, vec2> voronoi_points;
  352. array<vec2> m_vertices;
  353. Shader *m_screen_shader;
  354. ShaderAttrib m_screen_coord;
  355. ShaderUniform m_screen_texture;
  356. VertexDeclaration *m_vdecl;
  357. VertexBuffer *m_vbo;
  358. array<Framebuffer *, Shader *, array<ShaderUniform>, array<ShaderAttrib> > m_fbos;
  359. Framebuffer *temp_buffer;
  360. int mode;
  361. int m_cur_fbo;
  362. double m_time;
  363. vec3 m_hotspot, m_color;
  364. bool m_ready;
  365. float m_timer;
  366. };
  367. int main(int argc, char **argv)
  368. {
  369. System::Init(argc, argv);
  370. Application app("Tutorial 12: Jump Flooding Algorithm & Voronoi", ivec2(512, 512), 60.0f);
  371. new Voronoi();
  372. app.Run();
  373. return EXIT_SUCCESS;
  374. }