2
0

hextile.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.platformData.type = entry::getNativeWindowHandleType();
  75. init.resolution.width = m_width;
  76. init.resolution.height = m_height;
  77. init.resolution.reset = m_reset;
  78. bgfx::init(init);
  79. // Enable m_debug text.
  80. bgfx::setDebug(m_debug);
  81. // Set view 0 clear state.
  82. bgfx::setViewClear(0
  83. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  84. , 0x303030ff
  85. , 1.0f
  86. , 0
  87. );
  88. m_hexTileData.m_showWeights = false;
  89. m_hexTileData.m_pauseAnimation = false;
  90. // Create vertex stream declaration.
  91. PosTextCoord0Vertex::init();
  92. // Create static vertex buffer.
  93. m_vbh = bgfx::createVertexBuffer(
  94. // Static data can be passed with bgfx::makeRef
  95. bgfx::makeRef(s_screenSpaceQuadVertices, sizeof(s_screenSpaceQuadVertices))
  96. , PosTextCoord0Vertex::ms_layout
  97. );
  98. // Create static index buffer
  99. m_ibh = bgfx::createIndexBuffer(
  100. // Static data can be passed with bgfx::makeRef
  101. bgfx::makeRef(s_screenSpaceQuadIndices, sizeof(s_screenSpaceQuadIndices))
  102. );
  103. // Create program from shaders.
  104. m_hextileProgram = loadProgram("vs_hextile", "fs_hextile");
  105. // load texture to hextile
  106. m_tileTexture = loadTexture("textures/aerial_rocks_04_diff_2k.ktx");
  107. // Imgui.
  108. imguiCreate();
  109. s_tileSampler = bgfx::createUniform("s_trx_d", bgfx::UniformType::Sampler);
  110. u_params = bgfx::createUniform("u_params", bgfx::UniformType::Vec4, 3);
  111. m_frameTime.reset();
  112. }
  113. virtual int shutdown() override
  114. {
  115. // Cleanup.
  116. imguiDestroy();
  117. if (bgfx::isValid(m_ibh))
  118. {
  119. bgfx::destroy(m_ibh);
  120. }
  121. if (bgfx::isValid(m_vbh))
  122. {
  123. bgfx::destroy(m_vbh);
  124. }
  125. if (bgfx::isValid(m_tileTexture))
  126. {
  127. bgfx::destroy(m_tileTexture);
  128. }
  129. if (bgfx::isValid(s_tileSampler))
  130. {
  131. bgfx::destroy(s_tileSampler);
  132. }
  133. if (bgfx::isValid(u_params))
  134. {
  135. bgfx::destroy(u_params);
  136. }
  137. bgfx::destroy(m_hextileProgram);
  138. /// When data is passed to bgfx via makeRef we need to make
  139. /// sure library is done with it before freeing memory blocks.
  140. bgfx::frame();
  141. // Shutdown bgfx.
  142. bgfx::shutdown();
  143. return 0;
  144. }
  145. bool update() override
  146. {
  147. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState))
  148. {
  149. m_frameTime.frame();
  150. const float deltaTime = bx::toSeconds<float>(m_frameTime.getDeltaTime() );
  151. imguiBeginFrame(m_mouseState.m_mx
  152. , m_mouseState.m_my
  153. , (m_mouseState.m_buttons[entry::MouseButton::Left] ? IMGUI_MBUT_LEFT : 0)
  154. | (m_mouseState.m_buttons[entry::MouseButton::Right] ? IMGUI_MBUT_RIGHT : 0)
  155. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  156. , m_mouseState.m_mz
  157. , uint16_t(m_width)
  158. , uint16_t(m_height)
  159. );
  160. showExampleDialog(this);
  161. ImGui::SetNextWindowPos(
  162. ImVec2(m_width - m_width / 4.5f - 5.0f, 10.0f)
  163. , ImGuiCond_FirstUseEver
  164. );
  165. ImGui::SetNextWindowSize(
  166. ImVec2(m_width / 4.5f, m_height / 4.0f)
  167. , ImGuiCond_FirstUseEver
  168. );
  169. ImGui::Begin("Settings"
  170. , NULL
  171. , 0
  172. );
  173. ImGui::Separator();
  174. ImGui::Checkbox("Use Regular Tiling", &m_hexTileData.m_useRegularTiling);
  175. ImGui::Checkbox("Show Weights", &m_hexTileData.m_showWeights);
  176. ImGui::Checkbox("Pause Animation", &m_hexTileData.m_pauseAnimation);
  177. ImGui::SliderInt("Tile Rate", &m_hexTileData.m_tileRate, 2, 25);
  178. ImGui::SliderFloat("Tile Rotation", &m_hexTileData.m_tileRotationStrength, 0.0f, 20.0f);
  179. ImGui::Separator();
  180. ImGui::End();
  181. imguiEndFrame();
  182. // This dummy draw call is here to make sure that view 0 is cleared
  183. // if no other draw calls are submitted to view 0.
  184. bgfx::touch(0);
  185. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  186. if (!m_hexTileData.m_pauseAnimation)
  187. {
  188. m_eye.z = bx::abs(m_eye.z) + (deltaTime / 4.0f);
  189. if (m_eye.z < 10.0f)
  190. {
  191. m_eye.z *= -1;
  192. }
  193. else
  194. {
  195. m_eye.z = -0.01f;
  196. }
  197. }
  198. float viewMtx[16];
  199. bx::mtxLookAt(viewMtx, m_eye, at);
  200. float projMtx[16];
  201. bx::mtxProj(projMtx, 30.0f, float(m_width) / float(m_height), 0.1f, 1000.0f, bgfx::getCaps()->homogeneousDepth);
  202. bgfx::setViewTransform(0, viewMtx, projMtx);
  203. // Set view 0 default viewport.
  204. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height));
  205. float modelTransform[16];
  206. bx::mtxSRT(modelTransform, 30.0f, 30.0f, 30.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
  207. bgfx::setTransform(modelTransform);
  208. bgfx::setVertexBuffer(0, m_vbh);
  209. bgfx::setIndexBuffer(m_ibh);
  210. bgfx::setTexture(0, s_tileSampler, m_tileTexture);
  211. const float data[4] = { float(m_hexTileData.m_showWeights), float(m_hexTileData.m_tileRate),
  212. float(m_hexTileData.m_tileRotationStrength), float(m_hexTileData.m_useRegularTiling) };
  213. bgfx::setUniform(u_params, data);
  214. bgfx::setState(0
  215. | BGFX_STATE_WRITE_RGB
  216. | BGFX_STATE_WRITE_A
  217. | BGFX_STATE_WRITE_Z
  218. | BGFX_STATE_DEPTH_TEST_LESS
  219. | BGFX_STATE_MSAA
  220. );
  221. bgfx::submit(0, m_hextileProgram);
  222. // Advance to next frame. Rendering thread will be kicked to
  223. // process submitted rendering primitives.
  224. bgfx::frame();
  225. return true;
  226. }
  227. return false;
  228. }
  229. bgfx::VertexBufferHandle m_vbh;
  230. bgfx::IndexBufferHandle m_ibh;
  231. bgfx::ProgramHandle m_hextileProgram;
  232. bgfx::UniformHandle s_tileSampler;
  233. bgfx::TextureHandle m_tileTexture;
  234. uint32_t m_width;
  235. uint32_t m_height;
  236. uint32_t m_debug;
  237. uint32_t m_reset;
  238. HextileData m_hexTileData;
  239. entry::MouseState m_mouseState;
  240. bgfx::UniformHandle u_params;
  241. bx::Vec3 m_eye = { 0.0f, 2.0f, -0.01f };
  242. FrameTime m_frameTime;
  243. };
  244. } // namespace
  245. ENTRY_IMPLEMENT_MAIN(
  246. ExampleHextile
  247. , "49-hextile"
  248. , "Hextile example."
  249. , "https://bkaradzic.github.io/bgfx/examples.html#hextile"
  250. );