1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketcherPrs_SymbolPrs.cpp
4 // Created: 12 March 2015
5 // Author: Vitaly SMETANNIKOV
7 #include "SketcherPrs_SymbolPrs.h"
8 #include "SketcherPrs_Tools.h"
9 #include "SketcherPrs_PositionMgr.h"
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Vertex.h>
13 #include <GeomAPI_Curve.h>
15 #include <Graphic3d_ArrayOfSegments.hxx>
16 #include <Graphic3d_BndBox4f.hxx>
18 #include <SelectMgr_Selection.hxx>
19 #include <SelectMgr_SelectionManager.hxx>
20 #include <Select3D_SensitivePoint.hxx>
21 #include <TopLoc_Location.hxx>
22 #include <AIS_InteractiveContext.hxx>
23 #include <V3d_Viewer.hxx>
24 #include <Prs3d_Root.hxx>
25 #include <Geom_CartesianPoint.hxx>
26 #include <GeomAdaptor_Curve.hxx>
27 #include <StdPrs_DeflectionCurve.hxx>
28 #include <StdPrs_Point.hxx>
29 #include <StdPrs_Curve.hxx>
31 #include <OpenGl_Element.hxx>
32 #include <OpenGl_GraphicDriver.hxx>
33 #include <OpenGl_Context.hxx>
34 #include <OpenGl_View.hxx>
35 #include <OpenGl_PointSprite.hxx>
36 #include <OpenGl_VertexBuffer.hxx>
37 #include <OpenGl_ShaderManager.hxx>
38 #include <OpenGl_VertexBufferCompat.hxx>
39 #include <OpenGl_GraphicDriver.hxx>
47 /// Step between icons
48 static const double MyDist = 0.02;
50 /// Function to convert opengl data type
51 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
54 case Graphic3d_TOD_USHORT:
56 return GL_UNSIGNED_SHORT;
57 case Graphic3d_TOD_UINT:
59 return GL_UNSIGNED_INT;
60 case Graphic3d_TOD_VEC2:
63 case Graphic3d_TOD_VEC3:
66 case Graphic3d_TOD_VEC4:
69 case Graphic3d_TOD_VEC4UB:
71 return GL_UNSIGNED_BYTE;
78 //*******************************************************************
79 //! Auxiliary class for Vertex buffer with interleaved attributes.
80 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
85 //! Create uninitialized VBO..
86 SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
87 const Standard_Integer theStride)
88 : Stride (theStride), NbAttributes(1)
91 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
94 //! Create uninitialized VBO.
95 SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
96 : Stride (theAttribs.Stride), NbAttributes(1)
98 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
101 virtual bool HasColorAttribute() const
103 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
104 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
105 if (anAttrib.Id == Graphic3d_TOA_COLOR) {
112 virtual bool HasNormalAttribute() const
114 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
115 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
116 if (anAttrib.Id == Graphic3d_TOA_NORM) {
123 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
125 if (!OpenGl_VertexBuffer::IsValid()) {
129 OpenGl_VertexBuffer::Bind (theGlCtx);
131 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
132 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
133 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
134 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
135 if (aDataType == GL_NONE) {
137 } else if (anAttrib.Id == Graphic3d_TOA_POS) {
138 OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
142 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
146 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
148 if (!OpenGl_VertexBuffer::IsValid())
151 OpenGl_VertexBuffer::Bind (theGlCtx);
153 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
154 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
156 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
157 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
158 if (aDataType == GL_NONE)
161 OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
162 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
166 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
168 if (!OpenGl_VertexBuffer::IsValid())
170 OpenGl_VertexBuffer::Unbind (theGlCtx);
172 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
173 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
174 OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
180 Graphic3d_Attribute Attribs[1];
181 Standard_Integer Stride;
182 Standard_Integer NbAttributes;
185 //**************************************************************
186 //! Redefinition of OpenGl_Element
187 class SketcherPrs_Element: public OpenGl_Element
190 SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) :
191 OpenGl_Element(), myObj(theObj) {}
193 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
196 myObj->Render(theWorkspace);
199 virtual void Release (OpenGl_Context* theContext)
202 myObj->Release(theContext);
206 Handle(SketcherPrs_SymbolPrs) myObj;
210 //**************************************************************
211 //! Definition of call back
212 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
214 Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
215 if (anIObj.IsNull()) {
216 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
218 return new SketcherPrs_Element(anIObj);
222 //*****************************************************************************
223 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
224 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
227 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
230 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
231 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
232 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
234 SetAutoHilight(Standard_False);
237 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
239 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
240 aMgr->deleteConstraint(this);
245 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
247 if (myIconsMap.count(iconName()) == 1) {
248 return myIconsMap[iconName()];
250 TCollection_AsciiString aFile(getenv("NewGeomResources"));
253 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
254 if (aPixMap->Load(aFile)) {
255 myIconsMap[iconName()] = aPixMap;
258 return Handle(Image_AlienPixMap)();
261 void SketcherPrs_SymbolPrs::ClearSelected()
263 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );
264 if( !aSelectionPrs.IsNull() ) {
265 aSelectionPrs->Clear();
269 void SketcherPrs_SymbolPrs::prepareAspect()
271 if (myAspect.IsNull()) {
272 myAspect = new Graphic3d_AspectMarker3d(icon());
276 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
278 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
279 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
280 if (aLine.get() == NULL)
282 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
284 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
285 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
287 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
288 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
289 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
290 theGroup->AddPrimitiveArray(aLines);
293 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
294 const SelectMgr_SequenceOfOwner& theOwners)
297 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
298 aSelectionPrs->Clear();
299 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
301 aSelectionPrs->SetDisplayPriority(9);
302 aSelectionPrs->Display();
303 thePM->Highlight(this);
306 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
307 const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
309 thePM->Color(this, theColor);
311 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
312 aHilightPrs->Clear();
313 drawLines(aHilightPrs, theColor);
315 if (thePM->IsImmediateModeOn())
316 thePM->AddToImmediateList(aHilightPrs);
319 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
320 const Handle(Prs3d_Presentation)& thePresentation,
321 const Standard_Integer theMode)
325 Handle(AIS_InteractiveContext) aCtx = GetContext();
326 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
327 if (!aDriver.IsNull()) {
328 // register the custom element factory function
329 aDriver->UserDrawCallback() = SymbolPrsCallBack;
332 if (!updatePoints(20))
335 int aNbVertex = myPntArray->VertexNumber();
336 if (myOwner.IsNull()) {
337 myOwner = new SelectMgr_EntityOwner(this);
338 for (int i = 1; i <= aNbVertex; i++) {
339 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, myPntArray, i);
340 mySPoints.Append(aSP);
344 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
345 aGroup->SetPrimitivesAspect(myAspect);
347 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
350 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
351 aVert = myPntArray->Vertice(i);
352 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
355 aGroup->UserDraw(this, true);
356 //aGroup->AddPrimitiveArray(myPntArray);
361 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
362 const Standard_Integer aMode)
365 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
366 for (int i = 1; i <= mySPoints.Length(); i++)
367 aSelection->Add(mySPoints.Value(i));
372 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
374 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
375 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
376 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
378 double aScale = aView->Camera()->Scale();
379 if (!updatePoints(MyDist * aScale))
382 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
384 if (myVboAttribs.IsNull()) {
385 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
388 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
389 myVboAttribs->Release (aCtx.operator->());
390 myVboAttribs.Nullify();
394 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
396 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
398 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
399 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
400 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
401 ? anAspectMarker->SpriteHighlightRes(aCtx)
403 theWorkspace->EnableTexture (aSprite);
404 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
405 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
406 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
407 aLineColor = theWorkspace->HighlightColor;
409 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
412 myVboAttribs->BindAllAttributes(aCtx);
413 // Textured markers will be drawn with the point sprites
414 aCtx->SetPointSize (anAspectMarker->MarkerSize());
415 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
416 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
418 aCtx->core11fwd->glEnable (GL_BLEND);
419 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
421 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
423 aCtx->core11fwd->glDisable (GL_BLEND);
424 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
425 aCtx->SetPointSize (1.0f);
427 theWorkspace->EnableTexture (aTextureBack);
428 aCtx->BindProgram (NULL);
430 // Update selection position only if there is no selected object
431 // because it can corrupt selection of other objects
432 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
433 GetContext()->RecomputeSelectionOnly(this);
437 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
439 if (!myVboAttribs.IsNull()) {
441 theContext->DelayedRelease (myVboAttribs);
443 myVboAttribs.Nullify();
447 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
448 const Handle(Prs3d_Presentation)& thePrs) const
450 if (theShape->isEdge()) {
451 std::shared_ptr<GeomAPI_Curve> aCurve =
452 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
453 if (aCurve->isLine()) {
454 GeomAdaptor_Curve aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
455 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
457 GeomAdaptor_Curve aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
458 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
460 } else if (theShape->isVertex()) {
461 std::shared_ptr<GeomAPI_Vertex> aVertex =
462 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
463 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
464 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
465 StdPrs_Point::Add(thePrs, aPoint, myDrawer);