mesh.cpp 3.1 KB

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