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.

143 lines
4.2 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. #include "lolgl.h"
  16. using namespace std;
  17. using namespace lol;
  18. #if defined _WIN32
  19. # include <direct.h>
  20. #endif
  21. extern char const *lolfx_08_fbo;
  22. class FBO : public WorldEntity
  23. {
  24. public:
  25. FBO()
  26. {
  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_vertices << vec2(-1.0, 1.0);
  33. m_ready = false;
  34. }
  35. virtual void TickGame(float seconds)
  36. {
  37. WorldEntity::TickGame(seconds);
  38. m_time += seconds;
  39. m_hotspot = 0.4f * vec3(lol::sin(m_time * 4.f) + lol::cos(m_time * 5.3f),
  40. lol::sin(m_time * 5.7f) + lol::cos(m_time * 4.4f),
  41. lol::sin(m_time * 5.f));
  42. m_color = 0.25f * vec3(1.1f + lol::sin(m_time * 1.5f + 1.f),
  43. 1.1f + lol::sin(m_time * 1.8f + 1.3f),
  44. 1.1f + lol::sin(m_time * 1.7f));
  45. /* Saturate dot color */
  46. float x = std::max(m_color.x, std::max(m_color.y, m_color.z));
  47. m_color /= x;
  48. }
  49. virtual void TickDraw(float seconds)
  50. {
  51. WorldEntity::TickDraw(seconds);
  52. if (!m_ready)
  53. {
  54. m_shader = Shader::Create(lolfx_08_fbo);
  55. m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  56. m_uni_flag = m_shader->GetUniformLocation("in_Flag");
  57. m_uni_point = m_shader->GetUniformLocation("in_Point");
  58. m_uni_color = m_shader->GetUniformLocation("in_Color");
  59. m_uni_texture = m_shader->GetUniformLocation("in_Texture");
  60. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  61. m_vbo = new VertexBuffer(m_vertices.Bytes());
  62. void *vertices = m_vbo->Lock(0, 0);
  63. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  64. m_vbo->Unlock();
  65. m_fbo = new FrameBuffer(Video::GetSize());
  66. m_fbo->Bind();
  67. glClearColor(0.0, 0.0, 0.0, 1.0f);
  68. glClearDepth(1.0f);
  69. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  70. m_fbo->Unbind();
  71. m_ready = true;
  72. /* FIXME: this object never cleans up */
  73. }
  74. m_fbo->Bind();
  75. /* FIXME: we should just disable depth test in the shader */
  76. glClear(GL_DEPTH_BUFFER_BIT);
  77. m_shader->Bind();
  78. m_shader->SetUniform(m_uni_flag, 0.f);
  79. m_shader->SetUniform(m_uni_point, m_hotspot);
  80. m_shader->SetUniform(m_uni_color, m_color);
  81. m_vdecl->SetStream(m_vbo, m_coord);
  82. m_vdecl->Bind();
  83. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
  84. m_vdecl->Unbind();
  85. m_shader->Unbind();
  86. m_fbo->Unbind();
  87. m_shader->Bind();
  88. m_shader->SetUniform(m_uni_flag, 1.f);
  89. m_shader->SetTexture(m_uni_texture, m_fbo->GetTexture(), 0);
  90. m_vdecl->SetStream(m_vbo, m_coord);
  91. m_vdecl->Bind();
  92. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
  93. m_vdecl->Unbind();
  94. m_shader->Unbind();
  95. }
  96. private:
  97. Array<vec2> m_vertices;
  98. Shader *m_shader;
  99. ShaderAttrib m_coord;
  100. ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
  101. VertexDeclaration *m_vdecl;
  102. VertexBuffer *m_vbo;
  103. FrameBuffer *m_fbo;
  104. double m_time;
  105. vec3 m_hotspot, m_color;
  106. bool m_ready;
  107. };
  108. int main(int argc, char **argv)
  109. {
  110. Application app("Tutorial 08: Framebuffer Object", ivec2(640, 480), 60.0f);
  111. #if defined _MSC_VER && !defined _XBOX
  112. _chdir("..");
  113. #elif defined _WIN32 && !defined _XBOX
  114. _chdir("../..");
  115. #endif
  116. new FBO();
  117. app.Run();
  118. return EXIT_SUCCESS;
  119. }