cubes.cpp 5.9 KB

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