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.
 
 
 

140 lines
4.0 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::cos(m_time * 2.f) + lol::cos(m_time * 3.3f),
  40. lol::sin(m_time * 4.4f) + lol::sin(m_time * 3.2f),
  41. lol::sin(m_time * 5.f));
  42. m_color = 0.25f * vec3(3.f + lol::sin(m_time * 4.5f + 1.f),
  43. 3.f + lol::sin(m_time * 2.8f + 1.3f),
  44. 3.f + lol::sin(m_time * 3.7f));
  45. }
  46. virtual void TickDraw(float seconds)
  47. {
  48. WorldEntity::TickDraw(seconds);
  49. if (!m_ready)
  50. {
  51. m_shader = Shader::Create(lolfx_08_fbo);
  52. m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  53. m_uni_point = m_shader->GetUniformLocation("in_Point");
  54. m_uni_color = m_shader->GetUniformLocation("in_Color");
  55. m_uni_flag = m_shader->GetUniformLocation("in_Flag");
  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. glClearColor(0.0, 0.0, 0.0, 1.0f);
  65. glClearDepth(1.0f);
  66. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  67. m_fbo->Unbind();
  68. m_ready = true;
  69. /* FIXME: this object never cleans up */
  70. }
  71. m_fbo->Bind();
  72. /* FIXME: we should just disable depth test in the shader */
  73. glClear(GL_DEPTH_BUFFER_BIT);
  74. m_shader->Bind();
  75. m_shader->SetUniform(m_uni_flag, 0.f);
  76. m_shader->SetUniform(m_uni_point, m_hotspot);
  77. m_shader->SetUniform(m_uni_color, m_color);
  78. m_vdecl->SetStream(m_vbo, m_coord);
  79. m_vdecl->Bind();
  80. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
  81. m_vdecl->Unbind();
  82. m_shader->Unbind();
  83. m_fbo->Unbind();
  84. m_shader->Bind();
  85. m_shader->SetUniform(m_uni_flag, 1.f);
  86. m_shader->SetTexture(m_uni_texture, m_fbo->GetTexture(), 0);
  87. m_vdecl->SetStream(m_vbo, m_coord);
  88. m_vdecl->Bind();
  89. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
  90. m_vdecl->Unbind();
  91. m_shader->Unbind();
  92. }
  93. private:
  94. Array<vec2> m_vertices;
  95. Shader *m_shader;
  96. ShaderAttrib m_coord;
  97. ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
  98. VertexDeclaration *m_vdecl;
  99. VertexBuffer *m_vbo;
  100. FrameBuffer *m_fbo;
  101. double m_time;
  102. vec3 m_hotspot, m_color;
  103. bool m_ready;
  104. };
  105. int main(int argc, char **argv)
  106. {
  107. Application app("Tutorial 08: Framebuffer Object", ivec2(640, 480), 60.0f);
  108. #if defined _MSC_VER && !defined _XBOX
  109. _chdir("..");
  110. #elif defined _WIN32 && !defined _XBOX
  111. _chdir("../..");
  112. #endif
  113. new FBO();
  114. app.Run();
  115. return EXIT_SUCCESS;
  116. }