Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <lol/main.h>
  14. #include "loldebug.h"
  15. using namespace lol;
  16. LOLFX_RESOURCE_DECLARE(08_fbo);
  17. class FBO : public WorldEntity
  18. {
  19. public:
  20. FBO()
  21. : m_time(0.f),
  22. m_ready(false)
  23. {
  24. m_vertices << vec2( 1.0, 1.0);
  25. m_vertices << vec2(-1.0, -1.0);
  26. m_vertices << vec2( 1.0, -1.0);
  27. m_vertices << vec2(-1.0, -1.0);
  28. m_vertices << vec2( 1.0, 1.0);
  29. m_vertices << vec2(-1.0, 1.0);
  30. }
  31. virtual void TickGame(float seconds)
  32. {
  33. WorldEntity::TickGame(seconds);
  34. m_time += seconds;
  35. m_hotspot = 0.4f * vec3(lol::sin(m_time * 4.f) + lol::cos(m_time * 5.3f),
  36. lol::sin(m_time * 5.7f) + lol::cos(m_time * 4.4f),
  37. lol::sin(m_time * 5.f));
  38. m_color = 0.25f * vec3(1.1f + lol::sin(m_time * 2.5f + 1.f),
  39. 1.1f + lol::sin(m_time * 2.8f + 1.3f),
  40. 1.1f + lol::sin(m_time * 2.7f));
  41. /* Saturate dot color */
  42. float x = std::max(m_color.x, std::max(m_color.y, m_color.z));
  43. m_color /= x;
  44. }
  45. virtual void TickDraw(float seconds, Scene &scene)
  46. {
  47. WorldEntity::TickDraw(seconds, scene);
  48. if (!m_ready)
  49. {
  50. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(08_fbo));
  51. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  52. m_uni_flag = m_shader->GetUniformLocation("in_Flag");
  53. m_uni_point = m_shader->GetUniformLocation("in_Point");
  54. m_uni_color = m_shader->GetUniformLocation("in_Color");
  55. m_uni_texture = m_shader->GetUniformLocation("u_texture");
  56. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  57. m_vbo = new VertexBuffer(m_vertices.Bytes());
  58. void *vertices = m_vbo->Lock(0, 0);
  59. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  60. m_vbo->Unlock();
  61. m_fbo = new Framebuffer(Video::GetSize());
  62. m_fbo->Bind();
  63. {
  64. RenderContext rc;
  65. rc.SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  66. rc.SetClearDepth(1.f);
  67. g_renderer->Clear(ClearMask::Color | ClearMask::Depth);
  68. }
  69. m_fbo->Unbind();
  70. m_ready = true;
  71. /* FIXME: this object never cleans up */
  72. }
  73. /* FIXME: we should just disable depth test in the shader */
  74. RenderContext rc;
  75. rc.SetDepthFunc(DepthFunc::Disabled);
  76. m_fbo->Bind();
  77. m_shader->Bind();
  78. #if _XBOX
  79. /* FIXME: the Xbox enforces full EDRAM clears on each frame, so
  80. * we cannot expect the render target contents to be preserved.
  81. * This code snippet should be moved inside the Framebuffer class. */
  82. m_shader->SetUniform(m_uni_flag, 1.f);
  83. m_shader->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0);
  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. #endif
  89. m_shader->SetUniform(m_uni_flag, 0.f);
  90. m_shader->SetUniform(m_uni_point, m_hotspot);
  91. m_shader->SetUniform(m_uni_color, m_color);
  92. m_vdecl->SetStream(m_vbo, m_coord);
  93. m_vdecl->Bind();
  94. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  95. m_vdecl->Unbind();
  96. m_shader->Unbind();
  97. m_fbo->Unbind();
  98. m_shader->Bind();
  99. m_shader->SetUniform(m_uni_flag, 1.f);
  100. m_shader->SetUniform(m_uni_texture, m_fbo->GetTextureUniform(), 0);
  101. m_vdecl->SetStream(m_vbo, m_coord);
  102. m_vdecl->Bind();
  103. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  104. m_vdecl->Unbind();
  105. m_shader->Unbind();
  106. }
  107. private:
  108. array<vec2> m_vertices;
  109. Shader *m_shader;
  110. ShaderAttrib m_coord;
  111. ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
  112. VertexDeclaration *m_vdecl;
  113. VertexBuffer *m_vbo;
  114. Framebuffer *m_fbo;
  115. double m_time;
  116. vec3 m_hotspot, m_color;
  117. bool m_ready;
  118. };
  119. int main(int argc, char **argv)
  120. {
  121. System::Init(argc, argv);
  122. Application app("Tutorial 08: Framebuffer Object", ivec2(512, 512), 60.0f);
  123. new FBO();
  124. app.Run();
  125. return EXIT_SUCCESS;
  126. }