mesh.cpp 3.7 KB

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