mesh.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2011-2016 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, m_width, 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. float proj[16];
  74. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  75. bgfx::setViewTransform(0, view, proj);
  76. // Set view 0 default viewport.
  77. //
  78. // Use HMD's width/height since HMD's internal frame buffer size
  79. // might be much larger than window size.
  80. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  81. }
  82. else
  83. {
  84. float view[16];
  85. bx::mtxLookAt(view, eye, at);
  86. float proj[16];
  87. bx::mtxProj(proj, 60.0f, float(m_width)/float(m_height), 0.1f, 100.0f);
  88. bgfx::setViewTransform(0, view, proj);
  89. // Set view 0 default viewport.
  90. bgfx::setViewRect(0, 0, 0, m_width, m_height);
  91. }
  92. float mtx[16];
  93. bx::mtxRotateXY(mtx
  94. , 0.0f
  95. , time*0.37f
  96. );
  97. meshSubmit(m_mesh, 0, m_program, mtx);
  98. // Advance to next frame. Rendering thread will be kicked to
  99. // process submitted rendering primitives.
  100. bgfx::frame();
  101. return true;
  102. }
  103. return false;
  104. }
  105. uint32_t m_width;
  106. uint32_t m_height;
  107. uint32_t m_debug;
  108. uint32_t m_reset;
  109. int64_t m_timeOffset;
  110. Mesh* m_mesh;
  111. bgfx::ProgramHandle m_program;
  112. bgfx::UniformHandle u_time;
  113. };
  114. ENTRY_IMPLEMENT_MAIN(ExampleMesh);