raymarch.cpp 6.3 KB

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