1 // Created on: 2011-07-13
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2013 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_AspectFace.hxx>
17 #include <OpenGl_Context.hxx>
18 #include <OpenGl_GraphicDriver.hxx>
19 #include <OpenGl_IndexBuffer.hxx>
20 #include <OpenGl_PointSprite.hxx>
21 #include <OpenGl_PrimitiveArray.hxx>
22 #include <OpenGl_ShaderManager.hxx>
23 #include <OpenGl_ShaderProgram.hxx>
24 #include <OpenGl_Structure.hxx>
25 #include <OpenGl_VertexBufferCompat.hxx>
26 #include <OpenGl_Workspace.hxx>
27 #include <Graphic3d_TextureParams.hxx>
28 #include <NCollection_AlignedAllocator.hxx>
32 //! Convert data type to GL info
33 inline GLenum toGlDataType (const Graphic3d_TypeOfData theType,
38 case Graphic3d_TOD_USHORT:
40 return GL_UNSIGNED_SHORT;
41 case Graphic3d_TOD_UINT:
43 return GL_UNSIGNED_INT;
44 case Graphic3d_TOD_VEC2:
47 case Graphic3d_TOD_VEC3:
50 case Graphic3d_TOD_VEC4:
53 case Graphic3d_TOD_VEC4UB:
55 return GL_UNSIGNED_BYTE;
56 case Graphic3d_TOD_FLOAT:
66 //! Auxiliary template for VBO with interleaved attributes.
67 template<class TheBaseClass, int NbAttributes>
68 class OpenGl_VertexBufferT : public TheBaseClass
73 //! Create uninitialized VBO.
74 OpenGl_VertexBufferT (const Graphic3d_Attribute* theAttribs,
75 const Standard_Integer theStride)
78 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
81 //! Create uninitialized VBO.
82 OpenGl_VertexBufferT (const Graphic3d_Buffer& theAttribs)
83 : Stride (theAttribs.Stride)
85 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
88 virtual bool HasColorAttribute() const
90 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
92 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
93 if (anAttrib.Id == Graphic3d_TOA_COLOR)
101 virtual bool HasNormalAttribute() const
103 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
105 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
106 if (anAttrib.Id == Graphic3d_TOA_NORM)
114 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
116 if (!TheBaseClass::IsValid())
121 TheBaseClass::Bind (theGlCtx);
123 const GLubyte* anOffset = TheBaseClass::myOffset;
124 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)
132 else if (anAttrib.Id == Graphic3d_TOA_POS)
134 TheBaseClass::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
138 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
142 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
144 if (!TheBaseClass::IsValid())
149 TheBaseClass::Bind (theGlCtx);
151 const GLubyte* anOffset = TheBaseClass::myOffset;
152 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
154 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
155 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
156 if (aDataType == GL_NONE)
161 TheBaseClass::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 (!TheBaseClass::IsValid())
172 TheBaseClass::Unbind (theGlCtx);
174 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
176 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
177 TheBaseClass::unbindAttribute (theGlCtx, anAttrib.Id);
183 Graphic3d_Attribute Attribs[NbAttributes];
184 Standard_Integer Stride;
188 // =======================================================================
189 // function : clearMemoryGL
191 // =======================================================================
192 void OpenGl_PrimitiveArray::clearMemoryGL (const Handle(OpenGl_Context)& theGlCtx) const
194 if (!myVboIndices.IsNull())
196 myVboIndices->Release (theGlCtx.operator->());
197 myVboIndices.Nullify();
199 if (!myVboAttribs.IsNull())
201 myVboAttribs->Release (theGlCtx.operator->());
202 myVboAttribs.Nullify();
206 // =======================================================================
207 // function : initNormalVbo
209 // =======================================================================
210 Standard_Boolean OpenGl_PrimitiveArray::initNormalVbo (const Handle(OpenGl_Context)& theCtx) const
212 switch (myAttribs->NbAttributes)
214 case 1: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 1> (*myAttribs); break;
215 case 2: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 2> (*myAttribs); break;
216 case 3: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 3> (*myAttribs); break;
217 case 4: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 4> (*myAttribs); break;
218 case 5: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 5> (*myAttribs); break;
219 case 6: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 6> (*myAttribs); break;
220 case 7: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 7> (*myAttribs); break;
221 case 8: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 8> (*myAttribs); break;
222 case 9: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 9> (*myAttribs); break;
223 case 10: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 10>(*myAttribs); break;
226 if (!myVboAttribs->init (theCtx, 0, myAttribs->NbElements, myAttribs->Data(), GL_NONE, myAttribs->Stride))
228 TCollection_ExtendedString aMsg;
229 aMsg += "VBO creation for Primitive Array has failed for ";
230 aMsg += myAttribs->NbElements;
231 aMsg += " vertices. Out of memory?";
232 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
234 clearMemoryGL (theCtx);
235 return Standard_False;
237 else if (myIndices.IsNull())
239 return Standard_True;
242 myVboIndices = new OpenGl_IndexBuffer();
244 switch (myIndices->Stride)
248 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLushort*> (myIndices->Data()));
253 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLuint*> (myIndices->Data()));
258 clearMemoryGL (theCtx);
259 return Standard_False;
264 TCollection_ExtendedString aMsg;
265 aMsg += "VBO creation for Primitive Array has failed for ";
266 aMsg += myIndices->NbElements;
267 aMsg += " indices. Out of memory?";
268 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
269 clearMemoryGL (theCtx);
270 return Standard_False;
272 return Standard_True;
275 // =======================================================================
276 // function : buildVBO
278 // =======================================================================
279 Standard_Boolean OpenGl_PrimitiveArray::buildVBO (const Handle(OpenGl_Context)& theCtx,
280 const Standard_Boolean theToKeepData) const
282 bool isNormalMode = theCtx->ToUseVbo();
283 clearMemoryGL (theCtx);
284 if (myAttribs.IsNull()
285 || myAttribs->IsEmpty()
286 || myAttribs->NbElements < 1
287 || myAttribs->NbAttributes < 1
288 || myAttribs->NbAttributes > 10)
290 // vertices should be always defined - others are optional
291 return Standard_False;
295 && initNormalVbo (theCtx))
297 if (!theCtx->caps->keepArrayData
303 return Standard_True;
306 Handle(OpenGl_VertexBufferCompat) aVboAttribs;
307 switch (myAttribs->NbAttributes)
309 case 1: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 1> (*myAttribs); break;
310 case 2: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 2> (*myAttribs); break;
311 case 3: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 3> (*myAttribs); break;
312 case 4: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 4> (*myAttribs); break;
313 case 5: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 5> (*myAttribs); break;
314 case 6: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 6> (*myAttribs); break;
315 case 7: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 7> (*myAttribs); break;
316 case 8: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 8> (*myAttribs); break;
317 case 9: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 9> (*myAttribs); break;
318 case 10: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 10>(*myAttribs); break;
320 aVboAttribs->initLink (myAttribs, 0, myAttribs->NbElements, GL_NONE);
321 if (!myIndices.IsNull())
323 Handle(OpenGl_VertexBufferCompat) aVboIndices = new OpenGl_VertexBufferCompat();
324 switch (myIndices->Stride)
328 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_SHORT);
333 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_INT);
338 return Standard_False;
341 myVboIndices = aVboIndices;
343 myVboAttribs = aVboAttribs;
344 if (!theCtx->caps->keepArrayData
347 // does not make sense for compatibility mode
348 //myIndices.Nullify();
349 //myAttribs.Nullify();
352 return Standard_True;
355 // =======================================================================
356 // function : drawArray
358 // =======================================================================
359 void OpenGl_PrimitiveArray::drawArray (const Handle(OpenGl_Workspace)& theWorkspace,
360 const Graphic3d_Vec4* theFaceColors,
361 const Standard_Boolean theHasVertColor) const
363 if (myVboAttribs.IsNull())
365 #if !defined(GL_ES_VERSION_2_0)
366 if (myDrawMode == GL_POINTS)
368 // extreme compatibility mode - without sprites but with markers
369 drawMarkers (theWorkspace);
375 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
376 const bool toHilight = theWorkspace->ToHighlight();
377 myVboAttribs->BindAllAttributes (aGlContext);
378 if (theHasVertColor && toHilight)
380 // disable per-vertex color
381 OpenGl_VertexBuffer::unbindAttribute (aGlContext, Graphic3d_TOA_COLOR);
383 if (!myVboIndices.IsNull())
385 myVboIndices->Bind (aGlContext);
386 GLubyte* anOffset = myVboIndices->GetDataOffset();
387 if (!myBounds.IsNull())
389 // draw primitives by vertex count with the indices
390 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
391 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
393 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
394 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
395 glDrawElements (myDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
396 anOffset += aStride * aNbElemsInGroup;
401 // draw one (or sequential) primitive by the indices
402 glDrawElements (myDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
404 myVboIndices->Unbind (aGlContext);
406 else if (!myBounds.IsNull())
408 GLint aFirstElem = 0;
409 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
411 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
412 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
413 glDrawArrays (myDrawMode, aFirstElem, aNbElemsInGroup);
414 aFirstElem += aNbElemsInGroup;
419 if (myDrawMode == GL_POINTS)
421 drawMarkers (theWorkspace);
425 glDrawArrays (myDrawMode, 0, myVboAttribs->GetElemsNb());
430 myVboAttribs->UnbindAllAttributes (aGlContext);
433 // =======================================================================
434 // function : drawEdges
436 // =======================================================================
437 void OpenGl_PrimitiveArray::drawEdges (const OpenGl_Vec4& theEdgeColour,
438 const Handle(OpenGl_Workspace)& theWorkspace) const
440 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
441 if (myVboAttribs.IsNull())
446 #if !defined(GL_ES_VERSION_2_0)
447 if (aGlContext->core11 != NULL)
449 glDisable (GL_LIGHTING);
453 const OpenGl_AspectLine* anAspectLineOld = theWorkspace->SetAspectLine (theWorkspace->AspectFace()->AspectEdge());
454 const OpenGl_AspectLine* anAspect = theWorkspace->ApplyAspectLine();
456 #if !defined(GL_ES_VERSION_2_0)
457 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
460 if (aGlContext->core20fwd != NULL)
462 aGlContext->ShaderManager()->BindLineProgram (NULL,
463 anAspect->Aspect()->Type() != Aspect_TOL_SOLID,
466 anAspect->ShaderProgramRes (aGlContext));
469 /// OCC22236 NOTE: draw edges for all situations:
470 /// 1) draw elements with GL_LINE style as edges from myPArray->bufferVBO[VBOEdges] indices array
471 /// 2) draw elements from vertex array, when bounds defines count of primitive's vertices.
472 /// 3) draw primitive's edges by vertexes if no edges and bounds array is specified
473 myVboAttribs->BindPositionAttribute (aGlContext);
475 aGlContext->SetColor4fv (theEdgeColour);
476 aGlContext->SetTypeOfLine (anAspect->Aspect()->Type());
477 aGlContext->SetLineWidth (anAspect->Aspect()->Width());
479 if (!myVboIndices.IsNull())
481 myVboIndices->Bind (aGlContext);
482 GLubyte* anOffset = myVboIndices->GetDataOffset();
484 // draw primitives by vertex count with the indices
485 if (!myBounds.IsNull())
487 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
488 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
490 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
491 glDrawElements (myDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
492 anOffset += aStride * aNbElemsInGroup;
495 // draw one (or sequential) primitive by the indices
498 glDrawElements (myDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
500 myVboIndices->Unbind (aGlContext);
502 else if (!myBounds.IsNull())
504 GLint aFirstElem = 0;
505 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
507 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
508 glDrawArrays (myDrawMode, aFirstElem, aNbElemsInGroup);
509 aFirstElem += aNbElemsInGroup;
514 glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
518 myVboAttribs->UnbindAttribute (aGlContext, Graphic3d_TOA_POS);
520 // restore line context
521 theWorkspace->SetAspectLine (anAspectLineOld);
524 // =======================================================================
525 // function : drawMarkers
527 // =======================================================================
528 void OpenGl_PrimitiveArray::drawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const
530 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->ApplyAspectMarker();
531 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
532 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (aCtx);
533 if (!aSpriteNorm.IsNull()
534 && !aSpriteNorm->IsDisplayList())
536 // Textured markers will be drawn with the point sprites
537 aCtx->SetPointSize (anAspectMarker->MarkerSize());
538 aCtx->SetPointSpriteOrigin();
539 #if !defined(GL_ES_VERSION_2_0)
540 if (aCtx->core11 != NULL)
542 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
543 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
547 aCtx->core11fwd->glEnable (GL_BLEND);
548 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
550 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
552 aCtx->core11fwd->glDisable (GL_BLEND);
553 #if !defined(GL_ES_VERSION_2_0)
554 if (aCtx->core11 != NULL)
556 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
559 aCtx->SetPointSize (1.0f);
562 else if (anAspectMarker->Aspect()->Type() == Aspect_TOM_POINT)
564 aCtx->SetPointSize (anAspectMarker->MarkerSize());
565 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
566 aCtx->SetPointSize (1.0f);
568 #if !defined(GL_ES_VERSION_2_0)
569 // Textured markers will be drawn with the glBitmap
570 else if (anAspectMarker->Aspect()->Type() != Aspect_TOM_POINT
571 && !aSpriteNorm.IsNull())
573 /**if (!isHilight && (myPArray->vcolours != NULL))
575 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
577 glColor4ubv (myPArray->vcolours[anIter].GetData());
578 glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
579 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
584 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
586 aCtx->core11->glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
587 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
594 // =======================================================================
595 // function : OpenGl_PrimitiveArray
597 // =======================================================================
598 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver)
600 : myDrawMode (DRAW_MODE_NONE),
601 myIsVboInit (Standard_False)
603 if (theDriver != NULL)
605 myUID = theDriver->GetNextPrimitiveArrayUID();
609 // =======================================================================
610 // function : OpenGl_PrimitiveArray
612 // =======================================================================
613 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver,
614 const Graphic3d_TypeOfPrimitiveArray theType,
615 const Handle(Graphic3d_IndexBuffer)& theIndices,
616 const Handle(Graphic3d_Buffer)& theAttribs,
617 const Handle(Graphic3d_BoundBuffer)& theBounds)
619 : myIndices (theIndices),
620 myAttribs (theAttribs),
621 myBounds (theBounds),
622 myDrawMode (DRAW_MODE_NONE),
623 myIsVboInit (Standard_False)
625 if (!myIndices.IsNull()
626 && myIndices->NbElements < 1)
628 // dummy index buffer?
632 if (theDriver != NULL)
634 myUID = theDriver->GetNextPrimitiveArrayUID();
635 #if defined (GL_ES_VERSION_2_0)
636 const Handle(OpenGl_Context)& aCtx = theDriver->GetSharedContext();
639 processIndices (aCtx);
644 setDrawMode (theType);
647 // =======================================================================
648 // function : ~OpenGl_PrimitiveArray
650 // =======================================================================
651 OpenGl_PrimitiveArray::~OpenGl_PrimitiveArray()
656 // =======================================================================
657 // function : Release
659 // =======================================================================
660 void OpenGl_PrimitiveArray::Release (OpenGl_Context* theContext)
662 myIsVboInit = Standard_False;
663 if (!myVboIndices.IsNull())
667 theContext->DelayedRelease (myVboIndices);
669 myVboIndices.Nullify();
671 if (!myVboAttribs.IsNull())
675 theContext->DelayedRelease (myVboAttribs);
677 myVboAttribs.Nullify();
681 // =======================================================================
684 // =======================================================================
685 void OpenGl_PrimitiveArray::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
687 if (myDrawMode == DRAW_MODE_NONE)
692 const OpenGl_AspectFace* anAspectFace = theWorkspace->ApplyAspectFace();
693 const OpenGl_AspectLine* anAspectLine = theWorkspace->ApplyAspectLine();
694 const OpenGl_AspectMarker* anAspectMarker = myDrawMode == GL_POINTS
695 ? theWorkspace->ApplyAspectMarker()
696 : theWorkspace->AspectMarker();
698 // create VBOs on first render call
699 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
702 // compatibility - keep data to draw markers using display lists
703 const Standard_Boolean toKeepData = myDrawMode == GL_POINTS
704 && !anAspectMarker->SpriteRes (aCtx).IsNull()
705 && anAspectMarker->SpriteRes (aCtx)->IsDisplayList();
706 #if defined (GL_ES_VERSION_2_0)
707 processIndices (aCtx);
709 buildVBO (aCtx, toKeepData);
710 myIsVboInit = Standard_True;
713 const Standard_Boolean hasColorAttrib = !myVboAttribs.IsNull()
714 && myVboAttribs->HasColorAttribute();
715 const Standard_Boolean isLightOn = !anAspectFace->IsNoLighting()
716 && !myVboAttribs.IsNull()
717 && myVboAttribs->HasNormalAttribute();
719 // Temporarily disable environment mapping
720 Handle(OpenGl_Texture) aTextureBack;
721 bool toDrawArray = true;
722 if (myDrawMode > GL_LINE_STRIP)
724 toDrawArray = anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_EMPTY;
726 else if (myDrawMode <= GL_LINE_STRIP)
728 aTextureBack = theWorkspace->DisableTexture();
729 if (myDrawMode == GL_POINTS)
731 toDrawArray = anAspectMarker->Aspect()->Type() != Aspect_TOM_EMPTY;
735 toDrawArray = anAspectLine->Aspect()->Type() != Aspect_TOL_EMPTY;
741 const bool toHilight = theWorkspace->ToHighlight();
742 const Standard_Boolean hasVertColor = hasColorAttrib && !toHilight;
747 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (aCtx);
748 if (!aSpriteNorm.IsNull()
749 && !aSpriteNorm->IsDisplayList())
751 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes (aCtx)->IsValid())
752 ? anAspectMarker->SpriteHighlightRes (aCtx)
754 theWorkspace->EnableTexture (aSprite);
755 aCtx->ShaderManager()->BindMarkerProgram (aSprite, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
759 aCtx->ShaderManager()->BindMarkerProgram (NULL, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
766 aCtx->ShaderManager()->BindLineProgram (NULL,
767 anAspectLine->Aspect()->Type() != Aspect_TOL_SOLID,
770 anAspectLine->ShaderProgramRes (aCtx));
775 const Handle(OpenGl_Texture)& aTexture = theWorkspace->ActiveTexture();
776 const Standard_Boolean isLightOnFace = isLightOn
777 && (aTexture.IsNull()
778 || aTexture->GetParams()->IsModulate());
779 const Standard_Boolean toEnableEnvMap = (!aTexture.IsNull() && (aTexture == theWorkspace->EnvironmentTexture()));
780 aCtx->ShaderManager()->BindFaceProgram (aTexture,
784 anAspectFace->ShaderProgramRes (aCtx));
789 #if !defined(GL_ES_VERSION_2_0)
790 // manage FFP lighting
791 if (aCtx->ActiveProgram().IsNull()
792 && aCtx->core11 != NULL)
796 glDisable (GL_LIGHTING);
800 glEnable (GL_LIGHTING);
805 if (!theWorkspace->ActiveTexture().IsNull()
806 && myDrawMode != GL_POINTS) // transformation is not supported within point sprites
808 aCtx->SetTextureMatrix (theWorkspace->ActiveTexture()->GetParams());
811 if (myDrawMode <= GL_LINE_STRIP)
813 const OpenGl_Vec4& aLineColor = myDrawMode == GL_POINTS ? theWorkspace->MarkerColor() : theWorkspace->LineColor();
814 aCtx->SetColor4fv (aLineColor);
818 const OpenGl_Vec4& anInteriorColor = theWorkspace->InteriorColor();
819 aCtx->SetColor4fv (anInteriorColor);
821 if (myDrawMode == GL_LINES
822 || myDrawMode == GL_LINE_STRIP)
824 aCtx->SetTypeOfLine (anAspectLine->Aspect()->Type());
825 aCtx->SetLineWidth (anAspectLine->Aspect()->Width());
828 const Graphic3d_Vec4* aFaceColors = !myBounds.IsNull() && !toHilight && anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_HIDDENLINE
832 const Standard_Boolean isHighlightWithTransparency = toHilight &&
833 myDrawMode > GL_LINE_STRIP &&
834 theWorkspace->InteriorColor().a() > 0.05f;
835 GLint aPrevBlendSrc = GL_SRC_ALPHA, aPrevBlendDst = GL_ONE_MINUS_SRC_ALPHA;
836 GLboolean wasBlendEnabled = GL_FALSE;
837 if (isHighlightWithTransparency)
839 wasBlendEnabled = glIsEnabled (GL_BLEND);
840 #if !defined(GL_ES_VERSION_2_0)
841 glGetIntegerv (GL_BLEND_SRC, &aPrevBlendSrc);
842 glGetIntegerv (GL_BLEND_DST, &aPrevBlendDst);
844 if (!wasBlendEnabled)
848 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
851 drawArray (theWorkspace, aFaceColors, hasColorAttrib);
853 if (isHighlightWithTransparency)
855 glBlendFunc (aPrevBlendSrc, aPrevBlendDst);
856 if (!wasBlendEnabled)
858 glDisable (GL_BLEND);
863 if (myDrawMode <= GL_LINE_STRIP)
865 theWorkspace->EnableTexture (aTextureBack);
869 if (anAspectFace->Aspect()->ToDrawEdges()
870 || anAspectFace->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
872 const OpenGl_Vec4& anEdgeColor = theWorkspace->EdgeColor();
873 drawEdges (anEdgeColor, theWorkspace);
875 // restore OpenGL polygon mode if needed
876 #if !defined(GL_ES_VERSION_2_0)
877 if (anAspectFace->Aspect()->InteriorStyle() >= Aspect_IS_HATCH)
879 glPolygonMode (GL_FRONT_AND_BACK,
880 anAspectFace->Aspect()->InteriorStyle() == Aspect_IS_POINT ? GL_POINT : GL_FILL);
887 // =======================================================================
888 // function : setDrawMode
890 // =======================================================================
891 void OpenGl_PrimitiveArray::setDrawMode (const Graphic3d_TypeOfPrimitiveArray theType)
893 if (myAttribs.IsNull())
895 myDrawMode = DRAW_MODE_NONE;
901 case Graphic3d_TOPA_POINTS:
902 myDrawMode = GL_POINTS;
904 case Graphic3d_TOPA_POLYLINES:
905 myDrawMode = GL_LINE_STRIP;
907 case Graphic3d_TOPA_SEGMENTS:
908 myDrawMode = GL_LINES;
910 case Graphic3d_TOPA_TRIANGLES:
911 myDrawMode = GL_TRIANGLES;
913 case Graphic3d_TOPA_TRIANGLESTRIPS:
914 myDrawMode = GL_TRIANGLE_STRIP;
916 case Graphic3d_TOPA_TRIANGLEFANS:
917 myDrawMode = GL_TRIANGLE_FAN;
919 #if !defined(GL_ES_VERSION_2_0)
920 case Graphic3d_TOPA_POLYGONS:
921 myDrawMode = GL_POLYGON;
923 case Graphic3d_TOPA_QUADRANGLES:
924 myDrawMode = GL_QUADS;
926 case Graphic3d_TOPA_QUADRANGLESTRIPS:
927 myDrawMode = GL_QUAD_STRIP;
930 case Graphic3d_TOPA_POLYGONS:
931 case Graphic3d_TOPA_QUADRANGLES:
932 case Graphic3d_TOPA_QUADRANGLESTRIPS:
934 case Graphic3d_TOPA_UNDEFINED:
939 // =======================================================================
940 // function : processIndices
942 // =======================================================================
943 Standard_Boolean OpenGl_PrimitiveArray::processIndices (const Handle(OpenGl_Context)& theContext) const
945 if (myIndices.IsNull()
946 || myAttribs.IsNull()
947 || theContext->hasUintIndex)
949 return Standard_True;
952 if (myAttribs->NbElements > std::numeric_limits<GLushort>::max())
954 Handle(Graphic3d_Buffer) anAttribs = new Graphic3d_Buffer (new NCollection_AlignedAllocator (16));
955 if (!anAttribs->Init (myIndices->NbElements, myAttribs->AttributesArray(), myAttribs->NbAttributes))
957 return Standard_False; // failed to initialize attribute array
960 for (Standard_Integer anIdxIdx = 0; anIdxIdx < myIndices->NbElements; ++anIdxIdx)
962 const Standard_Integer anIndex = myIndices->Index (anIdxIdx);
963 memcpy (anAttribs->ChangeData() + myAttribs->Stride * anIdxIdx,
964 myAttribs->Data() + myAttribs->Stride * anIndex,
969 myAttribs = anAttribs;
972 return Standard_True;
975 // =======================================================================
976 // function : InitBuffers
978 // =======================================================================
979 void OpenGl_PrimitiveArray::InitBuffers (const Handle(OpenGl_Context)& theContext,
980 const Graphic3d_TypeOfPrimitiveArray theType,
981 const Handle(Graphic3d_IndexBuffer)& theIndices,
982 const Handle(Graphic3d_Buffer)& theAttribs,
983 const Handle(Graphic3d_BoundBuffer)& theBounds)
985 // Release old graphic resources
986 Release (theContext.operator->());
988 myIndices = theIndices;
989 myAttribs = theAttribs;
990 myBounds = theBounds;
991 #if defined(GL_ES_VERSION_2_0)
992 processIndices (theContext);
995 setDrawMode (theType);