mesh.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. class ExampleMesh : public entry::AppI
  8. {
  9. void init(int _argc, char** _argv) BX_OVERRIDE
  10. {
  11. Args args(_argc, _argv);
  12. m_width = 1280;
  13. m_height = 720;
  14. m_debug = BGFX_DEBUG_TEXT;
  15. m_reset = BGFX_RESET_VSYNC;
  16. bgfx::init(args.m_type, args.m_pciId);
  17. bgfx::reset(m_width, m_height, m_reset);
  18. // Enable debug text.
  19. bgfx::setDebug(m_debug);
  20. // Set view 0 clear state.
  21. bgfx::setViewClear(0
  22. , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
  23. , 0x303030ff
  24. , 1.0f
  25. , 0
  26. );
  27. u_time = bgfx::createUniform("u_time", bgfx::UniformType::Vec4);
  28. // Create program from shaders.
  29. m_program = loadProgram("vs_mesh", "fs_mesh");
  30. m_mesh = meshLoad("meshes/bunny.bin");
  31. m_timeOffset = bx::getHPCounter();
  32. }
  33. int shutdown() BX_OVERRIDE
  34. {
  35. meshUnload(m_mesh);
  36. // Cleanup.
  37. bgfx::destroyProgram(m_program);
  38. bgfx::destroyUniform(u_time);
  39. // Shutdown bgfx.
  40. bgfx::shutdown();
  41. return 0;
  42. }
  43. bool update() BX_OVERRIDE
  44. {
  45. if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
  46. {
  47. // Set view 0 default viewport.
  48. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  49. // This dummy draw call is here to make sure that view 0 is cleared
  50. // if no other draw calls are submitted to view 0.
  51. bgfx::touch(0);
  52. int64_t now = bx::getHPCounter();
  53. static int64_t last = now;
  54. const int64_t frameTime = now - last;
  55. last = now;
  56. const double freq = double(bx::getHPFrequency() );
  57. const double toMs = 1000.0/freq;
  58. float time = (float)( (bx::getHPCounter()-m_timeOffset)/double(bx::getHPFrequency() ) );
  59. bgfx::setUniform(u_time, &time);
  60. // Use debug font to print information about this example.
  61. bgfx::dbgTextClear();
  62. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/04-mesh");
  63. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading meshes.");
  64. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  65. float at[3] = { 0.0f, 1.0f, 0.0f };
  66. float eye[3] = { 0.0f, 1.0f, -2.5f };
  67. // Set view and projection matrix for view 0.
  68. const bgfx::HMD* hmd = bgfx::getHMD();
  69. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  70. {
  71. float view[16];
  72. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  73. bgfx::setViewTransform(0, view, hmd->eye[0].projection, BGFX_VIEW_STEREO, hmd->eye[1].projection);
  74. // Set view 0 default viewport.
  75. //
  76. // Use HMD's width/height since HMD's internal frame buffer size
  77. // might be much larger than window size.
  78. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  79. }
  80. else
  81. {
  82. float view[16];
  83. bx::mtxLookAt(view, eye, at);
  84. float proj[16];
  85. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  86. bgfx::setViewTransform(0, view, proj);
  87. // Set view 0 default viewport.
  88. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  89. }
  90. float mtx[16];
  91. bx::mtxRotateXY(mtx
  92. , 0.0f
  93. , time*0.37f
  94. );
  95. meshSubmit(m_mesh, 0, m_program, mtx);
  96. // Advance to next frame. Rendering thread will be kicked to
  97. // process submitted rendering primitives.
  98. bgfx::frame();
  99. return true;
  100. }
  101. return false;
  102. }
  103. uint32_t m_width;
  104. uint32_t m_height;
  105. uint32_t m_debug;
  106. uint32_t m_reset;
  107. int64_t m_timeOffset;
  108. Mesh* m_mesh;
  109. bgfx::ProgramHandle m_program;
  110. bgfx::UniformHandle u_time;
  111. };
  112. ENTRY_IMPLEMENT_MAIN(ExampleMesh);