1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-09 13:22:24 +03:00

0030953: Data Exchange - implement export of mesh data into glTF 2.0 format

Added new class RWGltf_CafWriter for exporting XCAF document into glTF file
as well as Draw Harness command WriteGltf.

Added auxiliary method OSD_Path::FileNameAndExtension() splitting file name into Name and Extension.
This commit is contained in:
kgv
2019-11-19 02:09:09 +03:00
committed by bugmaster
parent df8c7e3e18
commit 01b2f506d6
26 changed files with 3620 additions and 12 deletions

View File

@@ -15,9 +15,16 @@ RWGltf_MaterialCommon.hxx
RWGltf_MaterialMetallicRoughness.hxx
RWGltf_CafReader.cxx
RWGltf_CafReader.hxx
RWGltf_CafWriter.cxx
RWGltf_CafWriter.hxx
RWGltf_GltfMaterialMap.cxx
RWGltf_GltfMaterialMap.hxx
RWGltf_GltfJsonParser.cxx
RWGltf_GltfJsonParser.pxx
RWGltf_GltfOStreamWriter.hxx
RWGltf_GltfSceneNodeMap.hxx
RWGltf_PrimitiveArrayReader.cxx
RWGltf_PrimitiveArrayReader.hxx
RWGltf_TriangulationReader.cxx
RWGltf_TriangulationReader.hxx
RWGltf_WriterTrsfFormat.hxx

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,294 @@
// Copyright (c) 2017-2019 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _RWGltf_CafWriter_HeaderFiler
#define _RWGltf_CafWriter_HeaderFiler
#include <TColStd_IndexedDataMapOfStringString.hxx>
#include <TColStd_MapOfAsciiString.hxx>
#include <TDF_LabelSequence.hxx>
#include <TopTools_ShapeMapHasher.hxx>
#include <RWGltf_GltfBufferView.hxx>
#include <RWGltf_GltfFace.hxx>
#include <RWGltf_WriterTrsfFormat.hxx>
#include <RWMesh_CoordinateSystemConverter.hxx>
#include <XCAFPrs_Style.hxx>
#include <memory>
class Message_ProgressIndicator;
class RWMesh_FaceIterator;
class RWGltf_GltfOStreamWriter;
class RWGltf_GltfMaterialMap;
class RWGltf_GltfSceneNodeMap;
class TDocStd_Document;
//! glTF writer context from XCAF document.
class RWGltf_CafWriter : public Standard_Transient
{
DEFINE_STANDARD_RTTIEXT(RWGltf_CafWriter, Standard_Transient)
public:
//! Main constructor.
//! @param theFile [in] path to output glTF file
//! @param theIsBinary [in] flag to write into binary glTF format (.glb)
Standard_EXPORT RWGltf_CafWriter (const TCollection_AsciiString& theFile,
Standard_Boolean theIsBinary);
//! Destructor.
Standard_EXPORT virtual ~RWGltf_CafWriter();
//! Return transformation from OCCT to glTF coordinate system.
const RWMesh_CoordinateSystemConverter& CoordinateSystemConverter() const { return myCSTrsf; }
//! Return transformation from OCCT to glTF coordinate system.
RWMesh_CoordinateSystemConverter& ChangeCoordinateSystemConverter() { return myCSTrsf; }
//! Set transformation from OCCT to glTF coordinate system.
void SetCoordinateSystemConverter (const RWMesh_CoordinateSystemConverter& theConverter) { myCSTrsf = theConverter; }
//! Return flag to write into binary glTF format (.glb), specified within class constructor.
bool IsBinary() const { return myIsBinary; }
//! Return preferred transformation format for writing into glTF file; RWGltf_WriterTrsfFormat_Compact by default.
RWGltf_WriterTrsfFormat TransformationFormat() const { return myTrsfFormat; }
//! Set preferred transformation format for writing into glTF file.
void SetTransformationFormat (RWGltf_WriterTrsfFormat theFormat) { myTrsfFormat = theFormat; }
//! Return TRUE to export UV coordinates even if there are no mapped texture; FALSE by default.
bool IsForcedUVExport() const { return myIsForcedUVExport; }
//! Set flag to export UV coordinates even if there are no mapped texture; FALSE by default.
void SetForcedUVExport (bool theToForce) { myIsForcedUVExport = theToForce; }
//! Return default material definition to be used for nodes with only color defined.
const XCAFPrs_Style& DefaultStyle() const { return myDefaultStyle; }
//! Set default material definition to be used for nodes with only color defined.
void SetDefaultStyle (const XCAFPrs_Style& theStyle) { myDefaultStyle = theStyle; }
//! Write glTF file and associated binary file.
//! Triangulation data should be precomputed within shapes!
//! @param theDocument [in] input document
//! @param theRootLabels [in] list of root shapes to export
//! @param theLabelFilter [in] optional filter with document nodes to export,
//! with keys defined by XCAFPrs_DocumentExplorer::DefineChildId() and filled recursively
//! (leaves and parent assembly nodes at all levels);
//! when not NULL, all nodes not included into the map will be ignored
//! @param theFileInfo [in] map with file metadata to put into glTF header section
//! @param theProgress [in] optional progress indicator
//! @return FALSE on file writing failure
Standard_EXPORT virtual bool Perform (const Handle(TDocStd_Document)& theDocument,
const TDF_LabelSequence& theRootLabels,
const TColStd_MapOfAsciiString* theLabelFilter,
const TColStd_IndexedDataMapOfStringString& theFileInfo,
const Handle(Message_ProgressIndicator)& theProgress);
//! Write glTF file and associated binary file.
//! Triangulation data should be precomputed within shapes!
//! @param theDocument [in] input document
//! @param theFileInfo [in] map with file metadata to put into glTF header section
//! @param theProgress [in] optional progress indicator
//! @return FALSE on file writing failure
Standard_EXPORT virtual bool Perform (const Handle(TDocStd_Document)& theDocument,
const TColStd_IndexedDataMapOfStringString& theFileInfo,
const Handle(Message_ProgressIndicator)& theProgress);
protected:
//! Write binary data file with triangulation data.
//! Triangulation data should be precomputed within shapes!
//! @param theDocument [in] input document
//! @param theRootLabels [in] list of root shapes to export
//! @param theLabelFilter [in] optional filter with document nodes to export
//! @param theProgress [in] optional progress indicator
//! @return FALSE on file writing failure
Standard_EXPORT virtual bool writeBinData (const Handle(TDocStd_Document)& theDocument,
const TDF_LabelSequence& theRootLabels,
const TColStd_MapOfAsciiString* theLabelFilter,
const Handle(Message_ProgressIndicator)& theProgress);
//! Write JSON file with glTF structure (should be called after writeBinData()).
//! @param theDocument [in] input document
//! @param theRootLabels [in] list of root shapes to export
//! @param theLabelFilter [in] optional filter with document nodes to export
//! @param theFileInfo [in] map with file metadata to put into glTF header section
//! @param theProgress [in] optional progress indicator
//! @return FALSE on file writing failure
Standard_EXPORT virtual bool writeJson (const Handle(TDocStd_Document)& theDocument,
const TDF_LabelSequence& theRootLabels,
const TColStd_MapOfAsciiString* theLabelFilter,
const TColStd_IndexedDataMapOfStringString& theFileInfo,
const Handle(Message_ProgressIndicator)& theProgress);
protected:
//! Return TRUE if face mesh should be skipped (e.g. because it is invalid or empty).
Standard_EXPORT virtual Standard_Boolean toSkipFaceMesh (const RWMesh_FaceIterator& theFaceIter);
//! Write mesh nodes into binary file.
//! @param theGltfFace [out] glTF face definition
//! @param theBinFile [out] output file to write into
//! @param theFaceIter [in] current face to write
//! @param theAccessorNb [in] [out] last accessor index
Standard_EXPORT virtual void saveNodes (RWGltf_GltfFace& theGltfFace,
std::ostream& theBinFile,
const RWMesh_FaceIterator& theFaceIter,
Standard_Integer& theAccessorNb) const;
//! Write mesh normals into binary file.
//! @param theGltfFace [out] glTF face definition
//! @param theBinFile [out] output file to write into
//! @param theFaceIter [in] current face to write
//! @param theAccessorNb [in] [out] last accessor index
Standard_EXPORT virtual void saveNormals (RWGltf_GltfFace& theGltfFace,
std::ostream& theBinFile,
RWMesh_FaceIterator& theFaceIter,
Standard_Integer& theAccessorNb) const;
//! Write mesh texture UV coordinates into binary file.
//! @param theGltfFace [out] glTF face definition
//! @param theBinFile [out] output file to write into
//! @param theFaceIter [in] current face to write
//! @param theAccessorNb [in] [out] last accessor index
Standard_EXPORT virtual void saveTextCoords (RWGltf_GltfFace& theGltfFace,
std::ostream& theBinFile,
const RWMesh_FaceIterator& theFaceIter,
Standard_Integer& theAccessorNb) const;
//! Write mesh indexes into binary file.
//! @param theGltfFace [out] glTF face definition
//! @param theBinFile [out] output file to write into
//! @param theFaceIter [in] current face to write
//! @param theAccessorNb [in] [out] last accessor index
Standard_EXPORT virtual void saveIndices (RWGltf_GltfFace& theGltfFace,
std::ostream& theBinFile,
const RWMesh_FaceIterator& theFaceIter,
Standard_Integer& theAccessorNb);
protected:
//! Write bufferView for vertex positions within RWGltf_GltfRootElement_Accessors section
//! @param theGltfFace [in] face definition to write
Standard_EXPORT virtual void writePositions (const RWGltf_GltfFace& theGltfFace);
//! Write bufferView for vertex normals within RWGltf_GltfRootElement_Accessors section
//! @param theGltfFace [in] face definition to write
Standard_EXPORT virtual void writeNormals (const RWGltf_GltfFace& theGltfFace);
//! Write bufferView for vertex texture coordinates within RWGltf_GltfRootElement_Accessors section
//! @param theGltfFace [in] face definition to write
Standard_EXPORT virtual void writeTextCoords (const RWGltf_GltfFace& theGltfFace);
//! Write bufferView for triangle indexes within RWGltf_GltfRootElement_Accessors section.
//! @param theGltfFace [in] face definition to write
Standard_EXPORT virtual void writeIndices (const RWGltf_GltfFace& theGltfFace);
protected:
//! Write RWGltf_GltfRootElement_Accessors section.
//! @param theSceneNodeMap [in] ordered map of scene nodes
Standard_EXPORT virtual void writeAccessors (const RWGltf_GltfSceneNodeMap& theSceneNodeMap);
//! Write RWGltf_GltfRootElement_Animations section (reserved).
Standard_EXPORT virtual void writeAnimations();
//! Write RWGltf_GltfRootElement_Asset section.
//! @param theFileInfo [in] optional metadata to write into file header
Standard_EXPORT virtual void writeAsset (const TColStd_IndexedDataMapOfStringString& theFileInfo);
//! Write RWGltf_GltfRootElement_BufferViews section.
//! @param theBinDataBufferId [in] index of binary buffer with vertex data
Standard_EXPORT virtual void writeBufferViews (const Standard_Integer theBinDataBufferId);
//! Write RWGltf_GltfRootElement_Buffers section.
Standard_EXPORT virtual void writeBuffers();
//! Write RWGltf_GltfRootElement_ExtensionsUsed/RWGltf_GltfRootElement_ExtensionsRequired sections (reserved).
Standard_EXPORT virtual void writeExtensions();
//! Write RWGltf_GltfRootElement_Images section.
//! @param theSceneNodeMap [in] ordered map of scene nodes
//! @param theMaterialMap [out] map of materials, filled with image files used by textures
Standard_EXPORT virtual void writeImages (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
RWGltf_GltfMaterialMap& theMaterialMap);
//! Write RWGltf_GltfRootElement_Materials section.
//! @param theSceneNodeMap [in] ordered map of scene nodes
//! @param theMaterialMap [out] map of materials, filled with materials
Standard_EXPORT virtual void writeMaterials (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
RWGltf_GltfMaterialMap& theMaterialMap);
//! Write RWGltf_GltfRootElement_Meshes section.
//! @param theSceneNodeMap [in] ordered map of scene nodes
//! @param theMaterialMap [in] map of materials
Standard_EXPORT virtual void writeMeshes (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
const RWGltf_GltfMaterialMap& theMaterialMap);
//! Write RWGltf_GltfRootElement_Nodes section.
//! @param theDocument [in] input document
//! @param theRootLabels [in] list of root shapes to export
//! @param theLabelFilter [in] optional filter with document nodes to export
//! @param theSceneNodeMap [in] ordered map of scene nodes
//! @param theSceneRootNodeInds [out] sequence of scene nodes pointing to root shapes (to be used for writeScenes())
Standard_EXPORT virtual void writeNodes (const Handle(TDocStd_Document)& theDocument,
const TDF_LabelSequence& theRootLabels,
const TColStd_MapOfAsciiString* theLabelFilter,
const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
NCollection_Sequence<Standard_Integer>& theSceneRootNodeInds);
//! Write RWGltf_GltfRootElement_Samplers section.
Standard_EXPORT virtual void writeSamplers (const RWGltf_GltfMaterialMap& theMaterialMap);
//! Write RWGltf_GltfRootElement_Scene section.
//! @param theDefSceneId [in] index of default scene (0)
Standard_EXPORT virtual void writeScene (const Standard_Integer theDefSceneId);
//! Write RWGltf_GltfRootElement_Scenes section.
//! @param theSceneRootNodeInds [in] sequence of scene nodes pointing to root shapes
Standard_EXPORT virtual void writeScenes (const NCollection_Sequence<Standard_Integer>& theSceneRootNodeInds);
//! Write RWGltf_GltfRootElement_Skins section (reserved).
Standard_EXPORT virtual void writeSkins();
//! Write RWGltf_GltfRootElement_Textures section.
//! @param theSceneNodeMap [in] ordered map of scene nodes
//! @param theMaterialMap [out] map of materials, filled with textures
Standard_EXPORT virtual void writeTextures (const RWGltf_GltfSceneNodeMap& theSceneNodeMap,
RWGltf_GltfMaterialMap& theMaterialMap);
protected:
TCollection_AsciiString myFile; //!< output glTF file
TCollection_AsciiString myBinFileNameFull; //!< output file with binary data (full path)
TCollection_AsciiString myBinFileNameShort; //!< output file with binary data (short path)
RWGltf_WriterTrsfFormat myTrsfFormat; //!< transformation format to write into glTF file
Standard_Boolean myIsBinary; //!< flag to write into binary glTF format (.glb)
Standard_Boolean myIsForcedUVExport; //!< export UV coordinates even if there are no mapped texture
RWMesh_CoordinateSystemConverter myCSTrsf; //!< transformation from OCCT to glTF coordinate system
XCAFPrs_Style myDefaultStyle; //!< default material definition to be used for nodes with only color defined
opencascade::std::shared_ptr<RWGltf_GltfOStreamWriter>
myWriter; //!< JSON writer
RWGltf_GltfBufferView myBuffViewPos; //!< current buffer view with nodes positions
RWGltf_GltfBufferView myBuffViewNorm; //!< current buffer view with nodes normals
RWGltf_GltfBufferView myBuffViewTextCoord; //!< current buffer view with nodes UV coordinates
RWGltf_GltfBufferView myBuffViewInd; //!< current buffer view with triangulation indexes
NCollection_DataMap<TopoDS_Shape, RWGltf_GltfFace,
TopTools_ShapeMapHasher> myBinDataMap; //!< map for TopoDS_Face to glTF face (merging duplicates)
int64_t myBinDataLen64; //!< length of binary file
};
#endif // _RWGltf_CafWriter_HeaderFiler

