Skip to content

Commit

Permalink
update obj loader
Browse files Browse the repository at this point in the history
  • Loading branch information
fuchuyuan committed Aug 13, 2019
1 parent 1981493 commit 10108cd
Show file tree
Hide file tree
Showing 12 changed files with 475 additions and 365 deletions.
41 changes: 21 additions & 20 deletions Extras/obj2sdf/obj2sdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ int main(int argc, char* argv[])
b3FileUtils::extractPath(fileNameWithPath, materialPrefixPath, MAX_PATH_LEN);

std::vector<tinyobj::shape_t> shapes;
tinyobj::attrib_t attribute;

b3BulletDefaultFileIO fileIO;
std::string err = tinyobj::LoadObj(shapes, fileNameWithPath, materialPrefixPath,&fileIO);
std::string err = tinyobj::LoadObj(attribute, shapes, fileNameWithPath, materialPrefixPath,&fileIO);

char sdfFileName[MAX_PATH_LEN];
sprintf(sdfFileName, "%s%s.sdf", materialPrefixPath, "newsdf");
Expand Down Expand Up @@ -117,20 +118,20 @@ int main(int argc, char* argv[])
int curTexcoords = shapeC->texcoords.size() / 2;

int faceCount = shape.mesh.indices.size();
int vertexCount = shape.mesh.positions.size();
int vertexCount = attribute.vertices.size();
for (int v = 0; v < vertexCount; v++)
{
shapeC->positions.push_back(shape.mesh.positions[v]);
shapeC->positions.push_back(attribute.vertices[v]);
}
int numNormals = int(shape.mesh.normals.size());
int numNormals = int(attribute.normals.size());
for (int vn = 0; vn < numNormals; vn++)
{
shapeC->normals.push_back(shape.mesh.normals[vn]);
shapeC->normals.push_back(attribute.normals[vn]);
}
int numTexCoords = int(shape.mesh.texcoords.size());
int numTexCoords = int(attribute.texcoords.size());
for (int vt = 0; vt < numTexCoords; vt++)
{
shapeC->texcoords.push_back(shape.mesh.texcoords[vt]);
shapeC->texcoords.push_back(attribute.texcoords[vt]);
}

for (int face = 0; face < faceCount; face += 3)
Expand All @@ -140,9 +141,9 @@ int main(int argc, char* argv[])
continue;
}

shapeC->indices.push_back(shape.mesh.indices[face] + curPositions);
shapeC->indices.push_back(shape.mesh.indices[face + 1] + curPositions);
shapeC->indices.push_back(shape.mesh.indices[face + 2] + curPositions);
shapeC->indices.push_back(shape.mesh.indices[face].vertex_index + curPositions);
shapeC->indices.push_back(shape.mesh.indices[face + 1].vertex_index + curPositions);
shapeC->indices.push_back(shape.mesh.indices[face + 2].vertex_index + curPositions);
}
}
}
Expand Down Expand Up @@ -329,7 +330,7 @@ int main(int argc, char* argv[])
}

int faceCount = shape.mesh.indices.size();
int vertexCount = shape.mesh.positions.size();
int vertexCount = attribute.vertices.size();
tinyobj::material_t mat = shape.material;
if (shape.name.length())
{
Expand All @@ -339,7 +340,7 @@ int main(int argc, char* argv[])
}
for (int v = 0; v < vertexCount / 3; v++)
{
fprintf(f, "v %f %f %f\n", shape.mesh.positions[v * 3 + 0], shape.mesh.positions[v * 3 + 1], shape.mesh.positions[v * 3 + 2]);
fprintf(f, "v %f %f %f\n", attribute.vertices[v * 3 + 0], attribute.vertices[v * 3 + 1], attribute.vertices[v * 3 + 2]);
}

if (mat.name.length())
Expand All @@ -352,18 +353,18 @@ int main(int argc, char* argv[])
}

fprintf(f, "\n");
int numNormals = int(shape.mesh.normals.size());
int numNormals = int(attribute.normals.size());

for (int vn = 0; vn < numNormals / 3; vn++)
{
fprintf(f, "vn %f %f %f\n", shape.mesh.normals[vn * 3 + 0], shape.mesh.normals[vn * 3 + 1], shape.mesh.normals[vn * 3 + 2]);
fprintf(f, "vn %f %f %f\n", attribute.normals[vn * 3 + 0], attribute.normals[vn * 3 + 1], attribute.normals[vn * 3 + 2]);
}

fprintf(f, "\n");
int numTexCoords = int(shape.mesh.texcoords.size());
int numTexCoords = int(attribute.texcoords.size());
for (int vt = 0; vt < numTexCoords / 2; vt++)
{
fprintf(f, "vt %f %f\n", shape.mesh.texcoords[vt * 2 + 0], shape.mesh.texcoords[vt * 2 + 1]);
fprintf(f, "vt %f %f\n", attribute.texcoords[vt * 2 + 0], attribute.texcoords[vt * 2 + 1]);
}

