Salome HOME
Create check box "Show constraints"
[modules/shaper.git] / src / SketcherPrs / SketcherPrs_SymbolPrs.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        SketcherPrs_SymbolPrs.cpp
4 // Created:     12 March 2015
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "SketcherPrs_SymbolPrs.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
10
11 #include <GeomAPI_Edge.h>
12
13 #include <Graphic3d_ArrayOfSegments.hxx>
14 #include <Graphic3d_BndBox4f.hxx>
15
16 #include <SelectMgr_Selection.hxx>
17 #include <Select3D_SensitivePoint.hxx>
18 #include <TopLoc_Location.hxx>
19 #include <AIS_InteractiveContext.hxx>
20 #include <V3d_Viewer.hxx>
21 #include <Prs3d_Root.hxx>
22
23 #include <OpenGl_Element.hxx>
24 #include <OpenGl_GraphicDriver.hxx>
25 #include <OpenGl_Context.hxx>
26 #include <OpenGl_View.hxx>
27 #include <OpenGl_PointSprite.hxx>
28 #include <OpenGl_VertexBuffer.hxx>
29 #include <OpenGl_ShaderManager.hxx>
30 #include <OpenGl_VertexBufferCompat.hxx>
31 #include <OpenGl_GraphicDriver.hxx>
32
33 #ifdef WIN32
34 # define FSEP "\\"
35 #else
36 # define FSEP "/"
37 #endif
38
39 /// Step between icons
40 static const double MyDist = 0.02;
41
42 /// Function to convert opengl data type
43 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
44 {
45   switch (theType) {
46     case Graphic3d_TOD_USHORT:
47       theNbComp = 1;
48       return GL_UNSIGNED_SHORT;
49     case Graphic3d_TOD_UINT:
50       theNbComp = 1;
51       return GL_UNSIGNED_INT;
52     case Graphic3d_TOD_VEC2:
53       theNbComp = 2;
54       return GL_FLOAT;
55     case Graphic3d_TOD_VEC3:
56       theNbComp = 3;
57       return GL_FLOAT;
58     case Graphic3d_TOD_VEC4:
59       theNbComp = 4;
60       return GL_FLOAT;
61     case Graphic3d_TOD_VEC4UB:
62       theNbComp = 4;
63       return GL_UNSIGNED_BYTE;
64   }
65   theNbComp = 0;
66   return GL_NONE;
67 }
68
69
70 //*******************************************************************
71 //! Auxiliary class for Vertex buffer with interleaved attributes.
72 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
73 {
74
75 public:
76
77   //! Create uninitialized VBO..
78   SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
79                         const Standard_Integer     theStride)
80   : Stride (theStride), NbAttributes(1)
81   {
82
83     memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
84   }
85
86   //! Create uninitialized VBO.
87   SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
88   : Stride (theAttribs.Stride), NbAttributes(1)
89   {
90     memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
91   }
92
93   virtual bool HasColorAttribute() const
94   {
95     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
96       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
97       if (anAttrib.Id == Graphic3d_TOA_COLOR) {
98         return true;
99       }
100     }
101     return false;
102   }
103
104   virtual bool HasNormalAttribute() const
105   {
106     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
107       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
108       if (anAttrib.Id == Graphic3d_TOA_NORM) {
109         return true;
110       }
111     }
112     return false;
113   }
114
115   virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
116   {
117     if (!OpenGl_VertexBuffer::IsValid()) {
118       return;
119     }
120
121     OpenGl_VertexBuffer::Bind (theGlCtx);
122     GLint aNbComp;
123     const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
124     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
125       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
126       const GLenum   aDataType = toGlDataType (anAttrib.DataType, aNbComp);
127       if (aDataType == GL_NONE) {
128         continue;
129       } else if (anAttrib.Id == Graphic3d_TOA_POS) {
130         OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
131         break;
132       }
133
134       anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
135     }
136   }
137
138   virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
139   {
140     if (!OpenGl_VertexBuffer::IsValid())
141       return;
142
143     OpenGl_VertexBuffer::Bind (theGlCtx);
144     GLint aNbComp;
145     const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
146     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
147     {
148       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
149       const GLenum   aDataType = toGlDataType (anAttrib.DataType, aNbComp);
150       if (aDataType == GL_NONE)
151         continue;
152
153       OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
154       anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
155     }
156   }
157
158   virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
159   {
160     if (!OpenGl_VertexBuffer::IsValid())
161       return;
162     OpenGl_VertexBuffer::Unbind (theGlCtx);
163
164     for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
165       const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
166       OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
167     }
168   }
169
170 public:
171
172   Graphic3d_Attribute Attribs[1];
173   Standard_Integer    Stride;
174   Standard_Integer NbAttributes;
175 };
176
177 //**************************************************************
178 //! Redefinition of OpenGl_Element
179 class SketcherPrs_Element: public OpenGl_Element
180 {
181 public:
182   SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) : 
183   OpenGl_Element(), myObj(theObj) {}
184
185   virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
186   {
187     if (!myObj.IsNull())
188       myObj->Render(theWorkspace);
189   }
190
191   virtual void Release (OpenGl_Context* theContext) 
192   {
193     if (!myObj.IsNull())
194       myObj->Release(theContext);
195   }
196
197 private:
198   Handle(SketcherPrs_SymbolPrs) myObj;
199 };
200
201
202 //**************************************************************
203 //! Definition of call back
204 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
205 {
206   Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
207   if (anIObj.IsNull()) {
208     std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
209   }
210   return new SketcherPrs_Element(anIObj);
211 }
212
213
214 //*****************************************************************************
215 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
216 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
217
218
219 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
220
221
222 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(SketchPlugin_Constraint* theConstraint, 
223                                              const std::shared_ptr<GeomAPI_Ax3>& thePlane)
224  : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
225 {
226   SetAutoHilight(Standard_False);
227 }
228
229 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
230 {
231   SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
232   aMgr->deleteConstraint(this);
233 }
234
235
236
237 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
238 {
239   if (myIconsMap.count(iconName()) == 1) {
240     return myIconsMap[iconName()];
241   }
242   TCollection_AsciiString aFile(getenv("NewGeomResources"));
243   aFile += FSEP;
244   aFile += iconName();
245   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
246   if (aPixMap->Load(aFile)) {
247     myIconsMap[iconName()] = aPixMap;
248     return aPixMap;
249   }
250   return Handle(Image_AlienPixMap)();
251 }
252
253 void SketcherPrs_SymbolPrs::ClearSelected()
254 {
255   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );  
256   if( !aSelectionPrs.IsNull() ) {
257     aSelectionPrs->Clear(); 
258   }
259 }
260
261 void SketcherPrs_SymbolPrs::prepareAspect()
262 {
263   if (myAspect.IsNull()) {
264     myAspect = new Graphic3d_AspectMarker3d(icon());
265   }
266 }
267
268 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
269 {
270   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
271   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
272   if (aLine.get() == NULL)
273     return;
274   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
275
276   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
277   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
278
279   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
280   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
281   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
282   theGroup->AddPrimitiveArray(aLines);
283 }
284
285 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
286                                            const SelectMgr_SequenceOfOwner& theOwners)
287 {
288
289   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
290   aSelectionPrs->Clear();
291   drawLines(aSelectionPrs, Quantity_NOC_WHITE);
292
293   aSelectionPrs->SetDisplayPriority(9);
294   aSelectionPrs->Display();
295   thePM->Highlight(this);
296 }
297
298 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
299                                                  const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
300 {
301   thePM->Color(this, theColor);
302
303   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
304   aHilightPrs->Clear();
305   drawLines(aHilightPrs, theColor);
306
307   if (thePM->IsImmediateModeOn())
308     thePM->AddToImmediateList(aHilightPrs);
309 }
310
311 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
312                                    const Handle(Prs3d_Presentation)& thePresentation, 
313                                    const Standard_Integer theMode)
314 {
315   prepareAspect();
316
317   Handle(AIS_InteractiveContext) aCtx = GetContext();
318   Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
319   if (!aDriver.IsNull()) {
320     // register the custom element factory function
321     aDriver->UserDrawCallback() = SymbolPrsCallBack;
322   }
323
324   if (!updatePoints(20))
325     return;
326
327   int aNbVertex = myPntArray->VertexNumber();
328   if (myOwner.IsNull()) {
329     myOwner = new SelectMgr_EntityOwner(this);
330     for (int i = 1; i <= aNbVertex; i++) {
331       Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, myPntArray, i);
332       mySPoints.Append(aSP);
333     }
334   }
335
336   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
337   aGroup->SetPrimitivesAspect(myAspect);
338
339   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
340   gp_Pnt aVert;
341   aBnd.Clear();
342   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
343     aVert = myPntArray->Vertice(i);
344     aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
345   }
346
347   aGroup->UserDraw(this, true);
348   //aGroup->AddPrimitiveArray(myPntArray);
349 }
350
351
352
353 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
354                                             const Standard_Integer aMode)
355 {
356   ClearSelected();
357   for (int i = 1; i <= mySPoints.Length(); i++)
358     aSelection->Add(mySPoints.Value(i));
359 }
360
361
362 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
363 {
364   const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
365   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
366   Handle(OpenGl_View) aView = theWorkspace->ActiveView();
367   
368   double aScale = aView->Camera()->Scale();
369   if (!updatePoints(MyDist * aScale))
370     return;
371
372   Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
373
374   if (myVboAttribs.IsNull()) {
375     myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
376   }
377
378   if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
379     myVboAttribs->Release (aCtx.operator->());
380     myVboAttribs.Nullify();
381     return;
382   }
383     
384   Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
385
386   const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
387       
388   if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
389     const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
390     const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
391                                               ? anAspectMarker->SpriteHighlightRes(aCtx)
392                                               : aSpriteNorm;
393     theWorkspace->EnableTexture (aSprite);
394     aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
395     const TEL_COLOUR* aLineColor  =  &anAspectMarker->Color();
396     if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
397       aLineColor = theWorkspace->HighlightColor;
398
399     aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
400
401
402     myVboAttribs->BindAllAttributes(aCtx);
403     // Textured markers will be drawn with the point sprites
404     aCtx->SetPointSize (anAspectMarker->MarkerSize());
405     aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
406     aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
407
408     aCtx->core11fwd->glEnable (GL_BLEND);
409     aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
410
411     aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
412
413     aCtx->core11fwd->glDisable (GL_BLEND);
414     aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
415     aCtx->SetPointSize (1.0f);
416   }
417   theWorkspace->EnableTexture (aTextureBack);
418   aCtx->BindProgram (NULL);
419
420   // Update selection position
421   GetContext()->RecomputeSelectionOnly(this);
422 }
423
424
425 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
426 {
427   if (!myVboAttribs.IsNull()) {
428     if (theContext) {
429       theContext->DelayedRelease (myVboAttribs);
430     }
431     myVboAttribs.Nullify();
432   }
433 }
434