25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

92 satır
2.2 KiB

  1. //
  2. // Lol Engine - Noise tutorial
  3. //
  4. // Copyright: (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <lol/main.h>
  14. #include "loldebug.h"
  15. using namespace lol;
  16. LOLFX_RESOURCE_DECLARE(03_noise);
  17. class NoiseDemo : public WorldEntity
  18. {
  19. public:
  20. NoiseDemo()
  21. : m_vertices({ vec2(-1.0, 1.0),
  22. vec2(-1.0, -1.0),
  23. vec2( 1.0, -1.0),
  24. vec2(-1.0, 1.0),
  25. vec2( 1.0, -1.0),
  26. vec2( 1.0, 1.0), }),
  27. m_time(0.0),
  28. m_ready(false)
  29. {
  30. }
  31. virtual void TickDraw(float seconds, Scene &scene)
  32. {
  33. WorldEntity::TickDraw(seconds, scene);
  34. m_time += seconds;
  35. if (!m_ready)
  36. {
  37. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(03_noise));
  38. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  39. m_time_uni = m_shader->GetUniformLocation("u_time");
  40. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  41. m_vbo = new VertexBuffer(m_vertices.Bytes());
  42. void *vertices = m_vbo->Lock(0, 0);
  43. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  44. m_vbo->Unlock();
  45. m_ready = true;
  46. /* FIXME: this object never cleans up */
  47. }
  48. m_shader->Bind();
  49. m_shader->SetUniform(m_time_uni, m_time);
  50. m_vdecl->SetStream(m_vbo, m_coord);
  51. m_vdecl->Bind();
  52. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  53. m_vdecl->Unbind();
  54. }
  55. private:
  56. array<vec2> m_vertices;
  57. Shader *m_shader;
  58. ShaderAttrib m_coord;
  59. ShaderUniform m_time_uni;
  60. VertexDeclaration *m_vdecl;
  61. VertexBuffer *m_vbo;
  62. float m_time;
  63. bool m_ready;
  64. };
  65. int main(int argc, char **argv)
  66. {
  67. System::Init(argc, argv);
  68. Application app("Tutorial 3: Noise", ivec2(1280, 720), 60.0f);
  69. new NoiseDemo();
  70. app.Run();
  71. return EXIT_SUCCESS;
  72. }