cubes.cpp 5.6 KB

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