cubes.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include <bgfx.h>
  6. #include <bx/bx.h>
  7. #include <bx/timer.h>
  8. #include "../common/dbg.h"
  9. #include "../common/math.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. struct PosColorVertex
  13. {
  14. float m_x;
  15. float m_y;
  16. float m_z;
  17. uint32_t m_abgr;
  18. };
  19. static bgfx::VertexDecl s_PosColorDecl;
  20. static PosColorVertex s_cubeVertices[8] =
  21. {
  22. {-1.0f, 1.0f, 1.0f, 0xff000000 },
  23. { 1.0f, 1.0f, 1.0f, 0xff0000ff },
  24. {-1.0f, -1.0f, 1.0f, 0xff00ff00 },
  25. { 1.0f, -1.0f, 1.0f, 0xff00ffff },
  26. {-1.0f, 1.0f, -1.0f, 0xffff0000 },
  27. { 1.0f, 1.0f, -1.0f, 0xffff00ff },
  28. {-1.0f, -1.0f, -1.0f, 0xffffff00 },
  29. { 1.0f, -1.0f, -1.0f, 0xffffffff },
  30. };
  31. static const uint16_t s_cubeIndices[36] =
  32. {
  33. 0, 2, 1, // 0
  34. 1, 2, 3,
  35. 4, 5, 6, // 2
  36. 5, 7, 6,
  37. 0, 4, 2, // 4
  38. 4, 6, 2,
  39. 1, 3, 5, // 6
  40. 5, 3, 7,
  41. 0, 1, 4, // 8
  42. 4, 1, 5,
  43. 2, 6, 3, // 10
  44. 6, 7, 3,
  45. };
  46. static const char* s_shaderPath = NULL;
  47. static void shaderFilePath(char* _out, const char* _name)
  48. {
  49. strcpy(_out, s_shaderPath);
  50. strcat(_out, _name);
  51. strcat(_out, ".bin");
  52. }
  53. long int fsize(FILE* _file)
  54. {
  55. long int pos = ftell(_file);
  56. fseek(_file, 0L, SEEK_END);
  57. long int size = ftell(_file);
  58. fseek(_file, pos, SEEK_SET);
  59. return size;
  60. }
  61. static const bgfx::Memory* load(const char* _filePath)
  62. {
  63. FILE* file = fopen(_filePath, "rb");
  64. if (NULL != file)
  65. {
  66. uint32_t size = (uint32_t)fsize(file);
  67. const bgfx::Memory* mem = bgfx::alloc(size+1);
  68. size_t ignore = fread(mem->data, 1, size, file);
  69. BX_UNUSED(ignore);
  70. fclose(file);
  71. mem->data[mem->size-1] = '\0';
  72. return mem;
  73. }
  74. return NULL;
  75. }
  76. static const bgfx::Memory* loadShader(const char* _name)
  77. {
  78. char filePath[512];
  79. shaderFilePath(filePath, _name);
  80. return load(filePath);
  81. }
  82. int _main_(int _argc, char** _argv)
  83. {
  84. bgfx::init();
  85. bgfx::reset(1280, 720);
  86. // Enable debug text.
  87. bgfx::setDebug(BGFX_DEBUG_TEXT);
  88. // Set view 0 default viewport.
  89. bgfx::setViewRect(0, 0, 0, 1280, 720);
  90. // Set view 0 clear state.
  91. bgfx::setViewClear(0
  92. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  93. , 0x303030ff
  94. , 1.0f
  95. , 0
  96. );
  97. // Setup root path for binary shaders. Shader binaries are different
  98. // for each renderer.
  99. switch (bgfx::getRendererType() )
  100. {
  101. default:
  102. case bgfx::RendererType::Direct3D9:
  103. s_shaderPath = "shaders/dx9/";
  104. break;
  105. case bgfx::RendererType::Direct3D11:
  106. s_shaderPath = "shaders/dx11/";
  107. break;
  108. case bgfx::RendererType::OpenGL:
  109. s_shaderPath = "shaders/glsl/";
  110. break;
  111. case bgfx::RendererType::OpenGLES2:
  112. case bgfx::RendererType::OpenGLES3:
  113. s_shaderPath = "shaders/gles/";
  114. break;
  115. }
  116. // Create vertex stream declaration.
  117. s_PosColorDecl.begin();
  118. s_PosColorDecl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  119. s_PosColorDecl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  120. s_PosColorDecl.end();
  121. const bgfx::Memory* mem;
  122. // Create static vertex buffer.
  123. mem = bgfx::makeRef(s_cubeVertices, sizeof(s_cubeVertices) );
  124. bgfx::VertexBufferHandle vbh = bgfx::createVertexBuffer(mem, s_PosColorDecl);
  125. // Create static index buffer.
  126. mem = bgfx::makeRef(s_cubeIndices, sizeof(s_cubeIndices) );
  127. bgfx::IndexBufferHandle ibh = bgfx::createIndexBuffer(mem);
  128. // Load vertex shader.
  129. mem = loadShader("vs_cubes");
  130. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  131. // Load fragment shader.
  132. mem = loadShader("fs_cubes");
  133. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  134. // Create program from shaders.
  135. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  136. // We can destroy vertex and fragment shader here since
  137. // their reference is kept inside bgfx after calling createProgram.
  138. // Vertex and fragment shader will be destroyed once program is
  139. // destroyed.
  140. bgfx::destroyVertexShader(vsh);
  141. bgfx::destroyFragmentShader(fsh);
  142. while (true)
  143. {
  144. // This dummy draw call is here to make sure that view 0 is cleared
  145. // if no other draw calls are submitted to view 0.
  146. bgfx::submit(0);
  147. int64_t now = bx::getHPCounter();
  148. static int64_t last = now;
  149. const int64_t frameTime = now - last;
  150. last = now;
  151. const double freq = double(bx::getHPFrequency() );
  152. const double toMs = 1000.0/freq;
  153. // Use debug font to print information about this example.
  154. bgfx::dbgTextClear();
  155. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/01-cube");
  156. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Rendering simple static mesh.");
  157. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  158. float at[3] = { 0.0f, 0.0f, 0.0f };
  159. float eye[3] = { 0.0f, 0.0f, -35.0f };
  160. float view[16];
  161. float proj[16];
  162. mtxLookAt(view, eye, at);
  163. mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f);
  164. // Set view and projection matrix for view 0.
  165. bgfx::setViewTransform(0, view, proj);
  166. float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  167. // Submit 11x11 cubes.
  168. for (uint32_t yy = 0; yy < 11; ++yy)
  169. {
  170. for (uint32_t xx = 0; xx < 11; ++xx)
  171. {
  172. float mtx[16];
  173. mtxRotateXY(mtx, time + xx*0.21f, time + yy*0.37f);
  174. mtx[12] = -15.0f + float(xx)*3.0f;
  175. mtx[13] = -15.0f + float(yy)*3.0f;
  176. mtx[14] = 0.0f;
  177. // Set model matrix for rendering.
  178. bgfx::setTransform(mtx);
  179. // Set vertex and fragment shaders.
  180. bgfx::setProgram(program);
  181. // Set vertex and index buffer.
  182. bgfx::setVertexBuffer(vbh);
  183. bgfx::setIndexBuffer(ibh);
  184. // Set render states.
  185. bgfx::setState(BGFX_STATE_RGB_WRITE
  186. |BGFX_STATE_DEPTH_WRITE
  187. |BGFX_STATE_DEPTH_TEST_LESS
  188. );
  189. // Submit primitive for rendering to view 0.
  190. bgfx::submit(0);
  191. }
  192. }
  193. // Advance to next frame. Rendering thread will be kicked to
  194. // process submitted rendering primitives.
  195. bgfx::frame();
  196. }
  197. // Cleanup.
  198. bgfx::destroyIndexBuffer(ibh);
  199. bgfx::destroyVertexBuffer(vbh);
  200. bgfx::destroyProgram(program);
  201. // Shutdown bgfx.
  202. bgfx::shutdown();
  203. return 0;
  204. }