raymarch.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright 2011-2025 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include "imgui/imgui.h"
  8. namespace
  9. {
  10. struct PosColorTexCoord0Vertex
  11. {
  12. float m_x;
  13. float m_y;
  14. float m_z;
  15. uint32_t m_abgr;
  16. float m_u;
  17. float m_v;
  18. static void init()
  19. {
  20. ms_layout
  21. .begin()
  22. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  23. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  24. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  25. .end();
  26. }
  27. static bgfx::VertexLayout ms_layout;
  28. };
  29. bgfx::VertexLayout PosColorTexCoord0Vertex::ms_layout;
  30. void renderScreenSpaceQuad(uint8_t _view, bgfx::ProgramHandle _program, float _x, float _y, float _width, float _height)
  31. {
  32. bgfx::TransientVertexBuffer tvb;
  33. bgfx::TransientIndexBuffer tib;
  34. if (bgfx::allocTransientBuffers(&tvb, PosColorTexCoord0Vertex::ms_layout, 4, &tib, 6) )
  35. {
  36. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)tvb.data;
  37. float zz = 0.0f;
  38. const float minx = _x;
  39. const float maxx = _x + _width;
  40. const float miny = _y;
  41. const float maxy = _y + _height;
  42. float minu = -1.0f;
  43. float minv = -1.0f;
  44. float maxu = 1.0f;
  45. float maxv = 1.0f;
  46. vertex[0].m_x = minx;
  47. vertex[0].m_y = miny;
  48. vertex[0].m_z = zz;
  49. vertex[0].m_abgr = 0xff0000ff;
  50. vertex[0].m_u = minu;
  51. vertex[0].m_v = minv;
  52. vertex[1].m_x = maxx;
  53. vertex[1].m_y = miny;
  54. vertex[1].m_z = zz;
  55. vertex[1].m_abgr = 0xff00ff00;
  56. vertex[1].m_u = maxu;
  57. vertex[1].m_v = minv;
  58. vertex[2].m_x = maxx;
  59. vertex[2].m_y = maxy;
  60. vertex[2].m_z = zz;
  61. vertex[2].m_abgr = 0xffff0000;
  62. vertex[2].m_u = maxu;
  63. vertex[2].m_v = maxv;
  64. vertex[3].m_x = minx;
  65. vertex[3].m_y = maxy;
  66. vertex[3].m_z = zz;
  67. vertex[3].m_abgr = 0xffffffff;
  68. vertex[3].m_u = minu;
  69. vertex[3].m_v = maxv;
  70. uint16_t* indices = (uint16_t*)tib.data;
  71. indices[0] = 0;
  72. indices[1] = 2;
  73. indices[2] = 1;
  74. indices[3] = 0;
  75. indices[4] = 3;
  76. indices[5] = 2;
  77. bgfx::setState(BGFX_STATE_DEFAULT);
  78. bgfx::setIndexBuffer(&tib);
  79. bgfx::setVertexBuffer(0, &tvb);
  80. bgfx::submit(_view, _program);
  81. }
  82. }
  83. class ExampleRaymarch : public entry::AppI
  84. {
  85. public:
  86. ExampleRaymarch(const char* _name, const char* _description, const char* _url)
  87. : entry::AppI(_name, _description, _url)
  88. {
  89. }
  90. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  91. {
  92. Args args(_argc, _argv);
  93. m_width = _width;
  94. m_height = _height;
  95. m_debug = BGFX_DEBUG_NONE;
  96. m_reset = BGFX_RESET_VSYNC;
  97. bgfx::Init init;
  98. init.type = args.m_type;
  99. init.vendorId = args.m_pciId;
  100. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  101. init.platformData.ndt = entry::getNativeDisplayHandle();
  102. init.platformData.type = entry::getNativeWindowHandleType();
  103. init.resolution.width = m_width;
  104. init.resolution.height = m_height;
  105. init.resolution.reset = m_reset;
  106. bgfx::init(init);
  107. // Enable debug text.
  108. bgfx::setDebug(m_debug);
  109. // Set view 0 clear state.
  110. bgfx::setViewClear(0
  111. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  112. , 0x303030ff
  113. , 1.0f
  114. , 0
  115. );
  116. // Create vertex stream declaration.
  117. PosColorTexCoord0Vertex::init();
  118. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  119. u_lightDirTime = bgfx::createUniform("u_lightDirTime", bgfx::UniformType::Vec4);
  120. // Create program from shaders.
  121. m_program = loadProgram("vs_raymarching", "fs_raymarching");
  122. m_timeOffset = bx::getHPCounter();
  123. imguiCreate();
  124. }
  125. int shutdown() override
  126. {
  127. imguiDestroy();
  128. // Cleanup.
  129. bgfx::destroy(m_program);
  130. bgfx::destroy(u_mtx);
  131. bgfx::destroy(u_lightDirTime);
  132. // Shutdown bgfx.
  133. bgfx::shutdown();
  134. return 0;
  135. }
  136. bool update() override
  137. {
  138. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  139. {
  140. imguiBeginFrame(m_mouseState.m_mx
  141. , m_mouseState.m_my
  142. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  143. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  144. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  145. , m_mouseState.m_mz
  146. , uint16_t(m_width)
  147. , uint16_t(m_height)
  148. );
  149. showExampleDialog(this);
  150. imguiEndFrame();
  151. // Set view 0 default viewport.
  152. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  153. // Set view 1 default viewport.
  154. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  155. // This dummy draw call is here to make sure that view 0 is cleared
  156. // if no other draw calls are submitted to viewZ 0.
  157. bgfx::touch(0);
  158. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  159. const bx::Vec3 eye = { 0.0f, 0.0f, -15.0f };
  160. float view[16];
  161. float proj[16];
  162. bx::mtxLookAt(view, eye, at);
  163. const bgfx::Caps* caps = bgfx::getCaps();
  164. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
  165. // Set view and projection matrix for view 1.
  166. bgfx::setViewTransform(0, view, proj);
  167. float ortho[16];
  168. bx::mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f, 0.0, caps->homogeneousDepth);
  169. // Set view and projection matrix for view 0.
  170. bgfx::setViewTransform(1, NULL, ortho);
  171. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  172. float vp[16];
  173. bx::mtxMul(vp, view, proj);
  174. float mtx[16];
  175. bx::mtxRotateXY(mtx
  176. , time
  177. , time*0.37f
  178. );
  179. float mtxInv[16];
  180. bx::mtxInverse(mtxInv, mtx);
  181. float lightDirTime[4];
  182. const bx::Vec3 lightDirModelN = bx::normalize(bx::Vec3{-0.4f, -0.5f, -1.0f});
  183. bx::store(lightDirTime, bx::mul(lightDirModelN, mtxInv) );
  184. lightDirTime[3] = time;
  185. bgfx::setUniform(u_lightDirTime, lightDirTime);
  186. float mvp[16];
  187. bx::mtxMul(mvp, mtx, vp);
  188. float invMvp[16];
  189. bx::mtxInverse(invMvp, mvp);
  190. bgfx::setUniform(u_mtx, invMvp);
  191. renderScreenSpaceQuad(1, m_program, 0.0f, 0.0f, 1280.0f, 720.0f);
  192. // Advance to next frame. Rendering thread will be kicked to
  193. // process submitted rendering primitives.
  194. bgfx::frame();
  195. return true;
  196. }
  197. return false;
  198. }
  199. entry::MouseState m_mouseState;
  200. uint32_t m_width;
  201. uint32_t m_height;
  202. uint32_t m_debug;
  203. uint32_t m_reset;
  204. int64_t m_timeOffset;
  205. bgfx::UniformHandle u_mtx;
  206. bgfx::UniformHandle u_lightDirTime;
  207. bgfx::ProgramHandle m_program;
  208. };
  209. } // namespace
  210. ENTRY_IMPLEMENT_MAIN(
  211. ExampleRaymarch
  212. , "03-raymarch"
  213. , "Updating shader uniforms."
  214. , "https://bkaradzic.github.io/bgfx/examples.html#raymarch"
  215. );