raymarch.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/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_flipV = false;
  28. void renderScreenSpaceQuad(uint32_t _view, bgfx::ProgramHandle _program, float _x, float _y, float _width, float _height)
  29. {
  30. bgfx::TransientVertexBuffer tvb;
  31. bgfx::TransientIndexBuffer tib;
  32. if (bgfx::allocTransientBuffers(&tvb, PosColorTexCoord0Vertex::ms_decl, 4, &tib, 6) )
  33. {
  34. PosColorTexCoord0Vertex* vertex = (PosColorTexCoord0Vertex*)tvb.data;
  35. float zz = 0.0f;
  36. const float minx = _x;
  37. const float maxx = _x + _width;
  38. const float miny = _y;
  39. const float maxy = _y + _height;
  40. float minu = -1.0f;
  41. float minv = -1.0f;
  42. float maxu = 1.0f;
  43. float maxv = 1.0f;
  44. vertex[0].m_x = minx;
  45. vertex[0].m_y = miny;
  46. vertex[0].m_z = zz;
  47. vertex[0].m_abgr = 0xff0000ff;
  48. vertex[0].m_u = minu;
  49. vertex[0].m_v = minv;
  50. vertex[1].m_x = maxx;
  51. vertex[1].m_y = miny;
  52. vertex[1].m_z = zz;
  53. vertex[1].m_abgr = 0xff00ff00;
  54. vertex[1].m_u = maxu;
  55. vertex[1].m_v = minv;
  56. vertex[2].m_x = maxx;
  57. vertex[2].m_y = maxy;
  58. vertex[2].m_z = zz;
  59. vertex[2].m_abgr = 0xffff0000;
  60. vertex[2].m_u = maxu;
  61. vertex[2].m_v = maxv;
  62. vertex[3].m_x = minx;
  63. vertex[3].m_y = maxy;
  64. vertex[3].m_z = zz;
  65. vertex[3].m_abgr = 0xffffffff;
  66. vertex[3].m_u = minu;
  67. vertex[3].m_v = maxv;
  68. uint16_t* indices = (uint16_t*)tib.data;
  69. indices[0] = 0;
  70. indices[1] = 2;
  71. indices[2] = 1;
  72. indices[3] = 0;
  73. indices[4] = 3;
  74. indices[5] = 2;
  75. bgfx::setProgram(_program);
  76. bgfx::setState(BGFX_STATE_DEFAULT);
  77. bgfx::setIndexBuffer(&tib);
  78. bgfx::setVertexBuffer(&tvb);
  79. bgfx::submit(_view);
  80. }
  81. }
  82. int _main_(int /*_argc*/, char** /*_argv*/)
  83. {
  84. uint32_t width = 1280;
  85. uint32_t height = 720;
  86. uint32_t debug = BGFX_DEBUG_TEXT;
  87. uint32_t reset = BGFX_RESET_VSYNC;
  88. bgfx::init();
  89. bgfx::reset(width, height, reset);
  90. // Enable debug text.
  91. bgfx::setDebug(debug);
  92. // Set view 0 clear state.
  93. bgfx::setViewClear(0
  94. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  95. , 0x303030ff
  96. , 1.0f
  97. , 0
  98. );
  99. // Setup root path for binary shaders. Shader binaries are different
  100. // for each renderer.
  101. switch (bgfx::getRendererType() )
  102. {
  103. default:
  104. break;
  105. case bgfx::RendererType::OpenGL:
  106. case bgfx::RendererType::OpenGLES:
  107. s_flipV = true;
  108. break;
  109. }
  110. // Create vertex stream declaration.
  111. PosColorTexCoord0Vertex::init();
  112. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Uniform1f);
  113. bgfx::UniformHandle u_mtx = bgfx::createUniform("u_mtx", bgfx::UniformType::Uniform4x4fv);
  114. bgfx::UniformHandle u_lightDir = bgfx::createUniform("u_lightDir", bgfx::UniformType::Uniform3fv);
  115. // Create program from shaders.
  116. bgfx::ProgramHandle raymarching = loadProgram("vs_raymarching", "fs_raymarching");
  117. int64_t timeOffset = bx::getHPCounter();
  118. while (!entry::processEvents(width, height, debug, reset) )
  119. {
  120. // Set view 0 default viewport.
  121. bgfx::setViewRect(0, 0, 0, width, height);
  122. // Set view 1 default viewport.
  123. bgfx::setViewRect(1, 0, 0, width, height);
  124. // This dummy draw call is here to make sure that view 0 is cleared
  125. // if no other draw calls are submitted to viewZ 0.
  126. bgfx::submit(0);
  127. int64_t now = bx::getHPCounter();
  128. static int64_t last = now;
  129. const int64_t frameTime = now - last;
  130. last = now;
  131. const double freq = double(bx::getHPFrequency() );
  132. const double toMs = 1000.0/freq;
  133. // Use debug font to print information about this example.
  134. bgfx::dbgTextClear();
  135. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/03-raymarch");
  136. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Updating shader uniforms.");
  137. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  138. float at[3] = { 0.0f, 0.0f, 0.0f };
  139. float eye[3] = { 0.0f, 0.0f, -15.0f };
  140. float view[16];
  141. float proj[16];
  142. bx::mtxLookAt(view, eye, at);
  143. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  144. // Set view and projection matrix for view 1.
  145. bgfx::setViewTransform(0, view, proj);
  146. float ortho[16];
  147. bx::mtxOrtho(ortho, 0.0f, 1280.0f, 720.0f, 0.0f, 0.0f, 100.0f);
  148. // Set view and projection matrix for view 0.
  149. bgfx::setViewTransform(1, NULL, ortho);
  150. float time = (float)( (bx::getHPCounter()-timeOffset)/double(bx::getHPFrequency() ) );
  151. float vp[16];
  152. bx::mtxMul(vp, view, proj);
  153. float mtx[16];
  154. bx::mtxRotateXY(mtx
  155. , time
  156. , time*0.37f
  157. );
  158. float mtxInv[16];
  159. bx::mtxInverse(mtxInv, mtx);
  160. float lightDirModel[4] = { -0.4f, -0.5f, -1.0f, 0.0f };
  161. float lightDirModelN[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
  162. bx::vec3Norm(lightDirModelN, lightDirModel);
  163. float lightDir[4];
  164. bx::vec4MulMtx(lightDir, lightDirModelN, mtxInv);
  165. bgfx::setUniform(u_lightDir, lightDir);
  166. float mvp[16];
  167. bx::mtxMul(mvp, mtx, vp);
  168. float invMvp[16];
  169. bx::mtxInverse(invMvp, mvp);
  170. bgfx::setUniform(u_mtx, invMvp);
  171. bgfx::setUniform(u_time, &time);
  172. renderScreenSpaceQuad(1, raymarching, 0.0f, 0.0f, 1280.0f, 720.0f);
  173. // Advance to next frame. Rendering thread will be kicked to
  174. // process submitted rendering primitives.
  175. bgfx::frame();
  176. }
  177. // Cleanup.
  178. bgfx::destroyProgram(raymarching);
  179. bgfx::destroyUniform(u_time);
  180. bgfx::destroyUniform(u_mtx);
  181. bgfx::destroyUniform(u_lightDir);
  182. // Shutdown bgfx.
  183. bgfx::shutdown();
  184. return 0;
  185. }