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 <Select3D_SensitivePoint.hxx>
17 #include <TopLoc_Location.hxx>
18 #include <AIS_InteractiveContext.hxx>
19 #include <V3d_Viewer.hxx>
20 #include <Prs3d_Root.hxx>
22 #include <OpenGl_Element.hxx>
23 #include <OpenGl_GraphicDriver.hxx>
24 #include <OpenGl_Context.hxx>
25 #include <OpenGl_View.hxx>
26 #include <OpenGl_PointSprite.hxx>
27 #include <OpenGl_VertexBuffer.hxx>
28 #include <OpenGl_ShaderManager.hxx>
29 #include <OpenGl_VertexBufferCompat.hxx>
30 #include <OpenGl_GraphicDriver.hxx>
38 /// Step between icons
39 static const double MyDist = 0.02;
41 /// Function to convert opengl data type
42 GLenum toGlDataType (const Graphic3d_TypeOfData theType, GLint& theNbComp)
45 case Graphic3d_TOD_USHORT:
47 return GL_UNSIGNED_SHORT;
48 case Graphic3d_TOD_UINT:
50 return GL_UNSIGNED_INT;
51 case Graphic3d_TOD_VEC2:
54 case Graphic3d_TOD_VEC3:
57 case Graphic3d_TOD_VEC4:
60 case Graphic3d_TOD_VEC4UB:
62 return GL_UNSIGNED_BYTE;
69 //*******************************************************************
70 //! Auxiliary class for Vertex buffer with interleaved attributes.
71 class SketcherPrs_VertexBuffer : public OpenGl_VertexBuffer
76 //! Create uninitialized VBO..
77 SketcherPrs_VertexBuffer (const Graphic3d_Attribute* theAttribs,
78 const Standard_Integer theStride)
79 : Stride (theStride), NbAttributes(1)
82 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
85 //! Create uninitialized VBO.
86 SketcherPrs_VertexBuffer (const Graphic3d_Buffer& theAttribs)
87 : Stride (theAttribs.Stride), NbAttributes(1)
89 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
92 virtual bool HasColorAttribute() const
94 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
95 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
96 if (anAttrib.Id == Graphic3d_TOA_COLOR) {
103 virtual bool HasNormalAttribute() const
105 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
106 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
107 if (anAttrib.Id == Graphic3d_TOA_NORM) {
114 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
116 if (!OpenGl_VertexBuffer::IsValid()) {
120 OpenGl_VertexBuffer::Bind (theGlCtx);
122 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
123 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
124 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
125 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
126 if (aDataType == GL_NONE) {
128 } else if (anAttrib.Id == Graphic3d_TOA_POS) {
129 OpenGl_VertexBuffer::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
133 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
137 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
139 if (!OpenGl_VertexBuffer::IsValid())
142 OpenGl_VertexBuffer::Bind (theGlCtx);
144 const GLubyte* anOffset = OpenGl_VertexBuffer::myOffset;
145 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
147 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
148 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
149 if (aDataType == GL_NONE)
152 OpenGl_VertexBuffer::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
153 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
157 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
159 if (!OpenGl_VertexBuffer::IsValid())
161 OpenGl_VertexBuffer::Unbind (theGlCtx);
163 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter) {
164 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
165 OpenGl_VertexBuffer::unbindAttribute (theGlCtx, anAttrib.Id);
171 Graphic3d_Attribute Attribs[1];
172 Standard_Integer Stride;
173 Standard_Integer NbAttributes;
176 //**************************************************************
177 //! Redefinition of OpenGl_Element
178 class SketcherPrs_Element: public OpenGl_Element
181 SketcherPrs_Element(const Handle(SketcherPrs_SymbolPrs)& theObj) :
182 OpenGl_Element(), myObj(theObj) {}
184 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
187 myObj->Render(theWorkspace);
190 virtual void Release (OpenGl_Context* theContext)
193 myObj->Release(theContext);
197 Handle(SketcherPrs_SymbolPrs) myObj;
201 //**************************************************************
202 //! Definition of call back
203 OpenGl_Element* SymbolPrsCallBack(const CALL_DEF_USERDRAW * theUserDraw)
205 Handle(SketcherPrs_SymbolPrs) anIObj = (SketcherPrs_SymbolPrs*)theUserDraw->Data;
206 if (anIObj.IsNull()) {
207 std::cout << "VUserDrawCallback error: null object passed, the custom scene element will not be rendered" << std::endl;
209 return new SketcherPrs_Element(anIObj);
213 //*****************************************************************************
214 IMPLEMENT_STANDARD_HANDLE(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
215 IMPLEMENT_STANDARD_RTTIEXT(SketcherPrs_SymbolPrs, AIS_InteractiveObject);
218 std::map<const char*, Handle(Image_AlienPixMap)> SketcherPrs_SymbolPrs::myIconsMap;
221 SketcherPrs_SymbolPrs::SketcherPrs_SymbolPrs(SketchPlugin_Constraint* theConstraint,
222 const std::shared_ptr<GeomAPI_Ax3>& thePlane)
223 : AIS_InteractiveObject(), myConstraint(theConstraint), myPlane(thePlane)
225 SetAutoHilight(Standard_False);
229 Handle(Image_AlienPixMap) SketcherPrs_SymbolPrs::icon()
231 if (myIconsMap.count(iconName()) == 1) {
232 return myIconsMap[iconName()];
234 TCollection_AsciiString aFile(getenv("NewGeomResources"));
237 Handle(Image_AlienPixMap) aPixMap = new Image_AlienPixMap();
238 if (aPixMap->Load(aFile)) {
239 myIconsMap[iconName()] = aPixMap;
242 return Handle(Image_AlienPixMap)();
245 void SketcherPrs_SymbolPrs::ClearSelected()
247 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );
248 if( !aSelectionPrs.IsNull() ) {
249 aSelectionPrs->Clear();
253 void SketcherPrs_SymbolPrs::prepareAspect()
255 if (myAspect.IsNull()) {
256 myAspect = new Graphic3d_AspectMarker3d(icon());
260 void SketcherPrs_SymbolPrs::addLine(const Handle(Graphic3d_Group)& theGroup, std::string theAttrName) const
262 ObjectPtr aObj = SketcherPrs_Tools::getResult(myConstraint, theAttrName);
263 std::shared_ptr<GeomAPI_Shape> aLine = SketcherPrs_Tools::getShape(aObj);
264 if (aLine.get() == NULL)
266 std::shared_ptr<GeomAPI_Edge> aEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(aLine));
268 std::shared_ptr<GeomAPI_Pnt> aPnt1 = aEdge->firstPoint();
269 std::shared_ptr<GeomAPI_Pnt> aPnt2 = aEdge->lastPoint();
271 Handle(Graphic3d_ArrayOfSegments) aLines = new Graphic3d_ArrayOfSegments(2, 1);
272 aLines->AddVertex(aPnt1->impl<gp_Pnt>());
273 aLines->AddVertex(aPnt2->impl<gp_Pnt>());
274 theGroup->AddPrimitiveArray(aLines);
277 void SketcherPrs_SymbolPrs::HilightSelected(const Handle(PrsMgr_PresentationManager3d)& thePM,
278 const SelectMgr_SequenceOfOwner& theOwners)
281 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( thePM );
282 aSelectionPrs->Clear();
283 drawLines(aSelectionPrs, Quantity_NOC_WHITE);
285 aSelectionPrs->SetDisplayPriority(9);
286 aSelectionPrs->Display();
287 thePM->Highlight(this);
290 void SketcherPrs_SymbolPrs::HilightOwnerWithColor(const Handle(PrsMgr_PresentationManager3d)& thePM,
291 const Quantity_NameOfColor theColor, const Handle(SelectMgr_EntityOwner)& theOwner)
293 thePM->Color(this, theColor);
295 Handle( Prs3d_Presentation ) aHilightPrs = GetHilightPresentation( thePM );
296 aHilightPrs->Clear();
297 drawLines(aHilightPrs, theColor);
299 if (thePM->IsImmediateModeOn())
300 thePM->AddToImmediateList(aHilightPrs);
303 void SketcherPrs_SymbolPrs::Compute(const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
304 const Handle(Prs3d_Presentation)& thePresentation,
305 const Standard_Integer theMode)
309 Handle(AIS_InteractiveContext) aCtx = GetContext();
310 Handle(OpenGl_GraphicDriver) aDriver = Handle(OpenGl_GraphicDriver)::DownCast(aCtx->CurrentViewer()->Driver());
311 if (!aDriver.IsNull()) {
312 // register the custom element factory function
313 aDriver->UserDrawCallback() = SymbolPrsCallBack;
316 if (!updatePoints(20))
319 int aNbVertex = myPntArray->VertexNumber();
320 if (myOwner.IsNull()) {
321 myOwner = new SelectMgr_EntityOwner(this);
322 for (int i = 1; i <= aNbVertex; i++) {
323 Handle(SketcherPrs_SensitivePoint) aSP = new SketcherPrs_SensitivePoint(myOwner, myPntArray, i);
324 mySPoints.Append(aSP);
328 Handle(Graphic3d_Group) aGroup = Prs3d_Root::NewGroup(thePresentation);
329 aGroup->SetPrimitivesAspect(myAspect);
331 Graphic3d_BndBox4f& aBnd = aGroup->ChangeBoundingBox();
334 for (int i = 1; i <= myPntArray->ItemNumber(); i++) {
335 aVert = myPntArray->Vertice(i);
336 aBnd.Add (Graphic3d_Vec4((float)aVert.X(), (float)aVert.Y(), (float)aVert.Z(), 1.0f));
339 aGroup->UserDraw(this, true);
340 //aGroup->AddPrimitiveArray(myPntArray);
345 void SketcherPrs_SymbolPrs::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
346 const Standard_Integer aMode)
349 for (int i = 1; i <= mySPoints.Length(); i++)
350 aSelection->Add(mySPoints.Value(i));
354 void SketcherPrs_SymbolPrs::Render(const Handle(OpenGl_Workspace)& theWorkspace) const
356 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker(Standard_True);
357 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
358 Handle(OpenGl_View) aView = theWorkspace->ActiveView();
360 double aScale = aView->Camera()->Scale();
361 if (!updatePoints(MyDist * aScale))
364 Handle(Graphic3d_Buffer) aAttribs = myPntArray->Attributes();
366 if (myVboAttribs.IsNull()) {
367 myVboAttribs = new SketcherPrs_VertexBuffer(*aAttribs);
370 if (!myVboAttribs->init(aCtx, 0, aAttribs->NbElements, aAttribs->Data(), GL_NONE, aAttribs->Stride)) {
371 myVboAttribs->Release (aCtx.operator->());
372 myVboAttribs.Nullify();
376 Handle(OpenGl_Texture) aTextureBack = theWorkspace->DisableTexture();
378 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes(aCtx);
380 if (!aSpriteNorm.IsNull() && !aSpriteNorm->IsDisplayList()) {
381 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
382 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes(aCtx)->IsValid())
383 ? anAspectMarker->SpriteHighlightRes(aCtx)
385 theWorkspace->EnableTexture (aSprite);
386 aCtx->ShaderManager()->BindProgram(anAspectMarker, aSprite, Standard_False, Standard_False, anAspectMarker->ShaderProgramRes(aCtx));
387 const TEL_COLOUR* aLineColor = &anAspectMarker->Color();
388 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
389 aLineColor = theWorkspace->HighlightColor;
391 aCtx->SetColor4fv(*(const OpenGl_Vec4* )(aLineColor->rgb));
394 myVboAttribs->BindAllAttributes(aCtx);
395 // Textured markers will be drawn with the point sprites
396 aCtx->SetPointSize (anAspectMarker->MarkerSize());
397 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
398 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
400 aCtx->core11fwd->glEnable (GL_BLEND);
401 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
403 aCtx->core11fwd->glDrawArrays (0, 0, myVboAttribs->GetElemsNb());
405 aCtx->core11fwd->glDisable (GL_BLEND);
406 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
407 aCtx->SetPointSize (1.0f);
409 theWorkspace->EnableTexture (aTextureBack);
410 aCtx->BindProgram (NULL);
412 // Update selection position
413 GetContext()->RecomputeSelectionOnly(this);
417 void SketcherPrs_SymbolPrs::Release (OpenGl_Context* theContext)
419 if (!myVboAttribs.IsNull()) {
421 theContext->DelayedRelease (myVboAttribs);
423 myVboAttribs.Nullify();