Guest User

ObjExport

a guest
May 6th, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.76 KB | None | 0 0
  1.  
  2.     void MainFrame::DoObjExportScene( Castor::Path const & p_pathFile )const
  3.     {
  4.         StringStream l_mtl, l_obj;
  5.         Version l_version;
  6.         StringStream l_streamVersion;
  7.         l_streamVersion << l_version.m_iMajor << cuT( "." ) << l_version.m_iMinor << cuT( "." ) << l_version.m_iBuild;
  8.         // First we export the materials
  9.         Path l_pathFileMtl( p_pathFile );
  10.         str_utils::replace( l_pathFileMtl, l_pathFileMtl.GetExtension(), cuT( "mtl" ) );
  11.         MaterialManager const & l_mtlManager = m_pCastor3D->GetMaterialManager();
  12.         l_mtl << cuT( "#######################################################\n" );
  13.         l_mtl << cuT( "#      MTL File generated by CastorViewer v" ) + l_streamVersion.str() + cuT( "      #\n" );
  14.         l_mtl << cuT( "#######################################################\n\n" );
  15.         l_mtlManager.lock();
  16.         std::for_each( l_mtlManager.begin(), l_mtlManager.end(), [&]( std::pair< String, MaterialSPtr > p_pair )
  17.         {
  18.             l_mtl << DoObjExport( l_pathFileMtl.GetPath(), p_pair.second ) + cuT( "\n" );
  19.         } );
  20.         l_mtlManager.unlock();
  21.         TextFile l_fileMtl( l_pathFileMtl, File::eOPEN_MODE_WRITE, File::eENCODING_MODE_ASCII );
  22.         l_fileMtl.WriteText( l_mtl.str() );
  23.         // Then we export meshes
  24.         Path l_pathFileObj( p_pathFile );
  25.         str_utils::replace( l_pathFileObj, l_pathFileObj.GetExtension(), cuT( "obj" ) );
  26.         MeshCollection const & l_mshManager = m_pCastor3D->GetMeshManager();
  27.         uint32_t l_uiOffset = 1;
  28.         uint32_t l_uiCount = 0;
  29.         l_obj << cuT( "#######################################################\n" );
  30.         l_obj << cuT( "#      OBJ File generated by CastorViewer v" ) + l_streamVersion.str() + cuT( "      #\n" );
  31.         l_obj << cuT( "#######################################################\n\n" );
  32.         l_obj << cuT( "mtllib " ) + l_pathFileMtl.GetFullFileName() + cuT( "\n\n" );
  33.         l_mshManager.lock();
  34.  
  35.         for ( MeshCollection::TObjPtrMapConstIt l_it = l_mshManager.begin(); l_it != l_mshManager.end(); ++l_it )
  36.         {
  37.             l_obj << DoObjExport( l_it->second, l_uiOffset, l_uiCount ) << cuT( "\n" );
  38.         }
  39.  
  40.         l_mshManager.unlock();
  41.         TextFile l_fileObj( l_pathFileObj, File::eOPEN_MODE_WRITE, File::eENCODING_MODE_ASCII );
  42.         l_fileObj.WriteText( l_obj.str() );
  43.     }
  44.  
  45.  
  46.     Path GetTextureNewPath( Path const & p_pathSrcFile, Path const & p_pathFolder )
  47.     {
  48.         Path l_pathReturn( cuT( "Texture" ) );
  49.         l_pathReturn /= p_pathSrcFile.GetFullFileName();
  50.  
  51.         if ( !wxDirExists( ( p_pathFolder / cuT( "Texture" ) ).c_str() ) )
  52.         {
  53.             wxMkDir( str_utils::to_str( p_pathFolder / cuT( "Texture" ) ).c_str(), 0777 );
  54.         }
  55.  
  56.         if ( wxFileExists( p_pathSrcFile ) )
  57.         {
  58.             Logger::LogDebug( cuT( "Copying [" ) + p_pathSrcFile + cuT( "] to [" ) + p_pathFolder / l_pathReturn + cuT( "]" ) );
  59.             wxCopyFile( p_pathSrcFile, p_pathFolder / l_pathReturn );
  60.         }
  61.  
  62.         return l_pathReturn;
  63.     };
  64.  
  65.     String MainFrame::DoObjExport( Path const & p_pathMtlFolder, MaterialSPtr p_pMaterial )const
  66.     {
  67.         StringStream l_strReturn;
  68.         PassSPtr l_pPass = p_pMaterial->GetPass( 0 );
  69.  
  70.         if ( l_pPass )
  71.         {
  72.             Colour l_clAmbient = l_pPass->GetAmbient();
  73.             Colour l_clDiffuse = l_pPass->GetDiffuse();
  74.             Colour l_clSpecular = l_pPass->GetSpecular();
  75.             TextureUnitSPtr l_pAmbient  = l_pPass->GetTextureUnit( eTEXTURE_CHANNEL_AMBIENT );
  76.             TextureUnitSPtr l_pDiffuse  = l_pPass->GetTextureUnit( eTEXTURE_CHANNEL_DIFFUSE );
  77.             TextureUnitSPtr l_pNormal   = l_pPass->GetTextureUnit( eTEXTURE_CHANNEL_NORMAL  );
  78.             TextureUnitSPtr l_pOpacity  = l_pPass->GetTextureUnit( eTEXTURE_CHANNEL_OPACITY );
  79.             TextureUnitSPtr l_pSpecular = l_pPass->GetTextureUnit( eTEXTURE_CHANNEL_SPECULAR    );
  80.             TextureUnitSPtr l_pGloss    = l_pPass->GetTextureUnit( eTEXTURE_CHANNEL_GLOSS   );
  81.             l_strReturn << cuT( "newmtl "   ) << p_pMaterial->GetName() << cuT( "\n" );
  82.             l_strReturn << cuT( "   Ka "    ) << l_clAmbient.red().value() << cuT( " " ) << l_clAmbient.green().value() << cuT( " " ) << l_clAmbient.blue().value() << cuT( "\n" );
  83.             l_strReturn << cuT( "   Kd "    ) << l_clDiffuse.red().value() << cuT( " " ) << l_clDiffuse.green().value() << cuT( " " ) << l_clDiffuse.blue().value() << cuT( "\n" );
  84.             l_strReturn << cuT( "   Ks "    ) << l_clSpecular.red().value() << cuT( " " ) << l_clSpecular.green().value() << cuT( " " ) << l_clSpecular.blue().value() << cuT( "\n" );
  85.             l_strReturn << cuT( "   Ns "    ) << l_pPass->GetShininess() << cuT( "\n" );
  86.             l_strReturn << cuT( "   d " ) << l_pPass->GetAlpha() << cuT( "\n" );
  87.  
  88.             if ( l_pAmbient && !l_pAmbient->GetTexturePath().empty() )
  89.             {
  90.                 l_strReturn << cuT( "   map_Ka " ) + GetTextureNewPath( l_pAmbient->GetTexturePath(), p_pathMtlFolder ) << cuT( "\n" );
  91.             }
  92.  
  93.             if ( l_pDiffuse && !l_pDiffuse->GetTexturePath().empty() )
  94.             {
  95.                 l_strReturn << cuT( "   map_Kd " ) + GetTextureNewPath( l_pDiffuse->GetTexturePath(), p_pathMtlFolder ) << cuT( "\n" );
  96.             }
  97.  
  98.             if ( l_pNormal && !l_pNormal->GetTexturePath().empty() )
  99.             {
  100.                 l_strReturn << cuT( "   map_Bump " ) + GetTextureNewPath( l_pNormal->GetTexturePath(), p_pathMtlFolder ) << cuT( "\n" );
  101.             }
  102.  
  103.             if ( l_pOpacity && !l_pOpacity->GetTexturePath().empty() )
  104.             {
  105.                 l_strReturn << cuT( "   map_d " ) + GetTextureNewPath( l_pOpacity->GetTexturePath(), p_pathMtlFolder ) << cuT( "\n" );
  106.             }
  107.  
  108.             if ( l_pSpecular && !l_pSpecular->GetTexturePath().empty() )
  109.             {
  110.                 l_strReturn << cuT( "   map_Ks " ) + GetTextureNewPath( l_pSpecular->GetTexturePath(), p_pathMtlFolder ) << cuT( "\n" );
  111.             }
  112.  
  113.             if ( l_pGloss && !l_pGloss->GetTexturePath().empty() )
  114.             {
  115.                 l_strReturn << cuT( "   map_Ns " ) + GetTextureNewPath( l_pGloss->GetTexturePath(), p_pathMtlFolder ) << cuT( "\n" );
  116.             }
  117.         }
  118.  
  119.         return l_strReturn.str();
  120.     }
  121.  
  122.     String MainFrame::DoObjExport( MeshSPtr p_pMesh, uint32_t & p_uiOffset, uint32_t & p_uiCount )const
  123.     {
  124.         StringStream l_strReturn;
  125.         std::for_each( p_pMesh->Begin(), p_pMesh->End(), [&]( SubmeshSPtr p_pSubmesh )
  126.         {
  127.             StringStream l_strV;
  128.             StringStream l_strVT;
  129.             StringStream l_strVN;
  130.             StringStream l_strF;
  131.             SubmeshRendererSPtr l_pRenderer = p_pSubmesh->GetRenderer();
  132.             VertexBuffer & l_vtxBuffer = l_pRenderer->GetGeometryBuffers()->GetVertexBuffer();
  133.             IndexBuffer & l_idxBuffer = l_pRenderer->GetGeometryBuffers()->GetIndexBuffer();
  134.             uint32_t l_uiStride = l_vtxBuffer.GetDeclaration().GetStride();
  135.             uint32_t l_uiNbPoints = l_vtxBuffer.GetSize() / l_uiStride;
  136.             uint32_t l_uiNbFaces = l_idxBuffer.GetSize() / 3;
  137.             uint8_t * l_pVtx = l_vtxBuffer.data();
  138.             uint32_t * l_pIdx = l_idxBuffer.data();
  139.             Point3r l_ptPos;
  140.             Point3r l_ptNml;
  141.             Point3r l_ptTex;
  142.  
  143.             for ( uint32_t j = 0; j < l_uiNbPoints; j++ )
  144.             {
  145.                 real * l_pVertex = reinterpret_cast< real * >( &l_pVtx[j * l_uiStride] );
  146.                 Vertex::GetPosition(    l_pVertex, l_ptPos );
  147.                 Vertex::GetNormal(  l_pVertex, l_ptNml );
  148.                 Vertex::GetTexCoord(    l_pVertex, l_ptTex );
  149.                 l_strV  << cuT( "v " ) << l_ptPos[0] << " " << l_ptPos[1] << " " << l_ptPos[2] << cuT( "\n" );
  150.                 l_strVN << cuT( "vn " ) << l_ptNml[0] << " " << l_ptNml[1] << " " << l_ptNml[2] << cuT( "\n" );
  151.                 l_strVT << cuT( "vt " ) << l_ptTex[0] << " " << l_ptTex[1] << cuT( "\n" );
  152.             }
  153.  
  154.             l_strF << cuT( "usemtl " ) << p_pSubmesh->GetDefaultMaterial()->GetName() << cuT( "\ns off\n" );
  155.  
  156.             for ( uint32_t j = 0; j < l_uiNbFaces; j++ )
  157.             {
  158.                 uint32_t * l_pFace = &l_pIdx[j * 3];
  159.                 uint32_t l_v0 = p_uiOffset + l_pFace[0];
  160.                 uint32_t l_v1 = p_uiOffset + l_pFace[1];
  161.                 uint32_t l_v2 = p_uiOffset + l_pFace[2];
  162.                 l_strF << cuT( "f " ) << l_v0 << cuT( "/" ) << l_v0 << cuT( "/" ) << l_v0 << cuT( " " ) << l_v1 << cuT( "/" ) << l_v1 << cuT( "/" ) << l_v1 << cuT( " " ) << l_v2 << cuT( "/" ) << l_v2 << cuT( "/" ) << l_v2 << cuT( "\n" );
  163.             }
  164.  
  165.             l_strReturn << cuT( "g mesh" ) << p_uiCount << cuT( "\n" ) << l_strV.str() << cuT( "\n" ) << l_strVN.str() << cuT( "\n" ) << l_strVT.str() << cuT( "\n" ) << l_strF.str() << cuT( "\n" );
  166.             p_uiOffset += l_uiNbPoints;
  167.             p_uiCount++;
  168.         } );
  169.         return l_strReturn.str();
  170.     }
Advertisement
Add Comment
Please, Sign In to add comment