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.
 
 
 

146 line
4.3 KiB

  1. //
  2. // Lol Engine — Framebuffer Object tutorial
  3. //
  4. // Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
  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. using namespace lol;
  17. LOLFX_RESOURCE_DECLARE(08_fbo);
  18. class FBO : public WorldEntity
  19. {
  20. public:
  21. FBO()
  22. : m_vertices { vec2( 1.0, 1.0),
  23. vec2(-1.0, -1.0),
  24. vec2( 1.0, -1.0),
  25. vec2(-1.0, -1.0),
  26. vec2( 1.0, 1.0),
  27. vec2(-1.0, 1.0), },
  28. m_time(0.f),
  29. m_ready(false)
  30. {
  31. }
  32. virtual void TickGame(float seconds)
  33. {
  34. WorldEntity::TickGame(seconds);
  35. m_time += seconds;
  36. m_hotspot = 0.4f * vec3(
  37. lol::sin((float)m_time * 4.f) + lol::cos((float)m_time * 5.3f),
  38. lol::sin((float)m_time * 5.7f) + lol::cos((float)m_time * 4.4f),
  39. lol::sin((float)m_time * 5.f));
  40. m_color = 0.25f * vec3(1.1f + lol::sin((float)m_time * 2.5f + 1.f),
  41. 1.1f + lol::sin((float)m_time * 2.8f + 1.3f),
  42. 1.1f + lol::sin((float)m_time * 2.7f));
  43. /* Saturate dot color */
  44. float x = std::max(m_color.x, std::max(m_color.y, m_color.z));
  45. m_color /= x;
  46. }
  47. virtual void TickDraw(float seconds, Scene &scene)
  48. {
  49. WorldEntity::TickDraw(seconds, scene);
  50. if (!m_ready)
  51. {
  52. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(08_fbo));
  53. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  54. m_uni_flag = m_shader->GetUniformLocation("u_flag");
  55. m_uni_point = m_shader->GetUniformLocation("u_point");
  56. m_uni_color = m_shader->GetUniformLocation("u_color");
  57. m_uni_texture = m_shader->GetUniformLocation("u_texture");
  58. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  59. m_vbo = new VertexBuffer(m_vertices.bytes());
  60. void *vertices = m_vbo->Lock(0, 0);
  61. memcpy(vertices, &m_vertices[0], m_vertices.bytes());
  62. m_vbo->Unlock();
  63. m_fbo = new Framebuffer(Video::GetSize());
  64. m_fbo->Bind();
  65. {
  66. RenderContext rc;
  67. rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  68. rc.SetClearDepth(1.f);
  69. Renderer::Get()->Clear(ClearMask::Color | ClearMask::Depth);
  70. }
  71. m_fbo->Unbind();
  72. m_ready = true;
  73. /* FIXME: this object never cleans up */
  74. }
  75. /* FIXME: we should just disable depth test in the shader */
  76. RenderContext rc;
  77. rc.SetDepthFunc(DepthFunc::Disabled);
  78. /* FIXME: this no longer works because we don’t restore the
  79. * actually bound framebuffer. */
  80. m_fbo->Bind();
  81. m_shader->Bind();
  82. m_shader->SetUniform(m_uni_flag, 0.f);
  83. m_shader->SetUniform(m_uni_point, m_hotspot);
  84. m_shader->SetUniform(m_uni_color, m_color);
  85. m_vdecl->SetStream(m_vbo, m_coord);
  86. m_vdecl->Bind();
  87. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  88. m_vdecl->Unbind();
  89. m_shader->Unbind();
  90. m_fbo->Unbind();
  91. m_shader->Bind();
  92. m_shader->SetUniform(m_uni_flag, 1.f);
  93. m_shader->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0);
  94. m_vdecl->SetStream(m_vbo, m_coord);
  95. m_vdecl->Bind();
  96. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  97. m_vdecl->Unbind();
  98. m_shader->Unbind();
  99. }
  100. private:
  101. array<vec2> m_vertices;
  102. Shader *m_shader;
  103. ShaderAttrib m_coord;
  104. ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
  105. VertexDeclaration *m_vdecl;
  106. VertexBuffer *m_vbo;
  107. Framebuffer *m_fbo;
  108. double m_time;
  109. vec3 m_hotspot, m_color;
  110. bool m_ready;
  111. };
  112. int main(int argc, char **argv)
  113. {
  114. sys::init(argc, argv);
  115. Application app("Tutorial 08: Framebuffer Object", ivec2(512, 512), 60.0f);
  116. new FBO();
  117. app.Run();
  118. return EXIT_SUCCESS;
  119. }