|
|
@@ -1,22 +1,22 @@
|
|
|
-/*
|
|
|
- * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
|
|
|
- * License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
- */
|
|
|
-
|
|
|
-#include <bgfx.h>
|
|
|
-#include "../../src/vertexdecl.h"
|
|
|
-
|
|
|
-#include <stdio.h>
|
|
|
-#include <stdlib.h>
|
|
|
-#include <string.h>
|
|
|
-#include <algorithm>
|
|
|
-#include <vector>
|
|
|
+/*
|
|
|
+ * Copyright 2011-2012 Branimir Karadzic. All rights reserved.
|
|
|
+ * License: http://www.opensource.org/licenses/BSD-2-Clause
|
|
|
+ */
|
|
|
+
|
|
|
+#include <bgfx.h>
|
|
|
+#include "../../src/vertexdecl.h"
|
|
|
+
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdlib.h>
|
|
|
+#include <string.h>
|
|
|
+#include <algorithm>
|
|
|
+#include <vector>
|
|
|
namespace std { namespace tr1 {} using namespace tr1; } // namespace std
|
|
|
-#include <unordered_map>
|
|
|
-
|
|
|
-#include <forsythtriangleorderoptimizer.h>
|
|
|
-
|
|
|
-#if 0
|
|
|
+#include <unordered_map>
|
|
|
+
|
|
|
+#include <forsythtriangleorderoptimizer.h>
|
|
|
+
|
|
|
+#if 0
|
|
|
# define BX_TRACE(_format, ...) \
|
|
|
do { \
|
|
|
printf(BX_FILE_LINE_LITERAL "BGFX " _format "\n", ##__VA_ARGS__); \
|
|
|
@@ -38,30 +38,30 @@ namespace std { namespace tr1 {} using namespace tr1; } // namespace std
|
|
|
bx::debugBreak(); \
|
|
|
} \
|
|
|
} while(0)
|
|
|
-#endif // 0
|
|
|
+#endif // 0
|
|
|
|
|
|
-#define EXPECT(_condition) \
|
|
|
- do { \
|
|
|
+#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/countof.h>
|
|
|
-#include <bx/commandline.h>
|
|
|
-#include <bx/timer.h>
|
|
|
-#include <bx/readerwriter.h>
|
|
|
-#include <bx/hash.h>
|
|
|
-#include <bx/uint32_t.h>
|
|
|
-
|
|
|
-#include "tokenizecmd.h"
|
|
|
-#include "bounds.h"
|
|
|
-#include "math.h"
|
|
|
-
|
|
|
+ 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/countof.h>
|
|
|
+#include <bx/commandline.h>
|
|
|
+#include <bx/timer.h>
|
|
|
+#include <bx/readerwriter.h>
|
|
|
+#include <bx/hash.h>
|
|
|
+#include <bx/uint32_t.h>
|
|
|
+
|
|
|
+#include "tokenizecmd.h"
|
|
|
+#include "bounds.h"
|
|
|
+#include "math.h"
|
|
|
+
|
|
|
struct Vector3
|
|
|
{
|
|
|
float x;
|
|
|
@@ -69,8 +69,8 @@ struct Vector3
|
|
|
float z;
|
|
|
};
|
|
|
|
|
|
-typedef std::vector<Vector3> Vector3Array;
|
|
|
-
|
|
|
+typedef std::vector<Vector3> Vector3Array;
|
|
|
+
|
|
|
struct Index3
|
|
|
{
|
|
|
int32_t m_position;
|
|
|
@@ -79,43 +79,43 @@ struct Index3
|
|
|
int32_t m_vertexIndex;
|
|
|
};
|
|
|
|
|
|
-typedef std::unordered_map<uint64_t, Index3> Index3Map;
|
|
|
-
|
|
|
-struct Triangle
|
|
|
-{
|
|
|
- uint64_t m_index[3];
|
|
|
-};
|
|
|
-
|
|
|
-typedef std::vector<Triangle> TriangleArray;
|
|
|
-
|
|
|
-struct Group
|
|
|
-{
|
|
|
- uint32_t m_startTriangle;
|
|
|
- uint32_t m_numTriangles;
|
|
|
- std::string m_name;
|
|
|
- std::string m_material;
|
|
|
-};
|
|
|
-
|
|
|
-typedef std::vector<Group> GroupArray;
|
|
|
-
|
|
|
-struct Primitive
|
|
|
-{
|
|
|
- uint32_t m_startVertex;
|
|
|
- uint32_t m_startIndex;
|
|
|
- uint32_t m_numVertices;
|
|
|
- uint32_t m_numIndices;
|
|
|
- std::string m_name;
|
|
|
-};
|
|
|
-
|
|
|
-typedef std::vector<Primitive> PrimitiveArray;
|
|
|
-
|
|
|
-static uint32_t s_obbSteps = 17;
|
|
|
-
|
|
|
-#define BGFX_CHUNK_MAGIC_GEO BX_MAKEFOURCC('G', 'E', 'O', 0x0)
|
|
|
-#define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x0)
|
|
|
-#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
|
|
|
-#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
|
|
|
-
|
|
|
+typedef std::unordered_map<uint64_t, Index3> Index3Map;
|
|
|
+
|
|
|
+struct Triangle
|
|
|
+{
|
|
|
+ uint64_t m_index[3];
|
|
|
+};
|
|
|
+
|
|
|
+typedef std::vector<Triangle> TriangleArray;
|
|
|
+
|
|
|
+struct Group
|
|
|
+{
|
|
|
+ uint32_t m_startTriangle;
|
|
|
+ uint32_t m_numTriangles;
|
|
|
+ std::string m_name;
|
|
|
+ std::string m_material;
|
|
|
+};
|
|
|
+
|
|
|
+typedef std::vector<Group> GroupArray;
|
|
|
+
|
|
|
+struct Primitive
|
|
|
+{
|
|
|
+ uint32_t m_startVertex;
|
|
|
+ uint32_t m_startIndex;
|
|
|
+ uint32_t m_numVertices;
|
|
|
+ uint32_t m_numIndices;
|
|
|
+ std::string m_name;
|
|
|
+};
|
|
|
+
|
|
|
+typedef std::vector<Primitive> PrimitiveArray;
|
|
|
+
|
|
|
+static uint32_t s_obbSteps = 17;
|
|
|
+
|
|
|
+#define BGFX_CHUNK_MAGIC_GEO BX_MAKEFOURCC('G', 'E', 'O', 0x0)
|
|
|
+#define BGFX_CHUNK_MAGIC_VB BX_MAKEFOURCC('V', 'B', ' ', 0x0)
|
|
|
+#define BGFX_CHUNK_MAGIC_IB BX_MAKEFOURCC('I', 'B', ' ', 0x0)
|
|
|
+#define BGFX_CHUNK_MAGIC_PRI BX_MAKEFOURCC('P', 'R', 'I', 0x0)
|
|
|
+
|
|
|
long int fsize(FILE* _file)
|
|
|
{
|
|
|
long int pos = ftell(_file);
|
|
|
@@ -234,61 +234,61 @@ void calcTangents(void* _vertices, uint16_t _numVertices, bgfx::VertexDecl _decl
|
|
|
|
|
|
void writeBounds(bx::WriterI* _writer, const void* _vertices, uint32_t _numVertices, uint32_t _stride)
|
|
|
{
|
|
|
- Sphere maxSphere;
|
|
|
- calcMaxBoundingSphere(maxSphere, _vertices, _numVertices, _stride);
|
|
|
-
|
|
|
- Sphere minSphere;
|
|
|
- calcMinBoundingSphere(minSphere, _vertices, _numVertices, _stride);
|
|
|
-
|
|
|
- if (minSphere.m_radius > maxSphere.m_radius)
|
|
|
- {
|
|
|
- bx::write(_writer, maxSphere);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- bx::write(_writer, minSphere);
|
|
|
- }
|
|
|
-
|
|
|
+ Sphere maxSphere;
|
|
|
+ calcMaxBoundingSphere(maxSphere, _vertices, _numVertices, _stride);
|
|
|
+
|
|
|
+ Sphere minSphere;
|
|
|
+ calcMinBoundingSphere(minSphere, _vertices, _numVertices, _stride);
|
|
|
+
|
|
|
+ if (minSphere.m_radius > maxSphere.m_radius)
|
|
|
+ {
|
|
|
+ bx::write(_writer, maxSphere);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ bx::write(_writer, minSphere);
|
|
|
+ }
|
|
|
+
|
|
|
Aabb aabb;
|
|
|
- calcAabb(aabb, _vertices, _numVertices, _stride);
|
|
|
- bx::write(_writer, aabb);
|
|
|
-
|
|
|
- Obb obb;
|
|
|
- calcObb(obb, _vertices, _numVertices, _stride, s_obbSteps);
|
|
|
- bx::write(_writer, obb);
|
|
|
+ calcAabb(aabb, _vertices, _numVertices, _stride);
|
|
|
+ bx::write(_writer, aabb);
|
|
|
+
|
|
|
+ Obb obb;
|
|
|
+ calcObb(obb, _vertices, _numVertices, _stride, s_obbSteps);
|
|
|
+ bx::write(_writer, obb);
|
|
|
}
|
|
|
|
|
|
void write(bx::WriterI* _writer, const uint8_t* _vertices, uint32_t _numVertices, const bgfx::VertexDecl& _decl, const uint16_t* _indices, uint32_t _numIndices, const std::string& _material, const PrimitiveArray& _primitives)
|
|
|
{
|
|
|
uint32_t stride = _decl.getStride();
|
|
|
- bx::write(_writer, BGFX_CHUNK_MAGIC_VB);
|
|
|
- writeBounds(_writer, _vertices, _numVertices, stride);
|
|
|
-
|
|
|
- bx::write(_writer, _decl);
|
|
|
- bx::write(_writer, uint16_t(_numVertices) );
|
|
|
- bx::write(_writer, _vertices, _numVertices*stride);
|
|
|
-
|
|
|
- bx::write(_writer, BGFX_CHUNK_MAGIC_IB);
|
|
|
- bx::write(_writer, _numIndices);
|
|
|
- bx::write(_writer, _indices, _numIndices*2);
|
|
|
-
|
|
|
- bx::write(_writer, BGFX_CHUNK_MAGIC_PRI);
|
|
|
- uint16_t nameLen = uint16_t(_material.size() );
|
|
|
- bx::write(_writer, nameLen);
|
|
|
- bx::write(_writer, _material.c_str(), nameLen);
|
|
|
- bx::write(_writer, uint16_t(_primitives.size() ) );
|
|
|
- for (PrimitiveArray::const_iterator primIt = _primitives.begin(); primIt != _primitives.end(); ++primIt)
|
|
|
- {
|
|
|
- const Primitive& prim = *primIt;
|
|
|
- nameLen = uint16_t(prim.m_name.size() );
|
|
|
- bx::write(_writer, nameLen);
|
|
|
- bx::write(_writer, prim.m_name.c_str(), nameLen);
|
|
|
- bx::write(_writer, prim.m_startIndex);
|
|
|
- bx::write(_writer, prim.m_numIndices);
|
|
|
- bx::write(_writer, prim.m_startVertex);
|
|
|
- bx::write(_writer, prim.m_numVertices);
|
|
|
- writeBounds(_writer, &_vertices[prim.m_startVertex*stride], prim.m_numVertices, stride);
|
|
|
- }
|
|
|
+ bx::write(_writer, BGFX_CHUNK_MAGIC_VB);
|
|
|
+ writeBounds(_writer, _vertices, _numVertices, stride);
|
|
|
+
|
|
|
+ bx::write(_writer, _decl);
|
|
|
+ bx::write(_writer, uint16_t(_numVertices) );
|
|
|
+ bx::write(_writer, _vertices, _numVertices*stride);
|
|
|
+
|
|
|
+ bx::write(_writer, BGFX_CHUNK_MAGIC_IB);
|
|
|
+ bx::write(_writer, _numIndices);
|
|
|
+ bx::write(_writer, _indices, _numIndices*2);
|
|
|
+
|
|
|
+ bx::write(_writer, BGFX_CHUNK_MAGIC_PRI);
|
|
|
+ uint16_t nameLen = uint16_t(_material.size() );
|
|
|
+ bx::write(_writer, nameLen);
|
|
|
+ bx::write(_writer, _material.c_str(), nameLen);
|
|
|
+ bx::write(_writer, uint16_t(_primitives.size() ) );
|
|
|
+ for (PrimitiveArray::const_iterator primIt = _primitives.begin(); primIt != _primitives.end(); ++primIt)
|
|
|
+ {
|
|
|
+ const Primitive& prim = *primIt;
|
|
|
+ nameLen = uint16_t(prim.m_name.size() );
|
|
|
+ bx::write(_writer, nameLen);
|
|
|
+ bx::write(_writer, prim.m_name.c_str(), nameLen);
|
|
|
+ bx::write(_writer, prim.m_startIndex);
|
|
|
+ bx::write(_writer, prim.m_numIndices);
|
|
|
+ bx::write(_writer, prim.m_startVertex);
|
|
|
+ bx::write(_writer, prim.m_numVertices);
|
|
|
+ writeBounds(_writer, &_vertices[prim.m_startVertex*stride], prim.m_numVertices, stride);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void help(const char* _error = NULL)
|
|
|
@@ -343,75 +343,75 @@ inline uint32_t rgbaToAbgr(uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a)
|
|
|
;
|
|
|
}
|
|
|
|
|
|
-int main(int _argc, const char* _argv[])
|
|
|
-{
|
|
|
- bx::CommandLine cmdLine(_argc, _argv);
|
|
|
-
|
|
|
- const char* filePath = cmdLine.findOption('f');
|
|
|
- if (NULL == filePath)
|
|
|
- {
|
|
|
- help("Input file name must be specified.");
|
|
|
- return EXIT_FAILURE;
|
|
|
- }
|
|
|
-
|
|
|
- const char* outFilePath = cmdLine.findOption('o');
|
|
|
- if (NULL == outFilePath)
|
|
|
- {
|
|
|
- help("Output file name must be specified.");
|
|
|
- return EXIT_FAILURE;
|
|
|
- }
|
|
|
-
|
|
|
- float scale = 1.0f;
|
|
|
- const char* scaleArg = cmdLine.findOption('s', "scale");
|
|
|
- if (NULL != scaleArg)
|
|
|
- {
|
|
|
- scale = (float)atof(scaleArg);
|
|
|
- }
|
|
|
-
|
|
|
- cmdLine.hasArg(s_obbSteps, '\0', "obb");
|
|
|
- s_obbSteps = bx::uint32_min(bx::uint32_max(s_obbSteps, 1), 90);
|
|
|
-
|
|
|
- uint32_t packNormal = 0;
|
|
|
- cmdLine.hasArg(packNormal, '\0', "packnormal");
|
|
|
-
|
|
|
- uint32_t packUv = 0;
|
|
|
- cmdLine.hasArg(packNormal, '\0', "packuv");
|
|
|
-
|
|
|
- bool ccw = cmdLine.hasArg("ccw");
|
|
|
- bool flipV = cmdLine.hasArg("flipv");
|
|
|
- bool hasTangent = cmdLine.hasArg("tangent");
|
|
|
-
|
|
|
- FILE* file = fopen(filePath, "r");
|
|
|
- if (NULL == file)
|
|
|
- {
|
|
|
- printf("Unable to open input file '%s'.", filePath);
|
|
|
- exit(EXIT_FAILURE);
|
|
|
- }
|
|
|
-
|
|
|
- int64_t parseElapsed = -bx::getHPCounter();
|
|
|
- int64_t triReorderElapsed = 0;
|
|
|
-
|
|
|
- uint32_t size = (uint32_t)fsize(file);
|
|
|
- char* data = new char[size+1];
|
|
|
- size = (uint32_t)fread(data, 1, size, file);
|
|
|
- data[size] = '\0';
|
|
|
- fclose(file);
|
|
|
-
|
|
|
- // https://en.wikipedia.org/wiki/Wavefront_.obj_file
|
|
|
-
|
|
|
- Vector3Array positions;
|
|
|
- Vector3Array normals;
|
|
|
- Vector3Array texcoords;
|
|
|
- Index3Map indexMap;
|
|
|
- TriangleArray triangles;
|
|
|
- GroupArray groups;
|
|
|
-
|
|
|
- uint32_t num = 0;
|
|
|
-
|
|
|
- Group group;
|
|
|
- group.m_startTriangle = 0;
|
|
|
- group.m_numTriangles = 0;
|
|
|
-
|
|
|
+int main(int _argc, const char* _argv[])
|
|
|
+{
|
|
|
+ bx::CommandLine cmdLine(_argc, _argv);
|
|
|
+
|
|
|
+ const char* filePath = cmdLine.findOption('f');
|
|
|
+ if (NULL == filePath)
|
|
|
+ {
|
|
|
+ help("Input file name must be specified.");
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+
|
|
|
+ const char* outFilePath = cmdLine.findOption('o');
|
|
|
+ if (NULL == outFilePath)
|
|
|
+ {
|
|
|
+ help("Output file name must be specified.");
|
|
|
+ return EXIT_FAILURE;
|
|
|
+ }
|
|
|
+
|
|
|
+ float scale = 1.0f;
|
|
|
+ const char* scaleArg = cmdLine.findOption('s', "scale");
|
|
|
+ if (NULL != scaleArg)
|
|
|
+ {
|
|
|
+ scale = (float)atof(scaleArg);
|
|
|
+ }
|
|
|
+
|
|
|
+ cmdLine.hasArg(s_obbSteps, '\0', "obb");
|
|
|
+ s_obbSteps = bx::uint32_min(bx::uint32_max(s_obbSteps, 1), 90);
|
|
|
+
|
|
|
+ uint32_t packNormal = 0;
|
|
|
+ cmdLine.hasArg(packNormal, '\0', "packnormal");
|
|
|
+
|
|
|
+ uint32_t packUv = 0;
|
|
|
+ cmdLine.hasArg(packNormal, '\0', "packuv");
|
|
|
+
|
|
|
+ bool ccw = cmdLine.hasArg("ccw");
|
|
|
+ bool flipV = cmdLine.hasArg("flipv");
|
|
|
+ bool hasTangent = cmdLine.hasArg("tangent");
|
|
|
+
|
|
|
+ FILE* file = fopen(filePath, "r");
|
|
|
+ if (NULL == file)
|
|
|
+ {
|
|
|
+ printf("Unable to open input file '%s'.", filePath);
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
+ }
|
|
|
+
|
|
|
+ int64_t parseElapsed = -bx::getHPCounter();
|
|
|
+ int64_t triReorderElapsed = 0;
|
|
|
+
|
|
|
+ uint32_t size = (uint32_t)fsize(file);
|
|
|
+ char* data = new char[size+1];
|
|
|
+ size = (uint32_t)fread(data, 1, size, file);
|
|
|
+ data[size] = '\0';
|
|
|
+ fclose(file);
|
|
|
+
|
|
|
+ // https://en.wikipedia.org/wiki/Wavefront_.obj_file
|
|
|
+
|
|
|
+ Vector3Array positions;
|
|
|
+ Vector3Array normals;
|
|
|
+ Vector3Array texcoords;
|
|
|
+ Index3Map indexMap;
|
|
|
+ TriangleArray triangles;
|
|
|
+ GroupArray groups;
|
|
|
+
|
|
|
+ uint32_t num = 0;
|
|
|
+
|
|
|
+ Group group;
|
|
|
+ group.m_startTriangle = 0;
|
|
|
+ group.m_numTriangles = 0;
|
|
|
+
|
|
|
char commandLine[2048];
|
|
|
uint32_t len = sizeof(commandLine);
|
|
|
int argc;
|
|
|
@@ -467,6 +467,7 @@ int main(int _argc, const char* _argv[])
|
|
|
if (!result.second)
|
|
|
{
|
|
|
Index3& oldIndex = result.first->second;
|
|
|
+ BX_UNUSED(oldIndex);
|
|
|
BX_CHECK(oldIndex.m_position == index.m_position
|
|
|
&& oldIndex.m_texcoord == index.m_texcoord
|
|
|
&& oldIndex.m_normal == index.m_normal
|