mesh.cpp 3.8 KB

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