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.

126 lines
3.4 KiB

  1. //
  2. // Lol Engine — Graphing tutorial
  3. //
  4. // Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include "loldebug.h"
  17. using namespace lol;
  18. static int const TEXTURE_WIDTH = 256;
  19. LOLFX_RESOURCE_DECLARE(04_texture);
  20. class TextureDemo : public WorldEntity
  21. {
  22. public:
  23. TextureDemo()
  24. : m_vertices({ vec2(-1.0, 1.0),
  25. vec2(-1.0, -1.0),
  26. vec2( 1.0, -1.0),
  27. vec2(-1.0, 1.0),
  28. vec2( 1.0, -1.0),
  29. vec2( 1.0, 1.0), }),
  30. m_frames(0),
  31. m_ready(false)
  32. {
  33. m_heightmap.resize(TEXTURE_WIDTH * 1);
  34. }
  35. virtual void TickGame(float seconds)
  36. {
  37. WorldEntity::TickGame(seconds);
  38. /* Generate a new heightmap at the beginning */
  39. if (m_frames == 0)
  40. memset(m_heightmap.data(), 255, m_heightmap.bytes());
  41. /* Scroll left */
  42. for (int i = 0; i < m_heightmap.count() - 1; i++)
  43. m_heightmap[i] = m_heightmap[i + 1];
  44. int height = m_heightmap.last();
  45. height = (int)(height + 127 + 40 * lol::sin(m_frames * 0.03) + rand() % 97 - 38) / 2;
  46. height = std::max(15, std::min(height, 240));
  47. m_heightmap.last() = height;
  48. /* Update frame counter */
  49. ++m_frames;
  50. }
  51. virtual void TickDraw(float seconds, Scene &scene)
  52. {
  53. WorldEntity::TickDraw(seconds, scene);
  54. /* Initialise GPU data */
  55. if (!m_ready)
  56. {
  57. m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);
  58. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
  59. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  60. m_texture_uni = m_shader->GetUniformLocation("u_texture");
  61. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  62. m_vbo = new VertexBuffer(m_vertices.bytes());
  63. void *vertices = m_vbo->Lock(0, 0);
  64. memcpy(vertices, &m_vertices[0], m_vertices.bytes());
  65. m_vbo->Unlock();
  66. m_ready = true;
  67. /* FIXME: this object never cleans up */
  68. }
  69. /* Send new heightmap to GPU */
  70. m_texture->Bind();
  71. m_texture->SetData(m_heightmap.data());
  72. m_shader->Bind();
  73. m_shader->SetUniform(m_texture_uni, m_texture->GetTextureUniform(), 0);
  74. m_vdecl->SetStream(m_vbo, m_coord);
  75. m_vdecl->Bind();
  76. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  77. m_vdecl->Unbind();
  78. }
  79. private:
  80. array<vec2> m_vertices;
  81. Texture *m_texture;
  82. Shader *m_shader;
  83. ShaderAttrib m_coord;
  84. ShaderUniform m_texture_uni;
  85. VertexDeclaration *m_vdecl;
  86. VertexBuffer *m_vbo;
  87. array<uint8_t> m_heightmap;
  88. int m_frames;
  89. bool m_ready;
  90. };
  91. int main(int argc, char **argv)
  92. {
  93. sys::init(argc, argv);
  94. Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);
  95. //new DebugFps(50, 50);
  96. new TextureDemo();
  97. app.Run();
  98. return EXIT_SUCCESS;
  99. }