raymarch.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(uint32_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. int _main_(int _argc, char** _argv)
  86. {
  87. Args args(_argc, _argv);
  88. uint32_t width = 1280;
  89. uint32_t height = 720;
  90. uint32_t debug = BGFX_DEBUG_TEXT;
  91. uint32_t reset = BGFX_RESET_VSYNC;
  92. bgfx::init(args.m_type, args.m_pciId);
  93. bgfx::reset(width, height, reset);
  94. // Enable debug text.
  95. bgfx::setDebug(debug);
  96. // Set view 0 clear state.
  97. bgfx::setViewClear(0
  98. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  99. , 0x303030ff
  100. , 1.0f
  101. , 0
  102. );
  103. // Setup root path for binary shaders. Shader binaries are different
  104. // for each renderer.
  105. switch (bgfx::getRendererType() )
  106. {
  107. default:
  108. break;
  109. case bgfx::RendererType::OpenGL:
  110. case bgfx::RendererType::OpenGLES:
  111. s_oglNdc = true;
  112. break;
  113. }
  114. // Create vertex stream declaration.
  115. PosColorTexCoord0Vertex::init();
  116. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Mat4);
  117. bgfx::UniformHandle u_lightDirTime = bgfx::createUniform("u_lightDirTime", bgfx::UniformType::Vec4);
  118. // Create program from shaders.
  119. bgfx::ProgramHandle raymarching = loadProgram("vs_raymarching", "fs_raymarching");
  120. int64_t timeOffset = bx::getHPCounter();
  121. while (!entry::processEvents(width, height, debug, reset) )
  122. {
  123. // Set view 0 default viewport.
  124. bgfx::setViewRect(0, 0, 0, width, height);
  125. // Set view 1 default viewport.
  126. bgfx::setViewRect(1, 0, 0, width, height);
  127. // This dummy draw call is here to make sure that view 0 is cleared
  128. // if no other draw calls are submitted to viewZ 0.
  129. bgfx::touch(0);
  130. int64_t now = bx::getHPCounter();
  131. static int64_t last = now;
  132. const int64_t frameTime = now - last;
  133. last = now;
  134. const double freq = double(bx::getHPFrequency() );
  135. const double toMs = 1000.0/freq;
  136. // Use debug font to print information about this example.
  137. bgfx::dbgTextClear();
  138. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/03-raymarch");
  139. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms.");
  140. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  141. float at[3] = { 0.0f, 0.0f, 0.0f };
  142. float eye[3] = { 0.0f, 0.0f, -15.0f };
  143. float view[16];
  144. float proj[16];
  145. bx::mtxLookAt(view, eye, at);
  146. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  147. // Set view and projection matrix for view 1.
  148. bgfx::setViewTransform(0, view, proj);
  149. float ortho[16];
  150. bx::mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f);
  151. // Set view and projection matrix for view 0.
  152. bgfx::setViewTransform(1, NULL, ortho);
  153. float time = (float)( (bx::getHPCounter()-timeOffset)/double(bx::getHPFrequency() ) );
  154. float vp[16];
  155. bx::mtxMul(vp, view, proj);
  156. float mtx[16];
  157. bx::mtxRotateXY(mtx
  158. , time
  159. , time*0.37f
  160. );
  161. float mtxInv[16];
  162. bx::mtxInverse(mtxInv, mtx);
  163. float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f };
  164. float lightDirModelN[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  165. bx::vec3Norm(lightDirModelN, lightDirModel);
  166. float lightDirTime[4];
  167. bx::vec4MulMtx(lightDirTime, lightDirModelN, mtxInv);
  168. lightDirTime[3] = time;
  169. bgfx::setUniform(u_lightDirTime, lightDirTime);
  170. float mvp[16];
  171. bx::mtxMul(mvp, mtx, vp);
  172. float invMvp[16];
  173. bx::mtxInverse(invMvp, mvp);
  174. bgfx::setUniform(u_mtx, invMvp);
  175. renderScreenSpaceQuad(1, raymarching, 0.0f, 0.0f, 1280.0f, 720.0f);
  176. // Advance to next frame. Rendering thread will be kicked to
  177. // process submitted rendering primitives.
  178. bgfx::frame();
  179. }
  180. // Cleanup.
  181. bgfx::destroyProgram(raymarching);
  182. bgfx::destroyUniform(u_mtx);
  183. bgfx::destroyUniform(u_lightDirTime);
  184. // Shutdown bgfx.
  185. bgfx::shutdown();
  186. return 0;
  187. }