Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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