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"
10 #include <GeomAPI_Edge.h>
12 #include <Graphic3d_ArrayOfSegments.hxx>
13 #include <Graphic3d_BndBox4f.hxx>
15 #include <SelectMgr_Selection.hxx>
16 #include <SelectMgr_EntityOwner.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.03;
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(SketchPlugin_Constraint* theConstraint,
223 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
224 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
226 SetAutoHilight(Standard_False);
230 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
232 if (myIconsMap.count(iconName()) == 1) {
233 return myIconsMap[iconName()];
235 TCollection_AsciiString aFile(getenv("NewGeomResources"));
238 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
239 if (aPixMap->Load(aFile)) {
240 myIconsMap[iconName()] = aPixMap;
243 return Handle(Image_AlienPixMap)();
246 void SketcherPrs_SymbolPrs::ClearSelected()
248 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );
249 if( !aSelectionPrs.IsNull() ) {
250 aSelectionPrs->Clear();
254 void SketcherPrs_SymbolPrs::prepareAspect()
256 if (myAspect.IsNull()) {
257 myAspect = new Graphic3d_AspectMarker3d(icon());
261 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
263 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
264 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
265 if (aLine.get() == NULL)
267 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
269 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
270 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
272 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
273 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
274 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
275 theGroup->AddPrimitiveArray(aLines);
278 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
279 const SelectMgr_SequenceOfOwner& theOwners)
282 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
283 aSelectionPrs->Clear();
284 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
286 aSelectionPrs->SetDisplayPriority(9);
287 aSelectionPrs->Display();
288 thePM->Highlight(this);
291 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
292 const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
294 thePM->Color(this, theColor);
296 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
297 aHilightPrs->Clear();
298 drawLines(aHilightPrs, theColor);
300 if (thePM->IsImmediateModeOn())
301 thePM->AddToImmediateList(aHilightPrs);
304 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
305 const Handle(Prs3d_Presentation)& thePresentation,
306 const Standard_Integer theMode)
310 Handle(AIS_InteractiveContext) aCtx = GetContext();
311 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
312 if (!aDriver.IsNull()) {
313 // register the custom element factory function
314 aDriver->UserDrawCallback() = SymbolPrsCallBack;
317 if (!updatePoints(20))
320 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
321 aGroup->SetPrimitivesAspect(myAspect);
323 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
326 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
327 aVert = myPntArray->Vertice(i);
328 aBnd.Add (Graphic3d_Vec4 (aVert.X(), aVert.Y(), aVert.Z(), 1.0f));
331 aGroup->UserDraw(this, true);
332 //aGroup->AddPrimitiveArray(myPntArray);
337 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
338 const Standard_Integer aMode)
341 //if (!updatePoints(20))
344 Handle(SelectMgr_EntityOwner) aOwn = new SelectMgr_EntityOwner(this);
345 for (int i = 1; i <= myPntArray->VertexNumber(); i++) {
346 Handle(Select3D_SensitivePoint) aSP = new Select3D_SensitivePoint(aOwn, myPntArray->Vertice(i));
347 aSelection->Add(aSP);
352 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
354 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
355 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
356 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
358 double aScale = aView->Camera()->Scale();
359 if (!updatePoints(MyDist * aScale))
362 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
364 if (myVboAttribs.IsNull()) {
365 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
368 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
369 myVboAttribs->Release (aCtx.operator->());
370 myVboAttribs.Nullify();
374 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
376 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
378 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
379 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
380 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
381 ? anAspectMarker->SpriteHighlightRes(aCtx)
383 theWorkspace->EnableTexture (aSprite);
384 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
385 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
386 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
387 aLineColor = theWorkspace->HighlightColor;
389 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
392 myVboAttribs->BindAllAttributes(aCtx);
393 // Textured markers will be drawn with the point sprites
394 aCtx->SetPointSize (anAspectMarker->MarkerSize());
395 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
396 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
398 aCtx->core11fwd->glEnable (GL_BLEND);
399 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
401 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
403 aCtx->core11fwd->glDisable (GL_BLEND);
404 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
405 aCtx->SetPointSize (1.0f);
407 theWorkspace->EnableTexture (aTextureBack);
408 aCtx->BindProgram (NULL);
412 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
414 if (!myVboAttribs.IsNull()) {
416 theContext->DelayedRelease (myVboAttribs);
418 myVboAttribs.Nullify();