pom.cpp 11 KB

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