pom.cpp 11 KB

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