cubes.cpp 6.0 KB

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