Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

130 righe
3.5 KiB

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