pom.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright 2011-2017 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. #include <imgui/imgui.h>
  8. struct PosTangentBitangentTexcoordVertex
  9. {
  10. float m_x;
  11. float m_y;
  12. float m_z;
  13. uint32_t m_tangent;
  14. uint32_t m_bitangent;
  15. float m_u;
  16. float m_v;
  17. static void init()
  18. {
  19. ms_decl
  20. .begin()
  21. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  22. .add(bgfx::Attrib::Tangent, 4, bgfx::AttribType::Uint8, true, true)
  23. .add(bgfx::Attrib::Bitangent, 4, bgfx::AttribType::Uint8, true, true)
  24. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, true, true)
  25. .end();
  26. }
  27. static bgfx::VertexDecl ms_decl;
  28. };
  29. bgfx::VertexDecl PosTangentBitangentTexcoordVertex::ms_decl;
  30. uint32_t packUint32(uint8_t _x, uint8_t _y, uint8_t _z, uint8_t _w)
  31. {
  32. union
  33. {
  34. uint32_t ui32;
  35. uint8_t arr[4];
  36. } un;
  37. un.arr[0] = _x;
  38. un.arr[1] = _y;
  39. un.arr[2] = _z;
  40. un.arr[3] = _w;
  41. return un.ui32;
  42. }
  43. uint32_t packF4u(float _x, float _y = 0.0f, float _z = 0.0f, float _w = 0.0f)
  44. {
  45. const uint8_t xx = uint8_t(_x*127.0f + 128.0f);
  46. const uint8_t yy = uint8_t(_y*127.0f + 128.0f);
  47. const uint8_t zz = uint8_t(_z*127.0f + 128.0f);
  48. const uint8_t ww = uint8_t(_w*127.0f + 128.0f);
  49. return packUint32(xx, yy, zz, ww);
  50. }
  51. static PosTangentBitangentTexcoordVertex s_cubeVertices[24] =
  52. {
  53. {-1, -1, 1, packF4u(-1, 0, 0), packF4u( 0, -1, 0), 1, 1 }, // Back
  54. { 1, 1, 1, packF4u(-1, 0, 0), packF4u( 0, -1, 0), 0, 0 },
  55. {-1, 1, 1, packF4u(-1, 0, 0), packF4u( 0, -1, 0), 1, 0 },
  56. { 1, -1, 1, packF4u(-1, 0, 0), packF4u( 0, -1, 0), 0, 1 },
  57. {-1, -1, -1, packF4u( 1, 0, 0), packF4u( 0, -1, 0), 0, 1 }, // Front
  58. { 1, 1, -1, packF4u( 1, 0, 0), packF4u( 0, -1, 0), 1, 0 },
  59. {-1, 1, -1, packF4u( 1, 0, 0), packF4u( 0, -1, 0), 0, 0 },
  60. { 1, -1, -1, packF4u( 1, 0, 0), packF4u( 0, -1, 0), 1, 1 },
  61. { 1, -1, -1, packF4u( 0, 0, 1), packF4u( 0, -1, 0), 0, 1 }, // Right
  62. { 1, 1, 1, packF4u( 0, 0, 1), packF4u( 0, -1, 0), 1, 0 },
  63. { 1, -1, 1, packF4u( 0, 0, 1), packF4u( 0, -1, 0), 1, 1 },
  64. { 1, 1, -1, packF4u( 0, 0, 1), packF4u( 0, -1, 0), 0, 0 },
  65. {-1, -1, -1, packF4u( 0, 0, -1), packF4u( 0, -1, 0), 1, 1 }, // Left
  66. {-1, 1, 1, packF4u( 0, 0, -1), packF4u( 0, -1, 0), 0, 0 },
  67. {-1, -1, 1, packF4u( 0, 0, -1), packF4u( 0, -1, 0), 0, 1 },
  68. {-1, 1, -1, packF4u( 0, 0, -1), packF4u( 0, -1, 0), 1, 0 },
  69. {-1, 1, -1, packF4u( 1, 0, 0), packF4u( 0, 0, -1), 0, 1 }, // Top
  70. { 1, 1, 1, packF4u( 1, 0, 0), packF4u( 0, 0, -1), 1, 0 },
  71. {-1, 1, 1, packF4u( 1, 0, 0), packF4u( 0, 0, -1), 0, 0 },
  72. { 1, 1, -1, packF4u( 1, 0, 0), packF4u( 0, 0, -1), 1, 1 },
  73. {-1, -1, -1, packF4u( 1, 0, 0), packF4u( 0, 0, 1), 0, 0 }, // Bottom
  74. { 1, -1, 1, packF4u( 1, 0, 0), packF4u( 0, 0, 1), 1, 1 },
  75. {-1, -1, 1, packF4u( 1, 0, 0), packF4u( 0, 0, 1), 0, 1 },
  76. { 1, -1, -1, packF4u( 1, 0, 0), packF4u( 0, 0, 1), 1, 0 },
  77. };
  78. static const uint16_t s_cubeIndices[36] =
  79. {
  80. 0 , 1 , 2 ,
  81. 0 , 3 , 1 ,
  82. 4 , 6 , 5 ,
  83. 4 , 5 , 7 ,
  84. 8 , 9 , 10,
  85. 8 , 11, 9 ,
  86. 12, 14, 13,
  87. 12, 13, 15,
  88. 16, 18, 17,
  89. 16, 17, 19,
  90. 20, 21, 22,
  91. 20, 23, 21,
  92. };
  93. class ExamplePom : public entry::AppI
  94. {
  95. void init(int _argc, char** _argv) BX_OVERRIDE
  96. {
  97. Args args(_argc, _argv);
  98. m_width = 1280;
  99. m_height = 720;
  100. m_debug = BGFX_DEBUG_TEXT;
  101. m_reset = BGFX_RESET_VSYNC;
  102. bgfx::init(args.m_type, args.m_pciId);
  103. bgfx::reset(m_width, m_height, m_reset);
  104. // Enable debug text.
  105. bgfx::setDebug(m_debug);
  106. // Set view 0 clear state.
  107. bgfx::setViewClear(0
  108. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  109. , 0x303030ff
  110. , 1.0f
  111. , 0
  112. );
  113. // Create vertex stream declaration.
  114. PosTangentBitangentTexcoordVertex::init();
  115. // Create static vertex buffer.
  116. m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ),
  117. PosTangentBitangentTexcoordVertex::ms_decl);
  118. // Create static index buffer.
  119. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  120. // Create texture sampler uniforms.
  121. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  122. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
  123. s_texDepth = bgfx::createUniform("s_texDepth", bgfx::UniformType::Int1);
  124. u_light_pos = bgfx::createUniform("u_light_pos", bgfx::UniformType::Vec4);
  125. u_norm_mtx = bgfx::createUniform("u_norm_mtx", bgfx::UniformType::Mat4);
  126. u_pomParam = bgfx::createUniform("u_pomParam", bgfx::UniformType::Vec4);
  127. // Create program from shaders.
  128. m_program = loadProgram("vs_pom", "fs_pom");
  129. // Load diffuse texture.
  130. m_textureColor = loadTexture("textures/parallax-d.ktx");
  131. // Load normal texture.
  132. m_textureNormal = loadTexture("textures/parallax-n.ktx");
  133. // Load depth texture.
  134. m_textureDepth = loadTexture("textures/parallax-h.ktx");
  135. imguiCreate();
  136. m_timeOffset = bx::getHPCounter();
  137. m_shading_type = 4;
  138. m_show_diffuse_texture = true;
  139. m_parallax_scale = 50;
  140. m_num_steps = 16;
  141. }
  142. virtual int shutdown() BX_OVERRIDE
  143. {
  144. // Cleanup.
  145. bgfx::destroyIndexBuffer(m_ibh);
  146. bgfx::destroyVertexBuffer(m_vbh);
  147. bgfx::destroyProgram(m_program);
  148. bgfx::destroyTexture(m_textureColor);
  149. bgfx::destroyTexture(m_textureNormal);
  150. bgfx::destroyTexture(m_textureDepth);
  151. bgfx::destroyUniform(s_texColor);
  152. bgfx::destroyUniform(s_texNormal);
  153. bgfx::destroyUniform(s_texDepth);
  154. bgfx::destroyUniform(u_light_pos);
  155. bgfx::destroyUniform(u_norm_mtx);
  156. bgfx::destroyUniform(u_pomParam);
  157. imguiDestroy();
  158. // Shutdown bgfx.
  159. bgfx::shutdown();
  160. return 0;
  161. }
  162. bool update() BX_OVERRIDE
  163. {
  164. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  165. {
  166. // Set view 0 default viewport.
  167. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  168. // This dummy draw call is here to make sure that view 0 is cleared
  169. // if no other draw calls are submitted to view 0.
  170. bgfx::touch(0);
  171. int64_t now = bx::getHPCounter();
  172. static int64_t last = now;
  173. const int64_t frameTime = now - last;
  174. last = now;
  175. const double freq = double(bx::getHPFrequency() );
  176. const double toMs = 1000.0/freq;
  177. float time = (float)( (now-m_timeOffset)/freq);
  178. // Use debug font to print information about this example.
  179. bgfx::dbgTextClear();
  180. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/33-pom");
  181. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Parallax mapping.");
  182. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  183. float at[3] = { 0.0f, 0.0f, 1.0f };
  184. float eye[3] = { 0.0f, 0.0f, 0.0f };
  185. // Set view and projection matrix for view 0.
  186. const bgfx::HMD* hmd = bgfx::getHMD();
  187. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  188. {
  189. float view[16];
  190. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  191. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  192. // Set view 0 default viewport.
  193. //
  194. // Use HMD's width/height since HMD's internal frame buffer size
  195. // might be much larger than window size.
  196. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  197. }
  198. else
  199. {
  200. float view[16];
  201. bx::mtxLookAt(view, eye, at);
  202. float proj[16];
  203. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  204. bgfx::setViewTransform(0, view, proj);
  205. // Set view 0 default viewport.
  206. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  207. }
  208. imguiBeginFrame(
  209. m_mouseState.m_mx
  210. , m_mouseState.m_my
  211. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  212. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  213. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  214. , m_mouseState.m_mz
  215. , uint16_t(m_width)
  216. , uint16_t(m_height)
  217. );
  218. ImGui::Begin("Properties");
  219. ImGui::SetWindowPos(ImVec2(m_width - 240.0f, 20.0f));
  220. ImGui::RadioButton("No bump mapping", &m_shading_type, 0);
  221. ImGui::RadioButton("Normal mapping", &m_shading_type, 1);
  222. ImGui::RadioButton("Parallax mapping", &m_shading_type, 2);
  223. ImGui::RadioButton("Steep parallax mapping", &m_shading_type, 3);
  224. ImGui::RadioButton("Parallax occlusion mapping", &m_shading_type, 4);
  225. ImGui::Separator();
  226. ImGui::Checkbox("Show diffuse texture", &m_show_diffuse_texture);
  227. if (m_shading_type > 1)
  228. {
  229. ImGui::Separator();
  230. float multiplier = 1000.0f;
  231. float x = (float)m_parallax_scale / multiplier;
  232. ImGui::SliderFloat("Parallax scale", &x, 0.0f, 0.1f);
  233. m_parallax_scale = (int32_t)(x * multiplier);
  234. }
  235. if (m_shading_type > 2)
  236. {
  237. ImGui::Separator();
  238. ImGui::SliderInt("Number of steps", &m_num_steps, 1, 32);
  239. }
  240. ImGui::End();
  241. imguiEndFrame();
  242. float lightPos[4] = { 1.0f, 2.0f, 0.0f, 0.0f };
  243. bgfx::setUniform(u_light_pos, lightPos);
  244. float a[16];
  245. float b[16];
  246. float c[16];
  247. float d[16];
  248. float mtx[16];
  249. bx::mtxRotateY(a, time * 0.4f);
  250. bx::mtxRotateX(b, 0.4f);
  251. bx::mtxMul(c, a, b);
  252. bx::mtxTranslate(d, 0.0f, 0.0f, 4.0f);
  253. bx::mtxMul(mtx, c, d);
  254. // Set transform for draw call.
  255. bgfx::setTransform(mtx);
  256. float pomParam[4] = { float(m_shading_type), float(m_show_diffuse_texture), float(m_parallax_scale), float(m_num_steps) };
  257. bgfx::setUniform(u_pomParam, pomParam);
  258. // Set normal matrix uniform
  259. float inv[16];
  260. float transpose[16];
  261. bx::mtxInverse(inv, mtx);
  262. bx::mtxTranspose(transpose, inv);
  263. bgfx::setUniform(u_norm_mtx, transpose);
  264. // Set vertex and index buffer.
  265. bgfx::setVertexBuffer(0, m_vbh);
  266. bgfx::setIndexBuffer(m_ibh);
  267. // Bind textures.
  268. bgfx::setTexture(0, s_texColor, m_textureColor);
  269. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  270. bgfx::setTexture(2, s_texDepth, m_textureDepth);
  271. // Set render states.
  272. bgfx::setState(0
  273. | BGFX_STATE_RGB_WRITE
  274. | BGFX_STATE_ALPHA_WRITE
  275. | BGFX_STATE_DEPTH_WRITE
  276. | BGFX_STATE_DEPTH_TEST_LESS
  277. | BGFX_STATE_MSAA
  278. );
  279. // Submit primitive for rendering to view 0.
  280. bgfx::submit(0, m_program);
  281. // Advance to next frame. Rendering thread will be kicked to
  282. // process submitted rendering primitives.
  283. bgfx::frame();
  284. return true;
  285. }
  286. return false;
  287. }
  288. bgfx::VertexBufferHandle m_vbh;
  289. bgfx::IndexBufferHandle m_ibh;
  290. bgfx::UniformHandle s_texColor;
  291. bgfx::UniformHandle s_texNormal;
  292. bgfx::UniformHandle s_texDepth;
  293. bgfx::UniformHandle u_light_pos;
  294. bgfx::UniformHandle u_norm_mtx;
  295. bgfx::UniformHandle u_pomParam;
  296. bgfx::ProgramHandle m_program;
  297. bgfx::TextureHandle m_textureColor;
  298. bgfx::TextureHandle m_textureNormal;
  299. bgfx::TextureHandle m_textureDepth;
  300. entry::MouseState m_mouseState;
  301. uint32_t m_width;
  302. uint32_t m_height;
  303. uint32_t m_debug;
  304. uint32_t m_reset;
  305. int64_t m_timeOffset;
  306. int32_t m_shading_type;
  307. bool m_show_diffuse_texture;
  308. int32_t m_parallax_scale;
  309. int32_t m_num_steps;
  310. };
  311. ENTRY_IMPLEMENT_MAIN(ExamplePom);