mesh.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. class ExampleMesh : public entry::AppI
  11. {
  12. public:
  13. ExampleMesh(const char* _name, const char* _description, const char* _url)
  14. : entry::AppI(_name, _description, _url)
  15. {
  16. }
  17. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  18. {
  19. Args args(_argc, _argv);
  20. m_width = _width;
  21. m_height = _height;
  22. m_debug = BGFX_DEBUG_NONE;
  23. m_reset = BGFX_RESET_VSYNC;
  24. bgfx::Init init;
  25. init.type = args.m_type;
  26. init.vendorId = args.m_pciId;
  27. init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
  28. init.platformData.ndt = entry::getNativeDisplayHandle();
  29. init.resolution.width = m_width;
  30. init.resolution.height = m_height;
  31. init.resolution.reset = m_reset;
  32. bgfx::init(init);
  33. // Enable debug text.
  34. bgfx::setDebug(m_debug);
  35. // Set view 0 clear state.
  36. bgfx::setViewClear(0
  37. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  38. , 0x303030ff
  39. , 1.0f
  40. , 0
  41. );
  42. u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  43. // Create program from shaders.
  44. m_program = loadProgram("vs_mesh", "fs_mesh");
  45. m_mesh = meshLoad("meshes/bunny.bin");
  46. m_timeOffset = bx::getHPCounter();
  47. imguiCreate();
  48. }
  49. int shutdown() override
  50. {
  51. imguiDestroy();
  52. meshUnload(m_mesh);
  53. // Cleanup.
  54. bgfx::destroy(m_program);
  55. bgfx::destroy(u_time);
  56. // Shutdown bgfx.
  57. bgfx::shutdown();
  58. return 0;
  59. }
  60. bool update() override
  61. {
  62. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  63. {
  64. imguiBeginFrame(m_mouseState.m_mx
  65. , m_mouseState.m_my
  66. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  67. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  68. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  69. , m_mouseState.m_mz
  70. , uint16_t(m_width)
  71. , uint16_t(m_height)
  72. );
  73. showExampleDialog(this);
  74. imguiEndFrame();
  75. // Set view 0 default viewport.
  76. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  77. // This dummy draw call is here to make sure that view 0 is cleared
  78. // if no other draw calls are submitted to view 0.
  79. bgfx::touch(0);
  80. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  81. bgfx::setUniform(u_time, &time);
  82. const bx::Vec3 at = { 0.0f, 1.0f, 0.0f };
  83. const bx::Vec3 eye = { 0.0f, 1.0f, -2.5f };
  84. // Set view and projection matrix for view 0.
  85. {
  86. float view[16];
  87. bx::mtxLookAt(view, eye, at);
  88. float proj[16];
  89. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
  90. bgfx::setViewTransform(0, view, proj);
  91. // Set view 0 default viewport.
  92. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  93. }
  94. float mtx[16];
  95. bx::mtxRotateXY(mtx
  96. , 0.0f
  97. , time*0.37f
  98. );
  99. meshSubmit(m_mesh, 0, m_program, mtx);
  100. // Advance to next frame. Rendering thread will be kicked to
  101. // process submitted rendering primitives.
  102. bgfx::frame();
  103. return true;
  104. }
  105. return false;
  106. }
  107. entry::MouseState m_mouseState;
  108. uint32_t m_width;
  109. uint32_t m_height;
  110. uint32_t m_debug;
  111. uint32_t m_reset;
  112. int64_t m_timeOffset;
  113. Mesh* m_mesh;
  114. bgfx::ProgramHandle m_program;
  115. bgfx::UniformHandle u_time;
  116. };
  117. } // namespace
  118. ENTRY_IMPLEMENT_MAIN(
  119. ExampleMesh
  120. , "04-mesh"
  121. , "Loading meshes."
  122. , "https://bkaradzic.github.io/bgfx/examples.html#mesh"
  123. );