Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

129 řádky
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. m_vbo->set_data(vertices.data(), vertices.bytes());
  61. return true;
  62. }
  63. virtual void tick_draw(float seconds, Scene &scene) override
  64. {
  65. WorldEntity::tick_draw(seconds, scene);
  66. /* Send new heightmap to GPU */
  67. m_texture->Bind();
  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->Bind();
  72. m_vdecl->SetStream(m_vbo, m_coord);
  73. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  74. m_vdecl->Unbind();
  75. }
  76. virtual bool release_draw() override
  77. {
  78. m_texture.reset();
  79. m_shader.reset();
  80. m_vdecl.reset();
  81. m_vbo.reset();
  82. return true;
  83. }
  84. private:
  85. std::shared_ptr<Texture> m_texture;
  86. std::shared_ptr<Shader> m_shader;
  87. ShaderAttrib m_coord;
  88. ShaderUniform m_texture_uni;
  89. std::shared_ptr<VertexDeclaration> m_vdecl;
  90. std::shared_ptr<VertexBuffer> m_vbo;
  91. array<uint8_t> m_heightmap;
  92. int m_frames = 0;
  93. };
  94. int main(int argc, char **argv)
  95. {
  96. sys::init(argc, argv);
  97. Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);
  98. new TextureDemo();
  99. app.Run();
  100. return EXIT_SUCCESS;
  101. }