bump.cpp 13 KB

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