Skip to content

Commit

Permalink
Treating separate OBJ objects as separate meshes to help build multip…
Browse files Browse the repository at this point in the history
…le convex hulls from a single OBJ file
  • Loading branch information
adnanmunawar committed Mar 24, 2021
1 parent ff01f31 commit c70e217
Showing 2 changed files with 74 additions and 6 deletions.
67 changes: 63 additions & 4 deletions external/chai3d/src/files/CFileModelOBJ.cpp
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
\author <http://www.chai3d.org>
\author Tim Schroeder
\author Francois Conti
\author Adnan Munawar
\version 3.2.0 $Rev: 2187 $
*/
//==============================================================================
@@ -93,6 +94,33 @@ bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename)
// get information about file
int numMaterials = fileObj.m_OBJInfo.m_materialCount;

if (fileObj.m_OBJInfo.m_objectCount > numMaterials){
cMaterialInfo* matInfoCopy = new cMaterialInfo[fileObj.m_OBJInfo.m_objectCount];
std::copy(fileObj.m_pMaterials, fileObj.m_pMaterials + fileObj.m_OBJInfo.m_materialCount, matInfoCopy);

delete [] fileObj.m_pMaterials;

fileObj.m_pMaterials = new cMaterialInfo[fileObj.m_OBJInfo.m_objectCount];

for (int i = 0 ; i < numMaterials ; i++){
fileObj.m_pMaterials[i] = matInfoCopy[i];
}
// now just copy the last material to the rest of the materials
for (int i = numMaterials ; i < fileObj.m_OBJInfo.m_objectCount ; i++){
fileObj.m_pMaterials[i] = fileObj.m_pMaterials[numMaterials - 1];
}

// for (int i = 0 ; i < fileObj.m_OBJInfo.m_faceCount ; i++){
// if (fileObj.m_pFaces[i].m_materialIndex == -1){
// fileObj.m_pFaces[i].m_materialIndex = fileObj.m_pFaces[i].m_objectIndex;
// }
// }

numMaterials = fileObj.m_OBJInfo.m_objectCount;

delete[] matInfoCopy;
}

// extract materials
vector<cMaterial> materials;

@@ -237,7 +265,7 @@ bool cLoadFileOBJ(cMultiMesh* a_object, const std::string& a_filename)
cFace face = fileObj.m_pFaces[j];

// get material index attributed to the face
int objIndex = face.m_materialIndex;
int objIndex = face.m_objectIndex;

// the mesh that we're reading this triangle into
cMesh* curMesh = a_object->getMesh(objIndex);
@@ -696,6 +724,10 @@ cOBJModel::~cOBJModel()
{
delete [] m_groupNames[i];
}
for(unsigned int i=0; i<m_objectNames.size(); i++)
{
delete [] m_objectNames[i];
}
}

//------------------------------------------------------------------------------
@@ -710,6 +742,7 @@ bool cOBJModel::LoadModel(const char a_fileName[])
char str[C_OBJ_MAX_STR_SIZE] = ""; // buffer string for reading the file
char basePath[C_OBJ_SIZE_PATH]; // path were all paths in the OBJ start
unsigned int curMaterial = 0; // current material
unsigned int curObject = 0; // current material

// get base path
strcpy(basePath, a_fileName);
@@ -848,14 +881,14 @@ bool cOBJModel::LoadModel(const char a_fileName[])

// convert string into a face structure
parseFaceString(str, &m_pFaces[currentIndex.m_faceCount],
m_pVertices, m_pNormals, m_pTexCoords, curMaterial);
m_pVertices, m_pNormals, m_pTexCoords, curMaterial, curObject);

// next face
currentIndex.m_faceCount++;
}

// rest of the line contains face information
else if (!strncmp(str, C_OBJ_NAME_ID, sizeof(C_OBJ_NAME_ID)))
else if (!strncmp(str, C_OBJ_GROUP_ID, sizeof(C_OBJ_GROUP_ID)))
{
// read the rest of the line (the complete face)
getTokenParameter(str, sizeof(str) ,hFile);
@@ -865,6 +898,25 @@ bool cOBJModel::LoadModel(const char a_fileName[])
m_groupNames.push_back(name);
}

// check for object names
else if (!strncmp(str, C_OBJ_NAME_ID, sizeof(C_OBJ_NAME_ID)))
{
// read the rest of the line (the complete face)
getTokenParameter(str, sizeof(str) ,hFile);

char* name = new char[strlen(str)+1];
strcpy(name,str);
m_objectNames.push_back(name);

// find material array index for the material name
for (unsigned i=0; i<m_OBJInfo.m_objectCount; i++)
if (!strncmp(m_objectNames[i], str, sizeof(str)))
{
curObject = i;
break;
}
}

