hextile.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2022-2022 Preetish Kakkar. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <bx/allocator.h>
  6. #include <bx/debug.h>
  7. #include <bx/math.h>
  8. #include "common.h"
  9. #include "bgfx_utils.h"
  10. #include "imgui/imgui.h"
  11. namespace
  12. {
  13. struct PosTextCoord0Vertex
  14. {
  15. float m_x;
  16. float m_y;
  17. float m_z;
  18. float m_u;
  19. float m_v;
  20. static void init()
  21. {
  22. ms_layout
  23. .begin()
  24. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  25. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  26. .end();
  27. }
  28. static bgfx::VertexLayout ms_layout;
  29. };
  30. bgfx::VertexLayout PosTextCoord0Vertex::ms_layout;
  31. static PosTextCoord0Vertex s_screenSpaceQuadVertices[] =
  32. {
  33. {-1.0f, 0.0f, -1.0f, 0.0, 0.0 },
  34. {-1.0f, 0.0f, 1.0f, 0.0, 1.0 },
  35. { 1.0f, 0.0f, -1.0f, 1.0, 0.0 },
  36. { 1.0f, 0.0f, 1.0f, 1.0, 1.0 },
  37. };
  38. static const uint16_t s_screenSpaceQuadIndices[] =
  39. {
  40. 2, 3, 1,
  41. 0, 2, 1,
  42. };
  43. struct HextileData
  44. {
  45. bool m_showWeights = false;
  46. int m_tileRate = 10;
  47. float m_tileRotationStrength = 0.0f;
  48. bool m_useRegularTiling = false;
  49. bool m_pauseAnimation;
  50. };
  51. class ExampleHextile : public entry::AppI
  52. {
  53. public:
  54. ExampleHextile(const char* _name, const char* _description, const char* _url)
  55. : entry::AppI(_name, _description, _url)
  56. , m_width(0)
  57. , m_height(0)
  58. , m_debug(BGFX_DEBUG_NONE)
  59. , m_reset()
  60. {
  61. }
  62. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  63. {
  64. Args args(_argc, _argv);
  65. m_width = _width;
  66. m_height = _height;
  67. m_debug = BGFX_DEBUG_NONE;
  68. m_reset = BGFX_RESET_VSYNC;
  69. bgfx::Init init;
  70. init.type = args.m_type;
  71. init.vendorId = args.m_pciId;
  72. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  73. init.platformData.ndt = entry::getNativeDisplayHandle();
  74. init.resolution.width = m_width;
  75. init.resolution.height = m_height;
  76. init.resolution.reset = m_reset;
  77. bgfx::init(init);
  78. // Enable m_debug text.
  79. bgfx::setDebug(m_debug);
  80. // Set view 0 clear state.
  81. bgfx::setViewClear(0
  82. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  83. , 0x303030ff
  84. , 1.0f
  85. , 0
  86. );
  87. m_hexTileData.m_showWeights = false;
  88. m_hexTileData.m_pauseAnimation = false;
  89. // Create vertex stream declaration.
  90. PosTextCoord0Vertex::init();
  91. // Create static vertex buffer.
  92. m_vbh = bgfx::createVertexBuffer(
  93. // Static data can be passed with bgfx::makeRef
  94. bgfx::makeRef(s_screenSpaceQuadVertices, sizeof(s_screenSpaceQuadVertices))
  95. , PosTextCoord0Vertex::ms_layout
  96. );
  97. // Create static index buffer
  98. m_ibh = bgfx::createIndexBuffer(
  99. // Static data can be passed with bgfx::makeRef
  100. bgfx::makeRef(s_screenSpaceQuadIndices, sizeof(s_screenSpaceQuadIndices))
  101. );
  102. // Create program from shaders.
  103. m_hextileProgram = loadProgram("vs_hextile", "fs_hextile");
  104. // load texture to hextile
  105. m_tileTexture = loadTexture("textures/aerial_rocks_04_diff_2k.ktx");
  106. // Imgui.
  107. imguiCreate();
  108. m_timeOffset = bx::getHPCounter();
  109. s_tileSampler = bgfx::createUniform("s_trx_d", bgfx::UniformType::Sampler);
  110. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 3);
  111. }
  112. virtual int shutdown() override
  113. {
  114. // Cleanup.
  115. imguiDestroy();
  116. if (bgfx::isValid(m_ibh))
  117. {
  118. bgfx::destroy(m_ibh);
  119. }
  120. if (bgfx::isValid(m_vbh))
  121. {
  122. bgfx::destroy(m_vbh);
  123. }
  124. if (bgfx::isValid(m_tileTexture))
  125. {
  126. bgfx::destroy(m_tileTexture);
  127. }
  128. if (bgfx::isValid(s_tileSampler))
  129. {
  130. bgfx::destroy(s_tileSampler);
  131. }
  132. if (bgfx::isValid(u_params))
  133. {
  134. bgfx::destroy(u_params);
  135. }
  136. bgfx::destroy(m_hextileProgram);
  137. /// When data is passed to bgfx via makeRef we need to make
  138. /// sure library is done with it before freeing memory blocks.
  139. bgfx::frame();
  140. // Shutdown bgfx.
  141. bgfx::shutdown();
  142. return 0;
  143. }
  144. bool update() override
  145. {
  146. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState))
  147. {
  148. int64_t now = bx::getHPCounter();
  149. static int64_t last = now;
  150. const int64_t frameTime = now - last;
  151. last = now;
  152. const double freq = double(bx::getHPFrequency());
  153. const float deltaTime = float(frameTime / freq);
  154. imguiBeginFrame(m_mouseState.m_mx
  155. , m_mouseState.m_my
  156. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  157. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  158. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  159. , m_mouseState.m_mz
  160. , uint16_t(m_width)
  161. , uint16_t(m_height)
  162. );
  163. showExampleDialog(this);
  164. ImGui::SetNextWindowPos(
  165. ImVec2(m_width - m_width / 4.5f - 5.0f, 10.0f)
  166. , ImGuiCond_FirstUseEver
  167. );
  168. ImGui::SetNextWindowSize(
  169. ImVec2(m_width / 4.5f, m_height / 4.0f)
  170. , ImGuiCond_FirstUseEver
  171. );
  172. ImGui::Begin("Settings"
  173. , NULL
  174. , 0
  175. );
  176. ImGui::Separator();
  177. ImGui::Checkbox("Use Regular Tiling", &m_hexTileData.m_useRegularTiling);
  178. ImGui::Checkbox("Show Weights", &m_hexTileData.m_showWeights);
  179. ImGui::Checkbox("Pause Animation", &m_hexTileData.m_pauseAnimation);
  180. ImGui::SliderInt("Tile Rate", &m_hexTileData.m_tileRate, 2, 25);
  181. ImGui::SliderFloat("Tile Rotation", &m_hexTileData.m_tileRotationStrength, 0.0f, 20.0f);
  182. ImGui::Separator();
  183. ImGui::End();
  184. imguiEndFrame();
  185. // This dummy draw call is here to make sure that view 0 is cleared
  186. // if no other draw calls are submitted to view 0.
  187. bgfx::touch(0);
  188. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  189. if (!m_hexTileData.m_pauseAnimation)
  190. {
  191. m_eye.z = bx::abs(m_eye.z) + (deltaTime / 4.0f);
  192. if (m_eye.z < 10.0f)
  193. {
  194. m_eye.z *= -1;
  195. }
  196. else
  197. {
  198. m_eye.z = -0.01f;
  199. }
  200. }
  201. float viewMtx[16];
  202. bx::mtxLookAt(viewMtx, m_eye, at);
  203. float projMtx[16];
  204. bx::mtxProj(projMtx, 30.0f, float(m_width) / float(m_height), 0.1f, 1000.0f, bgfx::getCaps()->homogeneousDepth);
  205. bgfx::setViewTransform(0, viewMtx, projMtx);
  206. // Set view 0 default viewport.
  207. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height));
  208. float modelTransform[16];
  209. bx::mtxSRT(modelTransform, 30.0f, 30.0f, 30.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  210. bgfx::setTransform(modelTransform);
  211. bgfx::setVertexBuffer(0, m_vbh);
  212. bgfx::setIndexBuffer(m_ibh);
  213. bgfx::setTexture(0, s_tileSampler, m_tileTexture);
  214. const float data[4] = { float(m_hexTileData.m_showWeights), float(m_hexTileData.m_tileRate),
  215. float(m_hexTileData.m_tileRotationStrength), float(m_hexTileData.m_useRegularTiling) };
  216. bgfx::setUniform(u_params, data);
  217. bgfx::setState(0
  218. | BGFX_STATE_WRITE_RGB
  219. | BGFX_STATE_WRITE_A
  220. | BGFX_STATE_WRITE_Z
  221. | BGFX_STATE_DEPTH_TEST_LESS
  222. | BGFX_STATE_MSAA
  223. );
  224. bgfx::submit(0, m_hextileProgram);
  225. // Advance to next frame. Rendering thread will be kicked to
  226. // process submitted rendering primitives.
  227. bgfx::frame();
  228. return true;
  229. }
  230. return false;
  231. }
  232. bgfx::VertexBufferHandle m_vbh;
  233. bgfx::IndexBufferHandle m_ibh;
  234. bgfx::ProgramHandle m_hextileProgram;
  235. bgfx::UniformHandle s_tileSampler;
  236. bgfx::TextureHandle m_tileTexture;
  237. uint32_t m_width;
  238. uint32_t m_height;
  239. uint32_t m_debug;
  240. uint32_t m_reset;
  241. HextileData m_hexTileData;
  242. entry::MouseState m_mouseState;
  243. bgfx::UniformHandle u_params;
  244. int64_t m_timeOffset;
  245. bx::Vec3 m_eye = { 0.0f, 2.0f, -0.01f };
  246. };
  247. } // namespace
  248. ENTRY_IMPLEMENT_MAIN(
  249. ExampleHextile
  250. , "49-hextile"
  251. , "Hextile example."
  252. , "https://bkaradzic.github.io/bgfx/examples.html#hextile"
  253. );