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);
341 for (int i = 1; i <= aNbVertex; i++) {
342 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, myPntArray, i);
343 mySPoints.Append(aSP);
346 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
347 aGroup->SetPrimitivesAspect(myAspect);
349 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
352 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
353 aVert = myPntArray->Vertice(i);
354 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
357 aGroup->UserDraw(this, true);
358 //aGroup->AddPrimitiveArray(myPntArray);
363 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
364 const Standard_Integer aMode)
367 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
368 for (int i = 1; i <= mySPoints.Length(); i++)
369 aSelection->Add(mySPoints.Value(i));
374 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
376 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
377 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
378 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
380 double aScale = aView->Camera()->Scale();
381 if (!updatePoints(MyDist * aScale))
384 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
386 if (myVboAttribs.IsNull()) {
387 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
390 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
391 myVboAttribs->Release (aCtx.operator->());
392 myVboAttribs.Nullify();
396 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
398 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
400 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
401 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
402 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
403 ? anAspectMarker->SpriteHighlightRes(aCtx)
405 theWorkspace->EnableTexture (aSprite);
406 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
407 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
408 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
409 aLineColor = theWorkspace->HighlightColor;
411 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
414 myVboAttribs->BindAllAttributes(aCtx);
415 // Textured markers will be drawn with the point sprites
416 aCtx->SetPointSize (anAspectMarker->MarkerSize());
417 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
418 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
420 aCtx->core11fwd->glEnable (GL_BLEND);
421 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
423 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
425 aCtx->core11fwd->glDisable (GL_BLEND);
426 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
427 aCtx->SetPointSize (1.0f);
429 theWorkspace->EnableTexture (aTextureBack);
430 aCtx->BindProgram (NULL);
432 // Update selection position only if there is no selected object
433 // because it can corrupt selection of other objects
434 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
435 GetContext()->RecomputeSelectionOnly(this);
439 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
441 if (!myVboAttribs.IsNull()) {
443 theContext->DelayedRelease (myVboAttribs);
445 myVboAttribs.Nullify();
449 void SketcherPrs_SymbolPrs::drawShape(const std::shared_ptr<GeomAPI_Shape>& theShape,
450 const Handle(Prs3d_Presentation)& thePrs) const
452 if (theShape->isEdge()) {
453 std::shared_ptr<GeomAPI_Curve> aCurve =
454 std::shared_ptr<GeomAPI_Curve>(new GeomAPI_Curve(theShape));
455 if (aCurve->isLine()) {
456 GeomAdaptor_Curve aCurv(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
457 StdPrs_Curve::Add(thePrs, aCurv, myDrawer);
459 GeomAdaptor_Curve aAdaptor(aCurve->impl<Handle(Geom_Curve)>(), aCurve->startParam(), aCurve->endParam());
460 StdPrs_DeflectionCurve::Add(thePrs,aAdaptor,myDrawer);
462 } else if (theShape->isVertex()) {
463 std::shared_ptr<GeomAPI_Vertex> aVertex =
464 std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex(theShape));
465 std::shared_ptr<GeomAPI_Pnt> aPnt = aVertex->point();
466 Handle(Geom_CartesianPoint) aPoint = new Geom_CartesianPoint(aPnt->impl<gp_Pnt>());
467 StdPrs_Point::Add(thePrs, aPoint, myDrawer);
471 void SketcherPrs_SymbolPrs::drawListOfShapes(const std::shared_ptr<ModelAPI_AttributeRefList>& theListAttr,
472 const Handle(Prs3d_Presentation)& thePrs) const
474 int aNb = theListAttr->size();
479 for (i = 0; i < aNb; i++) {
480 aObj = theListAttr->object(i);
481 std::shared_ptr<GeomAPI_Shape> aShape = SketcherPrs_Tools::getShape(aObj);
482 if (aShape.get() != NULL)
483 drawShape(aShape, thePrs);