0032862: Visualization, Graphic3d_TextureMap - add 3D texture definition
[occt.git] / src / AIS / AIS_XRTrackedDevice.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <AIS_XRTrackedDevice.hxx>
15
16 #include <Graphic3d_ArrayOfSegments.hxx>
17 #include <Graphic3d_ArrayOfTriangles.hxx>
18 #include <Graphic3d_Group.hxx>
19 #include <Image_Texture.hxx>
20 #include <Prs3d_LineAspect.hxx>
21 #include <Prs3d_ShadingAspect.hxx>
22 #include <Select3D_SensitivePrimitiveArray.hxx>
23 #include <SelectMgr_EntityOwner.hxx>
24
25 //! Texture holder.
26 class AIS_XRTrackedDevice::XRTexture : public Graphic3d_Texture2D
27 {
28 public:
29
30   //! Constructor.
31   XRTexture (const Handle(Image_Texture)& theImageSource,
32              const Graphic3d_TextureUnit theUnit = Graphic3d_TextureUnit_BaseColor)
33   : Graphic3d_Texture2D (""), myImageSource (theImageSource)
34   {
35     if (!theImageSource->TextureId().IsEmpty())
36     {
37       myTexId = theImageSource->TextureId();
38     }
39     myParams->SetTextureUnit (theUnit);
40     myIsColorMap = theUnit == Graphic3d_TextureUnit_BaseColor
41                 || theUnit == Graphic3d_TextureUnit_Emissive;
42   }
43
44   //! Image reader.
45   virtual Handle(Image_PixMap) GetImage (const Handle(Image_SupportedFormats)& theSupported) Standard_OVERRIDE
46   {
47     return myImageSource->ReadImage (theSupported);
48   }
49
50 protected:
51   Handle(Image_Texture) myImageSource;
52 };
53
54 IMPLEMENT_STANDARD_RTTIEXT(AIS_XRTrackedDevice, AIS_InteractiveObject)
55
56 //=======================================================================
57 //function : AIS_XRTrackedDevice
58 //purpose  :
59 //=======================================================================
60 AIS_XRTrackedDevice::AIS_XRTrackedDevice (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
61                                           const Handle(Image_Texture)& theTexture)
62 : myTris (theTris),
63   myLaserColor (Quantity_NOC_BLUE),
64   myLaserLength (0.0f),
65   myUnitFactor (1.0f),
66   myRole (Aspect_XRTrackedDeviceRole_Other),
67   myToShowAxes (false)
68 {
69   myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
70   myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NameOfMaterial_DEFAULT);
71   myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
72   if (!theTexture.IsNull())
73   {
74     myDrawer->ShadingAspect()->Aspect()->SetTextureMap (new XRTexture (theTexture));
75     myDrawer->ShadingAspect()->Aspect()->SetTextureMapOn (true);
76   }
77 }
78
79 //=======================================================================
80 //function : AIS_XRTrackedDevice
81 //purpose  :
82 //=======================================================================
83 AIS_XRTrackedDevice::AIS_XRTrackedDevice()
84 : myLaserColor (Quantity_NOC_BLUE),
85   myLaserLength (0.0f),
86   myUnitFactor (1.0f),
87   myRole (Aspect_XRTrackedDeviceRole_Other),
88   myToShowAxes (true)
89 {
90   //
91 }
92
93 //=======================================================================
94 //function : SetLaserColor
95 //purpose  :
96 //=======================================================================
97 void AIS_XRTrackedDevice::SetLaserColor (const Quantity_Color& theColor)
98 {
99   if (!myLaserColor.IsEqual (theColor))
100   {
101     myLaserColor = theColor;
102     computeLaserRay();
103   }
104 }
105
106 //=======================================================================
107 //function : SetLaserLength
108 //purpose  :
109 //=======================================================================
110 void AIS_XRTrackedDevice::SetLaserLength (Standard_ShortReal theLength)
111 {
112   if (myLaserLength != theLength)
113   {
114     myLaserLength = theLength;
115     computeLaserRay();
116   }
117 }
118
119 //=======================================================================
120 //function : computeLaserRay
121 //purpose  :
122 //=======================================================================
123 void AIS_XRTrackedDevice::computeLaserRay()
124 {
125   if (myRayGroup.IsNull())
126   {
127     return;
128   }
129
130   if (!myRayGroup->IsEmpty())
131   {
132     myRayGroup->Clear();
133   }
134   if (myLaserLength <= 0.0f)
135   {
136     return;
137   }
138
139   Handle(Graphic3d_ArrayOfPrimitives) aLines = new Graphic3d_ArrayOfSegments (2, 0, Graphic3d_ArrayFlags_VertexColor);
140   aLines->AddVertex (gp_Pnt (0.0, 0.0, 0.0), myLaserColor);
141   aLines->AddVertex (gp_Pnt (0.0, 0.0, -myLaserLength), myLaserColor);
142   myRayGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
143   myRayGroup->AddPrimitiveArray (aLines, false); // do not extend camera frustum by ray
144 }
145
146 //=======================================================================
147 //function : Compute
148 //purpose  :
149 //=======================================================================
150 void AIS_XRTrackedDevice::Compute (const Handle(PrsMgr_PresentationManager)& ,
151                                    const Handle(Prs3d_Presentation)& thePrs,
152                                    const Standard_Integer theMode)
153 {
154   if (theMode != 0)
155   {
156     return;
157   }
158
159   thePrs->SetInfiniteState (myInfiniteState);
160   Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
161   if (!myTris.IsNull())
162   {
163     aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
164     aGroup->AddPrimitiveArray (myTris);
165   }
166
167   if (myToShowAxes || myTris.IsNull())
168   {
169     const float aSize = 0.1f * myUnitFactor;
170     aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
171     Handle(Graphic3d_ArrayOfPrimitives) aLines = new Graphic3d_ArrayOfSegments (6, 0, Graphic3d_ArrayFlags_VertexColor);
172     aLines->AddVertex (gp_Pnt (0.0,   0.0, 0.0),   Quantity_Color (Quantity_NOC_RED));
173     aLines->AddVertex (gp_Pnt (aSize, 0.0, 0.0),   Quantity_Color (Quantity_NOC_RED));
174     aLines->AddVertex (gp_Pnt (0.0,   0.0, 0.0),   Quantity_Color (Quantity_NOC_GREEN));
175     aLines->AddVertex (gp_Pnt (0.0, aSize, 0.0),   Quantity_Color (Quantity_NOC_GREEN));
176     aLines->AddVertex (gp_Pnt (0.0,   0.0, 0.0),   Quantity_Color (Quantity_NOC_BLUE));
177     aLines->AddVertex (gp_Pnt (0.0,   0.0, aSize), Quantity_Color (Quantity_NOC_BLUE));
178     aGroup->AddPrimitiveArray (aLines);
179   }
180
181   myRayGroup = thePrs->NewGroup();
182   computeLaserRay();
183 }
184
185 //=======================================================================
186 //function : ComputeSelection
187 //purpose  :
188 //=======================================================================
189 void AIS_XRTrackedDevice::ComputeSelection (const Handle(SelectMgr_Selection)& theSel,
190                                             const Standard_Integer theMode)
191 {
192   if (theMode != 0)
193   {
194     return;
195   }
196
197   if (!myTris.IsNull())
198   {
199     Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
200     Handle(Select3D_SensitivePrimitiveArray) aSensitive = new Select3D_SensitivePrimitiveArray (anOwner);
201     aSensitive->InitTriangulation (myTris->Attributes(), myTris->Indices(), TopLoc_Location(), true);
202     theSel->Add (aSensitive);
203   }
204 }