pom.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. const bgfx::HMD* hmd = bgfx::getHMD();
  190. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  191. {
  192. float view[16];
  193. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  194. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  195. // Set view 0 default viewport.
  196. //
  197. // Use HMD's width/height since HMD's internal frame buffer size
  198. // might be much larger than window size.
  199. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  200. }
  201. else
  202. {
  203. float view[16];
  204. bx::mtxLookAt(view, eye, at);
  205. float proj[16];
  206. bx::mtxProj(proj, 60.0f, float(m_width) / float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  207. bgfx::setViewTransform(0, view, proj);
  208. // Set view 0 default viewport.
  209. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  210. }
  211. imguiBeginFrame(
  212. m_mouseState.m_mx
  213. , m_mouseState.m_my
  214. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  215. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  216. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  217. , m_mouseState.m_mz
  218. , uint16_t(m_width)
  219. , uint16_t(m_height)
  220. );
  221. showExampleDialog(this);
  222. ImGui::SetNextWindowPos(
  223. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  224. , ImGuiCond_FirstUseEver
  225. );
  226. ImGui::SetNextWindowSize(
  227. ImVec2(m_width / 5.0f, m_height / 2.0f)
  228. , ImGuiCond_FirstUseEver
  229. );
  230. ImGui::Begin("Settings"
  231. , NULL
  232. , 0
  233. );
  234. ImGui::RadioButton("No bump mapping", &m_shading_type, 0);
  235. ImGui::RadioButton("Normal mapping", &m_shading_type, 1);
  236. ImGui::RadioButton("Parallax mapping", &m_shading_type, 2);
  237. ImGui::RadioButton("Steep parallax mapping", &m_shading_type, 3);
  238. ImGui::RadioButton("Parallax occlusion mapping", &m_shading_type, 4);
  239. ImGui::Separator();
  240. ImGui::Checkbox("Show diffuse texture", &m_show_diffuse_texture);
  241. if (m_shading_type > 1)
  242. {
  243. ImGui::Separator();
  244. float multiplier = 1000.0f;
  245. float x = (float)m_parallax_scale / multiplier;
  246. ImGui::SliderFloat("Parallax scale", &x, 0.0f, 0.1f);
  247. m_parallax_scale = (int32_t)(x * multiplier);
  248. }
  249. if (m_shading_type > 2)
  250. {
  251. ImGui::Separator();
  252. ImGui::SliderInt("Number of steps", &m_num_steps, 1, 32);
  253. }
  254. ImGui::End();
  255. imguiEndFrame();
  256. float lightPos[4] = { 1.0f, 2.0f, 0.0f, 0.0f };
  257. bgfx::setUniform(u_light_pos, lightPos);
  258. float a[16];
  259. float b[16];
  260. float c[16];
  261. float d[16];
  262. float mtx[16];
  263. bx::mtxRotateY(a, time * 0.4f);
  264. bx::mtxRotateX(b, 0.4f);
  265. bx::mtxMul(c, a, b);
  266. bx::mtxTranslate(d, 0.0f, 0.0f, 4.0f);
  267. bx::mtxMul(mtx, c, d);
  268. // Set transform for draw call.
  269. bgfx::setTransform(mtx);
  270. float pomParam[4] = { float(m_shading_type), float(m_show_diffuse_texture), float(m_parallax_scale), float(m_num_steps) };
  271. bgfx::setUniform(u_pomParam, pomParam);
  272. // Set normal matrix uniform
  273. float inv[16];
  274. float transpose[16];
  275. bx::mtxInverse(inv, mtx);
  276. bx::mtxTranspose(transpose, inv);
  277. bgfx::setUniform(u_norm_mtx, transpose);
  278. // Set vertex and index buffer.
  279. bgfx::setVertexBuffer(0, m_vbh);
  280. bgfx::setIndexBuffer(m_ibh);
  281. // Bind textures.
  282. bgfx::setTexture(0, s_texColor, m_textureColor);
  283. bgfx::setTexture(1, s_texNormal, m_textureNormal);
  284. bgfx::setTexture(2, s_texDepth, m_textureDepth);
  285. // Set render states.
  286. bgfx::setState(0
  287. | BGFX_STATE_WRITE_RGB
  288. | BGFX_STATE_WRITE_A
  289. | BGFX_STATE_WRITE_Z
  290. | BGFX_STATE_DEPTH_TEST_LESS
  291. | BGFX_STATE_MSAA
  292. );
  293. // Submit primitive for rendering to view 0.
  294. bgfx::submit(0, m_program);
  295. // Advance to next frame. Rendering thread will be kicked to
  296. // process submitted rendering primitives.
  297. bgfx::frame();
  298. return true;
  299. }
  300. return false;
  301. }
  302. bgfx::VertexBufferHandle m_vbh;
  303. bgfx::IndexBufferHandle m_ibh;
  304. bgfx::UniformHandle s_texColor;
  305. bgfx::UniformHandle s_texNormal;
  306. bgfx::UniformHandle s_texDepth;
  307. bgfx::UniformHandle u_light_pos;
  308. bgfx::UniformHandle u_norm_mtx;
  309. bgfx::UniformHandle u_pomParam;
  310. bgfx::ProgramHandle m_program;
  311. bgfx::TextureHandle m_textureColor;
  312. bgfx::TextureHandle m_textureNormal;
  313. bgfx::TextureHandle m_textureDepth;
  314. entry::MouseState m_mouseState;
  315. uint32_t m_width;
  316. uint32_t m_height;
  317. uint32_t m_debug;
  318. uint32_t m_reset;
  319. int64_t m_timeOffset;
  320. int32_t m_shading_type;
  321. bool m_show_diffuse_texture;
  322. int32_t m_parallax_scale;
  323. int32_t m_num_steps;
  324. };
  325. } // namespace
  326. ENTRY_IMPLEMENT_MAIN(ExamplePom, "33-pom", "Parallax mapping.");