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.
 
 
 

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