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>
13 #include <Graphic3d_ArrayOfSegments.hxx>
14 #include <Graphic3d_BndBox4f.hxx>
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>
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>
39 /// Step between icons
40 static const double MyDist = 0.02;
42 /// Function to convert opengl data type
43 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
46 case Graphic3d_TOD_USHORT:
48 return GL_UNSIGNED_SHORT;
49 case Graphic3d_TOD_UINT:
51 return GL_UNSIGNED_INT;
52 case Graphic3d_TOD_VEC2:
55 case Graphic3d_TOD_VEC3:
58 case Graphic3d_TOD_VEC4:
61 case Graphic3d_TOD_VEC4UB:
63 return GL_UNSIGNED_BYTE;
70 //*******************************************************************
71 //! Auxiliary class for Vertex buffer with interleaved attributes.
72 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
77 //! Create uninitialized VBO..
78 SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
79 const Standard_Integer theStride)
80 : Stride (theStride), NbAttributes(1)
83 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
86 //! Create uninitialized VBO.
87 SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
88 : Stride (theAttribs.Stride), NbAttributes(1)
90 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
93 virtual bool HasColorAttribute() const
95 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
96 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
97 if (anAttrib.Id == Graphic3d_TOA_COLOR) {
104 virtual bool HasNormalAttribute() const
106 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
107 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
108 if (anAttrib.Id == Graphic3d_TOA_NORM) {
115 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
117 if (!OpenGl_VertexBuffer::IsValid()) {
121 OpenGl_VertexBuffer::Bind (theGlCtx);
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) {
129 } else if (anAttrib.Id == Graphic3d_TOA_POS) {
130 OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
134 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
138 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
140 if (!OpenGl_VertexBuffer::IsValid())
143 OpenGl_VertexBuffer::Bind (theGlCtx);
145 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
146 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
148 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
149 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
150 if (aDataType == GL_NONE)
153 OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
154 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
158 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
160 if (!OpenGl_VertexBuffer::IsValid())
162 OpenGl_VertexBuffer::Unbind (theGlCtx);
164 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
165 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
166 OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
172 Graphic3d_Attribute Attribs[1];
173 Standard_Integer Stride;
174 Standard_Integer NbAttributes;
177 //**************************************************************
178 //! Redefinition of OpenGl_Element
179 class SketcherPrs_Element: public OpenGl_Element
182 SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) :
183 OpenGl_Element(), myObj(theObj) {}
185 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
188 myObj->Render(theWorkspace);
191 virtual void Release (OpenGl_Context* theContext)
194 myObj->Release(theContext);
198 Handle(SketcherPrs_SymbolPrs) myObj;
202 //**************************************************************
203 //! Definition of call back
204 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
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;
210 return new SketcherPrs_Element(anIObj);
214 //*****************************************************************************
215 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
216 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
219 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
222 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
223 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
224 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
226 SetAutoHilight(Standard_False);
229 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
231 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
232 aMgr->deleteConstraint(this);
237 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
239 if (myIconsMap.count(iconName()) == 1) {
240 return myIconsMap[iconName()];
242 TCollection_AsciiString aFile(getenv("NewGeomResources"));
245 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
246 if (aPixMap->Load(aFile)) {
247 myIconsMap[iconName()] = aPixMap;
250 return Handle(Image_AlienPixMap)();
253 void SketcherPrs_SymbolPrs::ClearSelected()
255 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );
256 if( !aSelectionPrs.IsNull() ) {
257 aSelectionPrs->Clear();
261 void SketcherPrs_SymbolPrs::prepareAspect()
263 if (myAspect.IsNull()) {
264 myAspect = new Graphic3d_AspectMarker3d(icon());
268 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
270 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
271 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
272 if (aLine.get() == NULL)
274 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
276 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
277 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
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);
285 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
286 const SelectMgr_SequenceOfOwner& theOwners)
289 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
290 aSelectionPrs->Clear();
291 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
293 aSelectionPrs->SetDisplayPriority(9);
294 aSelectionPrs->Display();
295 thePM->Highlight(this);
298 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
299 const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
301 thePM->Color(this, theColor);
303 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
304 aHilightPrs->Clear();
305 drawLines(aHilightPrs, theColor);
307 if (thePM->IsImmediateModeOn())
308 thePM->AddToImmediateList(aHilightPrs);
311 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
312 const Handle(Prs3d_Presentation)& thePresentation,
313 const Standard_Integer theMode)
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;
324 if (!updatePoints(20))
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);
336 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
337 aGroup->SetPrimitivesAspect(myAspect);
339 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
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));
347 aGroup->UserDraw(this, true);
348 //aGroup->AddPrimitiveArray(myPntArray);
353 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
354 const Standard_Integer aMode)
357 for (int i = 1; i <= mySPoints.Length(); i++)
358 aSelection->Add(mySPoints.Value(i));
362 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
364 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
365 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
366 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
368 double aScale = aView->Camera()->Scale();
369 if (!updatePoints(MyDist * aScale))
372 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
374 if (myVboAttribs.IsNull()) {
375 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
378 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
379 myVboAttribs->Release (aCtx.operator->());
380 myVboAttribs.Nullify();
384 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
386 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
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)
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;
399 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
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);
408 aCtx->core11fwd->glEnable (GL_BLEND);
409 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
411 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
413 aCtx->core11fwd->glDisable (GL_BLEND);
414 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
415 aCtx->SetPointSize (1.0f);
417 theWorkspace->EnableTexture (aTextureBack);
418 aCtx->BindProgram (NULL);
420 // Update selection position
421 GetContext()->RecomputeSelectionOnly(this);
425 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
427 if (!myVboAttribs.IsNull()) {
429 theContext->DelayedRelease (myVboAttribs);
431 myVboAttribs.Nullify();