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.
 
 
 

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