Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

147 rader
4.5 KiB

  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 "core.h"
  14. #include "loldebug.h"
  15. using namespace std;
  16. using namespace lol;
  17. LOLFX_RESOURCE_DECLARE(08_fbo);
  18. class FBO : public WorldEntity
  19. {
  20. public:
  21. FBO()
  22. {
  23. m_vertices << vec2( 1.0, 1.0);
  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_ready = false;
  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)
  46. {
  47. WorldEntity::TickDraw(seconds);
  48. if (!m_ready)
  49. {
  50. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(08_fbo));
  51. m_coord = m_shader->GetAttribLocation("in_Position", 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("in_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. Video::SetClearColor(vec4(0.f, 0.f, 0.f, 1.f));
  64. Video::SetClearDepth(1.f);
  65. Video::Clear(ClearMask::Color | ClearMask::Depth);
  66. m_fbo->Unbind();
  67. m_ready = true;
  68. /* FIXME: this object never cleans up */
  69. }
  70. m_fbo->Bind();
  71. /* FIXME: we should just disable depth test in the shader */
  72. Video::Clear(ClearMask::Depth);
  73. m_shader->Bind();
  74. #if _XBOX
  75. /* FIXME: the Xbox enforces full EDRAM clears on each frame, so
  76. * we cannot expect the render target contents to be preserved.
  77. * This code snippet should be moved inside the Framebuffer class. */
  78. m_shader->SetUniform(m_uni_flag, 1.f);
  79. m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0);
  80. m_vdecl->SetStream(m_vbo, m_coord);
  81. m_vdecl->Bind();
  82. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  83. m_vdecl->Unbind();
  84. #endif
  85. m_shader->SetUniform(m_uni_flag, 0.f);
  86. m_shader->SetUniform(m_uni_point, m_hotspot);
  87. m_shader->SetUniform(m_uni_color, m_color);
  88. m_vdecl->SetStream(m_vbo, m_coord);
  89. m_vdecl->Bind();
  90. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  91. m_vdecl->Unbind();
  92. m_shader->Unbind();
  93. m_fbo->Unbind();
  94. m_shader->Bind();
  95. m_shader->SetUniform(m_uni_flag, 1.f);
  96. m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0);
  97. m_vdecl->SetStream(m_vbo, m_coord);
  98. m_vdecl->Bind();
  99. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  100. m_vdecl->Unbind();
  101. m_shader->Unbind();
  102. }
  103. private:
  104. Array<vec2> m_vertices;
  105. Shader *m_shader;
  106. ShaderAttrib m_coord;
  107. ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
  108. VertexDeclaration *m_vdecl;
  109. VertexBuffer *m_vbo;
  110. Framebuffer *m_fbo;
  111. double m_time;
  112. vec3 m_hotspot, m_color;
  113. bool m_ready;
  114. };
  115. int main(int argc, char **argv)
  116. {
  117. System::Init(argc, argv);
  118. Application app("Tutorial 08: Framebuffer Object", ivec2(640, 480), 60.0f);
  119. new FBO();
  120. app.Run();
  121. return EXIT_SUCCESS;
  122. }