Salome HOME
f7ef9256ef03ba9e36697376cd5155972b873d61
[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
10 #include <GeomAPI_Edge.h>
11
12 #include <Graphic3d_ArrayOfSegments.hxx>
13 #include <Graphic3d_BndBox4f.hxx>
14
15 #include <SelectMgr_Selection.hxx>
16 #include <SelectMgr_EntityOwner.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.03;
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
230 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
231 {
232   if (myIconsMap.count(iconName()) == 1) {
233     return myIconsMap[iconName()];
234   }
235   TCollection_AsciiString aFile(getenv("NewGeomResources"));
236   aFile += FSEP;
237   aFile += iconName();
238   Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
239   if (aPixMap->Load(aFile)) {
240     myIconsMap[iconName()] = aPixMap;
241     return aPixMap;
242   }
243   return Handle(Image_AlienPixMap)();
244 }
245
246 void SketcherPrs_SymbolPrs::ClearSelected()
247 {
248   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );  
249   if( !aSelectionPrs.IsNull() ) {
250     aSelectionPrs->Clear(); 
251   }
252 }
253
254 void SketcherPrs_SymbolPrs::prepareAspect()
255 {
256   if (myAspect.IsNull()) {
257     myAspect = new Graphic3d_AspectMarker3d(icon());
258   }
259 }
260
261 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
262 {
263   ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
264   std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
265   if (aLine.get() == NULL)
266     return;
267   std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
268
269   std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
270   std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
271
272   Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
273   aLines->AddVertex(aPnt1->impl<gp_Pnt>());
274   aLines->AddVertex(aPnt2->impl<gp_Pnt>());
275   theGroup->AddPrimitiveArray(aLines);
276 }
277
278 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM, 
279                                            const SelectMgr_SequenceOfOwner& theOwners)
280 {
281
282   Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
283   aSelectionPrs->Clear();
284   drawLines(aSelectionPrs, Quantity_NOC_WHITE);
285
286   aSelectionPrs->SetDisplayPriority(9);
287   aSelectionPrs->Display();
288   thePM->Highlight(this);
289 }
290
291 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM, 
292                                                  const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
293 {
294   thePM->Color(this, theColor);
295
296   Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
297   aHilightPrs->Clear();
298   drawLines(aHilightPrs, theColor);
299
300   if (thePM->IsImmediateModeOn())
301     thePM->AddToImmediateList(aHilightPrs);
302 }
303
304 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
305                                    const Handle(Prs3d_Presentation)& thePresentation, 
306                                    const Standard_Integer theMode)
307 {
308   prepareAspect();
309
310   Handle(AIS_InteractiveContext) aCtx = GetContext();
311   Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
312   if (!aDriver.IsNull()) {
313     // register the custom element factory function
314     aDriver->UserDrawCallback() = SymbolPrsCallBack;
315   }
316
317   if (!updatePoints(20))
318     return;
319
320   Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
321   aGroup->SetPrimitivesAspect(myAspect);
322
323   Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
324   gp_Pnt aVert;
325   aBnd.Clear();
326   for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
327     aVert = myPntArray->Vertice(i);
328     aBnd.Add (Graphic3d_Vec4 (aVert.X(), aVert.Y(), aVert.Z(), 1.0f));
329   }
330
331   aGroup->UserDraw(this, true);
332   //aGroup->AddPrimitiveArray(myPntArray);
333 }
334
335
336
337 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
338                                             const Standard_Integer aMode)
339 {
340   ClearSelected();
341   //if (!updatePoints(20))
342   //  return;
343
344   Handle(SelectMgr_EntityOwner) aOwn = new SelectMgr_EntityOwner(this);
345   for (int i = 1; i <= myPntArray->VertexNumber(); i++) {
346     Handle(Select3D_SensitivePoint) aSP = new Select3D_SensitivePoint(aOwn, myPntArray->Vertice(i));
347     aSelection->Add(aSP);
348   }
349 }
350
351
352 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
353 {
354   const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
355   const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
356   Handle(OpenGl_View) aView = theWorkspace->ActiveView();
357   
358   double aScale = aView->Camera()->Scale();
359   if (!updatePoints(MyDist * aScale))
360     return;
361
362   Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
363
364   if (myVboAttribs.IsNull()) {
365     myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
366   }
367
368   if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
369     myVboAttribs->Release (aCtx.operator->());
370     myVboAttribs.Nullify();
371     return;
372   }
373     
374   Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
375
376   const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
377       
378   if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
379     const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
380     const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
381                                               ? anAspectMarker->SpriteHighlightRes(aCtx)
382                                               : aSpriteNorm;
383     theWorkspace->EnableTexture (aSprite);
384     aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
385     const TEL_COLOUR* aLineColor  =  &anAspectMarker->Color();
386     if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
387       aLineColor = theWorkspace->HighlightColor;
388
389     aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
390
391
392     myVboAttribs->BindAllAttributes(aCtx);
393     // Textured markers will be drawn with the point sprites
394     aCtx->SetPointSize (anAspectMarker->MarkerSize());
395     aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
396     aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
397
398     aCtx->core11fwd->glEnable (GL_BLEND);
399     aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
400
401     aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
402
403     aCtx->core11fwd->glDisable (GL_BLEND);
404     aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
405     aCtx->SetPointSize (1.0f);
406   }
407   theWorkspace->EnableTexture (aTextureBack);
408   aCtx->BindProgram (NULL);
409 }
410
411
412 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
413 {
414   if (!myVboAttribs.IsNull()) {
415     if (theContext) {
416       theContext->DelayedRelease (myVboAttribs);
417     }
418     myVboAttribs.Nullify();
419   }
420 }
421