選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

151 行
3.8 KiB

  1. //
  2. // Lol Engine - Noise 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://sam.zoy.org/projects/COPYING.WTFPL 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. extern char const *lolfx_04_texture;
  18. class TextureDemo : public WorldEntity
  19. {
  20. public:
  21. TextureDemo() :
  22. m_frames(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_heightmap = new uint8_t[4 * 512 * 1];
  32. }
  33. virtual ~TextureDemo()
  34. {
  35. delete m_heightmap;
  36. }
  37. virtual void TickGame(float seconds)
  38. {
  39. WorldEntity::TickGame(seconds);
  40. /* Generate a new heightmap every 400 frames */
  41. if (m_frames % 400 == 0)
  42. {
  43. for (int i = 0, height = 64; i < 512; i++)
  44. {
  45. m_heightmap[4 * i] = height;
  46. m_heightmap[4 * i + 1] = 255; /* unused */
  47. m_heightmap[4 * i + 2] = 255; /* unused */
  48. m_heightmap[4 * i + 3] = 255; /* unused */
  49. height += rand() % 17 - 8;
  50. height += rand() % 17 - 8;
  51. height = std::max(15, std::min(height, 240));
  52. }
  53. }
  54. /* Slightly disturb the terrain */
  55. for (int i = 1; i < 511; i++)
  56. {
  57. int delta = (rand() & 1) ? 1 : -1;
  58. if (rand() & 3)
  59. continue;
  60. uint8_t &center = m_heightmap[4 * i];
  61. uint8_t &side1 = m_heightmap[4 * (i - delta)];
  62. uint8_t &side2 = m_heightmap[4 * (i + delta)];
  63. if (center > side1)
  64. {
  65. center--;
  66. side1++;
  67. }
  68. else if (center > side2)
  69. {
  70. center--;
  71. side2++;
  72. }
  73. }
  74. /* Update frame counter */
  75. ++m_frames;
  76. }
  77. virtual void TickDraw(float seconds)
  78. {
  79. WorldEntity::TickDraw(seconds);
  80. /* Initialise GPU data */
  81. if (!m_ready)
  82. {
  83. m_texture = new Texture(ivec2(512, 1), PixelFormat::A8R8G8B8);
  84. m_shader = Shader::Create(lolfx_04_texture);
  85. m_coord = m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0);
  86. m_vdecl = new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position));
  87. m_vbo = new VertexBuffer(m_vertices.Bytes());
  88. void *vertices = m_vbo->Lock(0, 0);
  89. memcpy(vertices, &m_vertices[0], m_vertices.Bytes());
  90. m_vbo->Unlock();
  91. m_ready = true;
  92. /* FIXME: this object never cleans up */
  93. }
  94. /* Send new heightmap to GPU */
  95. m_texture->SetData(m_heightmap);
  96. m_shader->Bind();
  97. m_shader->SetUniform(m_texture_uni, m_texture->GetTexture(), 0);
  98. m_vdecl->SetStream(m_vbo, m_coord);
  99. m_vdecl->Bind();
  100. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  101. m_vdecl->Unbind();
  102. }
  103. private:
  104. Array<vec2> m_vertices;
  105. Texture *m_texture;
  106. Shader *m_shader;
  107. ShaderAttrib m_coord;
  108. ShaderUniform m_texture_uni;
  109. VertexDeclaration *m_vdecl;
  110. VertexBuffer *m_vbo;
  111. uint8_t *m_heightmap;
  112. int m_frames;
  113. bool m_ready;
  114. };
  115. int main(int argc, char **argv)
  116. {
  117. Application app("Tutorial 4: Texture", ivec2(640, 480), 60.0f);
  118. new TextureDemo();
  119. app.Run();
  120. return EXIT_SUCCESS;
  121. }