pom.cpp 11 KB

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