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 line
4.7 KiB

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