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.
 
 
 

131 line
3.4 KiB

  1. //
  2. // Lol Engine — Graphing tutorial
  3. //
  4. // Copyright © 2012—2019 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. virtual bool init_game() override
  24. {
  25. /* Generate a new heightmap at the beginning */
  26. m_heightmap.resize(TEXTURE_WIDTH * 1);
  27. memset(m_heightmap.data(), 255, m_heightmap.bytes());
  28. return true;
  29. }
  30. virtual void tick_game(float seconds) override
  31. {
  32. WorldEntity::tick_game(seconds);
  33. /* Scroll left */
  34. for (int i = 0; i < m_heightmap.count() - 1; i++)
  35. m_heightmap[i] = m_heightmap[i + 1];
  36. int height = m_heightmap.last();
  37. height = (int)(height + 127 + 40 * lol::sin(m_frames * 0.03) + rand() % 97 - 38) / 2;
  38. height = std::max(15, std::min(height, 240));
  39. m_heightmap.last() = height;
  40. /* Update frame counter */
  41. ++m_frames;
  42. }
  43. virtual bool init_draw() override
  44. {
  45. array<vec2> vertices
  46. {
  47. vec2(-1.0, 1.0),
  48. vec2(-1.0, -1.0),
  49. vec2( 1.0, -1.0),
  50. vec2(-1.0, 1.0),
  51. vec2( 1.0, -1.0),
  52. vec2( 1.0, 1.0),
  53. };
  54. m_texture = std::make_shared<Texture>(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);
  55. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
  56. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  57. m_texture_uni = m_shader->GetUniformLocation("u_texture");
  58. m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
  59. m_vbo = std::make_shared<VertexBuffer>(vertices.bytes());
  60. void *data = m_vbo->Lock(0, 0);
  61. memcpy(data, vertices.data(), vertices.bytes());
  62. m_vbo->Unlock();
  63. return true;
  64. }
  65. virtual void tick_draw(float seconds, Scene &scene)
  66. {
  67. WorldEntity::tick_draw(seconds, scene);
  68. /* Send new heightmap to GPU */
  69. m_texture->Bind();
  70. m_texture->SetData(m_heightmap.data());
  71. m_shader->Bind();
  72. m_shader->SetUniform(m_texture_uni, m_texture->GetTextureUniform(), 0);
  73. m_vdecl->SetStream(m_vbo, m_coord);
  74. m_vdecl->Bind();
  75. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  76. m_vdecl->Unbind();
  77. }
  78. virtual bool release_draw() override
  79. {
  80. m_texture.reset();
  81. m_shader.reset();
  82. m_vdecl.reset();
  83. m_vbo.reset();
  84. return true;
  85. }
  86. private:
  87. std::shared_ptr<Texture> m_texture;
  88. std::shared_ptr<Shader> m_shader;
  89. ShaderAttrib m_coord;
  90. ShaderUniform m_texture_uni;
  91. std::shared_ptr<VertexDeclaration> m_vdecl;
  92. std::shared_ptr<VertexBuffer> m_vbo;
  93. array<uint8_t> m_heightmap;
  94. int m_frames = 0;
  95. };
  96. int main(int argc, char **argv)
  97. {
  98. sys::init(argc, argv);
  99. Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);
  100. new TextureDemo();
  101. app.Run();
  102. return EXIT_SUCCESS;
  103. }