You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

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