pom.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright 2011-2018 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. 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_decl
  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::VertexDecl ms_decl;
  30. };
  31. bgfx::VertexDecl PosTangentBitangentTexcoordVertex::ms_decl;
  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)
  99. : entry::AppI(_name, _description)
  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.resolution.width = m_width;
  113. init.resolution.height = m_height;
  114. init.resolution.reset = m_reset;
  115. bgfx::init(init);
  116. // Enable debug text.
  117. bgfx::setDebug(m_debug);
  118. // Set view 0 clear state.
  119. bgfx::setViewClear(0
  120. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  121. , 0x303030ff
  122. , 1.0f
  123. , 0
  124. );
  125. // Create vertex stream declaration.
  126. PosTangentBitangentTexcoordVertex::init();
  127. // Create static vertex buffer.
  128. m_vbh = bgfx::createVertexBuffer(bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) ),
  129. PosTangentBitangentTexcoordVertex::ms_decl);
  130. // Create static index buffer.
  131. m_ibh = bgfx::createIndexBuffer(bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) ) );
  132. // Create texture sampler uniforms.
  133. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  134. s_texNormal = bgfx::createUniform("s_texNormal", bgfx::UniformType::Int1);
  135. s_texDepth = bgfx::createUniform("s_texDepth", bgfx::UniformType::Int1);
  136. u_light_pos = bgfx::createUniform("u_light_pos", bgfx::UniformType::Vec4);
  137. u_norm_mtx = bgfx::createUniform("u_norm_mtx", bgfx::UniformType::Mat4);
  138. u_pomParam = bgfx::createUniform("u_pomParam", bgfx::UniformType::Vec4);
  139. // Create program from shaders.
  140. m_program = loadProgram("vs_pom", "fs_pom");
  141. // Load diffuse texture.
  142. m_textureColor = loadTexture("textures/parallax-d.ktx");
  143. // Load normal texture.
  144. m_textureNormal = loadTexture("textures/parallax-n.ktx");
  145. // Load depth texture.
  146. m_textureDepth = loadTexture("textures/parallax-h.ktx");
  147. imguiCreate();
  148. m_timeOffset = bx::getHPCounter();
  149. m_shading_type = 4;
  150. m_show_diffuse_texture = true;
  151. m_parallax_scale = 50;
  152. m_num_steps = 16;
  153. }
  154. virtual int shutdown() override
  155. {
  156. // Cleanup.
  157. bgfx::destroy(m_ibh);
  158. bgfx::destroy(m_vbh);
  159. bgfx::destroy(m_program);
  160. bgfx::destroy(m_textureColor);
  161. bgfx::destroy(m_textureNormal);
  162. bgfx::destroy(m_textureDepth);
  163. bgfx::destroy(s_texColor);
  164. bgfx::destroy(s_texNormal);
  165. bgfx::destroy(s_texDepth);
  166. bgfx::destroy(u_light_pos);
  167. bgfx::destroy(u_norm_mtx);
  168. bgfx::destroy(u_pomParam);
  169. imguiDestroy();
  170. // Shutdown bgfx.
  171. bgfx::shutdown();
  172. return 0;
  173. }
  174. bool update() override
  175. {
  176. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  177. {
  178. // Set view 0 default viewport.
  179. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  180. // This dummy draw call is here to make sure that view 0 is cleared
  181. // if no other draw calls are submitted to view 0.
  182. bgfx::touch(0);
  183. int64_t now = bx::getHPCounter();
  184. const double freq = double(bx::getHPFrequency() );
  185. float time = (float)( (now-m_timeOffset)/freq);
  186. float at[3] = { 0.0f, 0.0f, 1.0f };
  187. float eye[3] = { 0.0f, 0.0f, 0.0f };
  188. // Set view and projection matrix for view 0.
  189. {
  190. float view[16];
  191. bx::mtxLookAt(view, eye, at);
  192. float proj[16];
  193. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  194. bgfx::setViewTransform(0, view, proj);
  195. // Set view 0 default viewport.
  196. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  197. }
  198. imguiBeginFrame(
  199. m_mouseState.m_mx
  200. , m_mouseState.m_my
  201. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  202. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  203. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  204. , m_mouseState.m_mz
  205. , uint16_t(m_width)
  206. , uint16_t(m_height)
  207. );
  208. showExampleDialog(this);
  209. ImGui::SetNextWindowPos(
  210. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  211. , ImGuiCond_FirstUseEver
  212. );
  213. ImGui::SetNextWindowSize(
  214. ImVec2(m_width / 5.0f, m_height / 2.0f)
  215. , ImGuiCond_FirstUseEver
  216. );
  217. ImGui::Begin("Settings"
  218. , NULL
  219. , 0
  220. );
  221. ImGui::RadioButton("No bump mapping", &m_shading_type, 0);
  222. ImGui::RadioButton("Normal mapping", &m_shading_type, 1);
  223. ImGui::RadioButton("Parallax mapping", &m_shading_type, 2);
  224. ImGui::RadioButton("Steep parallax mapping", &m_shading_type, 3);
  225. ImGui::RadioButton("Parallax occlusion mapping", &m_shading_type, 4);
  226. ImGui::Separator();
  227. ImGui::Checkbox("Show diffuse texture", &m_show_diffuse_texture);
  228. if (m_shading_type > 1)
  229. {
  230. ImGui::Separator();
  231. float multiplier = 1000.0f;
  232. float x = (float)m_parallax_scale / multiplier;
  233. ImGui::SliderFloat("Parallax scale", &x, 0.0f, 0.1f);
  234. m_parallax_scale = (int32_t)(x * multiplier);
  235. }
  236. if (m_shading_type > 2)
  237. {
  238. ImGui::Separator();
  239. ImGui::SliderInt("Number of steps", &m_num_steps, 1, 32);
  240. }
  241. ImGui::End();
  242. imguiEndFrame();
  243. float lightPos[4] = { 1.0f, 2.0f, 0.0f, 0.0f };
  244. bgfx::setUniform(u_light_pos, lightPos);
  245. float a[16];
  246. float b[16];
  247. float c[16];
  248. float d[16];
  249. float mtx[16];
  250. bx::mtxRotateY(a, time * 0.4f);
  251. bx::mtxRotateX(b, 0.4f);
  252. bx::mtxMul(c, a, b);
  253. bx::mtxTranslate(d, 0.0f, 0.0f, 4.0f);
  254. bx::mtxMul(mtx, c, d);
  255. // Set transform for draw call.
  256. bgfx::setTransform(mtx);
  257. float pomParam[4] = { float(m_shading_type), float(m_show_diffuse_texture), float(m_parallax_scale), float(m_num_steps) };
  258. bgfx::setUniform(u_pomParam, pomParam);
  259. // Set normal matrix uniform
  260. float inv[16];
  261. float transpose[16];
  262. bx::mtxInverse(inv, mtx);
  263. bx::mtxTranspose(transpose, inv);
  264. bgfx::setUniform(u_norm_mtx, transpose);
  265. // Set vertex and index buffer.
  266. bgfx::setVertexBuffer(0, m_vbh);
  267. bgfx::setIndexBuffer(m_ibh);
  268. // Bind textures.
  269. bgfx::setTexture(0, s_texColor, m_textureColor);
  270. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  271. bgfx::setTexture(2, s_texDepth, m_textureDepth);
  272. // Set render states.
  273. bgfx::setState(0
  274. | BGFX_STATE_WRITE_RGB
  275. | BGFX_STATE_WRITE_A
  276. | BGFX_STATE_WRITE_Z
  277. | BGFX_STATE_DEPTH_TEST_LESS
  278. | BGFX_STATE_MSAA
  279. );
  280. // Submit primitive for rendering to view 0.
  281. bgfx::submit(0, m_program);
  282. // Advance to next frame. Rendering thread will be kicked to
  283. // process submitted rendering primitives.
  284. bgfx::frame();
  285. return true;
  286. }
  287. return false;
  288. }
  289. bgfx::VertexBufferHandle m_vbh;
  290. bgfx::IndexBufferHandle m_ibh;
  291. bgfx::UniformHandle s_texColor;
  292. bgfx::UniformHandle s_texNormal;
  293. bgfx::UniformHandle s_texDepth;
  294. bgfx::UniformHandle u_light_pos;
  295. bgfx::UniformHandle u_norm_mtx;
  296. bgfx::UniformHandle u_pomParam;
  297. bgfx::ProgramHandle m_program;
  298. bgfx::TextureHandle m_textureColor;
  299. bgfx::TextureHandle m_textureNormal;
  300. bgfx::TextureHandle m_textureDepth;
  301. entry::MouseState m_mouseState;
  302. uint32_t m_width;
  303. uint32_t m_height;
  304. uint32_t m_debug;
  305. uint32_t m_reset;
  306. int64_t m_timeOffset;
  307. int32_t m_shading_type;
  308. bool m_show_diffuse_texture;
  309. int32_t m_parallax_scale;
  310. int32_t m_num_steps;
  311. };
  312. } // namespace
  313. ENTRY_IMPLEMENT_MAIN(ExamplePom, "33-pom", "Parallax mapping.");