mesh.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <openctm.h>
  9. #include "../common/dbg.h"
  10. #include "../common/math.h"
  11. #include <stdio.h>
  12. #include <string.h>
  13. void fatalCb(bgfx::Fatal::Enum _code, const char* _str)
  14. {
  15. DBG("%x: %s", _code, _str);
  16. }
  17. static const char* s_shaderPath = NULL;
  18. static bool s_flipV = false;
  19. static void shaderFilePath(char* _out, const char* _name)
  20. {
  21. strcpy(_out, s_shaderPath);
  22. strcat(_out, _name);
  23. strcat(_out, ".bin");
  24. }
  25. long int fsize(FILE* _file)
  26. {
  27. long int pos = ftell(_file);
  28. fseek(_file, 0L, SEEK_END);
  29. long int size = ftell(_file);
  30. fseek(_file, pos, SEEK_SET);
  31. return size;
  32. }
  33. static const bgfx::Memory* load(const char* _filePath)
  34. {
  35. FILE* file = fopen(_filePath, "rb");
  36. if (NULL != file)
  37. {
  38. uint32_t size = (uint32_t)fsize(file);
  39. const bgfx::Memory* mem = bgfx::alloc(size+1);
  40. size_t ignore = fread(mem->data, 1, size, file);
  41. BX_UNUSED(ignore);
  42. fclose(file);
  43. mem->data[mem->size-1] = '\0';
  44. return mem;
  45. }
  46. return NULL;
  47. }
  48. static const bgfx::Memory* loadShader(const char* _name, const char* _default = NULL)
  49. {
  50. char filePath[512];
  51. shaderFilePath(filePath, _name);
  52. BX_UNUSED(_default);
  53. return load(filePath);
  54. }
  55. static bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
  56. {
  57. const bgfx::Memory* mem;
  58. // Load vertex shader.
  59. mem = loadShader(_vsName);
  60. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  61. // Load fragment shader.
  62. mem = loadShader(_fsName);
  63. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  64. // Create program from shaders.
  65. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  66. // We can destroy vertex and fragment shader here since
  67. // their reference is kept inside bgfx after calling createProgram.
  68. // Vertex and fragment shader will be destroyed once program is
  69. // destroyed.
  70. bgfx::destroyVertexShader(vsh);
  71. bgfx::destroyFragmentShader(fsh);
  72. return program;
  73. }
  74. struct Mesh
  75. {
  76. void load(const char* _filePath)
  77. {
  78. CTMcontext ctm;
  79. ctm = ctmNewContext(CTM_IMPORT);
  80. ctmLoad(ctm, _filePath);
  81. // Create vertex decleration.
  82. {
  83. m_decl.begin();
  84. m_decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  85. if (ctmGetInteger(ctm, CTM_HAS_NORMALS) )
  86. {
  87. m_decl.add(bgfx::Attrib::Normal, 3, bgfx::AttribType::Float, true);
  88. }
  89. if (0 < ctmGetInteger(ctm, CTM_UV_MAP_COUNT) )
  90. {
  91. m_decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  92. }
  93. CTMenum colorAttrib = ctmGetNamedAttribMap(ctm, "Color");
  94. if (CTM_NONE != colorAttrib)
  95. {
  96. m_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  97. }
  98. m_decl.end();
  99. }
  100. // Allocate vertex buffer and copy vertex attributes.
  101. {
  102. CTMuint numVertices = ctmGetInteger(ctm, CTM_VERTEX_COUNT);
  103. uint32_t stride = m_decl.m_stride;
  104. const CTMfloat* vertices = ctmGetFloatArray(ctm, CTM_VERTICES);
  105. const CTMfloat* normals = ctmGetFloatArray(ctm, CTM_NORMALS);
  106. const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
  107. uint8_t* data = mem->data;
  108. const uint16_t normalOffset = m_decl.getOffset(bgfx::Attrib::Normal);
  109. const uint16_t color0Offset = m_decl.getOffset(bgfx::Attrib::Color0);
  110. const bool hasColor0 = m_decl.has(bgfx::Attrib::Color0);
  111. for (uint32_t ii = 0; ii < numVertices; ++ii)
  112. {
  113. {
  114. float* xyz = (float*)data;
  115. xyz[0] = vertices[0];
  116. xyz[1] = vertices[1];
  117. xyz[2] = vertices[2];
  118. vertices += 3;
  119. }
  120. if (hasColor0)
  121. {
  122. uint32_t* abgr = (uint32_t*)&data[color0Offset];
  123. abgr[0] = 0xff000000;
  124. abgr[0] |= uint8_t( (ii%37)/37.0f*255.0f)<<16;
  125. abgr[0] |= uint8_t( (ii%59)/59.0f*255.0f)<<8;
  126. abgr[0] |= uint8_t( (ii%79)/79.0f*255.0f);
  127. }
  128. if (NULL != normals)
  129. {
  130. float* nxyz = (float*)&data[normalOffset];
  131. nxyz[0] = normals[0];
  132. nxyz[1] = normals[1];
  133. nxyz[2] = normals[2];
  134. normals += 3;
  135. }
  136. data += stride;
  137. }
  138. m_vbh = bgfx::createVertexBuffer(mem, m_decl);
  139. }
  140. // Allocated static index buffer and fill with indices.
  141. {
  142. CTMuint numTriangles = ctmGetInteger(ctm, CTM_TRIANGLE_COUNT);
  143. const CTMuint* indices = ctmGetIntegerArray(ctm, CTM_INDICES);
  144. const bgfx::Memory* mem = bgfx::alloc(numTriangles*3*sizeof(uint16_t) );
  145. uint16_t* data = (uint16_t*)mem->data;
  146. for (uint32_t ii = 0, num = numTriangles * 3; ii < num; ++ii)
  147. {
  148. data[ii] = (uint16_t)indices[ii];
  149. }
  150. m_ibh = bgfx::createIndexBuffer(mem);
  151. }
  152. ctmFreeContext(ctm);
  153. }
  154. void setup()
  155. {
  156. bgfx::setIndexBuffer(m_ibh);
  157. bgfx::setVertexBuffer(m_vbh);
  158. }
  159. bgfx::VertexDecl m_decl;
  160. bgfx::VertexBufferHandle m_vbh;
  161. bgfx::IndexBufferHandle m_ibh;
  162. };
  163. int _main_(int _argc, char** _argv)
  164. {
  165. bgfx::init(BX_PLATFORM_WINDOWS, fatalCb);
  166. bgfx::reset(1280, 720);
  167. // Enable debug text.
  168. bgfx::setDebug(BGFX_DEBUG_TEXT);
  169. // Set view 0 default viewport.
  170. bgfx::setViewRect(0, 0, 0, 1280, 720);
  171. // Set view 1 default viewport.
  172. bgfx::setViewRect(1, 0, 0, 1280, 720);
  173. // Set view 0 clear state.
  174. bgfx::setViewClear(0
  175. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  176. , 0x303030ff
  177. , 1.0f
  178. , 0
  179. );
  180. // Setup root path for binary shaders. Shader binaries are different
  181. // for each renderer.
  182. switch (bgfx::getRendererType() )
  183. {
  184. case bgfx::RendererType::Null:
  185. case bgfx::RendererType::Direct3D9:
  186. s_shaderPath = "shaders/dx9/";
  187. break;
  188. case bgfx::RendererType::Direct3D11:
  189. s_shaderPath = "shaders/dx11/";
  190. break;
  191. case bgfx::RendererType::OpenGL:
  192. s_shaderPath = "shaders/glsl/";
  193. s_flipV = true;
  194. break;
  195. case bgfx::RendererType::OpenGLES2:
  196. s_shaderPath = "shaders/gles/";
  197. s_flipV = true;
  198. break;
  199. }
  200. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::ConstantType::Uniform1f);
  201. bgfx::ProgramHandle program = loadProgram("vs_mesh", "fs_mesh");
  202. Mesh mesh;
  203. mesh.load("meshes/bunny.ctm");
  204. while (true)
  205. {
  206. // This dummy draw call is here to make sure that view 0 is cleared
  207. // if no other draw calls are submitted to view 0.
  208. bgfx::submit(0);
  209. int64_t now = bx::getHPCounter();
  210. static int64_t last = now;
  211. const int64_t frameTime = now - last;
  212. last = now;
  213. const double freq = double(bx::getHPFrequency() );
  214. const double toMs = 1000.0/freq;
  215. float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  216. bgfx::setUniform(u_time, &time);
  217. // Use debug font to print information about this example.
  218. bgfx::dbgTextClear();
  219. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/04-mesh");
  220. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Loading OpenCTM meshes.");
  221. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  222. float at[3] = { 0.0f, 1.0f, 0.0f };
  223. float eye[3] = { 0.0f, 1.0f, -2.5f };
  224. float view[16];
  225. float proj[16];
  226. mtxLookAt(view, eye, at);
  227. mtxProj(proj, 60.0f, 16.0f/9.0f, 0.1f, 100.0f);
  228. // Set view and projection matrix for view 0.
  229. bgfx::setViewTransform(0, view, proj);
  230. float mtx[16];
  231. mtxRotateXY(mtx
  232. , 0.0f
  233. , time*0.37f
  234. );
  235. // Set model matrix for rendering.
  236. bgfx::setTransform(mtx);
  237. bgfx::setProgram(program);
  238. mesh.setup();
  239. // Set render states.
  240. bgfx::setState(BGFX_STATE_RGB_WRITE
  241. |BGFX_STATE_DEPTH_WRITE
  242. |BGFX_STATE_DEPTH_TEST_LESS
  243. );
  244. // Submit primitive for rendering to view 0.
  245. bgfx::submit(0);
  246. // Advance to next frame. Rendering thread will be kicked to
  247. // process submitted rendering primitives.
  248. bgfx::frame();
  249. }
  250. // Cleanup.
  251. bgfx::destroyProgram(program);
  252. bgfx::destroyUniform(u_time);
  253. // Shutdown bgfx.
  254. bgfx::shutdown();
  255. return 0;
  256. }