bump.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 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_BIT|BGFX_CLEAR_DEPTH_BIT
  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. float view[16];
  162. float proj[16];
  163. bx::mtxLookAt(view, eye, at);
  164. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  165. float lightPosRadius[4][4];
  166. for (uint32_t ii = 0; ii < numLights; ++ii)
  167. {
  168. lightPosRadius[ii][0] = sin( (time*(0.1f + ii*0.17f) + float(ii*M_PI_2)*1.37f ) )*3.0f;
  169. lightPosRadius[ii][1] = cos( (time*(0.2f + ii*0.29f) + float(ii*M_PI_2)*1.49f ) )*3.0f;
  170. lightPosRadius[ii][2] = -2.5f;
  171. lightPosRadius[ii][3] = 3.0f;
  172. }
  173. bgfx::setUniform(u_lightPosRadius, lightPosRadius, numLights);
  174. float lightRgbInnerR[4][4] =
  175. {
  176. { 1.0f, 0.7f, 0.2f, 0.8f },
  177. { 0.7f, 0.2f, 1.0f, 0.8f },
  178. { 0.2f, 1.0f, 0.7f, 0.8f },
  179. { 1.0f, 0.4f, 0.2f, 0.8f },
  180. };
  181. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, numLights);
  182. // Set view and projection matrix for view 0.
  183. bgfx::setViewTransform(0, view, proj);
  184. const uint16_t instanceStride = 64;
  185. const uint16_t numInstances = 3;
  186. if (instancingSupported)
  187. {
  188. // Write instance data for 3x3 cubes.
  189. for (uint32_t yy = 0; yy < 3; ++yy)
  190. {
  191. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
  192. if (NULL != idb)
  193. {
  194. uint8_t* data = idb->data;
  195. for (uint32_t xx = 0; xx < 3; ++xx)
  196. {
  197. float* mtx = (float*)data;
  198. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  199. mtx[12] = -3.0f + float(xx)*3.0f;
  200. mtx[13] = -3.0f + float(yy)*3.0f;
  201. mtx[14] = 0.0f;
  202. data += instanceStride;
  203. }
  204. // Set instance data buffer.
  205. bgfx::setInstanceDataBuffer(idb, numInstances);
  206. // Set vertex and fragment shaders.
  207. bgfx::setProgram(program);
  208. // Set vertex and index buffer.
  209. bgfx::setVertexBuffer(vbh);
  210. bgfx::setIndexBuffer(ibh);
  211. // Bind textures.
  212. bgfx::setTexture(0, u_texColor, textureColor);
  213. bgfx::setTexture(1, u_texNormal, textureNormal);
  214. // Set render states.
  215. bgfx::setState(0
  216. | BGFX_STATE_RGB_WRITE
  217. | BGFX_STATE_ALPHA_WRITE
  218. | BGFX_STATE_DEPTH_WRITE
  219. | BGFX_STATE_DEPTH_TEST_LESS
  220. | BGFX_STATE_MSAA
  221. );
  222. // Submit primitive for rendering to view 0.
  223. bgfx::submit(0);
  224. }
  225. }
  226. }
  227. else
  228. {
  229. for (uint32_t yy = 0; yy < 3; ++yy)
  230. {
  231. for (uint32_t xx = 0; xx < 3; ++xx)
  232. {
  233. float mtx[16];
  234. bx::mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  235. mtx[12] = -3.0f + float(xx)*3.0f;
  236. mtx[13] = -3.0f + float(yy)*3.0f;
  237. mtx[14] = 0.0f;
  238. // Set transform for draw call.
  239. bgfx::setTransform(mtx);
  240. // Set vertex and fragment shaders.
  241. bgfx::setProgram(program);
  242. // Set vertex and index buffer.
  243. bgfx::setVertexBuffer(vbh);
  244. bgfx::setIndexBuffer(ibh);
  245. // Bind textures.
  246. bgfx::setTexture(0, u_texColor, textureColor);
  247. bgfx::setTexture(1, u_texNormal, textureNormal);
  248. // Set render states.
  249. bgfx::setState(0
  250. | BGFX_STATE_RGB_WRITE
  251. | BGFX_STATE_ALPHA_WRITE
  252. | BGFX_STATE_DEPTH_WRITE
  253. | BGFX_STATE_DEPTH_TEST_LESS
  254. | BGFX_STATE_MSAA
  255. );
  256. // Submit primitive for rendering to view 0.
  257. bgfx::submit(0);
  258. }
  259. }
  260. }
  261. // Advance to next frame. Rendering thread will be kicked to
  262. // process submitted rendering primitives.
  263. bgfx::frame();
  264. }
  265. // Cleanup.
  266. bgfx::destroyIndexBuffer(ibh);
  267. bgfx::destroyVertexBuffer(vbh);
  268. bgfx::destroyProgram(program);
  269. bgfx::destroyTexture(textureColor);
  270. bgfx::destroyTexture(textureNormal);
  271. bgfx::destroyUniform(u_texColor);
  272. bgfx::destroyUniform(u_texNormal);
  273. bgfx::destroyUniform(u_lightPosRadius);
  274. bgfx::destroyUniform(u_lightRgbInnerR);
  275. // Shutdown bgfx.
  276. bgfx::shutdown();
  277. return 0;
  278. }