您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

04_texture.cpp 3.2 KiB

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