View File

@@ -1780,7 +1780,7 @@ bool RWGltf_GltfJsonParser::Parse (const Handle(Message_ProgressIndicator)& theP
}
return true;
#else
Message::DefaultMessenger()->Send ("Error: glTF reader is unavailable - OCCT has been built without RapidJSON support.", Message_Fail);
Message::DefaultMessenger()->Send ("Error: glTF reader is unavailable - OCCT has been built without RapidJSON support [HAVE_RAPIDJSON undefined].", Message_Fail);
return false;
#endif
}

View File

@@ -0,0 +1,461 @@
// Copyright (c) 2017-2019 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <RWGltf_GltfMaterialMap.hxx>
#include <RWGltf_GltfRootElement.hxx>
#ifdef HAVE_RAPIDJSON
#include <RWGltf_GltfOStreamWriter.hxx>
#endif
// =======================================================================
// function : baseColorTexture
// purpose :
// =======================================================================
const Handle(Image_Texture)& RWGltf_GltfMaterialMap::baseColorTexture (const Handle(XCAFDoc_VisMaterial)& theMat)
{
static const Handle(Image_Texture) THE_NULL_TEXTURE;
if (theMat.IsNull())
{
return THE_NULL_TEXTURE;
}
else if (theMat->HasPbrMaterial()
&& !theMat->PbrMaterial().BaseColorTexture.IsNull())
{
return theMat->PbrMaterial().BaseColorTexture;
}
else if (theMat->HasCommonMaterial()
&& !theMat->CommonMaterial().DiffuseTexture.IsNull())
{
return theMat->CommonMaterial().DiffuseTexture;
}
return THE_NULL_TEXTURE;
}
// =======================================================================
// function : RWGltf_GltfMaterialMap
// purpose :
// =======================================================================
RWGltf_GltfMaterialMap::RWGltf_GltfMaterialMap (const TCollection_AsciiString& theFile,
const Standard_Integer theDefSamplerId)
: RWMesh_MaterialMap (theFile),
myWriter (NULL),
myDefSamplerId (theDefSamplerId),
myNbImages (0)
{
myMatNameAsKey = false;
}
// =======================================================================
// function : ~RWGltf_GltfMaterialMap
// purpose :
// =======================================================================
RWGltf_GltfMaterialMap::~RWGltf_GltfMaterialMap()
{
//
}
// =======================================================================
// function : AddImages
// purpose :
// =======================================================================
void RWGltf_GltfMaterialMap::AddImages (RWGltf_GltfOStreamWriter* theWriter,
const XCAFPrs_Style& theStyle,
Standard_Boolean& theIsStarted)
{
if (theWriter == NULL
|| theStyle.Material().IsNull()
|| theStyle.Material()->IsEmpty())
{
return;
}
addImage (theWriter, baseColorTexture (theStyle.Material()), theIsStarted);
addImage (theWriter, theStyle.Material()->PbrMaterial().MetallicRoughnessTexture, theIsStarted);
addImage (theWriter, theStyle.Material()->PbrMaterial().NormalTexture, theIsStarted);
addImage (theWriter, theStyle.Material()->PbrMaterial().EmissiveTexture, theIsStarted);
addImage (theWriter, theStyle.Material()->PbrMaterial().OcclusionTexture, theIsStarted);
}
// =======================================================================
// function : addImage
// purpose :
// =======================================================================
void RWGltf_GltfMaterialMap::addImage (RWGltf_GltfOStreamWriter* theWriter,
const Handle(Image_Texture)& theTexture,
Standard_Boolean& theIsStarted)
{
#ifdef HAVE_RAPIDJSON
if (theTexture.IsNull()
|| myImageMap.IsBound1 (theTexture)
|| myImageFailMap.Contains (theTexture))
{
return;
}
TCollection_AsciiString aGltfImgKey = myNbImages;
++myNbImages;
for (; myImageMap.IsBound2 (aGltfImgKey); ++myNbImages)
{
aGltfImgKey = myNbImages;
}
TCollection_AsciiString aTextureUri;
if (!CopyTexture (aTextureUri, theTexture, aGltfImgKey))
{
myImageFailMap.Add (theTexture);
return;
}
myImageMap.Bind (theTexture, aGltfImgKey);
if (!theIsStarted)
{
theWriter->Key (RWGltf_GltfRootElementName (RWGltf_GltfRootElement_Images));
theWriter->StartArray();
theIsStarted = true;
}
theWriter->StartObject();
{
theWriter->Key ("uri");
theWriter->String (aTextureUri.ToCString());
}
theWriter->EndObject();
#else
(void )theWriter;
(void )theTexture;
(void )theIsStarted;
#endif
}
// =======================================================================
// function : AddMaterial
// purpose :
// =======================================================================
void RWGltf_GltfMaterialMap::AddMaterial (RWGltf_GltfOStreamWriter* theWriter,
const XCAFPrs_Style& theStyle,
Standard_Boolean& theIsStarted)
{
#ifdef HAVE_RAPIDJSON
if (theWriter == NULL
|| ((theStyle.Material().IsNull() || theStyle.Material()->IsEmpty())
&& !theStyle.IsSetColorSurf()))
{
return;
}
if (!theIsStarted)
{
theWriter->Key (RWGltf_GltfRootElementName (RWGltf_GltfRootElement_Materials));
theWriter->StartArray();
theIsStarted = true;
}
myWriter = theWriter;
AddMaterial (theStyle);
myWriter = NULL;
#else
(void )theWriter;
(void )theStyle;
(void )theIsStarted;
#endif
}
// =======================================================================
// function : AddTextures
// purpose :
// =======================================================================
void RWGltf_GltfMaterialMap::AddTextures (RWGltf_GltfOStreamWriter* theWriter,
const XCAFPrs_Style& theStyle,
Standard_Boolean& theIsStarted)
{
if (theWriter == NULL
|| theStyle.Material().IsNull()
|| theStyle.Material()->IsEmpty())
{
return;
}
addTexture (theWriter, baseColorTexture (theStyle.Material()), theIsStarted);
addTexture (theWriter, theStyle.Material()->PbrMaterial().MetallicRoughnessTexture, theIsStarted);
addTexture (theWriter, theStyle.Material()->PbrMaterial().NormalTexture, theIsStarted);
addTexture (theWriter, theStyle.Material()->PbrMaterial().EmissiveTexture, theIsStarted);
addTexture (theWriter, theStyle.Material()->PbrMaterial().OcclusionTexture, theIsStarted);
}
// =======================================================================
// function : addTexture
// purpose :
// =======================================================================
void RWGltf_GltfMaterialMap::addTexture (RWGltf_GltfOStreamWriter* theWriter,
const Handle(Image_Texture)& theTexture,
Standard_Boolean& theIsStarted)
{
#ifdef HAVE_RAPIDJSON
if (theTexture.IsNull()
|| myTextureMap.Contains (theTexture)
|| !myImageMap .IsBound1 (theTexture))
{
return;
}
const TCollection_AsciiString anImgKey = myImageMap.Find1 (theTexture);
myTextureMap.Add (theTexture);
if (anImgKey.IsEmpty())
{
return;
}
if (!theIsStarted)
{
theWriter->Key (RWGltf_GltfRootElementName (RWGltf_GltfRootElement_Textures));
theWriter->StartArray();
theIsStarted = true;
}
theWriter->StartObject();
{
theWriter->Key ("sampler");
theWriter->Int (myDefSamplerId); // mandatory field by specs
theWriter->Key ("source");
theWriter->Int (anImgKey.IntegerValue());
}
theWriter->EndObject();
#else
(void )theWriter;
(void )theTexture;
(void )theIsStarted;
#endif
}
// =======================================================================
// function : AddMaterial
// purpose :
// =======================================================================
TCollection_AsciiString RWGltf_GltfMaterialMap::AddMaterial (const XCAFPrs_Style& theStyle)
{
return RWMesh_MaterialMap::AddMaterial (theStyle);
}
// =======================================================================
// function : DefineMaterial
// purpose :
// =======================================================================
void RWGltf_GltfMaterialMap::DefineMaterial (const XCAFPrs_Style& theStyle,
const TCollection_AsciiString& /*theKey*/,
const TCollection_AsciiString& theName)
{
#ifdef HAVE_RAPIDJSON
if (myWriter == NULL)
{
Standard_ProgramError::Raise ("RWGltf_GltfMaterialMap::DefineMaterial() should be called with JSON Writer");
return;
}
XCAFDoc_VisMaterialPBR aPbrMat;
const bool hasMaterial = !theStyle.Material().IsNull()
&& !theStyle.Material()->IsEmpty();
if (hasMaterial)
{
aPbrMat = theStyle.Material()->ConvertToPbrMaterial();
}
else if (!myDefaultStyle.Material().IsNull()
&& myDefaultStyle.Material()->HasPbrMaterial())
{
aPbrMat = myDefaultStyle.Material()->PbrMaterial();
}
if (theStyle.IsSetColorSurf())
{
aPbrMat.BaseColor.SetRGB (theStyle.GetColorSurf());
if (theStyle.GetColorSurfRGBA().Alpha() < 1.0f)
{
aPbrMat.BaseColor.SetAlpha (theStyle.GetColorSurfRGBA().Alpha());
}
}
myWriter->StartObject();
{
myWriter->Key ("name");
myWriter->String (theName.ToCString());
myWriter->Key ("pbrMetallicRoughness");
myWriter->StartObject();
{
myWriter->Key ("baseColorFactor");
myWriter->StartArray();
{
myWriter->Double (aPbrMat.BaseColor.GetRGB().Red());
myWriter->Double (aPbrMat.BaseColor.GetRGB().Green());
myWriter->Double (aPbrMat.BaseColor.GetRGB().Blue());
myWriter->Double (aPbrMat.BaseColor.Alpha());
}
myWriter->EndArray();
if (const Handle(Image_Texture)& aBaseTexture = baseColorTexture (theStyle.Material()))
{
if (myImageMap.IsBound1 (aBaseTexture))
{
myWriter->Key ("baseColorTexture");
myWriter->StartObject();
{
myWriter->Key ("index");
const TCollection_AsciiString& anImageIdx = myImageMap.Find1 (aBaseTexture);
if (!anImageIdx.IsEmpty())
{
myWriter->Int (anImageIdx.IntegerValue());
}
}
myWriter->EndObject();
}
}
if (hasMaterial
|| aPbrMat.Metallic != 1.0f)
{
myWriter->Key ("metallicFactor");
myWriter->Double (aPbrMat.Metallic);
}
if (!aPbrMat.MetallicRoughnessTexture.IsNull()
&& myImageMap.IsBound1 (aPbrMat.MetallicRoughnessTexture))
{
myWriter->Key ("metallicRoughnessTexture");
myWriter->StartObject();
{
myWriter->Key ("index");
const TCollection_AsciiString& anImageIdx = myImageMap.Find1 (aPbrMat.MetallicRoughnessTexture);
if (!anImageIdx.IsEmpty())
{
myWriter->Int (anImageIdx.IntegerValue());
}
}
myWriter->EndObject();
}
if (hasMaterial
|| aPbrMat.Roughness != 1.0f)
{
myWriter->Key ("roughnessFactor");
myWriter->Double (aPbrMat.Roughness);
}
}
myWriter->EndObject();
if (theStyle.Material().IsNull()
|| theStyle.Material()->IsDoubleSided())
{
myWriter->Key ("doubleSided");
myWriter->Bool (true);
}
const Graphic3d_AlphaMode anAlphaMode = !theStyle.Material().IsNull() ? theStyle.Material()->AlphaMode() : Graphic3d_AlphaMode_BlendAuto;
switch (anAlphaMode)
{
case Graphic3d_AlphaMode_BlendAuto:
{
if (aPbrMat.BaseColor.Alpha() < 1.0f)
{
myWriter->Key ("alphaMode");
myWriter->String ("BLEND");
}
break;
}
case Graphic3d_AlphaMode_Opaque:
{
break;
}
case Graphic3d_AlphaMode_Mask:
{
myWriter->Key ("alphaMode");
myWriter->String ("MASK");
break;
}
case Graphic3d_AlphaMode_Blend:
{
myWriter->Key ("alphaMode");
myWriter->String ("BLEND");
break;
}
}
if (!theStyle.Material().IsNull()
&& theStyle.Material()->AlphaCutOff() != 0.5f)
{
myWriter->Key ("alphaCutoff");
myWriter->Double (theStyle.Material()->AlphaCutOff());
}
if (aPbrMat.EmissiveFactor != Graphic3d_Vec3 (0.0f, 0.0f, 0.0f))
{
myWriter->Key ("emissiveFactor");
myWriter->StartArray();
{
myWriter->Double (aPbrMat.EmissiveFactor.r());
myWriter->Double (aPbrMat.EmissiveFactor.g());
myWriter->Double (aPbrMat.EmissiveFactor.b());
}
myWriter->EndArray();
}
if (!aPbrMat.EmissiveTexture.IsNull()
&& myImageMap.IsBound1 (aPbrMat.EmissiveTexture))
{
myWriter->Key ("emissiveTexture");
myWriter->StartObject();
{
myWriter->Key ("index");
const TCollection_AsciiString& anImageIdx = myImageMap.Find1 (aPbrMat.EmissiveTexture);
if (!anImageIdx.IsEmpty())
{
myWriter->Int (anImageIdx.IntegerValue());
}
}
myWriter->EndObject();
}
if (!aPbrMat.NormalTexture.IsNull()
&& myImageMap.IsBound1 (aPbrMat.NormalTexture))
{
myWriter->Key ("normalTexture");
myWriter->StartObject();
{
myWriter->Key ("index");
const TCollection_AsciiString& anImageIdx = myImageMap.Find1 (aPbrMat.NormalTexture);
if (!anImageIdx.IsEmpty())
{
myWriter->Int (anImageIdx.IntegerValue());
}
}
myWriter->EndObject();
}
if (!aPbrMat.OcclusionTexture.IsNull()
&& myImageMap.IsBound1 (aPbrMat.OcclusionTexture))
{
myWriter->Key ("occlusionTexture");
myWriter->StartObject();
{
myWriter->Key ("index");
const TCollection_AsciiString& anImageIdx = myImageMap.Find1 (aPbrMat.OcclusionTexture);
if (!anImageIdx.IsEmpty())
{
myWriter->Int (anImageIdx.IntegerValue());
}
}
myWriter->EndObject();
}
}
myWriter->EndObject();
#else
(void )theStyle;
(void )theName;
#endif
}

