選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123 行
3.2 KiB

  1. //
  2. // Lol Engine - Graphing 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 HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/engine.h>
  14. #include "loldebug.h"
  15. using namespace lol;
  16. static int const TEXTURE_WIDTH = 256;
  17. LOLFX_RESOURCE_DECLARE(04_texture);
  18. class TextureDemo : public WorldEntity
  19. {
  20. public:
  21. TextureDemo()
  22. : m_vertices({ vec2(-1.0, 1.0),
  23. vec2(-1.0, -1.0),
  24. vec2( 1.0, -1.0),
  25. vec2(-1.0, 1.0),
  26. vec2( 1.0, -1.0),
  27. vec2( 1.0, 1.0), }),
  28. m_frames(0),
  29. m_ready(false)
  30. {
  31. m_heightmap.Resize(TEXTURE_WIDTH * 1);
  32. }
  33. virtual void TickGame(float seconds)
  34. {
  35. WorldEntity::TickGame(seconds);
  36. /* Generate a new heightmap at the beginning */
  37. if (m_frames == 0)
  38. memset(m_heightmap.Data(), 255, m_heightmap.Bytes());
  39. /* Scroll left */
  40. for (int i = 0; i < m_heightmap.Count() - 1; i++)
  41. m_heightmap[i] = m_heightmap[i + 1];
  42. int height = m_heightmap.Last();
  43. height = (height + 127 + 40 * lol::sin(m_frames * 0.03) + rand() % 97 - 38) / 2;
  44. height = std::max(15, std::min(height, 240));
  45. m_heightmap.Last() = height;
  46. /* Update frame counter */
  47. ++m_frames;
  48. }
  49. virtual void TickDraw(float seconds, Scene &scene)
  50. {
  51. WorldEntity::TickDraw(seconds, scene);
  52. /* Initialise GPU data */
  53. if (!m_ready)
  54. {
  55. m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);
  56. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
  57. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  58. m_texture_uni = m_shader->GetUniformLocation("u_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_ready = true;
  65. /* FIXME: this object never cleans up */
  66. }
  67. /* Send new heightmap to GPU */
  68. m_texture->SetData(m_heightmap.Data());
  69. m_shader->Bind();
  70. m_shader->SetUniform(m_texture_uni, m_texture->GetTextureUniform(), 0);
  71. m_vdecl->SetStream(m_vbo, m_coord);
  72. m_vdecl->Bind();
  73. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  74. m_vdecl->Unbind();
  75. }
  76. private:
  77. array<vec2> m_vertices;
  78. Texture *m_texture;
  79. Shader *m_shader;
  80. ShaderAttrib m_coord;
  81. ShaderUniform m_texture_uni;
  82. VertexDeclaration *m_vdecl;
  83. VertexBuffer *m_vbo;
  84. array<uint8_t> m_heightmap;
  85. int m_frames;
  86. bool m_ready;
  87. };
  88. int main(int argc, char **argv)
  89. {
  90. System::Init(argc, argv);
  91. Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);
  92. //new DebugFps(50, 50);
  93. new TextureDemo();
  94. app.Run();
  95. return EXIT_SUCCESS;
  96. }