0029384: Visualization, TKOpenGl - basic integration with OpenVR
[occt.git] / src / AIS / AIS_XRTrackedDevice.cxx
CommitLineData
b40cdc2b 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 <Graphic3d_Texture2Dmanual.hxx>
20#include <Image_Texture.hxx>
21#include <Prs3d_LineAspect.hxx>
22#include <Prs3d_ShadingAspect.hxx>
23#include <Select3D_SensitivePrimitiveArray.hxx>
24#include <SelectMgr_EntityOwner.hxx>
25
26//! Texture holder.
27class AIS_XRTrackedDevice::XRTexture : public Graphic3d_Texture2Dmanual
28{
29public:
30
31 //! Constructor.
32 XRTexture (const Handle(Image_Texture)& theImageSource,
33 const Graphic3d_TextureUnit theUnit = Graphic3d_TextureUnit_BaseColor)
34 : Graphic3d_Texture2Dmanual (""), myImageSource (theImageSource)
35 {
36 if (!theImageSource->TextureId().IsEmpty())
37 {
38 myTexId = theImageSource->TextureId();
39 }
40 myParams->SetTextureUnit (theUnit);
41 myIsColorMap = theUnit == Graphic3d_TextureUnit_BaseColor
42 || theUnit == Graphic3d_TextureUnit_Emissive;
43 }
44
45 //! Image reader.
46 virtual Handle(Image_PixMap) GetImage() const Standard_OVERRIDE { return myImageSource->ReadImage(); }
47
48protected:
49 Handle(Image_Texture) myImageSource;
50};
51
52IMPLEMENT_STANDARD_RTTIEXT(AIS_XRTrackedDevice, AIS_InteractiveObject)
53
54//=======================================================================
55//function : AIS_XRTrackedDevice
56//purpose :
57//=======================================================================
58AIS_XRTrackedDevice::AIS_XRTrackedDevice (const Handle(Graphic3d_ArrayOfTriangles)& theTris,
59 const Handle(Image_Texture)& theTexture)
60: myTris (theTris),
61 myLaserColor (Quantity_NOC_BLUE),
62 myLaserLength (0.0f),
63 myUnitFactor (1.0f),
64 myRole (Aspect_XRTrackedDeviceRole_Other),
65 myToShowAxes (false)
66{
67 myDrawer->SetShadingAspect (new Prs3d_ShadingAspect());
68 myDrawer->ShadingAspect()->SetMaterial (Graphic3d_NOM_DEFAULT);
69 myDrawer->ShadingAspect()->SetColor (Quantity_NOC_WHITE);
70 if (!theTexture.IsNull())
71 {
72 myDrawer->ShadingAspect()->Aspect()->SetTextureMap (new XRTexture (theTexture));
73 myDrawer->ShadingAspect()->Aspect()->SetTextureMapOn (true);
74 }
75}
76
77//=======================================================================
78//function : AIS_XRTrackedDevice
79//purpose :
80//=======================================================================
81AIS_XRTrackedDevice::AIS_XRTrackedDevice()
82: myLaserColor (Quantity_NOC_BLUE),
83 myLaserLength (0.0f),
84 myUnitFactor (1.0f),
85 myRole (Aspect_XRTrackedDeviceRole_Other),
86 myToShowAxes (true)
87{
88 //
89}
90
91//=======================================================================
92//function : SetLaserColor
93//purpose :
94//=======================================================================
95void AIS_XRTrackedDevice::SetLaserColor (const Quantity_Color& theColor)
96{
97 if (!myLaserColor.IsEqual (theColor))
98 {
99 myLaserColor = theColor;
100 computeLaserRay();
101 }
102}
103
104//=======================================================================
105//function : SetLaserLength
106//purpose :
107//=======================================================================
108void AIS_XRTrackedDevice::SetLaserLength (Standard_ShortReal theLength)
109{
110 if (myLaserLength != theLength)
111 {
112 myLaserLength = theLength;
113 computeLaserRay();
114 }
115}
116
117//=======================================================================
118//function : computeLaserRay
119//purpose :
120//=======================================================================
121void AIS_XRTrackedDevice::computeLaserRay()
122{
123 if (myRayGroup.IsNull())
124 {
125 return;
126 }
127
128 if (!myRayGroup->IsEmpty())
129 {
130 myRayGroup->Clear();
131 }
132 if (myLaserLength <= 0.0f)
133 {
134 return;
135 }
136
137 Handle(Graphic3d_ArrayOfPrimitives) aLines = new Graphic3d_ArrayOfSegments (2, 0, Graphic3d_ArrayFlags_VertexColor);
138 aLines->AddVertex (gp_Pnt (0.0, 0.0, 0.0), myLaserColor);
139 aLines->AddVertex (gp_Pnt (0.0, 0.0, -myLaserLength), myLaserColor);
140 myRayGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
141 myRayGroup->AddPrimitiveArray (aLines, false); // do not extend camera frustum by ray
142}
143
144//=======================================================================
145//function : Compute
146//purpose :
147//=======================================================================
148void AIS_XRTrackedDevice::Compute (const Handle(PrsMgr_PresentationManager3d)& ,
149 const Handle(Prs3d_Presentation)& thePrs,
150 const Standard_Integer theMode)
151{
152 if (theMode != 0)
153 {
154 return;
155 }
156
157 thePrs->SetInfiniteState (myInfiniteState);
158 Handle(Graphic3d_Group) aGroup = thePrs->NewGroup();
159 if (!myTris.IsNull())
160 {
161 aGroup->SetGroupPrimitivesAspect (myDrawer->ShadingAspect()->Aspect());
162 aGroup->AddPrimitiveArray (myTris);
163 }
164
165 if (myToShowAxes || myTris.IsNull())
166 {
167 const float aSize = 0.1f * myUnitFactor;
168 aGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
169 Handle(Graphic3d_ArrayOfPrimitives) aLines = new Graphic3d_ArrayOfSegments (6, 0, Graphic3d_ArrayFlags_VertexColor);
170 aLines->AddVertex (gp_Pnt (0.0, 0.0, 0.0), Quantity_Color (Quantity_NOC_RED));
171 aLines->AddVertex (gp_Pnt (aSize, 0.0, 0.0), Quantity_Color (Quantity_NOC_RED));
172 aLines->AddVertex (gp_Pnt (0.0, 0.0, 0.0), Quantity_Color (Quantity_NOC_GREEN));
173 aLines->AddVertex (gp_Pnt (0.0, aSize, 0.0), Quantity_Color (Quantity_NOC_GREEN));
174 aLines->AddVertex (gp_Pnt (0.0, 0.0, 0.0), Quantity_Color (Quantity_NOC_BLUE));
175 aLines->AddVertex (gp_Pnt (0.0, 0.0, aSize), Quantity_Color (Quantity_NOC_BLUE));
176 aGroup->AddPrimitiveArray (aLines);
177 }
178
179 myRayGroup = thePrs->NewGroup();
180 computeLaserRay();
181}
182
183//=======================================================================
184//function : ComputeSelection
185//purpose :
186//=======================================================================
187void AIS_XRTrackedDevice::ComputeSelection (const Handle(SelectMgr_Selection)& theSel,
188 const Standard_Integer theMode)
189{
190 if (theMode != 0)
191 {
192 return;
193 }
194
195 if (!myTris.IsNull())
196 {
197 Handle(SelectMgr_EntityOwner) anOwner = new SelectMgr_EntityOwner (this);
198 Handle(Select3D_SensitivePrimitiveArray) aSensitive = new Select3D_SensitivePrimitiveArray (anOwner);
199 aSensitive->InitTriangulation (myTris->Attributes(), myTris->Indices(), TopLoc_Location(), true);
200 theSel->Add (aSensitive);
201 }
202}