bump.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 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. class ExampleBump : public entry::AppI
  93. {
  94. void init(int _argc, char** _argv) BX_OVERRIDE
  95. {
  96. Args args(_argc, _argv);
  97. m_width = 1280;
  98. m_height = 720;
  99. m_debug = BGFX_DEBUG_TEXT;
  100. m_reset = BGFX_RESET_VSYNC;
  101. bgfx::init(args.m_type, args.m_pciId);
  102. bgfx::reset(m_width, m_height, m_reset);
  103. // Enable debug text.
  104. bgfx::setDebug(m_debug);
  105. // Set view 0 clear state.
  106. bgfx::setViewClear(0
  107. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  108. , 0x303030ff
  109. , 1.0f
  110. , 0
  111. );
  112. // Get renderer capabilities info.
  113. const bgfx::Caps* caps = bgfx::getCaps();
  114. m_instancingSupported = 0 != (caps->supported & BGFX_CAPS_INSTANCING);
  115. // Create vertex stream declaration.
  116. PosNormalTangentTexcoordVertex::init();
  117. calcTangents(s_cubeVertices
  118. , BX_COUNTOF(s_cubeVertices)
  119. , PosNormalTangentTexcoordVertex::ms_decl
  120. , s_cubeIndices
  121. , BX_COUNTOF(s_cubeIndices)
  122. );
  123. // Create static vertex buffer.
  124. m_vbh = bgfx::createVertexBuffer(
  125. bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) )
  126. , PosNormalTangentTexcoordVertex::ms_decl
  127. );
  128. // Create static index buffer.
  129. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  130. // Create texture sampler uniforms.
  131. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  132. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
  133. m_numLights = 4;
  134. u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Vec4, m_numLights);
  135. u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Vec4, m_numLights);
  136. // Create program from shaders.
  137. m_program = loadProgram(m_instancingSupported ? "vs_bump_instanced" : "vs_bump", "fs_bump");
  138. // Load diffuse texture.
  139. m_textureColor = loadTexture("textures/fieldstone-rgba.dds");
  140. // Load normal texture.
  141. m_textureNormal = loadTexture("textures/fieldstone-n.dds");
  142. m_timeOffset = bx::getHPCounter();
  143. }
  144. virtual int shutdown() BX_OVERRIDE
  145. {
  146. // Cleanup.
  147. bgfx::destroyIndexBuffer(m_ibh);
  148. bgfx::destroyVertexBuffer(m_vbh);
  149. bgfx::destroyProgram(m_program);
  150. bgfx::destroyTexture(m_textureColor);
  151. bgfx::destroyTexture(m_textureNormal);
  152. bgfx::destroyUniform(s_texColor);
  153. bgfx::destroyUniform(s_texNormal);
  154. bgfx::destroyUniform(u_lightPosRadius);
  155. bgfx::destroyUniform(u_lightRgbInnerR);
  156. // Shutdown bgfx.
  157. bgfx::shutdown();
  158. return 0;
  159. }
  160. bool update() BX_OVERRIDE
  161. {
  162. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  163. {
  164. // Set view 0 default viewport.
  165. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  166. // This dummy draw call is here to make sure that view 0 is cleared
  167. // if no other draw calls are submitted to view 0.
  168. bgfx::touch(0);
  169. int64_t now = bx::getHPCounter();
  170. static int64_t last = now;
  171. const int64_t frameTime = now - last;
  172. last = now;
  173. const double freq = double(bx::getHPFrequency() );
  174. const double toMs = 1000.0/freq;
  175. float time = (float)( (now-m_timeOffset)/freq);
  176. // Use debug font to print information about this example.
  177. bgfx::dbgTextClear();
  178. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/06-bump");
  179. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading textures.");
  180. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  181. float at[3] = { 0.0f, 0.0f, 0.0f };
  182. float eye[3] = { 0.0f, 0.0f, -7.0f };
  183. // Set view and projection matrix for view 0.
  184. const bgfx::HMD* hmd = bgfx::getHMD();
  185. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  186. {
  187. float view[16];
  188. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  189. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  190. // Set view 0 default viewport.
  191. //
  192. // Use HMD's width/height since HMD's internal frame buffer size
  193. // might be much larger than window size.
  194. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  195. }
  196. else
  197. {
  198. float view[16];
  199. bx::mtxLookAt(view, eye, at);
  200. float proj[16];
  201. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  202. bgfx::setViewTransform(0, view, proj);
  203. // Set view 0 default viewport.
  204. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  205. }
  206. float lightPosRadius[4][4];
  207. for (uint32_t ii = 0; ii < m_numLights; ++ii)
  208. {
  209. lightPosRadius[ii][0] = sinf( (time*(0.1f + ii*0.17f) + ii*bx::piHalf*1.37f ) )*3.0f;
  210. lightPosRadius[ii][1] = cosf( (time*(0.2f + ii*0.29f) + ii*bx::piHalf*1.49f ) )*3.0f;
  211. lightPosRadius[ii][2] = -2.5f;
  212. lightPosRadius[ii][3] = 3.0f;
  213. }
  214. bgfx::setUniform(u_lightPosRadius, lightPosRadius, m_numLights);
  215. float lightRgbInnerR[4][4] =
  216. {
  217. { 1.0f, 0.7f, 0.2f, 0.8f },
  218. { 0.7f, 0.2f, 1.0f, 0.8f },
  219. { 0.2f, 1.0f, 0.7f, 0.8f },
  220. { 1.0f, 0.4f, 0.2f, 0.8f },
  221. };
  222. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, m_numLights);
  223. const uint16_t instanceStride = 64;
  224. const uint16_t numInstances = 3;
  225. if (m_instancingSupported)
  226. {
  227. // Write instance data for 3x3 cubes.
  228. for (uint32_t yy = 0; yy < 3; ++yy)
  229. {
  230. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
  231. if (NULL != idb)
  232. {
  233. uint8_t* data = idb->data;
  234. for (uint32_t xx = 0; xx < 3; ++xx)
  235. {
  236. float* mtx = (float*)data;
  237. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  238. mtx[12] = -3.0f + float(xx)*3.0f;
  239. mtx[13] = -3.0f + float(yy)*3.0f;
  240. mtx[14] = 0.0f;
  241. data += instanceStride;
  242. }
  243. // Set instance data buffer.
  244. bgfx::setInstanceDataBuffer(idb, numInstances);
  245. // Set vertex and index buffer.
  246. bgfx::setVertexBuffer(m_vbh);
  247. bgfx::setIndexBuffer(m_ibh);
  248. // Bind textures.
  249. bgfx::setTexture(0, s_texColor, m_textureColor);
  250. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  251. // Set render states.
  252. bgfx::setState(0
  253. | BGFX_STATE_RGB_WRITE
  254. | BGFX_STATE_ALPHA_WRITE
  255. | BGFX_STATE_DEPTH_WRITE
  256. | BGFX_STATE_DEPTH_TEST_LESS
  257. | BGFX_STATE_MSAA
  258. );
  259. // Submit primitive for rendering to view 0.
  260. bgfx::submit(0, m_program);
  261. }
  262. }
  263. }
  264. else
  265. {
  266. for (uint32_t yy = 0; yy < 3; ++yy)
  267. {
  268. for (uint32_t xx = 0; xx < 3; ++xx)
  269. {
  270. float mtx[16];
  271. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  272. mtx[12] = -3.0f + float(xx)*3.0f;
  273. mtx[13] = -3.0f + float(yy)*3.0f;
  274. mtx[14] = 0.0f;
  275. // Set transform for draw call.
  276. bgfx::setTransform(mtx);
  277. // Set vertex and index buffer.
  278. bgfx::setVertexBuffer(m_vbh);
  279. bgfx::setIndexBuffer(m_ibh);
  280. // Bind textures.
  281. bgfx::setTexture(0, s_texColor, m_textureColor);
  282. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  283. // Set render states.
  284. bgfx::setState(0
  285. | BGFX_STATE_RGB_WRITE
  286. | BGFX_STATE_ALPHA_WRITE
  287. | BGFX_STATE_DEPTH_WRITE
  288. | BGFX_STATE_DEPTH_TEST_LESS
  289. | BGFX_STATE_MSAA
  290. );
  291. // Submit primitive for rendering to view 0.
  292. bgfx::submit(0, m_program);
  293. }
  294. }
  295. }
  296. // Advance to next frame. Rendering thread will be kicked to
  297. // process submitted rendering primitives.
  298. bgfx::frame();
  299. return true;
  300. }
  301. return false;
  302. }
  303. bgfx::VertexBufferHandle m_vbh;
  304. bgfx::IndexBufferHandle m_ibh;
  305. bgfx::UniformHandle s_texColor;
  306. bgfx::UniformHandle s_texNormal;
  307. bgfx::UniformHandle u_lightPosRadius;
  308. bgfx::UniformHandle u_lightRgbInnerR;
  309. bgfx::ProgramHandle m_program;
  310. bgfx::TextureHandle m_textureColor;
  311. bgfx::TextureHandle m_textureNormal;
  312. uint16_t m_numLights;
  313. bool m_instancingSupported;
  314. uint32_t m_width;
  315. uint32_t m_height;
  316. uint32_t m_debug;
  317. uint32_t m_reset;
  318. int64_t m_timeOffset;
  319. };
  320. ENTRY_IMPLEMENT_MAIN(ExampleBump);