2
0

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)
  49. {
  50. char filePath[512];
  51. shaderFilePath(filePath, _name);
  52. return load(filePath);
  53. }
  54. static bgfx::ProgramHandle loadProgram(const char* _vsName, const char* _fsName)
  55. {
  56. const bgfx::Memory* mem;
  57. // Load vertex shader.
  58. mem = loadShader(_vsName);
  59. bgfx::VertexShaderHandle vsh = bgfx::createVertexShader(mem);
  60. // Load fragment shader.
  61. mem = loadShader(_fsName);
  62. bgfx::FragmentShaderHandle fsh = bgfx::createFragmentShader(mem);
  63. // Create program from shaders.
  64. bgfx::ProgramHandle program = bgfx::createProgram(vsh, fsh);
  65. // We can destroy vertex and fragment shader here since
  66. // their reference is kept inside bgfx after calling createProgram.
  67. // Vertex and fragment shader will be destroyed once program is
  68. // destroyed.
  69. bgfx::destroyVertexShader(vsh);
  70. bgfx::destroyFragmentShader(fsh);
  71. return program;
  72. }
  73. struct Mesh
  74. {
  75. void load(const char* _filePath)
  76. {
  77. CTMcontext ctm;
  78. ctm = ctmNewContext(CTM_IMPORT);
  79. ctmLoad(ctm, _filePath);
  80. // Create vertex decleration.
  81. {
  82. m_decl.begin();
  83. m_decl.add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float);
  84. if (ctmGetInteger(ctm, CTM_HAS_NORMALS) )
  85. {
  86. m_decl.add(bgfx::Attrib::Normal, 4, bgfx::AttribType::Uint8, true);
  87. }
  88. if (0 < ctmGetInteger(ctm, CTM_UV_MAP_COUNT) )
  89. {
  90. m_decl.add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float);
  91. }
  92. CTMenum colorAttrib = ctmGetNamedAttribMap(ctm, "Color");
  93. if (CTM_NONE != colorAttrib)
  94. {
  95. m_decl.add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true);
  96. }
  97. m_decl.end();
  98. }
  99. // Allocate vertex buffer and copy vertex attributes.
  100. {
  101. CTMuint numVertices = ctmGetInteger(ctm, CTM_VERTEX_COUNT);
  102. uint32_t stride = m_decl.m_stride;
  103. const CTMfloat* vertices = ctmGetFloatArray(ctm, CTM_VERTICES);
  104. const CTMfloat* normals = ctmGetFloatArray(ctm, CTM_NORMALS);
  105. const bgfx::Memory* mem = bgfx::alloc(numVertices*stride);
  106. uint8_t* data = mem->data;
  107. const uint16_t normalOffset = m_decl.getOffset(bgfx::Attrib::Normal);
  108. const uint16_t color0Offset = m_decl.getOffset(bgfx::Attrib::Color0);
  109. const bool hasColor0 = m_decl.has(bgfx::Attrib::Color0);
  110. for (uint32_t ii = 0; ii < numVertices; ++ii)
  111. {
  112. {
  113. float* xyz = (float*)data;
  114. xyz[0] = vertices[0];
  115. xyz[1] = vertices[1];
  116. xyz[2] = vertices[2];
  117. vertices += 3;
  118. }
  119. if (hasColor0)
  120. {
  121. uint32_t* abgr = (uint32_t*)&data[color0Offset];
  122. abgr[0] = 0xff000000;
  123. abgr[0] |= uint8_t( (ii%37)/37.0f*255.0f)<<16;
  124. abgr[0] |= uint8_t( (ii%59)/59.0f*255.0f)<<8;
  125. abgr[0] |= uint8_t( (ii%79)/79.0f*255.0f);
  126. }
  127. if (NULL != normals)
  128. {
  129. uint8_t* nxyz = (uint8_t*)&data[normalOffset];
  130. nxyz[0] = uint8_t(normals[0]*127.0f + 128.0f);
  131. nxyz[1] = uint8_t(normals[1]*127.0f + 128.0f);
  132. nxyz[2] = uint8_t(normals[2]*127.0f + 128.0f);
  133. normals += 3;
  134. }
  135. data += stride;
  136. }
  137. m_vbh = bgfx::createVertexBuffer(mem, m_decl);
  138. }
  139. // Allocated static index buffer and fill with indices.
  140. {
  141. CTMuint numTriangles = ctmGetInteger(ctm, CTM_TRIANGLE_COUNT);
  142. const CTMuint* indices = ctmGetIntegerArray(ctm, CTM_INDICES);
  143. const bgfx::Memory* mem = bgfx::alloc(numTriangles*3*sizeof(uint16_t) );
  144. uint16_t* data = (uint16_t*)mem->data;
  145. for (uint32_t ii = 0, num = numTriangles * 3; ii < num; ++ii)
  146. {
  147. data[ii] = (uint16_t)indices[ii];
  148. }
  149. m_ibh = bgfx::createIndexBuffer(mem);
  150. }
  151. ctmFreeContext(ctm);
  152. }
  153. void setup()
  154. {
  155. bgfx::setIndexBuffer(m_ibh);
  156. bgfx::setVertexBuffer(m_vbh);
  157. }
  158. bgfx::VertexDecl m_decl;
  159. bgfx::VertexBufferHandle m_vbh;
  160. bgfx::IndexBufferHandle m_ibh;
  161. };
  162. int _main_(int _argc, char** _argv)
  163. {
  164. bgfx::init(fatalCb);
  165. bgfx::reset(1280, 720);
  166. // Enable debug text.
  167. bgfx::setDebug(BGFX_DEBUG_TEXT);
  168. // Set view 0 default viewport.
  169. bgfx::setViewRect(0, 0, 0, 1280, 720);
  170. // Set view 1 default viewport.
  171. bgfx::setViewRect(1, 0, 0, 1280, 720);
  172. // Set view 0 clear state.
  173. bgfx::setViewClear(0
  174. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  175. , 0x303030ff
  176. , 1.0f
  177. , 0
  178. );
  179. // Setup root path for binary shaders. Shader binaries are different
  180. // for each renderer.
  181. switch (bgfx::getRendererType() )
  182. {
  183. default:
  184. case bgfx::RendererType::Direct3D9:
  185. s_shaderPath = "shaders/dx9/";
  186. break;
  187. case bgfx::RendererType::Direct3D11:
  188. s_shaderPath = "shaders/dx11/";
  189. break;
  190. case bgfx::RendererType::OpenGL:
  191. s_shaderPath = "shaders/glsl/";
  192. s_flipV = true;
  193. break;
  194. case bgfx::RendererType::OpenGLES2:
  195. case bgfx::RendererType::OpenGLES3:
  196. s_shaderPath = "shaders/gles/";
  197. s_flipV = true;
  198. break;
  199. }
  200. bgfx::UniformHandle u_time = bgfx::createUniform("u_time", bgfx::UniformType::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. }