fprintf(f, "s off\n");
Expand All @@ -375,9 +376,9 @@ int main(int argc, char* argv[])
continue;
}
fprintf(f, "f %d/%d/%d %d/%d/%d %d/%d/%d\n",
shape.mesh.indices[face] + 1, shape.mesh.indices[face] + 1, shape.mesh.indices[face] + 1,
shape.mesh.indices[face + 1] + 1, shape.mesh.indices[face + 1] + 1, shape.mesh.indices[face + 1] + 1,
shape.mesh.indices[face + 2] + 1, shape.mesh.indices[face + 2] + 1, shape.mesh.indices[face + 2] + 1);
shape.mesh.indices[face].vertex_index + 1, shape.mesh.indices[face].vertex_index + 1, shape.mesh.indices[face].vertex_index + 1,
shape.mesh.indices[face + 1].vertex_index + 1, shape.mesh.indices[face + 1].vertex_index + 1, shape.mesh.indices[face + 1].vertex_index + 1,
shape.mesh.indices[face + 2].vertex_index + 1, shape.mesh.indices[face + 2].vertex_index + 1, shape.mesh.indices[face + 2].vertex_index + 1);
}
fclose(f);

Expand Down Expand Up @@ -437,4 +438,4 @@ int main(int argc, char* argv[])
fclose(sdfFile);

return 0;
}
}
26 changes: 14 additions & 12 deletions examples/Importers/ImportMJCFDemo/BulletMJCFImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2265,7 +2265,7 @@ int BulletMJCFImporter::getBodyUniqueId() const
return m_data->m_activeBodyUniqueId;
}

