mesh.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2011-2014 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. float view[16];
  52. float proj[16];
  53. bx::mtxLookAt(view, eye, at);
  54. bx::mtxProj(proj, 60.0f, float(width)/float(height), 0.1f, 100.0f);
  55. // Set view and projection matrix for view 0.
  56. bgfx::setViewTransform(0, view, proj);
  57. float mtx[16];
  58. bx::mtxRotateXY(mtx
  59. , 0.0f
  60. , time*0.37f
  61. );
  62. meshSubmit(mesh, 0, program, mtx);
  63. // Advance to next frame. Rendering thread will be kicked to
  64. // process submitted rendering primitives.
  65. bgfx::frame();
  66. }
  67. meshUnload(mesh);
  68. // Cleanup.
  69. bgfx::destroyProgram(program);
  70. bgfx::destroyUniform(u_time);
  71. // Shutdown bgfx.
  72. bgfx::shutdown();
  73. return 0;
  74. }