Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

124 Zeilen
3.3 KiB

  1. //
  2. // Lol Engine - Graphing 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. static int const TEXTURE_WIDTH = 256;
  18. LOLFX_RESOURCE_DECLARE(04_texture);
  19. class TextureDemo : public WorldEntity
  20. {
  21. public:
  22. TextureDemo()
  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_frames(0),
  30. m_ready(false)
  31. {
  32. m_heightmap.Resize(TEXTURE_WIDTH * 1);
  33. }
  34. virtual void TickGame(float seconds)
  35. {
  36. WorldEntity::TickGame(seconds);
  37. /* Generate a new heightmap at the beginning */
  38. if (m_frames == 0)
  39. memset(m_heightmap.Data(), 255, m_heightmap.Bytes());
  40. /* Scroll left */
  41. for (int i = 0; i < m_heightmap.Count() - 1; i++)
  42. m_heightmap[i] = m_heightmap[i + 1];
  43. int height = m_heightmap.Last();
  44. height = (height + 127 + 40 * lol::sin(m_frames * 0.03) + rand() % 97 - 38) / 2;
  45. height = std::max(15, std::min(height, 240));
  46. m_heightmap.Last() = height;
  47. /* Update frame counter */
  48. ++m_frames;
  49. }
  50. virtual void TickDraw(float seconds, Scene &scene)
  51. {
  52. WorldEntity::TickDraw(seconds, scene);
  53. /* Initialise GPU data */
  54. if (!m_ready)
  55. {
  56. m_texture = new Texture(ivec2(TEXTURE_WIDTH, 1), PixelFormat::Y_8);
  57. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(04_texture));
  58. m_coord = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  59. m_texture_uni = m_shader->GetUniformLocation("u_Texture");
  60. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  61. m_vbo = new VertexBuffer(m_vertices.Bytes());
  62. void *vertices = m_vbo->Lock(0, 0);
  63. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  64. m_vbo->Unlock();
  65. m_ready = true;
  66. /* FIXME: this object never cleans up */
  67. }
  68. /* Send new heightmap to GPU */
  69. m_texture->SetData(m_heightmap.Data());
  70. m_shader->Bind();
  71. m_shader->SetUniform(m_texture_uni, m_texture->GetTextureUniform(), 0);
  72. m_vdecl->SetStream(m_vbo, m_coord);
  73. m_vdecl->Bind();
  74. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  75. m_vdecl->Unbind();
  76. }
  77. private:
  78. Array<vec2> m_vertices;
  79. Texture *m_texture;
  80. Shader *m_shader;
  81. ShaderAttrib m_coord;
  82. ShaderUniform m_texture_uni;
  83. VertexDeclaration *m_vdecl;
  84. VertexBuffer *m_vbo;
  85. Array<uint8_t> m_heightmap;
  86. int m_frames;
  87. bool m_ready;
  88. };
  89. int main(int argc, char **argv)
  90. {
  91. System::Init(argc, argv);
  92. Application app("Tutorial 4: Texture", ivec2(1280, 720), 60.0f);
  93. //new DebugFps(50, 50);
  94. new TextureDemo();
  95. app.Run();
  96. return EXIT_SUCCESS;
  97. }