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.

04_texture.cpp 3.2 KiB

12 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // Lol Engine - Graphing tutorial
  3. //
  4. // Copyright: (c) 2012 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. extern char const *lolfx_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[4 * 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, 4 * TEXTURE_WIDTH);
  44. /* Scroll left */
  45. for (int i = 0; i < TEXTURE_WIDTH - 1; i++)
  46. m_heightmap[4 * i] = m_heightmap[4 * i + 4];
  47. int height = m_heightmap[4 * (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[4 * (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::A8R8G8B8);
  61. m_shader = Shader::Create(lolfx_04_texture);
  62. m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  63. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  64. m_vbo = new VertexBuffer(m_vertices.Bytes());
  65. void *vertices = m_vbo->Lock(0, 0);
  66. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  67. m_vbo->Unlock();
  68. m_ready = true;
  69. /* FIXME: this object never cleans up */
  70. }
  71. /* Send new heightmap to GPU */
  72. m_texture->SetData(m_heightmap);
  73. m_shader->Bind();
  74. m_shader->SetUniform(m_texture_uni, m_texture->GetTexture(), 0);
  75. m_vdecl->SetStream(m_vbo, m_coord);
  76. m_vdecl->Bind();
  77. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  78. m_vdecl->Unbind();
  79. }
  80. private:
  81. Array<vec2> m_vertices;
  82. Texture *m_texture;
  83. Shader *m_shader;
  84. ShaderAttrib m_coord;
  85. ShaderUniform m_texture_uni;
  86. VertexDeclaration *m_vdecl;
  87. VertexBuffer *m_vbo;
  88. uint8_t *m_heightmap;
  89. int m_frames;
  90. bool m_ready;
  91. };
  92. int main(int argc, char **argv)
  93. {
  94. Application app("Tutorial 4: Texture", ivec2(640, 480), 60.0f);
  95. new TextureDemo();
  96. app.Run();
  97. return EXIT_SUCCESS;
  98. }