diff options
Diffstat (limited to '3rdparty/bgfx/tools/geometryc/geometryc.cpp')
-rw-r--r-- | 3rdparty/bgfx/tools/geometryc/geometryc.cpp | 198 |
1 files changed, 113 insertions, 85 deletions
diff --git a/3rdparty/bgfx/tools/geometryc/geometryc.cpp b/3rdparty/bgfx/tools/geometryc/geometryc.cpp index c18a3972352..d1d6e014b01 100644 --- a/3rdparty/bgfx/tools/geometryc/geometryc.cpp +++ b/3rdparty/bgfx/tools/geometryc/geometryc.cpp @@ -3,24 +3,27 @@ * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause */ +#include <stdio.h> + #include <algorithm> #include <vector> -#include <string> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +#include <bx/string.h> #include <bgfx/bgfx.h> #include "../../src/vertexdecl.h" #include <tinystl/allocator.h> #include <tinystl/unordered_map.h> #include <tinystl/unordered_set.h> +#include <tinystl/string.h> namespace stl = tinystl; #include <forsyth-too/forsythtriangleorderoptimizer.h> #include <ib-compress/indexbuffercompression.h> +#define BGFX_GEOMETRYC_VERSION_MAJOR 1 +#define BGFX_GEOMETRYC_VERSION_MINOR 0 + #if 0 # define BX_TRACE(_format, ...) \ do { \ @@ -45,23 +48,14 @@ namespace stl = tinystl; } while(0) #endif // 0 -#define EXPECT(_condition) \ - do { \ - if (!(_condition) ) \ - { \ - printf("Error parsing at:\n" BX_FILE_LINE_LITERAL "\nExpected: " #_condition "\n"); \ - exit(EXIT_FAILURE); \ - } \ - } while(0) - #include <bx/bx.h> #include <bx/debug.h> #include <bx/commandline.h> #include <bx/timer.h> #include <bx/hash.h> #include <bx/uint32_t.h> -#include <bx/fpumath.h> -#include <bx/crtimpl.h> +#include <bx/math.h> +#include <bx/file.h> #include "bounds.h" @@ -96,8 +90,8 @@ struct Group { uint32_t m_startTriangle; uint32_t m_numTriangles; - std::string m_name; - std::string m_material; + stl::string m_name; + stl::string m_material; }; typedef std::vector<Group> GroupArray; @@ -108,7 +102,7 @@ struct Primitive uint32_t m_startIndex; uint32_t m_numVertices; uint32_t m_numIndices; - std::string m_name; + stl::string m_name; }; typedef std::vector<Primitive> PrimitiveArray; @@ -133,7 +127,7 @@ void triangleReorder(uint16_t* _indices, uint32_t _numIndices, uint32_t _numVert { uint16_t* newIndexList = new uint16_t[_numIndices]; Forsyth::OptimizeFaces(_indices, _numIndices, _numVertices, newIndexList, _cacheSize); - memcpy(_indices, newIndexList, _numIndices*2); + bx::memCopy(_indices, newIndexList, _numIndices*2); delete [] newIndexList; } @@ -156,9 +150,9 @@ void triangleCompress(bx::WriterI* _writer, uint16_t* _indices, uint32_t _numInd { uint32_t remap = vertexRemap[ii]; remap = UINT32_MAX == remap ? ii : remap; - memcpy(&outVertexData[remap*_stride], &_vertexData[ii*_stride], _stride); + bx::memCopy(&outVertexData[remap*_stride], &_vertexData[ii*_stride], _stride); } - memcpy(_vertexData, outVertexData, _numVertices*_stride); + bx::memCopy(_vertexData, outVertexData, _numVertices*_stride); free(outVertexData); free(vertexRemap); @@ -181,7 +175,7 @@ void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl }; float* tangents = new float[6*_numVertices]; - memset(tangents, 0, 6*_numVertices*sizeof(float) ); + bx::memSet(tangents, 0, 6*_numVertices*sizeof(float) ); PosTexcoord v0; PosTexcoord v1; @@ -301,7 +295,7 @@ void write(bx::WriterI* _writer , uint32_t _numIndices , const uint8_t* _compressedIndices , uint32_t _compressedSize - , const std::string& _material + , const stl::string& _material , const PrimitiveArray& _primitives ) { @@ -350,6 +344,23 @@ void write(bx::WriterI* _writer } } +inline uint32_t rgbaToAbgr(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a) +{ + return (uint32_t(_r)<<0) + | (uint32_t(_g)<<8) + | (uint32_t(_b)<<16) + | (uint32_t(_a)<<24) + ; +} + +struct GroupSortByMaterial +{ + bool operator()(const Group& _lhs, const Group& _rhs) + { + return 0 < bx::strCmp(_lhs.m_material.c_str(), _rhs.m_material.c_str() ); + } +}; + void help(const char* _error = NULL) { if (NULL != _error) @@ -358,9 +369,12 @@ void help(const char* _error = NULL) } fprintf(stderr - , "geometryc, bgfx geometry compiler tool\n" + , "geometryc, bgfx geometry compiler tool, version %d.%d.%d.\n" "Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n" "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n" + , BGFX_GEOMETRYC_VERSION_MAJOR + , BGFX_GEOMETRYC_VERSION_MINOR + , BGFX_API_VERSION ); fprintf(stderr @@ -372,6 +386,8 @@ void help(const char* _error = NULL) "\n" "Options:\n" + " -h, --help Help.\n" + " -v, --version Version information only.\n" " -f <file path> Input file path.\n" " -o <file path> Output file path.\n" " -s, --scale <num> Scale factor.\n" @@ -395,39 +411,39 @@ void help(const char* _error = NULL) ); } -inline uint32_t rgbaToAbgr(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a) +int main(int _argc, const char* _argv[]) { - return (uint32_t(_r)<<0) - | (uint32_t(_g)<<8) - | (uint32_t(_b)<<16) - | (uint32_t(_a)<<24) - ; -} + bx::CommandLine cmdLine(_argc, _argv); -struct GroupSortByMaterial -{ - bool operator()(const Group& _lhs, const Group& _rhs) + if (cmdLine.hasArg('v', "version") ) { - return _lhs.m_material < _rhs.m_material; + fprintf(stderr + , "geometryc, bgfx geometry compiler tool, version %d.%d.%d.\n" + , BGFX_GEOMETRYC_VERSION_MAJOR + , BGFX_GEOMETRYC_VERSION_MINOR + , BGFX_API_VERSION + ); + return bx::kExitSuccess; } -}; -int main(int _argc, const char* _argv[]) -{ - bx::CommandLine cmdLine(_argc, _argv); + if (cmdLine.hasArg('h', "help") ) + { + help(); + return bx::kExitFailure; + } const char* filePath = cmdLine.findOption('f'); if (NULL == filePath) { help("Input file name must be specified."); - return EXIT_FAILURE; + return bx::kExitFailure; } const char* outFilePath = cmdLine.findOption('o'); if (NULL == outFilePath) { help("Output file name must be specified."); - return EXIT_FAILURE; + return bx::kExitFailure; } float scale = 1.0f; @@ -457,7 +473,7 @@ int main(int _argc, const char* _argv[]) if (NULL == file) { printf("Unable to open input file '%s'.", filePath); - exit(EXIT_FAILURE); + exit(bx::kExitFailure); } int64_t parseElapsed = -bx::getHPCounter(); @@ -494,17 +510,17 @@ int main(int _argc, const char* _argv[]) next = bx::tokenizeCommandLine(next, commandLine, len, argc, argv, BX_COUNTOF(argv), '\n'); if (0 < argc) { - if (0 == strcmp(argv[0], "#") ) + if (0 == bx::strCmp(argv[0], "#") ) { if (2 < argc - && 0 == strcmp(argv[2], "polygons") ) + && 0 == bx::strCmp(argv[2], "polygons") ) { } } - else if (0 == strcmp(argv[0], "f") ) + else if (0 == bx::strCmp(argv[0], "f") ) { Triangle triangle; - memset(&triangle, 0, sizeof(Triangle) ); + bx::memSet(&triangle, 0, sizeof(Triangle) ); const int numNormals = (int)normals.size(); const int numTexcoords = (int)texcoords.size(); @@ -524,29 +540,32 @@ int main(int _argc, const char* _argv[]) index.m_vbc = 0; } - char* vertex = argv[edge+1]; - char* texcoord = strchr(vertex, '/'); + const char* vertex = argv[edge+1]; + char* texcoord = const_cast<char*>(bx::strFind(vertex, '/') ); if (NULL != texcoord) { *texcoord++ = '\0'; - char* normal = strchr(texcoord, '/'); + char* normal = const_cast<char*>(bx::strFind(texcoord, '/') ); if (NULL != normal) { *normal++ = '\0'; - const int nn = atoi(normal); + int32_t nn; + bx::fromString(&nn, normal); index.m_normal = (nn < 0) ? nn+numNormals : nn-1; } // https://en.wikipedia.org/wiki/Wavefront_.obj_file#Vertex_Normal_Indices_Without_Texture_Coordinate_Indices if(*texcoord != '\0') { - const int tex = atoi(texcoord); + int32_t tex; + bx::fromString(&tex, texcoord); index.m_texcoord = (tex < 0) ? tex+numTexcoords : tex-1; } } - const int pos = atoi(vertex); + int32_t pos; + bx::fromString(&pos, vertex); index.m_position = (pos < 0) ? pos+numPositions : pos-1; uint64_t hash0 = index.m_position; @@ -599,9 +618,8 @@ int main(int _argc, const char* _argv[]) } } } - else if (0 == strcmp(argv[0], "g") ) + else if (0 == bx::strCmp(argv[0], "g") ) { - EXPECT(1 < argc); group.m_name = argv[1]; } else if (*argv[0] == 'v') @@ -614,7 +632,7 @@ int main(int _argc, const char* _argv[]) group.m_numTriangles = 0; } - if (0 == strcmp(argv[0], "vn") ) + if (0 == bx::strCmp(argv[0], "vn") ) { Vector3 normal; normal.x = (float)atof(argv[1]); @@ -623,7 +641,7 @@ int main(int _argc, const char* _argv[]) normals.push_back(normal); } - else if (0 == strcmp(argv[0], "vp") ) + else if (0 == bx::strCmp(argv[0], "vp") ) { static bool once = true; if (once) @@ -632,7 +650,7 @@ int main(int _argc, const char* _argv[]) printf("warning: 'parameter space vertices' are unsupported.\n"); } } - else if (0 == strcmp(argv[0], "vt") ) + else if (0 == bx::strCmp(argv[0], "vt") ) { Vector3 texcoord; texcoord.x = (float)atof(argv[1]); @@ -677,11 +695,11 @@ int main(int _argc, const char* _argv[]) positions.push_back(pos); } } - else if (0 == strcmp(argv[0], "usemtl") ) + else if (0 == bx::strCmp(argv[0], "usemtl") ) { - std::string material(argv[1]); + stl::string material(argv[1]); - if (material != group.m_material) + if (0 != bx::strCmp(material.c_str(), group.m_material.c_str() ) ) { group.m_numTriangles = (uint32_t)(triangles.size() ) - group.m_startTriangle; if (0 < group.m_numTriangles) @@ -695,13 +713,13 @@ int main(int _argc, const char* _argv[]) group.m_material = material; } // unsupported tags -// else if (0 == strcmp(argv[0], "mtllib") ) +// else if (0 == bx::strCmp(argv[0], "mtllib") ) // { // } -// else if (0 == strcmp(argv[0], "o") ) +// else if (0 == bx::strCmp(argv[0], "o") ) // { // } -// else if (0 == strcmp(argv[0], "s") ) +// else if (0 == bx::strCmp(argv[0], "s") ) // { // } } @@ -734,25 +752,35 @@ int main(int _argc, const char* _argv[]) hasNormal = -1 != it->second.m_normal; hasTexcoord = -1 != it->second.m_texcoord; - if (!hasTexcoord - && texcoords.size() == positions.size() ) + if (!hasTexcoord) { - hasTexcoord = true; + for (Index3Map::iterator jt = indexMap.begin(), jtEnd = indexMap.end(); jt != jtEnd && !hasTexcoord; ++jt) + { + hasTexcoord |= -1 != jt->second.m_texcoord; + } - for (Index3Map::iterator jt = indexMap.begin(), jtEnd = indexMap.end(); jt != jtEnd; ++jt) + if (hasTexcoord) { - jt->second.m_texcoord = jt->second.m_position; + for (Index3Map::iterator jt = indexMap.begin(), jtEnd = indexMap.end(); jt != jtEnd; ++jt) + { + jt->second.m_texcoord = -1 == jt->second.m_texcoord ? 0 : jt->second.m_texcoord; + } } } - if (!hasNormal - && normals.size() == positions.size() ) + if (!hasNormal) { - hasNormal = true; + for (Index3Map::iterator jt = indexMap.begin(), jtEnd = indexMap.end(); jt != jtEnd && !hasNormal; ++jt) + { + hasNormal |= -1 != jt->second.m_normal; + } - for (Index3Map::iterator jt = indexMap.begin(), jtEnd = indexMap.end(); jt != jtEnd; ++jt) + if (hasNormal) { - jt->second.m_normal = jt->second.m_position; + for (Index3Map::iterator jt = indexMap.begin(), jtEnd = indexMap.end(); jt != jtEnd; ++jt) + { + jt->second.m_normal = -1 == jt->second.m_normal ? 0 : jt->second.m_normal; + } } } } @@ -822,15 +850,15 @@ int main(int _argc, const char* _argv[]) uint8_t* vertices = vertexData; uint16_t* indices = indexData; - std::string material = groups.begin()->m_material; + stl::string material = groups.begin()->m_material; PrimitiveArray primitives; - bx::CrtFileWriter writer; + bx::FileWriter writer; if (!bx::open(&writer, outFilePath) ) { printf("Unable to open output file '%s'.", outFilePath); - exit(EXIT_FAILURE); + exit(bx::kExitFailure); } Primitive prim; @@ -840,7 +868,7 @@ int main(int _argc, const char* _argv[]) uint32_t positionOffset = decl.getOffset(bgfx::Attrib::Position); uint32_t color0Offset = decl.getOffset(bgfx::Attrib::Color0); - bx::CrtAllocator crtAllocator; + bx::DefaultAllocator crtAllocator; bx::MemoryBlock memBlock(&crtAllocator); uint32_t ii = 0; @@ -848,7 +876,7 @@ int main(int _argc, const char* _argv[]) { for (uint32_t tri = groupIt->m_startTriangle, end = tri + groupIt->m_numTriangles; tri < end; ++tri) { - if (material != groupIt->m_material + if (0 != bx::strCmp(material.c_str(), groupIt->m_material.c_str() ) || 65533 < numVertices) { prim.m_numVertices = numVertices - prim.m_startVertex; @@ -860,7 +888,7 @@ int main(int _argc, const char* _argv[]) if (hasTangent) { - calcTangents(vertexData, numVertices, decl, indexData, numIndices); + calcTangents(vertexData, uint16_t(numVertices), decl, indexData, numIndices); } bx::MemoryWriter memWriter(&memBlock); @@ -877,7 +905,7 @@ int main(int _argc, const char* _argv[]) , prim1.m_numIndices , vertexData + prim1.m_startVertex , numVertices - , stride + , uint16_t(stride) ); } } @@ -922,7 +950,7 @@ int main(int _argc, const char* _argv[]) index.m_vertexIndex = numVertices++; float* position = (float*)(vertices + positionOffset); - memcpy(position, &positions[index.m_position], 3*sizeof(float) ); + bx::memCopy(position, &positions[index.m_position], 3*sizeof(float) ); if (hasColor) { @@ -944,7 +972,7 @@ int main(int _argc, const char* _argv[]) if (hasTexcoord) { float uv[2]; - memcpy(uv, &texcoords[index.m_texcoord], 2*sizeof(float) ); + bx::memCopy(uv, &texcoords[index.m_texcoord], 2*sizeof(float) ); if (flipV) { @@ -991,7 +1019,7 @@ int main(int _argc, const char* _argv[]) { if (hasTangent) { - calcTangents(vertexData, numVertices, decl, indexData, numIndices); + calcTangents(vertexData, uint16_t(numVertices), decl, indexData, numIndices); } bx::MemoryWriter memWriter(&memBlock); @@ -1008,7 +1036,7 @@ int main(int _argc, const char* _argv[]) , prim1.m_numIndices , vertexData + prim1.m_startVertex , numVertices - , stride + , uint16_t(stride) ); } } @@ -1047,5 +1075,5 @@ int main(int _argc, const char* _argv[]) , numIndices ); - return EXIT_SUCCESS; + return bx::kExitSuccess; } |