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_Sampler.hxx>
23 #include <OpenGl_ShaderManager.hxx>
24 #include <OpenGl_ShaderProgram.hxx>
25 #include <OpenGl_Structure.hxx>
26 #include <OpenGl_VertexBufferCompat.hxx>
27 #include <OpenGl_Workspace.hxx>
28 #include <Graphic3d_TextureParams.hxx>
29 #include <NCollection_AlignedAllocator.hxx>
33 //! Convert data type to GL info
34 inline GLenum toGlDataType (const Graphic3d_TypeOfData theType,
39 case Graphic3d_TOD_USHORT:
41 return GL_UNSIGNED_SHORT;
42 case Graphic3d_TOD_UINT:
44 return GL_UNSIGNED_INT;
45 case Graphic3d_TOD_VEC2:
48 case Graphic3d_TOD_VEC3:
51 case Graphic3d_TOD_VEC4:
54 case Graphic3d_TOD_VEC4UB:
56 return GL_UNSIGNED_BYTE;
57 case Graphic3d_TOD_FLOAT:
67 //! Auxiliary template for VBO with interleaved attributes.
68 template<class TheBaseClass, int NbAttributes>
69 class OpenGl_VertexBufferT : public TheBaseClass
74 //! Create uninitialized VBO.
75 OpenGl_VertexBufferT (const Graphic3d_Attribute* theAttribs,
76 const Standard_Integer theStride)
79 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
82 //! Create uninitialized VBO.
83 OpenGl_VertexBufferT (const Graphic3d_Buffer& theAttribs)
84 : Stride (theAttribs.Stride)
86 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
89 virtual bool HasColorAttribute() const
91 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
93 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
94 if (anAttrib.Id == Graphic3d_TOA_COLOR)
102 virtual bool HasNormalAttribute() const
104 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
106 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
107 if (anAttrib.Id == Graphic3d_TOA_NORM)
115 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
117 if (!TheBaseClass::IsValid())
122 TheBaseClass::Bind (theGlCtx);
124 const GLubyte* anOffset = TheBaseClass::myOffset;
125 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
127 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
128 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
129 if (aDataType == GL_NONE)
133 else if (anAttrib.Id == Graphic3d_TOA_POS)
135 TheBaseClass::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
139 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
143 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
145 if (!TheBaseClass::IsValid())
150 TheBaseClass::Bind (theGlCtx);
152 const GLubyte* anOffset = TheBaseClass::myOffset;
153 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
155 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
156 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
157 if (aDataType == GL_NONE)
162 TheBaseClass::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
163 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
167 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
169 if (!TheBaseClass::IsValid())
173 TheBaseClass::Unbind (theGlCtx);
175 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
177 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
178 TheBaseClass::unbindAttribute (theGlCtx, anAttrib.Id);
184 Graphic3d_Attribute Attribs[NbAttributes];
185 Standard_Integer Stride;
189 // =======================================================================
190 // function : clearMemoryGL
192 // =======================================================================
193 void OpenGl_PrimitiveArray::clearMemoryGL (const Handle(OpenGl_Context)& theGlCtx) const
195 if (!myVboIndices.IsNull())
197 myVboIndices->Release (theGlCtx.operator->());
198 myVboIndices.Nullify();
200 if (!myVboAttribs.IsNull())
202 myVboAttribs->Release (theGlCtx.operator->());
203 myVboAttribs.Nullify();
207 // =======================================================================
208 // function : initNormalVbo
210 // =======================================================================
211 Standard_Boolean OpenGl_PrimitiveArray::initNormalVbo (const Handle(OpenGl_Context)& theCtx) const
213 switch (myAttribs->NbAttributes)
215 case 1: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 1> (*myAttribs); break;
216 case 2: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 2> (*myAttribs); break;
217 case 3: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 3> (*myAttribs); break;
218 case 4: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 4> (*myAttribs); break;
219 case 5: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 5> (*myAttribs); break;
220 case 6: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 6> (*myAttribs); break;
221 case 7: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 7> (*myAttribs); break;
222 case 8: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 8> (*myAttribs); break;
223 case 9: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 9> (*myAttribs); break;
224 case 10: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 10>(*myAttribs); break;
227 // specify data type as Byte and NbComponents as Stride, so that OpenGl_VertexBuffer::EstimatedDataSize() will return correct value
228 if (!myVboAttribs->init (theCtx, myAttribs->Stride, myAttribs->NbElements, myAttribs->Data(), GL_UNSIGNED_BYTE, myAttribs->Stride))
230 TCollection_ExtendedString aMsg;
231 aMsg += "VBO creation for Primitive Array has failed for ";
232 aMsg += myAttribs->NbElements;
233 aMsg += " vertices. Out of memory?";
234 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
236 clearMemoryGL (theCtx);
237 return Standard_False;
239 else if (myIndices.IsNull())
241 return Standard_True;
244 myVboIndices = new OpenGl_IndexBuffer();
246 switch (myIndices->Stride)
250 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLushort*> (myIndices->Data()));
255 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLuint*> (myIndices->Data()));
260 clearMemoryGL (theCtx);
261 return Standard_False;
266 TCollection_ExtendedString aMsg;
267 aMsg += "VBO creation for Primitive Array has failed for ";
268 aMsg += myIndices->NbElements;
269 aMsg += " indices. Out of memory?";
270 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
271 clearMemoryGL (theCtx);
272 return Standard_False;
274 return Standard_True;
277 // =======================================================================
278 // function : buildVBO
280 // =======================================================================
281 Standard_Boolean OpenGl_PrimitiveArray::buildVBO (const Handle(OpenGl_Context)& theCtx,
282 const Standard_Boolean theToKeepData) const
284 bool isNormalMode = theCtx->ToUseVbo();
285 clearMemoryGL (theCtx);
286 if (myAttribs.IsNull()
287 || myAttribs->IsEmpty()
288 || myAttribs->NbElements < 1
289 || myAttribs->NbAttributes < 1
290 || myAttribs->NbAttributes > 10)
292 // vertices should be always defined - others are optional
293 return Standard_False;
297 && initNormalVbo (theCtx))
299 if (!theCtx->caps->keepArrayData
305 return Standard_True;
308 Handle(OpenGl_VertexBufferCompat) aVboAttribs;
309 switch (myAttribs->NbAttributes)
311 case 1: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 1> (*myAttribs); break;
312 case 2: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 2> (*myAttribs); break;
313 case 3: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 3> (*myAttribs); break;
314 case 4: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 4> (*myAttribs); break;
315 case 5: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 5> (*myAttribs); break;
316 case 6: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 6> (*myAttribs); break;
317 case 7: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 7> (*myAttribs); break;
318 case 8: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 8> (*myAttribs); break;
319 case 9: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 9> (*myAttribs); break;
320 case 10: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 10>(*myAttribs); break;
322 aVboAttribs->initLink (myAttribs, 0, myAttribs->NbElements, GL_NONE);
323 if (!myIndices.IsNull())
325 Handle(OpenGl_VertexBufferCompat) aVboIndices = new OpenGl_VertexBufferCompat();
326 switch (myIndices->Stride)
330 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_SHORT);
335 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_INT);
340 return Standard_False;
343 myVboIndices = aVboIndices;
345 myVboAttribs = aVboAttribs;
346 if (!theCtx->caps->keepArrayData
349 // does not make sense for compatibility mode
350 //myIndices.Nullify();
351 //myAttribs.Nullify();
354 return Standard_True;
357 // =======================================================================
358 // function : drawArray
360 // =======================================================================
361 void OpenGl_PrimitiveArray::drawArray (const Handle(OpenGl_Workspace)& theWorkspace,
362 const Graphic3d_Vec4* theFaceColors,
363 const Standard_Boolean theHasVertColor) const
365 if (myVboAttribs.IsNull())
367 #if !defined(GL_ES_VERSION_2_0)
368 if (myDrawMode == GL_POINTS)
370 // extreme compatibility mode - without sprites but with markers
371 drawMarkers (theWorkspace);
377 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
378 const bool toHilight = theWorkspace->ToHighlight();
379 const GLenum aDrawMode = !aGlContext->ActiveProgram().IsNull()
380 && aGlContext->ActiveProgram()->HasTessellationStage()
383 myVboAttribs->BindAllAttributes (aGlContext);
384 if (theHasVertColor && toHilight)
386 // disable per-vertex color
387 OpenGl_VertexBuffer::unbindAttribute (aGlContext, Graphic3d_TOA_COLOR);
389 if (!myVboIndices.IsNull())
391 myVboIndices->Bind (aGlContext);
392 GLubyte* anOffset = myVboIndices->GetDataOffset();
393 if (!myBounds.IsNull())
395 // draw primitives by vertex count with the indices
396 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
397 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
399 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
400 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
401 glDrawElements (aDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
402 anOffset += aStride * aNbElemsInGroup;
407 // draw one (or sequential) primitive by the indices
408 glDrawElements (aDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
410 myVboIndices->Unbind (aGlContext);
412 else if (!myBounds.IsNull())
414 GLint aFirstElem = 0;
415 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
417 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
418 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
419 glDrawArrays (aDrawMode, aFirstElem, aNbElemsInGroup);
420 aFirstElem += aNbElemsInGroup;
425 if (myDrawMode == GL_POINTS)
427 drawMarkers (theWorkspace);
431 glDrawArrays (aDrawMode, 0, myVboAttribs->GetElemsNb());
436 myVboAttribs->UnbindAllAttributes (aGlContext);
439 // =======================================================================
440 // function : drawEdges
442 // =======================================================================
443 void OpenGl_PrimitiveArray::drawEdges (const OpenGl_Vec4& theEdgeColour,
444 const Handle(OpenGl_Workspace)& theWorkspace) const
446 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
447 if (myVboAttribs.IsNull())
452 const OpenGl_AspectLine* anAspectLineOld = theWorkspace->SetAspectLine (theWorkspace->AspectFace()->AspectEdge());
453 const OpenGl_AspectLine* anAspect = theWorkspace->ApplyAspectLine();
455 #if !defined(GL_ES_VERSION_2_0)
456 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
459 if (aGlContext->core20fwd != NULL)
461 aGlContext->ShaderManager()->BindLineProgram (NULL,
462 anAspect->Aspect()->Type() != Aspect_TOL_SOLID,
465 anAspect->ShaderProgramRes (aGlContext));
467 const GLenum aDrawMode = !aGlContext->ActiveProgram().IsNull()
468 && aGlContext->ActiveProgram()->HasTessellationStage()
471 #if !defined(GL_ES_VERSION_2_0)
472 if (aGlContext->ActiveProgram().IsNull()
473 && aGlContext->core11 != NULL)
475 glDisable (GL_LIGHTING);
479 /// OCC22236 NOTE: draw edges for all situations:
480 /// 1) draw elements with GL_LINE style as edges from myPArray->bufferVBO[VBOEdges] indices array
481 /// 2) draw elements from vertex array, when bounds defines count of primitive's vertices.
482 /// 3) draw primitive's edges by vertexes if no edges and bounds array is specified
483 myVboAttribs->BindPositionAttribute (aGlContext);
485 aGlContext->SetColor4fv (theEdgeColour);
486 aGlContext->SetTypeOfLine (anAspect->Aspect()->Type());
487 aGlContext->SetLineWidth (anAspect->Aspect()->Width());
489 if (!myVboIndices.IsNull())
491 myVboIndices->Bind (aGlContext);
492 GLubyte* anOffset = myVboIndices->GetDataOffset();
494 // draw primitives by vertex count with the indices
495 if (!myBounds.IsNull())
497 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
498 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
500 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
501 glDrawElements (aDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
502 anOffset += aStride * aNbElemsInGroup;
505 // draw one (or sequential) primitive by the indices
508 glDrawElements (aDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
510 myVboIndices->Unbind (aGlContext);
512 else if (!myBounds.IsNull())
514 GLint aFirstElem = 0;
515 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
517 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
518 glDrawArrays (aDrawMode, aFirstElem, aNbElemsInGroup);
519 aFirstElem += aNbElemsInGroup;
524 glDrawArrays (aDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
528 myVboAttribs->UnbindAttribute (aGlContext, Graphic3d_TOA_POS);
530 // restore line context
531 theWorkspace->SetAspectLine (anAspectLineOld);
534 // =======================================================================
535 // function : drawMarkers
537 // =======================================================================
538 void OpenGl_PrimitiveArray::drawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const
540 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->ApplyAspectMarker();
541 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
542 const GLenum aDrawMode = !aCtx->ActiveProgram().IsNull()
543 && aCtx->ActiveProgram()->HasTessellationStage()
547 const Handle(OpenGl_TextureSet)& aSpriteNormRes = anAspectMarker->SpriteRes (aCtx);
548 const OpenGl_PointSprite* aSpriteNorm = !aSpriteNormRes.IsNull() ? dynamic_cast<const OpenGl_PointSprite*> (aSpriteNormRes->First().get()) : NULL;
549 if (aSpriteNorm != NULL
550 && !aSpriteNorm->IsDisplayList())
552 // Textured markers will be drawn with the point sprites
553 aCtx->SetPointSize (anAspectMarker->MarkerSize());
554 aCtx->SetPointSpriteOrigin();
555 #if !defined(GL_ES_VERSION_2_0)
556 if (aCtx->core11 != NULL)
558 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
559 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
563 aCtx->core11fwd->glEnable (GL_BLEND);
564 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
566 aCtx->core11fwd->glDrawArrays (aDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
568 aCtx->core11fwd->glDisable (GL_BLEND);
569 #if !defined(GL_ES_VERSION_2_0)
570 if (aCtx->core11 != NULL)
572 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
575 aCtx->SetPointSize (1.0f);
578 else if (anAspectMarker->Aspect()->Type() == Aspect_TOM_POINT)
580 aCtx->SetPointSize (anAspectMarker->MarkerSize());
581 aCtx->core11fwd->glDrawArrays (aDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
582 aCtx->SetPointSize (1.0f);
584 #if !defined(GL_ES_VERSION_2_0)
585 // Textured markers will be drawn with the glBitmap
586 else if (anAspectMarker->Aspect()->Type() != Aspect_TOM_POINT
587 && aSpriteNorm != NULL)
589 /**if (!isHilight && (myPArray->vcolours != NULL))
591 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
593 glColor4ubv (myPArray->vcolours[anIter].GetData());
594 glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
595 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
600 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
602 aCtx->core11->glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
603 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
610 // =======================================================================
611 // function : OpenGl_PrimitiveArray
613 // =======================================================================
614 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver)
616 : myDrawMode (DRAW_MODE_NONE),
617 myIsFillType(Standard_False),
618 myIsVboInit (Standard_False)
620 if (theDriver != NULL)
622 myUID = theDriver->GetNextPrimitiveArrayUID();
626 // =======================================================================
627 // function : OpenGl_PrimitiveArray
629 // =======================================================================
630 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver,
631 const Graphic3d_TypeOfPrimitiveArray theType,
632 const Handle(Graphic3d_IndexBuffer)& theIndices,
633 const Handle(Graphic3d_Buffer)& theAttribs,
634 const Handle(Graphic3d_BoundBuffer)& theBounds)
636 : myIndices (theIndices),
637 myAttribs (theAttribs),
638 myBounds (theBounds),
639 myDrawMode (DRAW_MODE_NONE),
640 myIsFillType(Standard_False),
641 myIsVboInit (Standard_False)
643 if (!myIndices.IsNull()
644 && myIndices->NbElements < 1)
646 // dummy index buffer?
650 if (theDriver != NULL)
652 myUID = theDriver->GetNextPrimitiveArrayUID();
653 #if defined (GL_ES_VERSION_2_0)
654 const Handle(OpenGl_Context)& aCtx = theDriver->GetSharedContext();
657 processIndices (aCtx);
662 setDrawMode (theType);
665 // =======================================================================
666 // function : ~OpenGl_PrimitiveArray
668 // =======================================================================
669 OpenGl_PrimitiveArray::~OpenGl_PrimitiveArray()
674 // =======================================================================
675 // function : Release
677 // =======================================================================
678 void OpenGl_PrimitiveArray::Release (OpenGl_Context* theContext)
680 myIsVboInit = Standard_False;
681 if (!myVboIndices.IsNull())
685 theContext->DelayedRelease (myVboIndices);
687 myVboIndices.Nullify();
689 if (!myVboAttribs.IsNull())
693 theContext->DelayedRelease (myVboAttribs);
695 myVboAttribs.Nullify();
699 // =======================================================================
702 // =======================================================================
703 void OpenGl_PrimitiveArray::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
705 if (myDrawMode == DRAW_MODE_NONE)
710 const OpenGl_AspectFace* anAspectFace = theWorkspace->ApplyAspectFace();
711 const OpenGl_AspectLine* anAspectLine = theWorkspace->ApplyAspectLine();
712 const OpenGl_AspectMarker* anAspectMarker = myDrawMode == GL_POINTS
713 ? theWorkspace->ApplyAspectMarker()
714 : theWorkspace->AspectMarker();
716 // create VBOs on first render call
717 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
720 // compatibility - keep data to draw markers using display lists
721 Standard_Boolean toKeepData = Standard_False;
722 if (myDrawMode == GL_POINTS)
724 const Handle(OpenGl_TextureSet)& aSpriteNormRes = anAspectMarker->SpriteRes (aCtx);
725 const OpenGl_PointSprite* aSpriteNorm = !aSpriteNormRes.IsNull() ? dynamic_cast<const OpenGl_PointSprite*> (aSpriteNormRes->First().get()) : NULL;
726 toKeepData = aSpriteNorm != NULL
727 && aSpriteNorm->IsDisplayList();
729 #if defined (GL_ES_VERSION_2_0)
730 processIndices (aCtx);
732 buildVBO (aCtx, toKeepData);
733 myIsVboInit = Standard_True;
736 const Standard_Boolean hasColorAttrib = !myVboAttribs.IsNull()
737 && myVboAttribs->HasColorAttribute();
738 const Standard_Boolean isLightOn = !anAspectFace->IsNoLighting()
739 && !myVboAttribs.IsNull()
740 && myVboAttribs->HasNormalAttribute()
741 && aCtx->ColorMask();
743 // Temporarily disable environment mapping
744 Handle(OpenGl_TextureSet) aTextureBack;
745 bool toDrawArray = true;
746 if (myDrawMode > GL_LINE_STRIP)
748 toDrawArray = anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_EMPTY;
750 else if (myDrawMode <= GL_LINE_STRIP)
752 aTextureBack = aCtx->BindTextures (Handle(OpenGl_TextureSet)());
753 if (myDrawMode == GL_POINTS)
755 toDrawArray = anAspectMarker->Aspect()->Type() != Aspect_TOM_EMPTY;
759 toDrawArray = anAspectLine->Aspect()->Type() != Aspect_TOL_EMPTY;
765 const bool toHilight = theWorkspace->ToHighlight();
766 const Standard_Boolean hasVertColor = hasColorAttrib && !toHilight;
771 const Handle(OpenGl_TextureSet)& aSpriteNormRes = anAspectMarker->SpriteRes (aCtx);
772 const OpenGl_PointSprite* aSpriteNorm = !aSpriteNormRes.IsNull() ? dynamic_cast<const OpenGl_PointSprite*> (aSpriteNormRes->First().get()) : NULL;
773 if (aSpriteNorm != NULL
774 && !aSpriteNorm->IsDisplayList())
776 const Handle(OpenGl_TextureSet)& aSprite = toHilight && anAspectMarker->SpriteHighlightRes (aCtx)->First()->IsValid()
777 ? anAspectMarker->SpriteHighlightRes (aCtx)
779 aCtx->BindTextures (aSprite);
780 aCtx->ShaderManager()->BindMarkerProgram (aSprite, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
784 aCtx->ShaderManager()->BindMarkerProgram (Handle(OpenGl_TextureSet)(), isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
791 aCtx->ShaderManager()->BindLineProgram (NULL,
792 anAspectLine->Aspect()->Type() != Aspect_TOL_SOLID,
795 anAspectLine->ShaderProgramRes (aCtx));
800 const Handle(OpenGl_TextureSet)& aTextures = aCtx->ActiveTextures();
801 const Standard_Boolean isLightOnFace = isLightOn
802 && (aTextures.IsNull()
803 || aTextures->IsEmpty()
804 || aTextures->First().IsNull()
805 || aTextures->First()->Sampler()->Parameters()->IsModulate());
806 const Standard_Boolean toEnableEnvMap = (!aTextures.IsNull() && (aTextures == theWorkspace->EnvironmentTexture()));
807 aCtx->ShaderManager()->BindFaceProgram (aTextures,
811 anAspectFace->ShaderProgramRes (aCtx));
816 #if !defined(GL_ES_VERSION_2_0)
817 // manage FFP lighting
818 if (aCtx->ActiveProgram().IsNull()
819 && aCtx->core11 != NULL)
823 glDisable (GL_LIGHTING);
827 glEnable (GL_LIGHTING);
832 if (!aCtx->ActiveTextures().IsNull()
833 && !aCtx->ActiveTextures()->IsEmpty()
834 && !aCtx->ActiveTextures()->First().IsNull()
835 && myDrawMode != GL_POINTS) // transformation is not supported within point sprites
837 aCtx->SetTextureMatrix (aCtx->ActiveTextures()->First()->Sampler()->Parameters());
840 if (myDrawMode <= GL_LINE_STRIP)
842 const OpenGl_Vec4& aLineColor = myDrawMode == GL_POINTS ? theWorkspace->MarkerColor() : theWorkspace->LineColor();
843 aCtx->SetColor4fv (aLineColor);
847 const OpenGl_Vec4& anInteriorColor = theWorkspace->InteriorColor();
848 aCtx->SetColor4fv (anInteriorColor);
850 if (myDrawMode == GL_LINES
851 || myDrawMode == GL_LINE_STRIP)
853 aCtx->SetTypeOfLine (anAspectLine->Aspect()->Type());
854 aCtx->SetLineWidth (anAspectLine->Aspect()->Width());
857 const Graphic3d_Vec4* aFaceColors = !myBounds.IsNull() && !toHilight && anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_HIDDENLINE
860 drawArray (theWorkspace, aFaceColors, hasColorAttrib);
863 if (myDrawMode <= GL_LINE_STRIP)
865 aCtx->BindTextures (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;
896 myIsFillType = false;
902 case Graphic3d_TOPA_POINTS:
903 myDrawMode = GL_POINTS;
904 myIsFillType = false;
906 case Graphic3d_TOPA_SEGMENTS:
907 myDrawMode = GL_LINES;
908 myIsFillType = false;
910 case Graphic3d_TOPA_POLYLINES:
911 myDrawMode = GL_LINE_STRIP;
912 myIsFillType = false;
914 case Graphic3d_TOPA_TRIANGLES:
915 myDrawMode = GL_TRIANGLES;
918 case Graphic3d_TOPA_TRIANGLESTRIPS:
919 myDrawMode = GL_TRIANGLE_STRIP;
922 case Graphic3d_TOPA_TRIANGLEFANS:
923 myDrawMode = GL_TRIANGLE_FAN;
927 case Graphic3d_TOPA_LINES_ADJACENCY:
928 myDrawMode = GL_LINES_ADJACENCY;
929 myIsFillType = false;
931 case Graphic3d_TOPA_LINE_STRIP_ADJACENCY:
932 myDrawMode = GL_LINE_STRIP_ADJACENCY;
933 myIsFillType = false;
935 case Graphic3d_TOPA_TRIANGLES_ADJACENCY:
936 myDrawMode = GL_TRIANGLES_ADJACENCY;
939 case Graphic3d_TOPA_TRIANGLE_STRIP_ADJACENCY:
940 myDrawMode = GL_TRIANGLE_STRIP_ADJACENCY;
944 #if !defined(GL_ES_VERSION_2_0)
945 case Graphic3d_TOPA_QUADRANGLES:
946 myDrawMode = GL_QUADS;
949 case Graphic3d_TOPA_QUADRANGLESTRIPS:
950 myDrawMode = GL_QUAD_STRIP;
953 case Graphic3d_TOPA_POLYGONS:
954 myDrawMode = GL_POLYGON;
958 case Graphic3d_TOPA_QUADRANGLES:
959 case Graphic3d_TOPA_QUADRANGLESTRIPS:
960 case Graphic3d_TOPA_POLYGONS:
962 case Graphic3d_TOPA_UNDEFINED:
963 myDrawMode = DRAW_MODE_NONE;
964 myIsFillType = false;
969 // =======================================================================
970 // function : processIndices
972 // =======================================================================
973 Standard_Boolean OpenGl_PrimitiveArray::processIndices (const Handle(OpenGl_Context)& theContext) const
975 if (myIndices.IsNull()
976 || myAttribs.IsNull()
977 || theContext->hasUintIndex)
979 return Standard_True;
982 if (myAttribs->NbElements > std::numeric_limits<GLushort>::max())
984 Handle(Graphic3d_Buffer) anAttribs = new Graphic3d_Buffer (new NCollection_AlignedAllocator (16));
985 if (!anAttribs->Init (myIndices->NbElements, myAttribs->AttributesArray(), myAttribs->NbAttributes))
987 return Standard_False; // failed to initialize attribute array
990 for (Standard_Integer anIdxIdx = 0; anIdxIdx < myIndices->NbElements; ++anIdxIdx)
992 const Standard_Integer anIndex = myIndices->Index (anIdxIdx);
993 memcpy (anAttribs->ChangeData() + myAttribs->Stride * anIdxIdx,
994 myAttribs->Data() + myAttribs->Stride * anIndex,
999 myAttribs = anAttribs;
1002 return Standard_True;
1005 // =======================================================================
1006 // function : InitBuffers
1008 // =======================================================================
1009 void OpenGl_PrimitiveArray::InitBuffers (const Handle(OpenGl_Context)& theContext,
1010 const Graphic3d_TypeOfPrimitiveArray theType,
1011 const Handle(Graphic3d_IndexBuffer)& theIndices,
1012 const Handle(Graphic3d_Buffer)& theAttribs,
1013 const Handle(Graphic3d_BoundBuffer)& theBounds)
1015 // Release old graphic resources
1016 Release (theContext.operator->());
1018 myIndices = theIndices;
1019 myAttribs = theAttribs;
1020 myBounds = theBounds;
1021 #if defined(GL_ES_VERSION_2_0)
1022 processIndices (theContext);
1025 setDrawMode (theType);