View File

@@ -0,0 +1,88 @@
// Copyright (c) 2017-2019 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _RWGltf_GltfMaterialMap_HeaderFile
#define _RWGltf_GltfMaterialMap_HeaderFile
#include <RWMesh_MaterialMap.hxx>
class RWGltf_GltfOStreamWriter;
//! Material manager for exporting into glTF format.
class RWGltf_GltfMaterialMap : public RWMesh_MaterialMap
{
public:
//! Main constructor.
Standard_EXPORT RWGltf_GltfMaterialMap (const TCollection_AsciiString& theFile,
const Standard_Integer theDefSamplerId);
//! Destructor.
Standard_EXPORT virtual ~RWGltf_GltfMaterialMap();
//! Add material images.
Standard_EXPORT void AddImages (RWGltf_GltfOStreamWriter* theWriter,
const XCAFPrs_Style& theStyle,
Standard_Boolean& theIsStarted);
//! Add material.
Standard_EXPORT void AddMaterial (RWGltf_GltfOStreamWriter* theWriter,
const XCAFPrs_Style& theStyle,
Standard_Boolean& theIsStarted);
//! Add material textures.
Standard_EXPORT void AddTextures (RWGltf_GltfOStreamWriter* theWriter,
const XCAFPrs_Style& theStyle,
Standard_Boolean& theIsStarted);
//! Return extent of images map.
Standard_Integer NbImages() const { return myImageMap.Extent(); }
//! Return extent of textures map.
Standard_Integer NbTextures() const { return myTextureMap.Extent(); }
public:
//! Return base color texture.
Standard_EXPORT static const Handle(Image_Texture)& baseColorTexture (const Handle(XCAFDoc_VisMaterial)& theMat);
protected:
//! Add texture image.
Standard_EXPORT void addImage (RWGltf_GltfOStreamWriter* theWriter,
const Handle(Image_Texture)& theTexture,
Standard_Boolean& theIsStarted);
//! Add texture.
Standard_EXPORT void addTexture (RWGltf_GltfOStreamWriter* theWriter,
const Handle(Image_Texture)& theTexture,
Standard_Boolean& theIsStarted);
//! Add material
Standard_EXPORT virtual TCollection_AsciiString AddMaterial (const XCAFPrs_Style& theStyle) Standard_OVERRIDE;
//! Virtual method actually defining the material (e.g. export to the file).
Standard_EXPORT virtual void DefineMaterial (const XCAFPrs_Style& theStyle,
const TCollection_AsciiString& theKey,
const TCollection_AsciiString& theName) Standard_OVERRIDE;
protected:
RWGltf_GltfOStreamWriter* myWriter;
NCollection_DoubleMap<Handle(Image_Texture), TCollection_AsciiString, Image_Texture, TCollection_AsciiString> myImageMap;
NCollection_Map<Handle(Image_Texture), Image_Texture> myTextureMap;
Standard_Integer myDefSamplerId;
Standard_Integer myNbImages;
};
#endif // _RWGltf_GltfMaterialMap_HeaderFile

