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.
 
 
 

369 lines
13 KiB

  1. //
  2. // Lol Engine - Framebuffer Object tutorial
  3. //
  4. // Copyright: (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include "core.h"
  14. #include "loldebug.h"
  15. using namespace std;
  16. using namespace lol;
  17. LOLFX_RESOURCE_DECLARE(12_voronoi);
  18. LOLFX_RESOURCE_DECLARE(12_voronoi_setup);
  19. LOLFX_RESOURCE_DECLARE(12_voronoi_distance);
  20. LOLFX_RESOURCE_DECLARE(12_distance);
  21. LOLFX_RESOURCE_DECLARE(12_texture_to_screen);
  22. enum FboType
  23. {
  24. src_voronoi,
  25. voronoi,
  26. distance_voronoi,
  27. distance,
  28. MAX
  29. };
  30. class Voronoi : public WorldEntity
  31. {
  32. public:
  33. Voronoi()
  34. {
  35. m_vertices << vec2( 1.0, 1.0);
  36. m_vertices << vec2(-1.0, -1.0);
  37. m_vertices << vec2( 1.0, -1.0);
  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_ready = false;
  42. m_cur_fbo = 0;
  43. m_timer = -1.0f;
  44. }
  45. virtual void TickGame(float seconds)
  46. {
  47. WorldEntity::TickGame(seconds);
  48. {
  49. //Shutdown logic
  50. if (Input::WasReleased(Key::Escape))
  51. Ticker::Shutdown();
  52. }
  53. m_time += seconds;
  54. m_hotspot = 0.4f * vec3(lol::sin(m_time * 4.f) + lol::cos(m_time * 5.3f),
  55. lol::sin(m_time * 5.7f) + lol::cos(m_time * 4.4f),
  56. lol::sin(m_time * 5.f));
  57. m_color = 0.25f * vec3(1.1f + lol::sin(m_time * 2.5f + 1.f),
  58. 1.1f + lol::sin(m_time * 2.8f + 1.3f),
  59. 1.1f + lol::sin(m_time * 2.7f));
  60. /* Saturate dot color */
  61. float x = std::max(m_color.x, std::max(m_color.y, m_color.z));
  62. m_color /= x;
  63. }
  64. virtual void TickDraw(float seconds)
  65. {
  66. WorldEntity::TickDraw(seconds);
  67. if (!m_ready)
  68. {
  69. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  70. m_vbo = new VertexBuffer(m_vertices.Bytes());
  71. void *vertices = m_vbo->Lock(0, 0);
  72. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  73. m_vbo->Unlock();
  74. m_screen_shader = Shader::Create(LOLFX_RESOURCE_NAME(12_texture_to_screen));
  75. m_screen_coord = m_screen_shader->GetAttribLocation("in_position", VertexUsage::Position, 0);
  76. m_screen_texture = m_screen_shader->GetUniformLocation("in_texture");
  77. for (int i = 0; i < FboType::MAX; ++i)
  78. {
  79. m_fbos.Push(new FrameBuffer(Video::GetSize()), 0, Array<ShaderUniform>(), Array<ShaderAttrib>() );
  80. if (i == src_voronoi)
  81. {
  82. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_voronoi_setup));
  83. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_texture");
  84. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_source_point");
  85. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_screen_res");
  86. m_fbos[i].m4 << m_fbos[i].m2->GetAttribLocation("in_position", VertexUsage::Position, 0);
  87. }
  88. else if (i == voronoi)
  89. {
  90. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_voronoi));
  91. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_texture");
  92. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_step");
  93. m_fbos[i].m3 << m_fbos[i].m2->GetUniformLocation("in_screen_res");
  94. m_fbos[i].m4 << m_fbos[i].m2->GetAttribLocation("in_position", VertexUsage::Position, 0);
  95. }
  96. else if (i == distance_voronoi)
  97. {
  98. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_voronoi_distance));
  99. }
  100. else if (i == FboType::distance)
  101. {
  102. m_fbos[i].m2 = Shader::Create(LOLFX_RESOURCE_NAME(12_distance));
  103. }
  104. m_fbos.Last().m1->Bind();
  105. Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  106. Video::SetClearDepth(1.f);
  107. Video::Clear(ClearMask::Color | ClearMask::Depth);
  108. m_fbos.Last().m1->Unbind();
  109. }
  110. temp_buffer = new FrameBuffer(Video::GetSize());
  111. temp_buffer->Bind();
  112. Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  113. Video::SetClearDepth(1.f);
  114. Video::Clear(ClearMask::Color | ClearMask::Depth);
  115. temp_buffer->Unbind();
  116. m_ready = true;
  117. /* FIXME: this object never cleans up */
  118. //SRC SETUP
  119. int i = 4;
  120. while (i-- > 0)
  121. {
  122. voronoi_points.Push(vec3(rand<float>(512.f), rand<float>(512.f), .0f),
  123. vec2(64.f + rand<float>(64.f), 64.f + rand<float>(64.f)));
  124. }
  125. //voronoi_points.Push(vec3(511.f, 511.f, .0f), vec2(-128.f));
  126. //voronoi_points.Push(vec3(256.f, 256.f, .0f), vec2( 128.f));
  127. //voronoi_points.Push(vec3(128.f, 128.f, .0f), vec2(-128.f));
  128. //voronoi_points.Push(vec3(128.f, 400.f, .0f), vec2( 128.f));
  129. //voronoi_points.Push(vec3(400.f, 24.f, .0f), vec2(-128.f));
  130. m_cur_fbo = voronoi;
  131. }
  132. {
  133. //Shutdown logic
  134. if (Input::WasReleased(Key::O))
  135. voronoi_points.Pop();
  136. else if (Input::WasReleased(Key::P))
  137. voronoi_points.Push(vec3(rand<float>(512.f), rand<float>(512.f), .0f),
  138. vec2(64.f + rand<float>(64.f), 64.f + rand<float>(64.f)));
  139. else if (Input::WasReleased(Key::F1))
  140. m_cur_fbo = src_voronoi;
  141. else if (Input::WasReleased(Key::F2))
  142. m_cur_fbo = voronoi;
  143. }
  144. temp_buffer->Bind();
  145. Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  146. Video::SetClearDepth(1.f);
  147. Video::Clear(ClearMask::Color | ClearMask::Depth);
  148. temp_buffer->Unbind();
  149. {
  150. vec2 limit(1.f, 511.f);
  151. //SRC SETUP
  152. for (int j = 0; j < voronoi_points.Count(); ++j)
  153. {
  154. voronoi_points[j].m1 = vec3(voronoi_points[j].m1.xy + voronoi_points[j].m2 * seconds, voronoi_points[j].m1.z);
  155. if (voronoi_points[j].m1.x >= limit.y || voronoi_points[j].m1.x <= limit.x)
  156. {
  157. voronoi_points[j].m2.x *= -1.f;
  158. voronoi_points[j].m1.x = clamp(voronoi_points[j].m1.x, limit.x, limit.y);
  159. }
  160. if (voronoi_points[j].m1.y >= limit.y || voronoi_points[j].m1.y <= limit.x)
  161. {
  162. voronoi_points[j].m2.y *= -1.f;
  163. voronoi_points[j].m1.y = clamp(voronoi_points[j].m1.y, limit.x, limit.y);
  164. }
  165. voronoi_points[j].m1.z = ((float)j + 1) / ((float)voronoi_points.Count());
  166. }
  167. int f = src_voronoi;
  168. m_fbos[f].m1->Bind();
  169. Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  170. Video::SetClearDepth(1.f);
  171. Video::Clear(ClearMask::Color | ClearMask::Depth);
  172. m_fbos[f].m1->Unbind();
  173. int buf = voronoi_points.Count() % 2;
  174. for (int j = 0; j < voronoi_points.Count(); ++j)
  175. {
  176. FrameBuffer *dst_buf;
  177. FrameBuffer *src_buf;
  178. if (buf)
  179. {
  180. dst_buf = m_fbos[f].m1;
  181. src_buf = temp_buffer;
  182. }
  183. else
  184. {
  185. src_buf = m_fbos[f].m1;
  186. dst_buf = temp_buffer;
  187. }
  188. dst_buf->Bind();
  189. /* FIXME: we should just disable depth test in the shader */
  190. Video::Clear(ClearMask::Depth);
  191. m_fbos[f].m2->Bind();
  192. int i = 0;
  193. m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], src_buf->GetTexture(), 0); //"in_texture"
  194. m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], voronoi_points[j].m1); //"in_source_point"
  195. m_fbos[f].m2->SetUniform(m_fbos[f].m3[i++], vec2(512.f, 512.f)); //"in_screen_res"
  196. m_vdecl->SetStream(m_vbo, m_fbos[f].m4.Last());
  197. m_vdecl->Bind();
  198. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  199. m_vdecl->Unbind();
  200. m_fbos[f].m2->Unbind();
  201. dst_buf->Unbind();
  202. buf = 1 - buf;
  203. }
  204. }
  205. Video::Clear(ClearMask::Color | ClearMask::Depth);
  206. //FRAME BUFFER DRAW
  207. m_timer -= seconds;
  208. if (m_timer < .0f && m_cur_fbo != src_voronoi)
  209. {
  210. //m_timer = 1.0f;
  211. m_fbos[m_cur_fbo].m1->Bind();
  212. Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  213. Video::SetClearDepth(1.f);
  214. Video::Clear(ClearMask::Color | ClearMask::Depth);
  215. m_fbos[m_cur_fbo].m1->Unbind();
  216. ivec2 curres = ivec2(512, 512) / 2;
  217. int buf = 0;
  218. while (1)
  219. {
  220. FrameBuffer *dst_buf;
  221. FrameBuffer *src_buf;
  222. Shader *shader;
  223. if (curres == ivec2(0))
  224. shader = m_screen_shader;
  225. else
  226. shader = m_fbos[m_cur_fbo].m2;
  227. if (curres.x == 256)
  228. src_buf = m_fbos[src_voronoi].m1;
  229. else if (buf)
  230. src_buf = m_fbos[m_cur_fbo].m1;
  231. else
  232. src_buf = temp_buffer;
  233. if (buf)
  234. dst_buf = temp_buffer;
  235. else
  236. dst_buf = m_fbos[m_cur_fbo].m1;
  237. dst_buf->Bind();
  238. /* FIXME: we should just disable depth test in the shader */
  239. Video::Clear(ClearMask::Depth);
  240. shader->Bind();
  241. //08_FBO ??
  242. #if _XBOX
  243. /* FIXME: the Xbox enforces full EDRAM clears on each frame, so
  244. * we cannot expect the render target contents to be preserved.
  245. * This code snippet should be moved inside the FrameBuffer class. */
  246. //m_fbos[m_cur_fbo].m2->SetUniform(m_uni_flag, 1.f);
  247. //m_fbos[m_cur_fbo].m2->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0);
  248. //m_vdecl->SetStream(m_vbo, m_fbos[m_cur_fbo].m4.Last());
  249. //m_vdecl->Bind();
  250. //m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  251. //m_vdecl->Unbind();
  252. #endif
  253. int i = 0;
  254. if (curres == ivec2(0))
  255. m_screen_shader->SetUniform(m_screen_texture, src_buf->GetTexture(), 0);
  256. else if (m_cur_fbo == voronoi)
  257. {
  258. shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], src_buf->GetTexture(), 0); //"in_texture"
  259. shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], ((float)curres.x) / 512.f); //"in_step"
  260. shader->SetUniform(m_fbos[m_cur_fbo].m3[i++], vec2(512.f, 512.f)); //"in_screen_res"
  261. }
  262. m_vdecl->SetStream(m_vbo, m_fbos[m_cur_fbo].m4.Last());
  263. m_vdecl->Bind();
  264. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  265. m_vdecl->Unbind();
  266. m_fbos[m_cur_fbo].m2->Unbind();
  267. dst_buf->Unbind();
  268. if (curres == ivec2(0))
  269. break;
  270. if (curres == ivec2(1))
  271. {
  272. if (buf == 1)
  273. curres = ivec2(0);
  274. else
  275. break;
  276. }
  277. buf = 1 - buf;
  278. curres /= 2;
  279. }
  280. }
  281. //SCREEN DRAW
  282. m_screen_shader->Bind();
  283. m_screen_shader->SetUniform(m_screen_texture, m_fbos[m_cur_fbo].m1->GetTexture(), 0);
  284. m_vdecl->SetStream(m_vbo, m_screen_coord);
  285. m_vdecl->Bind();
  286. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  287. m_vdecl->Unbind();
  288. m_screen_shader->Unbind();
  289. }
  290. private:
  291. Array<vec3, vec2> voronoi_points;
  292. Array<vec2> m_vertices;
  293. Shader *m_screen_shader;
  294. ShaderAttrib m_screen_coord;
  295. ShaderUniform m_screen_texture;
  296. VertexDeclaration *m_vdecl;
  297. VertexBuffer *m_vbo;
  298. Array<FrameBuffer *, Shader *, Array<ShaderUniform>, Array<ShaderAttrib> > m_fbos;
  299. FrameBuffer *temp_buffer;
  300. int m_cur_fbo;
  301. double m_time;
  302. vec3 m_hotspot, m_color;
  303. bool m_ready;
  304. float m_timer;
  305. };
  306. int main(int argc, char **argv)
  307. {
  308. System::Init(argc, argv);
  309. Application app("Tutorial 12: Jump Flooding Algorithm & Voronoi", ivec2(512, 512), 60.0f);
  310. new Voronoi();
  311. app.Run();
  312. return EXIT_SUCCESS;
  313. }