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.

96 lines
2.4 KiB

  1. //
  2. // Lol Engine — Noise 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 <memory>
  16. #include <lol/engine.h>
  17. #include "loldebug.h"
  18. using namespace lol;
  19. LOLFX_RESOURCE_DECLARE(03_noise);
  20. class NoiseDemo : public WorldEntity
  21. {
  22. public:
  23. NoiseDemo()
  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_time(0.0),
  31. m_ready(false)
  32. {
  33. }
  34. virtual void tick_draw(float seconds, Scene &scene)
  35. {
  36. WorldEntity::tick_draw(seconds, scene);
  37. m_time += seconds;
  38. if (!m_ready)
  39. {
  40. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(03_noise));
  41. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  42. m_time_uni = m_shader->GetUniformLocation("u_time");
  43. m_vdecl = std::make_shared<VertexDeclaration>(VertexStream<vec2>(VertexUsage::Position));
  44. m_vbo = std::make_shared<VertexBuffer>(m_vertices.bytes());
  45. void *vertices = m_vbo->Lock(0, 0);
  46. memcpy(vertices, &m_vertices[0], m_vertices.bytes());
  47. m_vbo->Unlock();
  48. m_ready = true;
  49. /* FIXME: this object never cleans up */
  50. }
  51. m_shader->Bind();
  52. m_shader->SetUniform(m_time_uni, m_time);
  53. m_vdecl->SetStream(m_vbo, m_coord);
  54. m_vdecl->Bind();
  55. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  56. m_vdecl->Unbind();
  57. }
  58. private:
  59. array<vec2> m_vertices;
  60. std::shared_ptr<Shader> m_shader;
  61. ShaderAttrib m_coord;
  62. ShaderUniform m_time_uni;
  63. std::shared_ptr<VertexDeclaration> m_vdecl;
  64. std::shared_ptr<VertexBuffer> m_vbo;
  65. float m_time;
  66. bool m_ready;
  67. };
  68. int main(int argc, char **argv)
  69. {
  70. sys::init(argc, argv);
  71. Application app("Tutorial 3: Noise", ivec2(1280, 720), 60.0f);
  72. new NoiseDemo();
  73. app.Run();
  74. return EXIT_SUCCESS;
  75. }