raymarch.cpp 6.1 KB

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