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