bump.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright 2011-2015 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 PosNormalTangentTexcoordVertex
  8. {
  9. float m_x;
  10. float m_y;
  11. float m_z;
  12. uint32_t m_normal;
  13. uint32_t m_tangent;
  14. int16_t m_u;
  15. int16_t m_v;
  16. static void init()
  17. {
  18. ms_decl
  19. .begin()
  20. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  21. .add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true)
  22. .add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
  23. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true)
  24. .end();
  25. }
  26. static bgfx::VertexDecl ms_decl;
  27. };
  28. bgfx::VertexDecl PosNormalTangentTexcoordVertex::ms_decl;
  29. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  30. {
  31. union
  32. {
  33. uint32_t ui32;
  34. uint8_t arr[4];
  35. } un;
  36. un.arr[0] = _x;
  37. un.arr[1] = _y;
  38. un.arr[2] = _z;
  39. un.arr[3] = _w;
  40. return un.ui32;
  41. }
  42. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  43. {
  44. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  45. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  46. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  47. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  48. return packUint32(xx, yy, zz, ww);
  49. }
  50. static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
  51. {
  52. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
  53. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
  54. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
  55. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
  56. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
  57. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
  58. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
  59. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
  60. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
  61. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
  62. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
  63. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  64. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
  65. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
  66. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
  67. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  68. { 1.0f, -1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
  69. { 1.0f, 1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  70. { 1.0f, -1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  71. { 1.0f, 1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  72. {-1.0f, -1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
  73. {-1.0f, 1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  74. {-1.0f, -1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  75. {-1.0f, 1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  76. };
  77. static const uint16_t s_cubeIndices[36] =
  78. {
  79. 0, 2, 1,
  80. 1, 2, 3,
  81. 4, 5, 6,
  82. 5, 7, 6,
  83. 8, 10, 9,
  84. 9, 10, 11,
  85. 12, 13, 14,
  86. 13, 15, 14,
  87. 16, 18, 17,
  88. 17, 18, 19,
  89. 20, 21, 22,
  90. 21, 23, 22,
  91. };
  92. int _main_(int /*_argc*/, char** /*_argv*/)
  93. {
  94. uint32_t width = 1280;
  95. uint32_t height = 720;
  96. uint32_t debug = BGFX_DEBUG_TEXT;
  97. uint32_t reset = BGFX_RESET_VSYNC;
  98. bgfx::init();
  99. bgfx::reset(width, height, reset);
  100. // Enable debug text.
  101. bgfx::setDebug(debug);
  102. // Set view 0 clear state.
  103. bgfx::setViewClear(0
  104. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  105. , 0x303030ff
  106. , 1.0f
  107. , 0
  108. );
  109. // Get renderer capabilities info.
  110. const bgfx::Caps* caps = bgfx::getCaps();
  111. bool instancingSupported = 0 != (caps->supported & BGFX_CAPS_INSTANCING);
  112. // Create vertex stream declaration.
  113. PosNormalTangentTexcoordVertex::init();
  114. calcTangents(s_cubeVertices
  115. , BX_COUNTOF(s_cubeVertices)
  116. , PosNormalTangentTexcoordVertex::ms_decl
  117. , s_cubeIndices
  118. , BX_COUNTOF(s_cubeIndices)
  119. );
  120. // Create static vertex buffer.
  121. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(
  122. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  123. , PosNormalTangentTexcoordVertex::ms_decl
  124. );
  125. // Create static index buffer.
  126. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  127. // Create texture sampler uniforms.
  128. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  129. bgfx::UniformHandle u_texNormal = bgfx::createUniform("u_texNormal", bgfx::UniformType::Uniform1iv);
  130. uint16_t numLights = 4;
  131. bgfx::UniformHandle u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Uniform4fv, numLights);
  132. bgfx::UniformHandle u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Uniform4fv, numLights);
  133. // Create program from shaders.
  134. bgfx::ProgramHandle program = loadProgram(instancingSupported ? "vs_bump_instanced" : "vs_bump", "fs_bump");
  135. // Load diffuse texture.
  136. bgfx::TextureHandle textureColor = loadTexture("fieldstone-rgba.dds");
  137. // Load normal texture.
  138. bgfx::TextureHandle textureNormal = loadTexture("fieldstone-n.dds");
  139. int64_t timeOffset = bx::getHPCounter();
  140. while (!entry::processEvents(width, height, debug, reset) )
  141. {
  142. // Set view 0 default viewport.
  143. bgfx::setViewRect(0, 0, 0, width, height);
  144. // This dummy draw call is here to make sure that view 0 is cleared
  145. // if no other draw calls are submitted to view 0.
  146. bgfx::submit(0);
  147. int64_t now = bx::getHPCounter();
  148. static int64_t last = now;
  149. const int64_t frameTime = now - last;
  150. last = now;
  151. const double freq = double(bx::getHPFrequency() );
  152. const double toMs = 1000.0/freq;
  153. float time = (float)( (now-timeOffset)/freq);
  154. // Use debug font to print information about this example.
  155. bgfx::dbgTextClear();
  156. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/06-bump");
  157. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading textures.");
  158. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  159. float at[3] = { 0.0f, 0.0f, 0.0f };
  160. float eye[3] = { 0.0f, 0.0f, -7.0f };
  161. // Set view and projection matrix for view 0.
  162. const bgfx::HMD* hmd = bgfx::getHMD();
  163. if (NULL != hmd)
  164. {
  165. float view[16];
  166. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  167. float proj[16];
  168. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  169. bgfx::setViewTransform(0, view, proj);
  170. // Set view 0 default viewport.
  171. //
  172. // Use HMD's width/height since HMD's internal frame buffer size
  173. // might be much larger than window size.
  174. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  175. }
  176. else
  177. {
  178. float view[16];
  179. bx::mtxLookAt(view, eye, at);
  180. float proj[16];
  181. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  182. bgfx::setViewTransform(0, view, proj);
  183. // Set view 0 default viewport.
  184. bgfx::setViewRect(0, 0, 0, width, height);
  185. }
  186. float lightPosRadius[4][4];
  187. for (uint32_t ii = 0; ii < numLights; ++ii)
  188. {
  189. lightPosRadius[ii][0] = sin( (time*(0.1f + ii*0.17f) + ii*bx::piHalf*1.37f ) )*3.0f;
  190. lightPosRadius[ii][1] = cos( (time*(0.2f + ii*0.29f) + ii*bx::piHalf*1.49f ) )*3.0f;
  191. lightPosRadius[ii][2] = -2.5f;
  192. lightPosRadius[ii][3] = 3.0f;
  193. }
  194. bgfx::setUniform(u_lightPosRadius, lightPosRadius, numLights);
  195. float lightRgbInnerR[4][4] =
  196. {
  197. { 1.0f, 0.7f, 0.2f, 0.8f },
  198. { 0.7f, 0.2f, 1.0f, 0.8f },
  199. { 0.2f, 1.0f, 0.7f, 0.8f },
  200. { 1.0f, 0.4f, 0.2f, 0.8f },
  201. };
  202. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, numLights);
  203. const uint16_t instanceStride = 64;
  204. const uint16_t numInstances = 3;
  205. if (instancingSupported)
  206. {
  207. // Write instance data for 3x3 cubes.
  208. for (uint32_t yy = 0; yy < 3; ++yy)
  209. {
  210. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
  211. if (NULL != idb)
  212. {
  213. uint8_t* data = idb->data;
  214. for (uint32_t xx = 0; xx < 3; ++xx)
  215. {
  216. float* mtx = (float*)data;
  217. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  218. mtx[12] = -3.0f + float(xx)*3.0f;
  219. mtx[13] = -3.0f + float(yy)*3.0f;
  220. mtx[14] = 0.0f;
  221. data += instanceStride;
  222. }
  223. // Set instance data buffer.
  224. bgfx::setInstanceDataBuffer(idb, numInstances);
  225. // Set vertex and fragment shaders.
  226. bgfx::setProgram(program);
  227. // Set vertex and index buffer.
  228. bgfx::setVertexBuffer(vbh);
  229. bgfx::setIndexBuffer(ibh);
  230. // Bind textures.
  231. bgfx::setTexture(0, u_texColor, textureColor);
  232. bgfx::setTexture(1, u_texNormal, textureNormal);
  233. // Set render states.
  234. bgfx::setState(0
  235. | BGFX_STATE_RGB_WRITE
  236. | BGFX_STATE_ALPHA_WRITE
  237. | BGFX_STATE_DEPTH_WRITE
  238. | BGFX_STATE_DEPTH_TEST_LESS
  239. | BGFX_STATE_MSAA
  240. );
  241. // Submit primitive for rendering to view 0.
  242. bgfx::submit(0);
  243. }
  244. }
  245. }
  246. else
  247. {
  248. for (uint32_t yy = 0; yy < 3; ++yy)
  249. {
  250. for (uint32_t xx = 0; xx < 3; ++xx)
  251. {
  252. float mtx[16];
  253. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  254. mtx[12] = -3.0f + float(xx)*3.0f;
  255. mtx[13] = -3.0f + float(yy)*3.0f;
  256. mtx[14] = 0.0f;
  257. // Set transform for draw call.
  258. bgfx::setTransform(mtx);
  259. // Set vertex and fragment shaders.
  260. bgfx::setProgram(program);
  261. // Set vertex and index buffer.
  262. bgfx::setVertexBuffer(vbh);
  263. bgfx::setIndexBuffer(ibh);
  264. // Bind textures.
  265. bgfx::setTexture(0, u_texColor, textureColor);
  266. bgfx::setTexture(1, u_texNormal, textureNormal);
  267. // Set render states.
  268. bgfx::setState(0
  269. | BGFX_STATE_RGB_WRITE
  270. | BGFX_STATE_ALPHA_WRITE
  271. | BGFX_STATE_DEPTH_WRITE
  272. | BGFX_STATE_DEPTH_TEST_LESS
  273. | BGFX_STATE_MSAA
  274. );
  275. // Submit primitive for rendering to view 0.
  276. bgfx::submit(0);
  277. }
  278. }
  279. }
  280. // Advance to next frame. Rendering thread will be kicked to
  281. // process submitted rendering primitives.
  282. bgfx::frame();
  283. }
  284. // Cleanup.
  285. bgfx::destroyIndexBuffer(ibh);
  286. bgfx::destroyVertexBuffer(vbh);
  287. bgfx::destroyProgram(program);
  288. bgfx::destroyTexture(textureColor);
  289. bgfx::destroyTexture(textureNormal);
  290. bgfx::destroyUniform(u_texColor);
  291. bgfx::destroyUniform(u_texNormal);
  292. bgfx::destroyUniform(u_lightPosRadius);
  293. bgfx::destroyUniform(u_lightRgbInnerR);
  294. // Shutdown bgfx.
  295. bgfx::shutdown();
  296. return 0;
  297. }