@ -1191,14 +1191,15 @@ namespace BasicPLY {
} // namespace MeshInternal
} // namespace MeshInternal
// import the mesh from the given file
// import the mesh from the given file
bool Mesh : : Load ( const String & fileName , bool bLoadUV )
bool Mesh : : Load ( const String & fileName )
{
{
TD_TIMER_STARTD ( ) ;
TD_TIMER_STARTD ( ) ;
const String ext ( Util : : getFileExt ( fileName ) . ToLower ( ) ) ;
const String ext ( Util : : getFileExt ( fileName ) . ToLower ( ) ) ;
bool ret ;
bool ret ;
if ( ext = = _T ( " .obj " ) )
if ( ext = = _T ( " .obj " ) )
ret = LoadOBJ ( fileName , bLoadUV ) ;
ret = LoadOBJ ( fileName ) ;
else if ( ext = = _T ( " .gltf " ) | | ext = = _T ( " .glb " ) )
else
if ( ext = = _T ( " .gltf " ) | | ext = = _T ( " .glb " ) )
ret = LoadGLTF ( fileName , ext = = _T ( " .glb " ) ) ;
ret = LoadGLTF ( fileName , ext = = _T ( " .glb " ) ) ;
else
else
ret = LoadPLY ( fileName ) ;
ret = LoadPLY ( fileName ) ;
@ -1207,43 +1208,6 @@ bool Mesh::Load(const String& fileName, bool bLoadUV)
DEBUG_EXTRA ( " Mesh loaded: %u vertices, %u faces (%s) " , vertices . size ( ) , faces . size ( ) , TD_TIMER_GET_FMT ( ) . c_str ( ) ) ;
DEBUG_EXTRA ( " Mesh loaded: %u vertices, %u faces (%s) " , vertices . size ( ) , faces . size ( ) , TD_TIMER_GET_FMT ( ) . c_str ( ) ) ;
return true ;
return true ;
}
}
void Mesh : : CheckUVValid ( )
{
for ( int_t idxFace = 0 ; idxFace < ( int_t ) faces . size ( ) ; + + idxFace )
{
FOREACH ( idxFace , faces )
{
const FIndex faceID = ( FIndex ) idxFace ;
const TexCoord * uv = & faceTexcoords [ faceID * 3 ] ;
const Point2f & a = uv [ 0 ] ;
const Point2f & b = uv [ 1 ] ;
const Point2f & c = uv [ 2 ] ;
// DEBUG_EXTRA("a=(%f,%f),b=(%f,%f),c=(%f,%f)", a.x, a.y, b.x, b.y, c.x, c.y);
// 计算边向量
Point2f v0 = b - a ;
Point2f v1 = c - a ;
float denom = ( v0 . x * v0 . x + v0 . y * v0 . y ) * ( v1 . x * v1 . x + v1 . y * v1 . y ) -
std : : pow ( v0 . x * v1 . x + v0 . y * v1 . y , 2 ) ;
// 处理退化三角形情况(面积接近0)
const float epsilon = 1e-10 f ;
if ( std : : abs ( denom ) < epsilon )
{
// DEBUG_EXTRA("PointInTriangle - Degenerate triangle, denom=%.10f", denom);
}
else
{
DEBUG_EXTRA ( " PointInTriangle Yes idxFace=%d " , idxFace ) ;
}
}
}
}
// import the mesh as a PLY file
// import the mesh as a PLY file
bool Mesh : : LoadPLY ( const String & fileName )
bool Mesh : : LoadPLY ( const String & fileName )
{
{
@ -1338,29 +1302,17 @@ bool Mesh::LoadPLY(const String& fileName)
return true ;
return true ;
}
}
// import the mesh as a OBJ file
// import the mesh as a OBJ file
bool Mesh : : LoadOBJ ( const String & fileName , bool bLoadUV )
bool Mesh : : LoadOBJ ( const String & fileName )
{
{
ASSERT ( ! fileName . empty ( ) ) ;
ASSERT ( ! fileName . empty ( ) ) ;
Release ( ) ;
Release ( ) ;
// open and parse OBJ file
// open and parse OBJ file
ObjModel model ;
ObjModel model ;
if ( bLoadUV )
if ( ! model . Load ( fileName ) ) {
{
DEBUG_EXTRA ( " error: invalid OBJ file " ) ;
if ( ! model . LoadUV ( fileName , faceTexcoords , true ) ) {
DEBUG_EXTRA ( " error: LoadUV invalid OBJ file %s " , fileName . c_str ( ) ) ;
return false ;
return false ;
}
}
}
else
{
if ( ! model . LoadUV ( fileName , faceTexcoords ) ) {
// if (!model.Load(fileName)) {
DEBUG_EXTRA ( " error: Load invalid OBJ file %s " , fileName . c_str ( ) ) ;
return false ;
}
}
if ( model . get_vertices ( ) . empty ( ) | | model . get_groups ( ) . empty ( ) ) {
if ( model . get_vertices ( ) . empty ( ) | | model . get_groups ( ) . empty ( ) ) {
DEBUG_EXTRA ( " error: invalid mesh file " ) ;
DEBUG_EXTRA ( " error: invalid mesh file " ) ;
@ -1388,13 +1340,11 @@ bool Mesh::LoadOBJ(const String& fileName, bool bLoadUV)
for ( const ObjModel : : Face & f : group . faces ) {
for ( const ObjModel : : Face & f : group . faces ) {
ASSERT ( f . vertices [ 0 ] ! = NO_ID ) ;
ASSERT ( f . vertices [ 0 ] ! = NO_ID ) ;
faces . emplace_back ( f . vertices [ 0 ] , f . vertices [ 1 ] , f . vertices [ 2 ] ) ;
faces . emplace_back ( f . vertices [ 0 ] , f . vertices [ 1 ] , f . vertices [ 2 ] ) ;
/*
if ( f . texcoords [ 0 ] ! = NO_ID ) {
if ( f . texcoords [ 0 ] ! = NO_ID ) {
for ( int i = 0 ; i < 3 ; + + i )
for ( int i = 0 ; i < 3 ; + + i )
faceTexcoords . emplace_back ( model . get_texcoords ( ) [ f . texcoords [ i ] ] ) ;
faceTexcoords . emplace_back ( model . get_texcoords ( ) [ f . texcoords [ i ] ] ) ;
faceTexindices . emplace_back ( ( TexIndex ) groupIdx ) ;
faceTexindices . emplace_back ( ( TexIndex ) groupIdx ) ;
}
}
*/
if ( f . normals [ 0 ] ! = NO_ID ) {
if ( f . normals [ 0 ] ! = NO_ID ) {
Normal & n = faceNormals . emplace_back ( Normal : : ZERO ) ;
Normal & n = faceNormals . emplace_back ( Normal : : ZERO ) ;
for ( int i = 0 ; i < 3 ; + + i )
for ( int i = 0 ; i < 3 ; + + i )
@ -1495,13 +1445,13 @@ bool Mesh::LoadGLTF(const String& fileName, bool bBinary)
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
// export the mesh to the given file
// export the mesh to the given file
bool Mesh : : Save ( const String & fileName , const cList < String > & comments , bool bBinary , bool bUseExistingUV ) const
bool Mesh : : Save ( const String & fileName , const cList < String > & comments , bool bBinary ) const
{
{
TD_TIMER_STARTD ( ) ;
TD_TIMER_STARTD ( ) ;
const String ext ( Util : : getFileExt ( fileName ) . ToLower ( ) ) ;
const String ext ( Util : : getFileExt ( fileName ) . ToLower ( ) ) ;
bool ret ;
bool ret ;
if ( ext = = _T ( " .obj " ) )
if ( ext = = _T ( " .obj " ) )
ret = SaveOBJ ( fileName , bUseExistingUV ) ;
ret = SaveOBJ ( fileName ) ;
else
else
if ( ext = = _T ( " .gltf " ) | | ext = = _T ( " .glb " ) )
if ( ext = = _T ( " .gltf " ) | | ext = = _T ( " .glb " ) )
ret = SaveGLTF ( fileName , ext = = _T ( " .glb " ) ) ;
ret = SaveGLTF ( fileName , ext = = _T ( " .glb " ) ) ;
@ -1588,7 +1538,7 @@ bool Mesh::SavePLY(const String& fileName, const cList<String>& comments, bool b
return true ;
return true ;
}
}
// export the mesh as a OBJ file
// export the mesh as a OBJ file
bool Mesh : : SaveOBJ ( const String & fileName , bool bUseExistingUV ) const
bool Mesh : : SaveOBJ ( const String & fileName ) const
{
{
ASSERT ( ! fileName . empty ( ) ) ;
ASSERT ( ! fileName . empty ( ) ) ;
Util : : ensureFolder ( fileName ) ;
Util : : ensureFolder ( fileName ) ;
@ -1647,7 +1597,7 @@ bool Mesh::SaveOBJ(const String& fileName, bool bUseExistingUV) const
pMaterial - > diffuse_map = texturesDiffuse [ idxTexture ] ;
pMaterial - > diffuse_map = texturesDiffuse [ idxTexture ] ;
}
}
return model . Save ( fileName , 6U , true , bUseExistingUV ) ;
return model . Save ( fileName , 6U , true ) ;
}
}
// export the mesh as a GLTF file
// export the mesh as a GLTF file
template < typename T >
template < typename T >