mesh.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. int _main_(int /*_argc*/, char** /*_argv*/)
  8. {
  9. uint32_t width = 1280;
  10. uint32_t height = 720;
  11. uint32_t debug = BGFX_DEBUG_TEXT;
  12. uint32_t reset = BGFX_RESET_VSYNC;
  13. bgfx::init();
  14. bgfx::reset(width, height, reset);
  15. // Enable debug text.
  16. bgfx::setDebug(debug);
  17. // Set view 0 clear state.
  18. bgfx::setViewClear(0
  19. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  20. , 0x303030ff
  21. , 1.0f
  22. , 0
  23. );
  24. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::Uniform1f);
  25. // Create program from shaders.
  26. bgfx::ProgramHandle program = loadProgram("vs_mesh", "fs_mesh");
  27. Mesh* mesh = meshLoad("meshes/bunny.bin");
  28. int64_t timeOffset = bx::getHPCounter();
  29. while (!entry::processEvents(width, height, debug, reset) )
  30. {
  31. // Set view 0 default viewport.
  32. bgfx::setViewRect(0, 0, 0, width, height);
  33. // This dummy draw call is here to make sure that view 0 is cleared
  34. // if no other draw calls are submitted to view 0.
  35. bgfx::submit(0);
  36. int64_t now = bx::getHPCounter();
  37. static int64_t last = now;
  38. const int64_t frameTime = now - last;
  39. last = now;
  40. const double freq = double(bx::getHPFrequency() );
  41. const double toMs = 1000.0/freq;
  42. float time = (float)( (bx::getHPCounter()-timeOffset)/double(bx::getHPFrequency() ) );
  43. bgfx::setUniform(u_time, &time);
  44. // Use debug font to print information about this example.
  45. bgfx::dbgTextClear();
  46. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/04-mesh");
  47. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading meshes.");
  48. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  49. float at[3] = { 0.0f, 1.0f, 0.0f };
  50. float eye[3] = { 0.0f, 1.0f, -2.5f };
  51. // Set view and projection matrix for view 0.
  52. const bgfx::HMD* hmd = bgfx::getHMD();
  53. if (NULL != hmd)
  54. {
  55. float view[16];
  56. bx::mtxQuatTranslationHMD(view, hmd->eye[0].rotation, eye);
  57. float proj[16];
  58. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  59. bgfx::setViewTransform(0, view, proj);
  60. // Set view 0 default viewport.
  61. //
  62. // Use HMD's width/height since HMD's internal frame buffer size
  63. // might be much larger than window size.
  64. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  65. }
  66. else
  67. {
  68. float view[16];
  69. bx::mtxLookAt(view, eye, at);
  70. float proj[16];
  71. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  72. bgfx::setViewTransform(0, view, proj);
  73. // Set view 0 default viewport.
  74. bgfx::setViewRect(0, 0, 0, width, height);
  75. }
  76. float mtx[16];
  77. bx::mtxRotateXY(mtx
  78. , 0.0f
  79. , time*0.37f
  80. );
  81. meshSubmit(mesh, 0, program, mtx);
  82. // Advance to next frame. Rendering thread will be kicked to
  83. // process submitted rendering primitives.
  84. bgfx::frame();
  85. }
  86. meshUnload(mesh);
  87. // Cleanup.
  88. bgfx::destroyProgram(program);
  89. bgfx::destroyUniform(u_time);
  90. // Shutdown bgfx.
  91. bgfx::shutdown();
  92. return 0;
  93. }