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.

03_noise.cpp 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_time(0.0),
  23. m_ready(false)
  24. {
  25. m_vertices << vec2(-1.0, 1.0);
  26. m_vertices << vec2(-1.0, -1.0);
  27. m_vertices << vec2( 1.0, -1.0);
  28. m_vertices << vec2(-1.0, 1.0);
  29. m_vertices << vec2( 1.0, -1.0);
  30. m_vertices << vec2( 1.0, 1.0);
  31. m_ready = false;
  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. }