raymarch.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright 2011-2023 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.resolution.width = m_width;
  103. init.resolution.height = m_height;
  104. init.resolution.reset = m_reset;
  105. bgfx::init(init);
  106. // Enable debug text.
  107. bgfx::setDebug(m_debug);
  108. // Set view 0 clear state.
  109. bgfx::setViewClear(0
  110. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  111. , 0x303030ff
  112. , 1.0f
  113. , 0
  114. );
  115. // Create vertex stream declaration.
  116. PosColorTexCoord0Vertex::init();
  117. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  118. u_lightDirTime = bgfx::createUniform("u_lightDirTime", bgfx::UniformType::Vec4);
  119. // Create program from shaders.
  120. m_program = loadProgram("vs_raymarching", "fs_raymarching");
  121. m_timeOffset = bx::getHPCounter();
  122. imguiCreate();
  123. }
  124. int shutdown() override
  125. {
  126. imguiDestroy();
  127. // Cleanup.
  128. bgfx::destroy(m_program);
  129. bgfx::destroy(u_mtx);
  130. bgfx::destroy(u_lightDirTime);
  131. // Shutdown bgfx.
  132. bgfx::shutdown();
  133. return 0;
  134. }
  135. bool update() override
  136. {
  137. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  138. {
  139. imguiBeginFrame(m_mouseState.m_mx
  140. , m_mouseState.m_my
  141. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  142. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  143. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  144. , m_mouseState.m_mz
  145. , uint16_t(m_width)
  146. , uint16_t(m_height)
  147. );
  148. showExampleDialog(this);
  149. imguiEndFrame();
  150. // Set view 0 default viewport.
  151. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  152. // Set view 1 default viewport.
  153. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  154. // This dummy draw call is here to make sure that view 0 is cleared
  155. // if no other draw calls are submitted to viewZ 0.
  156. bgfx::touch(0);
  157. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  158. const bx::Vec3 eye = { 0.0f, 0.0f, -15.0f };
  159. float view[16];
  160. float proj[16];
  161. bx::mtxLookAt(view, eye, at);
  162. const bgfx::Caps* caps = bgfx::getCaps();
  163. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, caps->homogeneousDepth);
  164. // Set view and projection matrix for view 1.
  165. bgfx::setViewTransform(0, view, proj);
  166. float ortho[16];
  167. bx::mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f, 0.0, caps->homogeneousDepth);
  168. // Set view and projection matrix for view 0.
  169. bgfx::setViewTransform(1, NULL, ortho);
  170. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  171. float vp[16];
  172. bx::mtxMul(vp, view, proj);
  173. float mtx[16];
  174. bx::mtxRotateXY(mtx
  175. , time
  176. , time*0.37f
  177. );
  178. float mtxInv[16];
  179. bx::mtxInverse(mtxInv, mtx);
  180. float lightDirTime[4];
  181. const bx::Vec3 lightDirModelN = bx::normalize(bx::Vec3{-0.4f, -0.5f, -1.0f});
  182. bx::store(lightDirTime, bx::mul(lightDirModelN, mtxInv) );
  183. lightDirTime[3] = time;
  184. bgfx::setUniform(u_lightDirTime, lightDirTime);
  185. float mvp[16];
  186. bx::mtxMul(mvp, mtx, vp);
  187. float invMvp[16];
  188. bx::mtxInverse(invMvp, mvp);
  189. bgfx::setUniform(u_mtx, invMvp);
  190. renderScreenSpaceQuad(1, m_program, 0.0f, 0.0f, 1280.0f, 720.0f);
  191. // Advance to next frame. Rendering thread will be kicked to
  192. // process submitted rendering primitives.
  193. bgfx::frame();
  194. return true;
  195. }
  196. return false;
  197. }
  198. entry::MouseState m_mouseState;
  199. uint32_t m_width;
  200. uint32_t m_height;
  201. uint32_t m_debug;
  202. uint32_t m_reset;
  203. int64_t m_timeOffset;
  204. bgfx::UniformHandle u_mtx;
  205. bgfx::UniformHandle u_lightDirTime;
  206. bgfx::ProgramHandle m_program;
  207. };
  208. } // namespace
  209. ENTRY_IMPLEMENT_MAIN(
  210. ExampleRaymarch
  211. , "03-raymarch"
  212. , "Updating shader uniforms."
  213. , "https://bkaradzic.github.io/bgfx/examples.html#raymarch"
  214. );