選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

94 行
2.3 KiB

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