You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

394 rivejä
14 KiB

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