raymarch.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 2011-2016 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. struct PosColorTexCoord0Vertex
  8. {
  9. float m_x;
  10. float m_y;
  11. float m_z;
  12. uint32_t m_abgr;
  13. float m_u;
  14. float m_v;
  15. static void init()
  16. {
  17. ms_decl
  18. .begin()
  19. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  20. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  21. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  22. .end();
  23. }
  24. static bgfx::VertexDecl ms_decl;
  25. };
  26. bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
  27. static bool s_oglNdc = false;
  28. inline void mtxProj(float* _result, float _fovy, float _aspect, float _near, float _far)
  29. {
  30. bx::mtxProj(_result, _fovy, _aspect, _near, _far, s_oglNdc);
  31. }
  32. void renderScreenSpaceQuad(uint8_t _view, bgfx::ProgramHandle _program, float _x, float _y, float _width, float _height)
  33. {
  34. bgfx::TransientVertexBuffer tvb;
  35. bgfx::TransientIndexBuffer tib;
  36. if (bgfx::allocTransientBuffers(&tvb, PosColorTexCoord0Vertex::ms_decl, 4, &tib, 6) )
  37. {
  38. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)tvb.data;
  39. float zz = 0.0f;
  40. const float minx = _x;
  41. const float maxx = _x + _width;
  42. const float miny = _y;
  43. const float maxy = _y + _height;
  44. float minu = -1.0f;
  45. float minv = -1.0f;
  46. float maxu = 1.0f;
  47. float maxv = 1.0f;
  48. vertex[0].m_x = minx;
  49. vertex[0].m_y = miny;
  50. vertex[0].m_z = zz;
  51. vertex[0].m_abgr = 0xff0000ff;
  52. vertex[0].m_u = minu;
  53. vertex[0].m_v = minv;
  54. vertex[1].m_x = maxx;
  55. vertex[1].m_y = miny;
  56. vertex[1].m_z = zz;
  57. vertex[1].m_abgr = 0xff00ff00;
  58. vertex[1].m_u = maxu;
  59. vertex[1].m_v = minv;
  60. vertex[2].m_x = maxx;
  61. vertex[2].m_y = maxy;
  62. vertex[2].m_z = zz;
  63. vertex[2].m_abgr = 0xffff0000;
  64. vertex[2].m_u = maxu;
  65. vertex[2].m_v = maxv;
  66. vertex[3].m_x = minx;
  67. vertex[3].m_y = maxy;
  68. vertex[3].m_z = zz;
  69. vertex[3].m_abgr = 0xffffffff;
  70. vertex[3].m_u = minu;
  71. vertex[3].m_v = maxv;
  72. uint16_t* indices = (uint16_t*)tib.data;
  73. indices[0] = 0;
  74. indices[1] = 2;
  75. indices[2] = 1;
  76. indices[3] = 0;
  77. indices[4] = 3;
  78. indices[5] = 2;
  79. bgfx::setState(BGFX_STATE_DEFAULT);
  80. bgfx::setIndexBuffer(&tib);
  81. bgfx::setVertexBuffer(&tvb);
  82. bgfx::submit(_view, _program);
  83. }
  84. }
  85. class ExampleRaymarch : public entry::AppI
  86. {
  87. void init(int _argc, char** _argv) BX_OVERRIDE
  88. {
  89. Args args(_argc, _argv);
  90. m_width = 1280;
  91. m_height = 720;
  92. m_debug = BGFX_DEBUG_TEXT;
  93. m_reset = BGFX_RESET_VSYNC;
  94. bgfx::init(args.m_type, args.m_pciId);
  95. bgfx::reset(m_width, m_height, m_reset);
  96. // Enable debug text.
  97. bgfx::setDebug(m_debug);
  98. // Set view 0 clear state.
  99. bgfx::setViewClear(0
  100. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  101. , 0x303030ff
  102. , 1.0f
  103. , 0
  104. );
  105. const bgfx::Caps* caps = bgfx::getCaps();
  106. s_oglNdc = caps->homogeneousDepth;
  107. // Create vertex stream declaration.
  108. PosColorTexCoord0Vertex::init();
  109. u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  110. u_lightDirTime = bgfx::createUniform("u_lightDirTime", bgfx::UniformType::Vec4);
  111. // Create program from shaders.
  112. m_program = loadProgram("vs_raymarching", "fs_raymarching");
  113. m_timeOffset = bx::getHPCounter();
  114. }
  115. int shutdown() BX_OVERRIDE
  116. {
  117. // Cleanup.
  118. bgfx::destroyProgram(m_program);
  119. bgfx::destroyUniform(u_mtx);
  120. bgfx::destroyUniform(u_lightDirTime);
  121. // Shutdown bgfx.
  122. bgfx::shutdown();
  123. return 0;
  124. }
  125. bool update() BX_OVERRIDE
  126. {
  127. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  128. {
  129. // Set view 0 default viewport.
  130. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  131. // Set view 1 default viewport.
  132. bgfx::setViewRect(1, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  133. // This dummy draw call is here to make sure that view 0 is cleared
  134. // if no other draw calls are submitted to viewZ 0.
  135. bgfx::touch(0);
  136. int64_t now = bx::getHPCounter();
  137. static int64_t last = now;
  138. const int64_t frameTime = now - last;
  139. last = now;
  140. const double freq = double(bx::getHPFrequency() );
  141. const double toMs = 1000.0/freq;
  142. // Use debug font to print information about this example.
  143. bgfx::dbgTextClear();
  144. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/03-raymarch");
  145. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms.");
  146. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  147. float at[3] = { 0.0f, 0.0f, 0.0f };
  148. float eye[3] = { 0.0f, 0.0f, -15.0f };
  149. float view[16];
  150. float proj[16];
  151. bx::mtxLookAt(view, eye, at);
  152. mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  153. // Set view and projection matrix for view 1.
  154. bgfx::setViewTransform(0, view, proj);
  155. float ortho[16];
  156. bx::mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f);
  157. // Set view and projection matrix for view 0.
  158. bgfx::setViewTransform(1, NULL, ortho);
  159. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  160. float vp[16];
  161. bx::mtxMul(vp, view, proj);
  162. float mtx[16];
  163. bx::mtxRotateXY(mtx
  164. , time
  165. , time*0.37f
  166. );
  167. float mtxInv[16];
  168. bx::mtxInverse(mtxInv, mtx);
  169. float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f };
  170. float lightDirModelN[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  171. bx::vec3Norm(lightDirModelN, lightDirModel);
  172. float lightDirTime[4];
  173. bx::vec4MulMtx(lightDirTime, lightDirModelN, mtxInv);
  174. lightDirTime[3] = time;
  175. bgfx::setUniform(u_lightDirTime, lightDirTime);
  176. float mvp[16];
  177. bx::mtxMul(mvp, mtx, vp);
  178. float invMvp[16];
  179. bx::mtxInverse(invMvp, mvp);
  180. bgfx::setUniform(u_mtx, invMvp);
  181. renderScreenSpaceQuad(1, m_program, 0.0f, 0.0f, 1280.0f, 720.0f);
  182. // Advance to next frame. Rendering thread will be kicked to
  183. // process submitted rendering primitives.
  184. bgfx::frame();
  185. return true;
  186. }
  187. return false;
  188. }
  189. uint32_t m_width;
  190. uint32_t m_height;
  191. uint32_t m_debug;
  192. uint32_t m_reset;
  193. int64_t m_timeOffset;
  194. bgfx::UniformHandle u_mtx;
  195. bgfx::UniformHandle u_lightDirTime;
  196. bgfx::ProgramHandle m_program;
  197. };
  198. ENTRY_IMPLEMENT_MAIN(ExampleRaymarch);