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 <SelectMgr_SelectionManager.hxx>
18 #include <Select3D_SensitivePoint.hxx>
19 #include <TopLoc_Location.hxx>
20 #include <AIS_InteractiveContext.hxx>
21 #include <V3d_Viewer.hxx>
22 #include <Prs3d_Root.hxx>
24 #include <OpenGl_Element.hxx>
25 #include <OpenGl_GraphicDriver.hxx>
26 #include <OpenGl_Context.hxx>
27 #include <OpenGl_View.hxx>
28 #include <OpenGl_PointSprite.hxx>
29 #include <OpenGl_VertexBuffer.hxx>
30 #include <OpenGl_ShaderManager.hxx>
31 #include <OpenGl_VertexBufferCompat.hxx>
32 #include <OpenGl_GraphicDriver.hxx>
40 /// Step between icons
41 static const double MyDist = 0.02;
43 /// Function to convert opengl data type
44 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
47 case Graphic3d_TOD_USHORT:
49 return GL_UNSIGNED_SHORT;
50 case Graphic3d_TOD_UINT:
52 return GL_UNSIGNED_INT;
53 case Graphic3d_TOD_VEC2:
56 case Graphic3d_TOD_VEC3:
59 case Graphic3d_TOD_VEC4:
62 case Graphic3d_TOD_VEC4UB:
64 return GL_UNSIGNED_BYTE;
71 //*******************************************************************
72 //! Auxiliary class for Vertex buffer with interleaved attributes.
73 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
78 //! Create uninitialized VBO..
79 SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
80 const Standard_Integer theStride)
81 : Stride (theStride), NbAttributes(1)
84 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
87 //! Create uninitialized VBO.
88 SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
89 : Stride (theAttribs.Stride), NbAttributes(1)
91 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
94 virtual bool HasColorAttribute() const
96 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
97 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
98 if (anAttrib.Id == Graphic3d_TOA_COLOR) {
105 virtual bool HasNormalAttribute() const
107 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
108 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
109 if (anAttrib.Id == Graphic3d_TOA_NORM) {
116 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
118 if (!OpenGl_VertexBuffer::IsValid()) {
122 OpenGl_VertexBuffer::Bind (theGlCtx);
124 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
125 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
126 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
127 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
128 if (aDataType == GL_NONE) {
130 } else if (anAttrib.Id == Graphic3d_TOA_POS) {
131 OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
135 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
139 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
141 if (!OpenGl_VertexBuffer::IsValid())
144 OpenGl_VertexBuffer::Bind (theGlCtx);
146 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
147 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
149 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
150 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
151 if (aDataType == GL_NONE)
154 OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
155 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
159 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
161 if (!OpenGl_VertexBuffer::IsValid())
163 OpenGl_VertexBuffer::Unbind (theGlCtx);
165 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
166 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
167 OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
173 Graphic3d_Attribute Attribs[1];
174 Standard_Integer Stride;
175 Standard_Integer NbAttributes;
178 //**************************************************************
179 //! Redefinition of OpenGl_Element
180 class SketcherPrs_Element: public OpenGl_Element
183 SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) :
184 OpenGl_Element(), myObj(theObj) {}
186 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
189 myObj->Render(theWorkspace);
192 virtual void Release (OpenGl_Context* theContext)
195 myObj->Release(theContext);
199 Handle(SketcherPrs_SymbolPrs) myObj;
203 //**************************************************************
204 //! Definition of call back
205 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
207 Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
208 if (anIObj.IsNull()) {
209 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
211 return new SketcherPrs_Element(anIObj);
215 //*****************************************************************************
216 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
217 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
220 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
223 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(ModelAPI_Feature* theConstraint,
224 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
225 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
227 SetAutoHilight(Standard_False);
230 SketcherPrs_SymbolPrs::~SketcherPrs_SymbolPrs()
232 SketcherPrs_PositionMgr* aMgr = SketcherPrs_PositionMgr::get();
233 aMgr->deleteConstraint(this);
238 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
240 if (myIconsMap.count(iconName()) == 1) {
241 return myIconsMap[iconName()];
243 TCollection_AsciiString aFile(getenv("NewGeomResources"));
246 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
247 if (aPixMap->Load(aFile)) {
248 myIconsMap[iconName()] = aPixMap;
251 return Handle(Image_AlienPixMap)();
254 void SketcherPrs_SymbolPrs::ClearSelected()
256 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );
257 if( !aSelectionPrs.IsNull() ) {
258 aSelectionPrs->Clear();
262 void SketcherPrs_SymbolPrs::prepareAspect()
264 if (myAspect.IsNull()) {
265 myAspect = new Graphic3d_AspectMarker3d(icon());
269 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
271 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
272 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
273 if (aLine.get() == NULL)
275 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
277 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
278 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
280 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
281 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
282 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
283 theGroup->AddPrimitiveArray(aLines);
286 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
287 const SelectMgr_SequenceOfOwner& theOwners)
290 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
291 aSelectionPrs->Clear();
292 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
294 aSelectionPrs->SetDisplayPriority(9);
295 aSelectionPrs->Display();
296 thePM->Highlight(this);
299 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
300 const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
302 thePM->Color(this, theColor);
304 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
305 aHilightPrs->Clear();
306 drawLines(aHilightPrs, theColor);
308 if (thePM->IsImmediateModeOn())
309 thePM->AddToImmediateList(aHilightPrs);
312 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
313 const Handle(Prs3d_Presentation)& thePresentation,
314 const Standard_Integer theMode)
318 Handle(AIS_InteractiveContext) aCtx = GetContext();
319 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
320 if (!aDriver.IsNull()) {
321 // register the custom element factory function
322 aDriver->UserDrawCallback() = SymbolPrsCallBack;
325 if (!updatePoints(20))
328 int aNbVertex = myPntArray->VertexNumber();
329 if (myOwner.IsNull()) {
330 myOwner = new SelectMgr_EntityOwner(this);
331 for (int i = 1; i <= aNbVertex; i++) {
332 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, myPntArray, i);
333 mySPoints.Append(aSP);
337 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
338 aGroup->SetPrimitivesAspect(myAspect);
340 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
343 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
344 aVert = myPntArray->Vertice(i);
345 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
348 aGroup->UserDraw(this, true);
349 //aGroup->AddPrimitiveArray(myPntArray);
354 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
355 const Standard_Integer aMode)
358 if ((aMode == 0) || (aMode == SketcherPrs_Tools::Sel_Constraint)) {
359 for (int i = 1; i <= mySPoints.Length(); i++)
360 aSelection->Add(mySPoints.Value(i));
365 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
367 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
368 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
369 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
371 double aScale = aView->Camera()->Scale();
372 if (!updatePoints(MyDist * aScale))
375 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
377 if (myVboAttribs.IsNull()) {
378 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
381 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
382 myVboAttribs->Release (aCtx.operator->());
383 myVboAttribs.Nullify();
387 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
389 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
391 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
392 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
393 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
394 ? anAspectMarker->SpriteHighlightRes(aCtx)
396 theWorkspace->EnableTexture (aSprite);
397 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
398 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
399 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
400 aLineColor = theWorkspace->HighlightColor;
402 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
405 myVboAttribs->BindAllAttributes(aCtx);
406 // Textured markers will be drawn with the point sprites
407 aCtx->SetPointSize (anAspectMarker->MarkerSize());
408 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
409 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
411 aCtx->core11fwd->glEnable (GL_BLEND);
412 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
414 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
416 aCtx->core11fwd->glDisable (GL_BLEND);
417 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
418 aCtx->SetPointSize (1.0f);
420 theWorkspace->EnableTexture (aTextureBack);
421 aCtx->BindProgram (NULL);
423 // Update selection position only if there is no selected object
424 // because it can corrupt selection of other objects
425 if ((GetContext()->NbCurrents() == 0) && (GetContext()->NbSelected() == 0))
426 GetContext()->RecomputeSelectionOnly(this);
430 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
432 if (!myVboAttribs.IsNull()) {
434 theContext->DelayedRelease (myVboAttribs);
436 myVboAttribs.Nullify();