static btCollisionShape* MjcfCreateConvexHullFromShapes(std::vector<tinyobj::shape_t>& shapes, const btVector3& geomScale, btScalar collisionMargin)
static btCollisionShape* MjcfCreateConvexHullFromShapes(const tinyobj::attrib_t& attribute, std::vector<tinyobj::shape_t>& shapes, const btVector3& geomScale, btScalar collisionMargin)
{
btCompoundShape* compound = new btCompoundShape();
compound->setMargin(collisionMargin);
Expand All @@ -2278,25 +2278,26 @@ static btCollisionShape* MjcfCreateConvexHullFromShapes(std::vector<tinyobj::sha
btConvexHullShape* convexHull = new btConvexHullShape();
convexHull->setMargin(collisionMargin);
tinyobj::shape_t& shape = shapes[s];

int faceCount = shape.mesh.indices.size();

for (int f = 0; f < faceCount; f += 3)
{
btVector3 pt;
pt.setValue(shape.mesh.positions[shape.mesh.indices[f] * 3 + 0],
shape.mesh.positions[shape.mesh.indices[f] * 3 + 1],
shape.mesh.positions[shape.mesh.indices[f] * 3 + 2]);
pt.setValue(attribute.vertices[3 * shape.mesh.indices[f].vertex_index + 0],
attribute.vertices[3 * shape.mesh.indices[f].vertex_index + 1],
attribute.vertices[3 * shape.mesh.indices[f].vertex_index + 2]);

convexHull->addPoint(pt * geomScale, false);

pt.setValue(shape.mesh.positions[shape.mesh.indices[f + 1] * 3 + 0],
shape.mesh.positions[shape.mesh.indices[f + 1] * 3 + 1],
shape.mesh.positions[shape.mesh.indices[f + 1] * 3 + 2]);
pt.setValue(attribute.vertices[3 * shape.mesh.indices[f + 1].vertex_index + 0],
attribute.vertices[3 * shape.mesh.indices[f + 1].vertex_index + 1],
attribute.vertices[3 * shape.mesh.indices[f + 1].vertex_index + 2]);
convexHull->addPoint(pt * geomScale, false);

pt.setValue(shape.mesh.positions[shape.mesh.indices[f + 2] * 3 + 0],
shape.mesh.positions[shape.mesh.indices[f + 2] * 3 + 1],
shape.mesh.positions[shape.mesh.indices[f + 2] * 3 + 2]);
pt.setValue(attribute.vertices[3 * shape.mesh.indices[f + 2].vertex_index + 0],
attribute.vertices[3 * shape.mesh.indices[f + 2].vertex_index + 1],
attribute.vertices[3 * shape.mesh.indices[f + 2].vertex_index + 2]);
convexHull->addPoint(pt * geomScale, false);
}

Expand Down Expand Up @@ -2391,10 +2392,11 @@ class btCompoundShape* BulletMJCFImporter::convertLinkCollisionShapes(int linkIn
else
{
std::vector<tinyobj::shape_t> shapes;
std::string err = tinyobj::LoadObj(shapes, col->m_geometry.m_meshFileName.c_str(),"",m_data->m_fileIO);
tinyobj::attrib_t attribute;
std::string err = tinyobj::LoadObj(attribute, shapes, col->m_geometry.m_meshFileName.c_str(), "", m_data->m_fileIO);
//create a convex hull for each shape, and store it in a btCompoundShape

childShape = MjcfCreateConvexHullFromShapes(shapes, col->m_geometry.m_meshScale, m_data->m_globalDefaults.m_defaultCollisionMargin);
childShape = MjcfCreateConvexHullFromShapes(attribute, shapes, col->m_geometry.m_meshScale, m_data->m_globalDefaults.m_defaultCollisionMargin);
}
break;
}
Expand Down
7 changes: 4 additions & 3 deletions examples/Importers/ImportMeshUtility/b3ImportMeshUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ bool b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(const std::string&
btVector3 shift(0, 0, 0);

std::vector<tinyobj::shape_t> shapes;
tinyobj::attrib_t attribute;
{
B3_PROFILE("tinyobj::LoadObj");
std::string err = LoadFromCachedOrFromObj(shapes, relativeFileName, pathPrefix,fileIO);
std::string err = LoadFromCachedOrFromObj(attribute, shapes, relativeFileName, pathPrefix, fileIO);
//std::string err = tinyobj::LoadObj(shapes, relativeFileName, pathPrefix);
}

GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(shapes);
GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(attribute, shapes);
{
B3_PROFILE("Load Texture");
//int textureIndex = -1;
Expand All @@ -84,7 +85,7 @@ bool b3ImportMeshUtility::loadAndRegisterMeshFromFileInternal(const std::string&
meshData.m_rgbaColor[2] = shape.material.diffuse[2];
meshData.m_rgbaColor[3] = shape.material.transparency;
meshData.m_flags |= B3_IMPORT_MESH_HAS_RGBA_COLOR;

meshData.m_specularColor[0] = shape.material.specular[0];
meshData.m_specularColor[1] = shape.material.specular[1];
meshData.m_specularColor[2] = shape.material.specular[2];
Expand Down
14 changes: 9 additions & 5 deletions examples/Importers/ImportObjDemo/LoadMeshFromObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct CachedObjResult
{
std::string m_msg;
std::vector<tinyobj::shape_t> m_shapes;
tinyobj::attrib_t m_attribute;
};

static b3HashMap<b3HashString, CachedObjResult> gCachedObjResults;
Expand All @@ -31,24 +32,26 @@ void b3EnableFileCaching(int enable)
}

std::string LoadFromCachedOrFromObj(
tinyobj::attrib_t& attribute,
std::vector<tinyobj::shape_t>& shapes, // [output]
const char* filename,
const char* mtl_basepath,
struct CommonFileIOInterface* fileIO
)
struct CommonFileIOInterface* fileIO)
{
CachedObjResult* resultPtr = gCachedObjResults[filename];
if (resultPtr)
{
const CachedObjResult& result = *resultPtr;
shapes = result.m_shapes;
attribute = result.m_attribute;
return result.m_msg;
}

std::string err = tinyobj::LoadObj(shapes, filename, mtl_basepath,fileIO);
std::string err = tinyobj::LoadObj(attribute, shapes, filename, mtl_basepath, fileIO);
CachedObjResult result;
result.m_msg = err;
result.m_shapes = shapes;
result.m_attribute = attribute;
if (gEnableFileCaching)
{
gCachedObjResults.insert(filename, result);
Expand All @@ -60,14 +63,15 @@ GLInstanceGraphicsShape* LoadMeshFromObj(const char* relativeFileName, const cha
{
B3_PROFILE("LoadMeshFromObj");
std::vector<tinyobj::shape_t> shapes;
tinyobj::attrib_t attribute;
{
B3_PROFILE("tinyobj::LoadObj2");
std::string err = LoadFromCachedOrFromObj(shapes, relativeFileName, materialPrefixPath,fileIO);
std::string err = LoadFromCachedOrFromObj(attribute, shapes, relativeFileName, materialPrefixPath, fileIO);
}

{
B3_PROFILE("btgCreateGraphicsShapeFromWavefrontObj");
GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(shapes);
GLInstanceGraphicsShape* gfxShape = btgCreateGraphicsShapeFromWavefrontObj(attribute, shapes);
return gfxShape;
}
}
2 changes: 1 addition & 1 deletion examples/Importers/ImportObjDemo/LoadMeshFromObj.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ struct GLInstanceGraphicsShape;
int b3IsFileCachingEnabled();
void b3EnableFileCaching(int enable);


std::string LoadFromCachedOrFromObj(
tinyobj::attrib_t& attribute,
std::vector<tinyobj::shape_t>& shapes, // [output]
const char* filename,
const char* mtl_basepath,
Expand Down
Loading

0 comments on commit 10108cd

Please sign in to comment.