25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

149 satır
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. 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. void *data = m_vbo->Lock(0, 0);
  57. memcpy(data, vertices.data(), vertices.bytes());
  58. m_vbo->Unlock();
  59. m_fbo = std::make_shared<Framebuffer>(Video::GetSize());
  60. m_fbo->Bind();
  61. {
  62. Scene& scene = Scene::GetScene();
  63. render_context rc(scene.get_renderer());
  64. rc.clear_color(vec4(0.f, 0.f, 0.f, 1.f));
  65. rc.clear_depth(1.f);
  66. scene.get_renderer()->Clear(ClearMask::Color | ClearMask::Depth);
  67. }
  68. m_fbo->Unbind();
  69. return true;
  70. }
  71. virtual void tick_draw(float seconds, Scene &scene) override
  72. {
  73. WorldEntity::tick_draw(seconds, scene);
  74. /* FIXME: we should just disable depth test in the shader */
  75. render_context rc(scene.get_renderer());
  76. rc.depth_func(DepthFunc::Disabled);
  77. /* FIXME: this no longer works because we don’t restore the
  78. * actually bound framebuffer. */
  79. m_fbo->Bind();
  80. m_shader->Bind();
  81. m_shader->SetUniform(m_uni_flag, 0.f);
  82. m_shader->SetUniform(m_uni_point, m_hotspot);
  83. m_shader->SetUniform(m_uni_color, m_color);
  84. m_vdecl->SetStream(m_vbo, m_coord);
  85. m_vdecl->Bind();
  86. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  87. m_vdecl->Unbind();
  88. m_shader->Unbind();
  89. m_fbo->Unbind();
  90. m_shader->Bind();
  91. m_shader->SetUniform(m_uni_flag, 1.f);
  92. m_shader->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0);
  93. m_vdecl->SetStream(m_vbo, m_coord);
  94. m_vdecl->Bind();
  95. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  96. m_vdecl->Unbind();
  97. m_shader->Unbind();
  98. }
  99. virtual bool release_draw() override
  100. {
  101. m_shader.reset();
  102. m_vdecl.reset();
  103. m_vbo.reset();
  104. m_fbo.reset();
  105. return true;
  106. }
  107. private:
  108. std::shared_ptr<Shader> m_shader;
  109. ShaderAttrib m_coord;
  110. ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
  111. std::shared_ptr<VertexDeclaration> m_vdecl;
  112. std::shared_ptr<VertexBuffer> m_vbo;
  113. std::shared_ptr<Framebuffer> m_fbo;
  114. double m_time = 0.0f;
  115. vec3 m_hotspot, m_color;
  116. };
  117. int main(int argc, char **argv)
  118. {
  119. sys::init(argc, argv);
  120. Application app("Tutorial 08: Framebuffer Object", ivec2(512, 512), 60.0f);
  121. new FBO();
  122. app.Run();
  123. return EXIT_SUCCESS;
  124. }