Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

125 linhas
3.3 KiB

  1. //
  2. // Lol Engine — Graphing tutorial
  3. //
  4. // Copyright © 2012—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program 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->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. private:
  79. array<vec2> m_vertices;
  80. Texture *m_texture;
  81. Shader *m_shader;
  82. ShaderAttrib m_coord;
  83. ShaderUniform m_texture_uni;
  84. VertexDeclaration *m_vdecl;
  85. VertexBuffer *m_vbo;
  86. array<uint8_t> m_heightmap;
  87. int m_frames;
  88. bool m_ready;
  89. };
  90. int main(int argc, char **argv)
  91. {
  92. System::Init(argc, argv);
  93. Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);
  94. //new DebugFps(50, 50);
  95. new TextureDemo();
  96. app.Run();
  97. return EXIT_SUCCESS;
  98. }