View File

@@ -0,0 +1,29 @@
// Copyright (c) 2019 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _RWGltf_GltfOStreamWriter_HeaderFile
#define _RWGltf_GltfOStreamWriter_HeaderFile
#include <rapidjson/prettywriter.h>
#include <rapidjson/ostreamwrapper.h>
//! rapidjson::Writer wrapper for forward declaration.
class RWGltf_GltfOStreamWriter : public rapidjson::Writer<rapidjson::OStreamWrapper>
{
public:
//! Main constructor.
RWGltf_GltfOStreamWriter (rapidjson::OStreamWrapper& theOStream)
: rapidjson::Writer<rapidjson::OStreamWrapper> (theOStream) {}
};
#endif // _RWGltf_GltfOStreamWriter_HeaderFile

View File

@@ -0,0 +1,48 @@
// Copyright (c) 2018-2019 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _RWGltf_GltfSceneNodeMap_HeaderFile
#define _RWGltf_GltfSceneNodeMap_HeaderFile
#include <NCollection_IndexedMap.hxx>
#include <XCAFPrs_DocumentExplorer.hxx>
//! Indexed map of scene nodes with custom search algorithm.
class RWGltf_GltfSceneNodeMap : public NCollection_IndexedMap<XCAFPrs_DocumentNode, XCAFPrs_DocumentNode>
{
public:
//! Empty constructor.
RWGltf_GltfSceneNodeMap() {}
//! Find index from document node string identifier.
Standard_Integer FindIndex (const TCollection_AsciiString& theNodeId) const
{
if (IsEmpty())
{
return 0;
}
for (IndexedMapNode* aNode1Iter = (IndexedMapNode* )myData1[::HashCode (theNodeId, NbBuckets())]; aNode1Iter != NULL; aNode1Iter = (IndexedMapNode* )aNode1Iter->Next())
{
if (::IsEqual (aNode1Iter->Key1().Id, theNodeId))
{
return aNode1Iter->Index();
}
}
return 0;
}
};
#endif // _RWGltf_GltfSceneNodeMap_HeaderFile

View File

@@ -0,0 +1,26 @@
// Copyright (c) 2017-2019 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _RWGltf_WriterTrsfFormat_HeaderFile
#define _RWGltf_WriterTrsfFormat_HeaderFile
//! Transformation format.
enum RWGltf_WriterTrsfFormat
{
RWGltf_WriterTrsfFormat_Compact = 0, //!< automatically choose most compact representation between Mat4 and TRS
RWGltf_WriterTrsfFormat_Mat4 = 1, //!< 4x4 transformation Matrix
RWGltf_WriterTrsfFormat_TRS = 2, //!< transformation decomposed into Translation vector, Rotation quaternion and Scale factor (T * R * S)
};
enum { RWGltf_WriterTrsfFormat_LOWER = 0, RWGltf_WriterTrsfFormat_UPPER = RWGltf_WriterTrsfFormat_TRS }; // aliases
#endif // _RWGltf_WriterTrsfFormat_HeaderFile