選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

148 行
4.5 KiB

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