您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 * 1.5f + 1.f),
  42. 1.1f + lol::sin(m_time * 1.8f + 1.3f),
  43. 1.1f + lol::sin(m_time * 1.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. m_shader->SetUniform(m_uni_flag, 0.f);
  78. m_shader->SetUniform(m_uni_point, m_hotspot);
  79. m_shader->SetUniform(m_uni_color, m_color);
  80. m_vdecl->SetStream(m_vbo, m_coord);
  81. m_vdecl->Bind();
  82. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
  83. m_vdecl->Unbind();
  84. m_shader->Unbind();
  85. m_fbo->Unbind();
  86. m_shader->Bind();
  87. m_shader->SetUniform(m_uni_flag, 1.f);
  88. m_shader->SetUniform(m_uni_texture, m_fbo->GetTexture(), 0);
  89. m_vdecl->SetStream(m_vbo, m_coord);
  90. m_vdecl->Bind();
  91. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 2);
  92. m_vdecl->Unbind();
  93. m_shader->Unbind();
  94. }
  95. private:
  96. Array<vec2> m_vertices;
  97. Shader *m_shader;
  98. ShaderAttrib m_coord;
  99. ShaderUniform m_uni_flag, m_uni_point, m_uni_color, m_uni_texture;
  100. VertexDeclaration *m_vdecl;
  101. VertexBuffer *m_vbo;
  102. FrameBuffer *m_fbo;
  103. double m_time;
  104. vec3 m_hotspot, m_color;
  105. bool m_ready;
  106. };
  107. int main(int argc, char **argv)
  108. {
  109. Application app("Tutorial 08: Framebuffer Object", ivec2(640, 480), 60.0f);
  110. #if defined _MSC_VER && !defined _XBOX
  111. _chdir("..");
  112. #elif defined _WIN32 && !defined _XBOX
  113. _chdir("../..");
  114. #endif
  115. new FBO();
  116. app.Run();
  117. return EXIT_SUCCESS;
  118. }