0029528: Visualization, TKOpenGl - allow defining sRGB textures
[occt.git] / src / RWGltf / RWGltf_GltfLatePrimitiveArray.cxx
1 // Author: Kirill Gavrilov
2 // Copyright (c) 2018-2019 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <RWGltf_GltfLatePrimitiveArray.hxx>
16
17 #include <RWGltf_MaterialMetallicRoughness.hxx>
18 #include <RWGltf_MaterialCommon.hxx>
19
20 #include <Message.hxx>
21 #include <Message_Messenger.hxx>
22 #include <OSD_OpenFile.hxx>
23 #include <Standard_ArrayStreamBuffer.hxx>
24
25 #include <fstream>
26
27 IMPLEMENT_STANDARD_RTTIEXT(RWGltf_GltfLatePrimitiveArray, Poly_Triangulation)
28
29 // =======================================================================
30 // function : RWGltf_GltfLatePrimitiveArray
31 // purpose  :
32 // =======================================================================
33 RWGltf_GltfLatePrimitiveArray::RWGltf_GltfLatePrimitiveArray (const TCollection_AsciiString& theId,
34                                                               const TCollection_AsciiString& theName)
35 : Poly_Triangulation (3, 1, false),
36   myId (theId),
37   myName (theName),
38   myPrimMode (RWGltf_GltfPrimitiveMode_UNKNOWN)
39 {
40   SetBoundingBox (Bnd_Box());
41 }
42
43 // =======================================================================
44 // function : ~RWGltf_GltfLatePrimitiveArray
45 // purpose  :
46 // =======================================================================
47 RWGltf_GltfLatePrimitiveArray::~RWGltf_GltfLatePrimitiveArray()
48 {
49   //
50 }
51
52 // =======================================================================
53 // function : BaseColor
54 // purpose  :
55 // =======================================================================
56 Quantity_ColorRGBA RWGltf_GltfLatePrimitiveArray::BaseColor() const
57 {
58   if (!myMaterialPbr.IsNull())
59   {
60     return myMaterialPbr->BaseColor;
61   }
62   else if (!myMaterialCommon.IsNull())
63   {
64     return Quantity_ColorRGBA (myMaterialCommon->DiffuseColor, 1.0f - myMaterialCommon->Transparency);
65   }
66   return Quantity_ColorRGBA();
67 }
68
69 // =======================================================================
70 // function : AddPrimArrayData
71 // purpose  :
72 // =======================================================================
73 RWGltf_GltfPrimArrayData& RWGltf_GltfLatePrimitiveArray::AddPrimArrayData (RWGltf_GltfArrayType theType)
74 {
75   if (theType == RWGltf_GltfArrayType_Position)
76   {
77     // make sure positions go first
78     myData.Prepend (RWGltf_GltfPrimArrayData (theType));
79     return myData.ChangeFirst();
80   }
81   else if (theType == RWGltf_GltfArrayType_Indices)
82   {
83     // make sure indexes go after vertex positions but before any other vertex attributes
84     if (myData.First().Type == RWGltf_GltfArrayType_Position)
85     {
86       myData.InsertAfter (myData.Lower(), RWGltf_GltfPrimArrayData (theType));
87       return myData.ChangeValue (myData.Lower() + 1);
88     }
89     else
90     {
91       myData.Prepend (RWGltf_GltfPrimArrayData (theType));
92       return myData.ChangeFirst();
93     }
94   }
95   else
96   {
97     myData.Append (RWGltf_GltfPrimArrayData (theType));
98     return myData.ChangeLast();
99   }
100 }
101
102 // =======================================================================
103 // function : SetBoundingBox
104 // purpose  :
105 // =======================================================================
106 void RWGltf_GltfLatePrimitiveArray::SetBoundingBox (const Bnd_Box& theBox)
107 {
108   myBox = theBox;
109
110   if (theBox.IsVoid())
111   {
112     Poly_Triangulation::myNodes = TColgp_Array1OfPnt();
113     Poly_Triangulation::myTriangles = Poly_Array1OfTriangle();
114     return;
115   }
116
117   // define 8 nodes so that AABB will be huge enough to include mesh even with transformation applied
118   Poly_Triangulation::myNodes.Resize (1, 8, false);
119   const gp_Pnt aMin = theBox.CornerMin();
120   const gp_Pnt aMax = theBox.CornerMax();
121   Poly_Triangulation::ChangeNode(1).SetCoord(aMin.X(), aMin.Y(), aMin.Z());
122   Poly_Triangulation::ChangeNode(2).SetCoord(aMax.X(), aMax.Y(), aMax.Z());
123   Poly_Triangulation::ChangeNode(3).SetCoord(aMin.X(), aMin.Y(), aMax.Z());
124   Poly_Triangulation::ChangeNode(4).SetCoord(aMin.X(), aMax.Y(), aMax.Z());
125   Poly_Triangulation::ChangeNode(5).SetCoord(aMax.X(), aMax.Y(), aMin.Z());
126   Poly_Triangulation::ChangeNode(6).SetCoord(aMax.X(), aMin.Y(), aMin.Z());
127   Poly_Triangulation::ChangeNode(7).SetCoord(aMin.X(), aMax.Y(), aMin.Z());
128   Poly_Triangulation::ChangeNode(8).SetCoord(aMax.X(), aMin.Y(), aMax.Z());
129
130   Poly_Triangulation::myTriangles.Resize (1, 1, false);
131   Poly_Triangulation::ChangeTriangle (1).Set (1, 2, 1);
132   //Poly_Triangulation::myTriangles = Poly_Array1OfTriangle();
133 }