2015-04-16 16:55:32 +00:00
|
|
|
#include "LoadMeshFromObj.h"
|
2017-05-18 02:29:12 +00:00
|
|
|
|
2016-05-06 20:57:48 +00:00
|
|
|
#include "../../OpenGLWindow/GLInstanceGraphicsShape.h"
|
2018-09-23 21:17:31 +00:00
|
|
|
#include <stdio.h> //fopen
|
2015-04-16 16:55:32 +00:00
|
|
|
#include "Bullet3Common/b3AlignedObjectArray.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include "Wavefront2GLInstanceGraphicsShape.h"
|
2017-05-18 02:29:12 +00:00
|
|
|
#include "Bullet3Common/b3HashMap.h"
|
|
|
|
|
|
|
|
struct CachedObjResult
|
|
|
|
{
|
|
|
|
std::string m_msg;
|
|
|
|
std::vector<tinyobj::shape_t> m_shapes;
|
2019-08-13 23:53:51 +00:00
|
|
|
tinyobj::attrib_t m_attribute;
|
2017-05-18 02:29:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static b3HashMap<b3HashString, CachedObjResult> gCachedObjResults;
|
|
|
|
static int gEnableFileCaching = 1;
|
|
|
|
|
2017-10-05 18:43:14 +00:00
|
|
|
int b3IsFileCachingEnabled()
|
|
|
|
{
|
|
|
|
return gEnableFileCaching;
|
|
|
|
}
|
2017-05-18 02:29:12 +00:00
|
|
|
void b3EnableFileCaching(int enable)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
gEnableFileCaching = enable;
|
|
|
|
if (enable == 0)
|
2017-05-18 02:29:12 +00:00
|
|
|
{
|
|
|
|
gCachedObjResults.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string LoadFromCachedOrFromObj(
|
2019-08-13 23:53:51 +00:00
|
|
|
tinyobj::attrib_t& attribute,
|
2018-09-23 21:17:31 +00:00
|
|
|
std::vector<tinyobj::shape_t>& shapes, // [output]
|
|
|
|
const char* filename,
|
2018-10-09 04:27:08 +00:00
|
|
|
const char* mtl_basepath,
|
2019-08-13 23:53:51 +00:00
|
|
|
struct CommonFileIOInterface* fileIO)
|
2017-05-18 02:29:12 +00:00
|
|
|
{
|
|
|
|
CachedObjResult* resultPtr = gCachedObjResults[filename];
|
|
|
|
if (resultPtr)
|
|
|
|
{
|
|
|
|
const CachedObjResult& result = *resultPtr;
|
|
|
|
shapes = result.m_shapes;
|
2019-08-13 23:53:51 +00:00
|
|
|
attribute = result.m_attribute;
|
2017-05-18 02:29:12 +00:00
|
|
|
return result.m_msg;
|
|
|
|
}
|
|
|
|
|
2019-08-13 23:53:51 +00:00
|
|
|
std::string err = tinyobj::LoadObj(attribute, shapes, filename, mtl_basepath, fileIO);
|
2017-05-18 02:29:12 +00:00
|
|
|
CachedObjResult result;
|
|
|
|
result.m_msg = err;
|
|
|
|
result.m_shapes = shapes;
|
2019-08-13 23:53:51 +00:00
|
|
|
result.m_attribute = attribute;
|
2017-05-18 02:29:12 +00:00
|
|
|
if (gEnableFileCaching)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
gCachedObjResults.insert(filename, result);
|
2017-05-18 02:29:12 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-10-09 04:27:08 +00:00
|
|
|
GLInstanceGraphicsShape* LoadMeshFromObj(const char* relativeFileName, const char* materialPrefixPath, struct CommonFileIOInterface* fileIO)
|
2015-04-16 16:55:32 +00:00
|
|
|
{
|
2017-04-12 22:02:47 +00:00
|
|
|
B3_PROFILE("LoadMeshFromObj");
|
2015-04-16 16:55:32 +00:00
|
|
|
std::vector<tinyobj::shape_t> shapes;
|
2019-08-13 23:53:51 +00:00
|
|
|
tinyobj::attrib_t attribute;
|
2017-04-12 22:02:47 +00:00
|
|
|
{
|
|
|
|
B3_PROFILE("tinyobj::LoadObj2");
|
2019-08-13 23:53:51 +00:00
|
|
|
std::string err = LoadFromCachedOrFromObj(attribute, shapes, relativeFileName, materialPrefixPath, fileIO);
|
2017-04-12 22:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
B3_PROFILE("btgCreateGraphicsShapeFromWavefrontObj");
|
2019-08-13 23:53:51 +00:00
|
|
|
GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(attribute, shapes);
|
2017-04-12 22:02:47 +00:00
|
|
|
return gfxShape;
|
|
|
|
}
|
2015-04-16 22:52:30 +00:00
|
|
|
}
|