bump.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx.h>
  7. #include <bx/timer.h>
  8. #include "fpumath.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. struct PosNormalTangentTexcoordVertex
  12. {
  13. float m_x;
  14. float m_y;
  15. float m_z;
  16. uint32_t m_normal;
  17. uint32_t m_tangent;
  18. int16_t m_u;
  19. int16_t m_v;
  20. };
  21. static bgfx::VertexDecl s_PosNormalTangentTexcoordDecl;
  22. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  23. {
  24. union
  25. {
  26. uint32_t ui32;
  27. uint8_t arr[4];
  28. } un;
  29. un.arr[0] = _x;
  30. un.arr[1] = _y;
  31. un.arr[2] = _z;
  32. un.arr[3] = _w;
  33. return un.ui32;
  34. }
  35. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  36. {
  37. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  38. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  39. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  40. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  41. return packUint32(xx, yy, zz, ww);
  42. }
  43. static PosNormalTangentTexcoordVertex s_cubeVertices[24] =
  44. {
  45. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0 },
  46. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0 },
  47. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0, 0x7fff },
  48. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, 0.0f, 1.0f), 0, 0x7fff, 0x7fff },
  49. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0 },
  50. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0 },
  51. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0, 0x7fff },
  52. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, 0.0f, -1.0f), 0, 0x7fff, 0x7fff },
  53. {-1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0 },
  54. { 1.0f, 1.0f, 1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0 },
  55. {-1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0, 0x7fff },
  56. { 1.0f, 1.0f, -1.0f, packF4u( 0.0f, 1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  57. {-1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0 },
  58. { 1.0f, -1.0f, 1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0 },
  59. {-1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0, 0x7fff },
  60. { 1.0f, -1.0f, -1.0f, packF4u( 0.0f, -1.0f, 0.0f), 0, 0x7fff, 0x7fff },
  61. { 1.0f, -1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0 },
  62. { 1.0f, 1.0f, 1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  63. { 1.0f, -1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  64. { 1.0f, 1.0f, -1.0f, packF4u( 1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  65. {-1.0f, -1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0 },
  66. {-1.0f, 1.0f, 1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0 },
  67. {-1.0f, -1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0, 0x7fff },
  68. {-1.0f, 1.0f, -1.0f, packF4u(-1.0f, 0.0f, 0.0f), 0, 0x7fff, 0x7fff },
  69. };
  70. static const uint16_t s_cubeIndices[36] =
  71. {
  72. 0, 2, 1,
  73. 1, 2, 3,
  74. 4, 5, 6,
  75. 5, 7, 6,
  76. 8, 10, 9,
  77. 9, 10, 11,
  78. 12, 13, 14,
  79. 13, 15, 14,
  80. 16, 18, 17,
  81. 17, 18, 19,
  82. 20, 21, 22,
  83. 21, 23, 22,
  84. };
  85. static const char* s_shaderPath = NULL;
  86. static void shaderFilePath(char* _out, const char* _name)
  87. {
  88. strcpy(_out, s_shaderPath);
  89. strcat(_out, _name);
  90. strcat(_out, ".bin");
  91. }
  92. long int fsize(FILE* _file)
  93. {
  94. long int pos = ftell(_file);
  95. fseek(_file, 0L, SEEK_END);
  96. long int size = ftell(_file);
  97. fseek(_file, pos, SEEK_SET);
  98. return size;
  99. }
  100. static const bgfx::Memory* load(const char* _filePath)
  101. {
  102. FILE* file = fopen(_filePath, "rb");
  103. if (NULL != file)
  104. {
  105. uint32_t size = (uint32_t)fsize(file);
  106. const bgfx::Memory* mem = bgfx::alloc(size+1);
  107. size_t ignore = fread(mem->data, 1, size, file);
  108. BX_UNUSED(ignore);
  109. fclose(file);
  110. mem->data[mem->size-1] = '\0';
  111. return mem;
  112. }
  113. return NULL;
  114. }
  115. static const bgfx::Memory* loadShader(const char* _name)
  116. {
  117. char filePath[512];
  118. shaderFilePath(filePath, _name);
  119. return load(filePath);
  120. }
  121. static const bgfx::Memory* loadTexture(const char* _name)
  122. {
  123. char filePath[512];
  124. strcpy(filePath, "textures/");
  125. strcat(filePath, _name);
  126. return load(filePath);
  127. }
  128. void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
  129. {
  130. struct PosTexcoord
  131. {
  132. float m_x;
  133. float m_y;
  134. float m_z;
  135. float m_pad0;
  136. float m_u;
  137. float m_v;
  138. float m_pad1;
  139. float m_pad2;
  140. };
  141. float* tangents = new float[6*_numVertices];
  142. memset(tangents, 0, 6*_numVertices*sizeof(float) );
  143. PosTexcoord v0;
  144. PosTexcoord v1;
  145. PosTexcoord v2;
  146. for (uint32_t ii = 0, num = _numIndices/3; ii < num; ++ii)
  147. {
  148. const uint16_t* indices = &_indices[ii*3];
  149. uint32_t i0 = indices[0];
  150. uint32_t i1 = indices[1];
  151. uint32_t i2 = indices[2];
  152. bgfx::vertexUnpack(&v0.m_x, bgfx::Attrib::Position, _decl, _vertices, i0);
  153. bgfx::vertexUnpack(&v0.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i0);
  154. bgfx::vertexUnpack(&v1.m_x, bgfx::Attrib::Position, _decl, _vertices, i1);
  155. bgfx::vertexUnpack(&v1.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i1);
  156. bgfx::vertexUnpack(&v2.m_x, bgfx::Attrib::Position, _decl, _vertices, i2);
  157. bgfx::vertexUnpack(&v2.m_u, bgfx::Attrib::TexCoord0, _decl, _vertices, i2);
  158. const float bax = v1.m_x - v0.m_x;
  159. const float bay = v1.m_y - v0.m_y;
  160. const float baz = v1.m_z - v0.m_z;
  161. const float bau = v1.m_u - v0.m_u;
  162. const float bav = v1.m_v - v0.m_v;
  163. const float cax = v2.m_x - v0.m_x;
  164. const float cay = v2.m_y - v0.m_y;
  165. const float caz = v2.m_z - v0.m_z;
  166. const float cau = v2.m_u - v0.m_u;
  167. const float cav = v2.m_v - v0.m_v;
  168. const float det = (bau * cav - bav * cau);
  169. const float invDet = 1.0f / det;
  170. const float tx = (bax * cav - cax * bav) * invDet;
  171. const float ty = (bay * cav - cay * bav) * invDet;
  172. const float tz = (baz * cav - caz * bav) * invDet;
  173. const float bx = (cax * bau - bax * cau) * invDet;
  174. const float by = (cay * bau - bay * cau) * invDet;
  175. const float bz = (caz * bau - baz * cau) * invDet;
  176. for (uint32_t jj = 0; jj < 3; ++jj)
  177. {
  178. float* tanu = &tangents[indices[jj]*6];
  179. float* tanv = &tanu[3];
  180. tanu[0] += tx;
  181. tanu[1] += ty;
  182. tanu[2] += tz;
  183. tanv[0] += bx;
  184. tanv[1] += by;
  185. tanv[2] += bz;
  186. }
  187. }
  188. for (uint32_t ii = 0; ii < _numVertices; ++ii)
  189. {
  190. const float* tanu = &tangents[ii*6];
  191. const float* tanv = &tangents[ii*6 + 3];
  192. float normal[4];
  193. bgfx::vertexUnpack(normal, bgfx::Attrib::Normal, _decl, _vertices, ii);
  194. float ndt = vec3Dot(normal, tanu);
  195. float nxt[3];
  196. vec3Cross(nxt, normal, tanu);
  197. float tmp[3];
  198. tmp[0] = tanu[0] - normal[0] * ndt;
  199. tmp[1] = tanu[1] - normal[1] * ndt;
  200. tmp[2] = tanu[2] - normal[2] * ndt;
  201. float tangent[4];
  202. vec3Norm(tangent, tmp);
  203. tangent[3] = vec3Dot(nxt, tanv) < 0.0f ? -1.0f : 1.0f;
  204. bgfx::vertexPack(tangent, true, bgfx::Attrib::Tangent, _decl, _vertices, ii);
  205. }
  206. delete [] tangents;
  207. }
  208. int _main_(int /*_argc*/, char** /*_argv*/)
  209. {
  210. uint32_t width = 1280;
  211. uint32_t height = 720;
  212. uint32_t debug = BGFX_DEBUG_TEXT;
  213. uint32_t reset = BGFX_RESET_VSYNC;
  214. bgfx::init();
  215. bgfx::reset(width, height, reset);
  216. // Enable debug text.
  217. bgfx::setDebug(debug);
  218. // Set view 0 clear state.
  219. bgfx::setViewClear(0
  220. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  221. , 0x303030ff
  222. , 1.0f
  223. , 0
  224. );
  225. // Get renderer capabilities info.
  226. const bgfx::Caps* caps = bgfx::getCaps();
  227. bool instancingSupported = 0 != (caps->supported & BGFX_CAPS_INSTANCING);
  228. // Setup root path for binary shaders. Shader binaries are different
  229. // for each renderer.
  230. switch (caps->rendererType)
  231. {
  232. default:
  233. case bgfx::RendererType::Direct3D9:
  234. s_shaderPath = "shaders/dx9/";
  235. break;
  236. case bgfx::RendererType::Direct3D11:
  237. s_shaderPath = "shaders/dx11/";
  238. break;
  239. case bgfx::RendererType::OpenGL:
  240. s_shaderPath = "shaders/glsl/";
  241. break;
  242. case bgfx::RendererType::OpenGLES2:
  243. case bgfx::RendererType::OpenGLES3:
  244. s_shaderPath = "shaders/gles/";
  245. break;
  246. }
  247. // Create vertex stream declaration.
  248. s_PosNormalTangentTexcoordDecl.begin();
  249. s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  250. s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true, true);
  251. s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true);
  252. s_PosNormalTangentTexcoordDecl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Int16, true, true);
  253. s_PosNormalTangentTexcoordDecl.end();
  254. const bgfx::Memory* mem;
  255. calcTangents(s_cubeVertices, BX_COUNTOF(s_cubeVertices), s_PosNormalTangentTexcoordDecl, s_cubeIndices, BX_COUNTOF(s_cubeIndices) );
  256. // Create static vertex buffer.
  257. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  258. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosNormalTangentTexcoordDecl);
  259. // Create static index buffer.
  260. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  261. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  262. // Create texture sampler uniforms.
  263. bgfx::UniformHandle u_texColor = bgfx::createUniform("u_texColor", bgfx::UniformType::Uniform1iv);
  264. bgfx::UniformHandle u_texNormal = bgfx::createUniform("u_texNormal", bgfx::UniformType::Uniform1iv);
  265. uint16_t numLights = 4;
  266. bgfx::UniformHandle u_lightPosRadius = bgfx::createUniform("u_lightPosRadius", bgfx::UniformType::Uniform4fv, numLights);
  267. bgfx::UniformHandle u_lightRgbInnerR = bgfx::createUniform("u_lightRgbInnerR", bgfx::UniformType::Uniform4fv, numLights);
  268. // Load vertex shader.
  269. mem = loadShader(instancingSupported ? "vs_bump_instanced" : "vs_bump");
  270. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  271. // Load fragment shader.
  272. mem = loadShader("fs_bump");
  273. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  274. // Create program from shaders.
  275. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  276. // We can destroy vertex and fragment shader here since
  277. // their reference is kept inside bgfx after calling createProgram.
  278. // Vertex and fragment shader will be destroyed once program is
  279. // destroyed.
  280. bgfx::destroyVertexShader(vsh);
  281. bgfx::destroyFragmentShader(fsh);
  282. // Load diffuse texture.
  283. mem = loadTexture("fieldstone-rgba.dds");
  284. bgfx::TextureHandle textureColor = bgfx::createTexture(mem);
  285. // Load normal texture.
  286. mem = loadTexture("fieldstone-n.dds");
  287. bgfx::TextureHandle textureNormal = bgfx::createTexture(mem);
  288. int64_t timeOffset = bx::getHPCounter();
  289. while (!entry::processEvents(width, height, debug, reset) )
  290. {
  291. // Set view 0 default viewport.
  292. bgfx::setViewRect(0, 0, 0, width, height);
  293. // This dummy draw call is here to make sure that view 0 is cleared
  294. // if no other draw calls are submitted to view 0.
  295. bgfx::submit(0);
  296. int64_t now = bx::getHPCounter();
  297. static int64_t last = now;
  298. const int64_t frameTime = now - last;
  299. last = now;
  300. const double freq = double(bx::getHPFrequency() );
  301. const double toMs = 1000.0/freq;
  302. float time = (float)( (now-timeOffset)/freq);
  303. // Use debug font to print information about this example.
  304. bgfx::dbgTextClear();
  305. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/06-bump");
  306. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading textures.");
  307. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  308. float at[3] = { 0.0f, 0.0f, 0.0f };
  309. float eye[3] = { 0.0f, 0.0f, -7.0f };
  310. float view[16];
  311. float proj[16];
  312. mtxLookAt(view, eye, at);
  313. mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  314. float lightPosRadius[4][4];
  315. for (uint32_t ii = 0; ii < numLights; ++ii)
  316. {
  317. lightPosRadius[ii][0] = sin( (time*(0.1f + ii*0.17f) + float(ii*M_PI_2)*1.37f ) )*3.0f;
  318. lightPosRadius[ii][1] = cos( (time*(0.2f + ii*0.29f) + float(ii*M_PI_2)*1.49f ) )*3.0f;
  319. lightPosRadius[ii][2] = -2.5f;
  320. lightPosRadius[ii][3] = 3.0f;
  321. }
  322. bgfx::setUniform(u_lightPosRadius, lightPosRadius, numLights);
  323. float lightRgbInnerR[4][4] =
  324. {
  325. { 1.0f, 0.7f, 0.2f, 0.8f },
  326. { 0.7f, 0.2f, 1.0f, 0.8f },
  327. { 0.2f, 1.0f, 0.7f, 0.8f },
  328. { 1.0f, 0.4f, 0.2f, 0.8f },
  329. };
  330. bgfx::setUniform(u_lightRgbInnerR, lightRgbInnerR, numLights);
  331. // Set view and projection matrix for view 0.
  332. bgfx::setViewTransform(0, view, proj);
  333. const uint16_t instanceStride = 64;
  334. const uint16_t numInstances = 3;
  335. if (instancingSupported)
  336. {
  337. // Write instance data for 3x3 cubes.
  338. for (uint32_t yy = 0; yy < 3; ++yy)
  339. {
  340. const bgfx::InstanceDataBuffer* idb = bgfx::allocInstanceDataBuffer(numInstances, instanceStride);
  341. if (NULL != idb)
  342. {
  343. uint8_t* data = idb->data;
  344. for (uint32_t xx = 0; xx < 3; ++xx)
  345. {
  346. float* mtx = (float*)data;
  347. mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  348. mtx[12] = -3.0f + float(xx)*3.0f;
  349. mtx[13] = -3.0f + float(yy)*3.0f;
  350. mtx[14] = 0.0f;
  351. data += instanceStride;
  352. }
  353. // Set instance data buffer.
  354. bgfx::setInstanceDataBuffer(idb, numInstances);
  355. // Set vertex and fragment shaders.
  356. bgfx::setProgram(program);
  357. // Set vertex and index buffer.
  358. bgfx::setVertexBuffer(vbh);
  359. bgfx::setIndexBuffer(ibh);
  360. // Bind textures.
  361. bgfx::setTexture(0, u_texColor, textureColor);
  362. bgfx::setTexture(1, u_texNormal, textureNormal);
  363. // Set render states.
  364. bgfx::setState(0
  365. |BGFX_STATE_RGB_WRITE
  366. |BGFX_STATE_ALPHA_WRITE
  367. |BGFX_STATE_DEPTH_WRITE
  368. |BGFX_STATE_DEPTH_TEST_LESS
  369. |BGFX_STATE_MSAA
  370. );
  371. // Submit primitive for rendering to view 0.
  372. bgfx::submit(0);
  373. }
  374. }
  375. }
  376. else
  377. {
  378. for (uint32_t yy = 0; yy < 3; ++yy)
  379. {
  380. for (uint32_t xx = 0; xx < 3; ++xx)
  381. {
  382. float mtx[16];
  383. mtxRotateXY(mtx, time*0.023f + xx*0.21f, time*0.03f + yy*0.37f);
  384. mtx[12] = -3.0f + float(xx)*3.0f;
  385. mtx[13] = -3.0f + float(yy)*3.0f;
  386. mtx[14] = 0.0f;
  387. // Set transform for draw call.
  388. bgfx::setTransform(mtx);
  389. // Set vertex and fragment shaders.
  390. bgfx::setProgram(program);
  391. // Set vertex and index buffer.
  392. bgfx::setVertexBuffer(vbh);
  393. bgfx::setIndexBuffer(ibh);
  394. // Bind textures.
  395. bgfx::setTexture(0, u_texColor, textureColor);
  396. bgfx::setTexture(1, u_texNormal, textureNormal);
  397. // Set render states.
  398. bgfx::setState(0
  399. |BGFX_STATE_RGB_WRITE
  400. |BGFX_STATE_ALPHA_WRITE
  401. |BGFX_STATE_DEPTH_WRITE
  402. |BGFX_STATE_DEPTH_TEST_LESS
  403. |BGFX_STATE_MSAA
  404. );
  405. // Submit primitive for rendering to view 0.
  406. bgfx::submit(0);
  407. }
  408. }
  409. }
  410. // Advance to next frame. Rendering thread will be kicked to
  411. // process submitted rendering primitives.
  412. bgfx::frame();
  413. }
  414. // Cleanup.
  415. bgfx::destroyIndexBuffer(ibh);
  416. bgfx::destroyVertexBuffer(vbh);
  417. bgfx::destroyProgram(program);
  418. bgfx::destroyTexture(textureColor);
  419. bgfx::destroyTexture(textureNormal);
  420. bgfx::destroyUniform(u_texColor);
  421. bgfx::destroyUniform(u_texNormal);
  422. bgfx::destroyUniform(u_lightPosRadius);
  423. bgfx::destroyUniform(u_lightRgbInnerR);
  424. // Shutdown bgfx.
  425. bgfx::shutdown();
  426. return 0;
  427. }