// process material information only if needed
if (m_pMaterials)
{
@@ -1000,7 +1052,8 @@ void cOBJModel::parseFaceString(char a_faceString[], cFace *a_faceOut,
const cVector3d *a_pVertices,
const cVector3d *a_pNormals,
const cVector3d *a_pTexCoords,
const unsigned int a_materialIndex)
const unsigned int a_materialIndex,
const int a_objectIndex)
{
/////////////////////////////////////////////////////////////////////////
// CONVERT FACE STRING FROM THE OBJ FILE INTO A FACE STRUCTURE
@@ -1146,6 +1199,8 @@ void cOBJModel::parseFaceString(char a_faceString[], cFace *a_faceOut,
// set material
a_faceOut->m_materialIndex = a_materialIndex;

a_faceOut->m_objectIndex = a_objectIndex;

// process per-vertex data
for (i=0; i<(unsigned int) a_faceOut->m_numVertices; i++)
{
@@ -1402,6 +1457,10 @@ void cOBJModel::getFileInfo(FILE *a_hStream, cOBJFileInfo *a_info, const char a_
if (!strncmp(str, C_OBJ_FACE_ID, sizeof(C_OBJ_FACE_ID)))
a_info->m_faceCount++;

// face?
if (!strncmp(str, C_OBJ_NAME_ID, sizeof(C_OBJ_NAME_ID)))
a_info->m_objectCount++;

// material library definition?
if (!strncmp(str, C_OBJ_MTL_LIB_ID, sizeof(C_OBJ_MTL_LIB_ID)))
{
13 changes: 11 additions & 2 deletions external/chai3d/src/files/CFileModelOBJ.h
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
\author <http://www.chai3d.org>
\author Tim Schroder
\author Francois Conti
\author Adnan Munawar
\version 3.2.0 $Rev: 2146 $
*/
//==============================================================================
@@ -163,7 +164,8 @@ typedef std::map<vertexIndexSet,unsigned int,ltVertexIndexSet> vertexIndexSet_ui
#define C_OBJ_COMMENT_ID "#"
#define C_OBJ_MTL_LIB_ID "mtllib"
#define C_OBJ_USE_MTL_ID "usemtl"
#define C_OBJ_NAME_ID "g"
#define C_OBJ_NAME_ID "o"
#define C_OBJ_GROUP_ID "g"

// MTL File string identifiers
#define C_OBJ_NEW_MTL_ID "newmtl"
@@ -197,6 +199,7 @@ struct cOBJFileInfo
m_texCoordCount = 0;
m_normalCount = 0;
m_faceCount = 0;
m_objectCount = 0;
m_materialCount = 0;
}

@@ -205,6 +208,7 @@ struct cOBJFileInfo
unsigned int m_texCoordCount;
unsigned int m_normalCount;
unsigned int m_faceCount;
unsigned int m_objectCount;
unsigned int m_materialCount;
};

@@ -213,6 +217,7 @@ struct cFace
{
unsigned int m_numVertices;
unsigned int m_materialIndex;
int m_objectIndex;
int m_groupIndex;
int* m_pVertexIndices;
cVector3d* m_pVertices;
@@ -232,6 +237,7 @@ struct cFace
m_numVertices = 0;
m_materialIndex = -1;
m_groupIndex = -1;
m_objectIndex = -1;
m_pVertexIndices = NULL;
m_pVertices = NULL;
m_pColors = NULL;
@@ -342,6 +348,9 @@ class cOBJModel
//! List of names obtained from 'g' commands, with the most recent at the back...
std::vector<char*> m_groupNames;

//! List of object names obtained from 'o' commands, with the most recent at the back...
std::vector<char*> m_objectNames;


//--------------------------------------------------------------------------
// METHODS:
@@ -368,7 +377,7 @@ class cOBJModel

//! Parse information about face.
void parseFaceString(char a_faceString[], cFace *a_faceOut, const cVector3d *a_pVertices,
const cVector3d *a_pNormals, const cVector3d *a_pTexCoords, const unsigned int a_materialIndex);
const cVector3d *a_pNormals, const cVector3d *a_pTexCoords, const unsigned int a_materialIndex, const int a_objectIndex);

//! Read information about file.
void getFileInfo(FILE *a_hStream, cOBJFileInfo *a_stat, const char a_constBasePath[]);

0 comments on commit c70e217

Please sign in to comment.