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 if (!myVboAttribs->init (theCtx, 0, myAttribs->NbElements, myAttribs->Data(), GL_NONE, myAttribs->Stride))
229 TCollection_ExtendedString aMsg;
230 aMsg += "VBO creation for Primitive Array has failed for ";
231 aMsg += myAttribs->NbElements;
232 aMsg += " vertices. Out of memory?";
233 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
235 clearMemoryGL (theCtx);
236 return Standard_False;
238 else if (myIndices.IsNull())
240 return Standard_True;
243 myVboIndices = new OpenGl_IndexBuffer();
245 switch (myIndices->Stride)
249 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLushort*> (myIndices->Data()));
254 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLuint*> (myIndices->Data()));
259 clearMemoryGL (theCtx);
260 return Standard_False;
265 TCollection_ExtendedString aMsg;
266 aMsg += "VBO creation for Primitive Array has failed for ";
267 aMsg += myIndices->NbElements;
268 aMsg += " indices. Out of memory?";
269 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, GL_DEBUG_TYPE_PERFORMANCE, 0, GL_DEBUG_SEVERITY_LOW, aMsg);
270 clearMemoryGL (theCtx);
271 return Standard_False;
273 return Standard_True;
276 // =======================================================================
277 // function : buildVBO
279 // =======================================================================
280 Standard_Boolean OpenGl_PrimitiveArray::buildVBO (const Handle(OpenGl_Context)& theCtx,
281 const Standard_Boolean theToKeepData) const
283 bool isNormalMode = theCtx->ToUseVbo();
284 clearMemoryGL (theCtx);
285 if (myAttribs.IsNull()
286 || myAttribs->IsEmpty()
287 || myAttribs->NbElements < 1
288 || myAttribs->NbAttributes < 1
289 || myAttribs->NbAttributes > 10)
291 // vertices should be always defined - others are optional
292 return Standard_False;
296 && initNormalVbo (theCtx))
298 if (!theCtx->caps->keepArrayData
304 return Standard_True;
307 Handle(OpenGl_VertexBufferCompat) aVboAttribs;
308 switch (myAttribs->NbAttributes)
310 case 1: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 1> (*myAttribs); break;
311 case 2: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 2> (*myAttribs); break;
312 case 3: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 3> (*myAttribs); break;
313 case 4: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 4> (*myAttribs); break;
314 case 5: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 5> (*myAttribs); break;
315 case 6: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 6> (*myAttribs); break;
316 case 7: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 7> (*myAttribs); break;
317 case 8: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 8> (*myAttribs); break;
318 case 9: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 9> (*myAttribs); break;
319 case 10: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 10>(*myAttribs); break;
321 aVboAttribs->initLink (myAttribs, 0, myAttribs->NbElements, GL_NONE);
322 if (!myIndices.IsNull())
324 Handle(OpenGl_VertexBufferCompat) aVboIndices = new OpenGl_VertexBufferCompat();
325 switch (myIndices->Stride)
329 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_SHORT);
334 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_INT);
339 return Standard_False;
342 myVboIndices = aVboIndices;
344 myVboAttribs = aVboAttribs;
345 if (!theCtx->caps->keepArrayData
348 // does not make sense for compatibility mode
349 //myIndices.Nullify();
350 //myAttribs.Nullify();
353 return Standard_True;
356 // =======================================================================
357 // function : drawArray
359 // =======================================================================
360 void OpenGl_PrimitiveArray::drawArray (const Handle(OpenGl_Workspace)& theWorkspace,
361 const Graphic3d_Vec4* theFaceColors,
362 const Standard_Boolean theHasVertColor) const
364 if (myVboAttribs.IsNull())
366 #if !defined(GL_ES_VERSION_2_0)
367 if (myDrawMode == GL_POINTS)
369 // extreme compatibility mode - without sprites but with markers
370 drawMarkers (theWorkspace);
376 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
377 const bool toHilight = theWorkspace->ToHighlight();
378 myVboAttribs->BindAllAttributes (aGlContext);
379 if (theHasVertColor && toHilight)
381 // disable per-vertex color
382 OpenGl_VertexBuffer::unbindAttribute (aGlContext, Graphic3d_TOA_COLOR);
384 if (!myVboIndices.IsNull())
386 myVboIndices->Bind (aGlContext);
387 GLubyte* anOffset = myVboIndices->GetDataOffset();
388 if (!myBounds.IsNull())
390 // draw primitives by vertex count with the indices
391 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
392 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
394 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
395 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
396 glDrawElements (myDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
397 anOffset += aStride * aNbElemsInGroup;
402 // draw one (or sequential) primitive by the indices
403 glDrawElements (myDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
405 myVboIndices->Unbind (aGlContext);
407 else if (!myBounds.IsNull())
409 GLint aFirstElem = 0;
410 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
412 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
413 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
414 glDrawArrays (myDrawMode, aFirstElem, aNbElemsInGroup);
415 aFirstElem += aNbElemsInGroup;
420 if (myDrawMode == GL_POINTS)
422 drawMarkers (theWorkspace);
426 glDrawArrays (myDrawMode, 0, myVboAttribs->GetElemsNb());
431 myVboAttribs->UnbindAllAttributes (aGlContext);
434 // =======================================================================
435 // function : drawEdges
437 // =======================================================================
438 void OpenGl_PrimitiveArray::drawEdges (const OpenGl_Vec4& theEdgeColour,
439 const Handle(OpenGl_Workspace)& theWorkspace) const
441 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
442 if (myVboAttribs.IsNull())
447 #if !defined(GL_ES_VERSION_2_0)
448 if (aGlContext->core11 != NULL)
450 glDisable (GL_LIGHTING);
454 const OpenGl_AspectLine* anAspectLineOld = theWorkspace->SetAspectLine (theWorkspace->AspectFace()->AspectEdge());
455 const OpenGl_AspectLine* anAspect = theWorkspace->ApplyAspectLine();
457 #if !defined(GL_ES_VERSION_2_0)
458 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
461 if (aGlContext->core20fwd != NULL)
463 aGlContext->ShaderManager()->BindLineProgram (NULL,
464 anAspect->Aspect()->Type() != Aspect_TOL_SOLID,
467 anAspect->ShaderProgramRes (aGlContext));
470 /// OCC22236 NOTE: draw edges for all situations:
471 /// 1) draw elements with GL_LINE style as edges from myPArray->bufferVBO[VBOEdges] indices array
472 /// 2) draw elements from vertex array, when bounds defines count of primitive's vertices.
473 /// 3) draw primitive's edges by vertexes if no edges and bounds array is specified
474 myVboAttribs->BindPositionAttribute (aGlContext);
476 aGlContext->SetColor4fv (theEdgeColour);
477 aGlContext->SetTypeOfLine (anAspect->Aspect()->Type());
478 aGlContext->SetLineWidth (anAspect->Aspect()->Width());
480 if (!myVboIndices.IsNull())
482 myVboIndices->Bind (aGlContext);
483 GLubyte* anOffset = myVboIndices->GetDataOffset();
485 // draw primitives by vertex count with the indices
486 if (!myBounds.IsNull())
488 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
489 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
491 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
492 glDrawElements (myDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
493 anOffset += aStride * aNbElemsInGroup;
496 // draw one (or sequential) primitive by the indices
499 glDrawElements (myDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
501 myVboIndices->Unbind (aGlContext);
503 else if (!myBounds.IsNull())
505 GLint aFirstElem = 0;
506 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
508 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
509 glDrawArrays (myDrawMode, aFirstElem, aNbElemsInGroup);
510 aFirstElem += aNbElemsInGroup;
515 glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
519 myVboAttribs->UnbindAttribute (aGlContext, Graphic3d_TOA_POS);
521 // restore line context
522 theWorkspace->SetAspectLine (anAspectLineOld);
525 // =======================================================================
526 // function : drawMarkers
528 // =======================================================================
529 void OpenGl_PrimitiveArray::drawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const
531 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->ApplyAspectMarker();
532 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
533 const Handle(OpenGl_TextureSet)& aSpriteNormRes = anAspectMarker->SpriteRes (aCtx);
534 const OpenGl_PointSprite* aSpriteNorm = !aSpriteNormRes.IsNull() ? dynamic_cast<const OpenGl_PointSprite*> (aSpriteNormRes->First().get()) : NULL;
535 if (aSpriteNorm != NULL
536 && !aSpriteNorm->IsDisplayList())
538 // Textured markers will be drawn with the point sprites
539 aCtx->SetPointSize (anAspectMarker->MarkerSize());
540 aCtx->SetPointSpriteOrigin();
541 #if !defined(GL_ES_VERSION_2_0)
542 if (aCtx->core11 != NULL)
544 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
545 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
549 aCtx->core11fwd->glEnable (GL_BLEND);
550 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
552 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
554 aCtx->core11fwd->glDisable (GL_BLEND);
555 #if !defined(GL_ES_VERSION_2_0)
556 if (aCtx->core11 != NULL)
558 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
561 aCtx->SetPointSize (1.0f);
564 else if (anAspectMarker->Aspect()->Type() == Aspect_TOM_POINT)
566 aCtx->SetPointSize (anAspectMarker->MarkerSize());
567 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
568 aCtx->SetPointSize (1.0f);
570 #if !defined(GL_ES_VERSION_2_0)
571 // Textured markers will be drawn with the glBitmap
572 else if (anAspectMarker->Aspect()->Type() != Aspect_TOM_POINT
573 && aSpriteNorm != NULL)
575 /**if (!isHilight && (myPArray->vcolours != NULL))
577 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
579 glColor4ubv (myPArray->vcolours[anIter].GetData());
580 glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
581 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
586 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
588 aCtx->core11->glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
589 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
596 // =======================================================================
597 // function : OpenGl_PrimitiveArray
599 // =======================================================================
600 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver)
602 : myDrawMode (DRAW_MODE_NONE),
603 myIsVboInit (Standard_False)
605 if (theDriver != NULL)
607 myUID = theDriver->GetNextPrimitiveArrayUID();
611 // =======================================================================
612 // function : OpenGl_PrimitiveArray
614 // =======================================================================
615 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver,
616 const Graphic3d_TypeOfPrimitiveArray theType,
617 const Handle(Graphic3d_IndexBuffer)& theIndices,
618 const Handle(Graphic3d_Buffer)& theAttribs,
619 const Handle(Graphic3d_BoundBuffer)& theBounds)
621 : myIndices (theIndices),
622 myAttribs (theAttribs),
623 myBounds (theBounds),
624 myDrawMode (DRAW_MODE_NONE),
625 myIsVboInit (Standard_False)
627 if (!myIndices.IsNull()
628 && myIndices->NbElements < 1)
630 // dummy index buffer?
634 if (theDriver != NULL)
636 myUID = theDriver->GetNextPrimitiveArrayUID();
637 #if defined (GL_ES_VERSION_2_0)
638 const Handle(OpenGl_Context)& aCtx = theDriver->GetSharedContext();
641 processIndices (aCtx);
646 setDrawMode (theType);
649 // =======================================================================
650 // function : ~OpenGl_PrimitiveArray
652 // =======================================================================
653 OpenGl_PrimitiveArray::~OpenGl_PrimitiveArray()
658 // =======================================================================
659 // function : Release
661 // =======================================================================
662 void OpenGl_PrimitiveArray::Release (OpenGl_Context* theContext)
664 myIsVboInit = Standard_False;
665 if (!myVboIndices.IsNull())
669 theContext->DelayedRelease (myVboIndices);
671 myVboIndices.Nullify();
673 if (!myVboAttribs.IsNull())
677 theContext->DelayedRelease (myVboAttribs);
679 myVboAttribs.Nullify();
683 // =======================================================================
686 // =======================================================================
687 void OpenGl_PrimitiveArray::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
689 if (myDrawMode == DRAW_MODE_NONE)
694 const OpenGl_AspectFace* anAspectFace = theWorkspace->ApplyAspectFace();
695 const OpenGl_AspectLine* anAspectLine = theWorkspace->ApplyAspectLine();
696 const OpenGl_AspectMarker* anAspectMarker = myDrawMode == GL_POINTS
697 ? theWorkspace->ApplyAspectMarker()
698 : theWorkspace->AspectMarker();
700 // create VBOs on first render call
701 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
704 // compatibility - keep data to draw markers using display lists
705 Standard_Boolean toKeepData = Standard_False;
706 if (myDrawMode == GL_POINTS)
708 const Handle(OpenGl_TextureSet)& aSpriteNormRes = anAspectMarker->SpriteRes (aCtx);
709 const OpenGl_PointSprite* aSpriteNorm = !aSpriteNormRes.IsNull() ? dynamic_cast<const OpenGl_PointSprite*> (aSpriteNormRes->First().get()) : NULL;
710 toKeepData = aSpriteNorm != NULL
711 && aSpriteNorm->IsDisplayList();
713 #if defined (GL_ES_VERSION_2_0)
714 processIndices (aCtx);
716 buildVBO (aCtx, toKeepData);
717 myIsVboInit = Standard_True;
720 const Standard_Boolean hasColorAttrib = !myVboAttribs.IsNull()
721 && myVboAttribs->HasColorAttribute();
722 const Standard_Boolean isLightOn = !anAspectFace->IsNoLighting()
723 && !myVboAttribs.IsNull()
724 && myVboAttribs->HasNormalAttribute();
726 // Temporarily disable environment mapping
727 Handle(OpenGl_TextureSet) aTextureBack;
728 bool toDrawArray = true;
729 if (myDrawMode > GL_LINE_STRIP)
731 toDrawArray = anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_EMPTY;
733 else if (myDrawMode <= GL_LINE_STRIP)
735 aTextureBack = aCtx->BindTextures (Handle(OpenGl_TextureSet)());
736 if (myDrawMode == GL_POINTS)
738 toDrawArray = anAspectMarker->Aspect()->Type() != Aspect_TOM_EMPTY;
742 toDrawArray = anAspectLine->Aspect()->Type() != Aspect_TOL_EMPTY;
748 const bool toHilight = theWorkspace->ToHighlight();
749 const Standard_Boolean hasVertColor = hasColorAttrib && !toHilight;
754 const Handle(OpenGl_TextureSet)& aSpriteNormRes = anAspectMarker->SpriteRes (aCtx);
755 const OpenGl_PointSprite* aSpriteNorm = !aSpriteNormRes.IsNull() ? dynamic_cast<const OpenGl_PointSprite*> (aSpriteNormRes->First().get()) : NULL;
756 if (aSpriteNorm != NULL
757 && !aSpriteNorm->IsDisplayList())
759 const Handle(OpenGl_TextureSet)& aSprite = toHilight && anAspectMarker->SpriteHighlightRes (aCtx)->First()->IsValid()
760 ? anAspectMarker->SpriteHighlightRes (aCtx)
762 aCtx->BindTextures (aSprite);
763 aCtx->ShaderManager()->BindMarkerProgram (aSprite, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
767 aCtx->ShaderManager()->BindMarkerProgram (Handle(OpenGl_TextureSet)(), isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
774 aCtx->ShaderManager()->BindLineProgram (NULL,
775 anAspectLine->Aspect()->Type() != Aspect_TOL_SOLID,
778 anAspectLine->ShaderProgramRes (aCtx));
783 const Handle(OpenGl_TextureSet)& aTextures = aCtx->ActiveTextures();
784 const Standard_Boolean isLightOnFace = isLightOn
785 && (aTextures.IsNull()
786 || aTextures->IsEmpty()
787 || aTextures->First().IsNull()
788 || aTextures->First()->Sampler()->Parameters()->IsModulate());
789 const Standard_Boolean toEnableEnvMap = (!aTextures.IsNull() && (aTextures == theWorkspace->EnvironmentTexture()));
790 aCtx->ShaderManager()->BindFaceProgram (aTextures,
794 anAspectFace->ShaderProgramRes (aCtx));
799 #if !defined(GL_ES_VERSION_2_0)
800 // manage FFP lighting
801 if (aCtx->ActiveProgram().IsNull()
802 && aCtx->core11 != NULL)
806 glDisable (GL_LIGHTING);
810 glEnable (GL_LIGHTING);
815 if (!aCtx->ActiveTextures().IsNull()
816 && !aCtx->ActiveTextures()->IsEmpty()
817 && !aCtx->ActiveTextures()->First().IsNull()
818 && myDrawMode != GL_POINTS) // transformation is not supported within point sprites
820 aCtx->SetTextureMatrix (aCtx->ActiveTextures()->First()->Sampler()->Parameters());
823 if (myDrawMode <= GL_LINE_STRIP)
825 const OpenGl_Vec4& aLineColor = myDrawMode == GL_POINTS ? theWorkspace->MarkerColor() : theWorkspace->LineColor();
826 aCtx->SetColor4fv (aLineColor);
830 const OpenGl_Vec4& anInteriorColor = theWorkspace->InteriorColor();
831 aCtx->SetColor4fv (anInteriorColor);
833 if (myDrawMode == GL_LINES
834 || myDrawMode == GL_LINE_STRIP)
836 aCtx->SetTypeOfLine (anAspectLine->Aspect()->Type());
837 aCtx->SetLineWidth (anAspectLine->Aspect()->Width());
840 const Graphic3d_Vec4* aFaceColors = !myBounds.IsNull() && !toHilight && anAspectFace->Aspect()->InteriorStyle() != Aspect_IS_HIDDENLINE
843 drawArray (theWorkspace, aFaceColors, hasColorAttrib);
846 if (myDrawMode <= GL_LINE_STRIP)
848 aCtx->BindTextures (aTextureBack);
852 if (anAspectFace->Aspect()->ToDrawEdges()
853 || anAspectFace->Aspect()->InteriorStyle() == Aspect_IS_HIDDENLINE)
855 const OpenGl_Vec4& anEdgeColor = theWorkspace->EdgeColor();
856 drawEdges (anEdgeColor, theWorkspace);
858 // restore OpenGL polygon mode if needed
859 #if !defined(GL_ES_VERSION_2_0)
860 if (anAspectFace->Aspect()->InteriorStyle() >= Aspect_IS_HATCH)
862 glPolygonMode (GL_FRONT_AND_BACK,
863 anAspectFace->Aspect()->InteriorStyle() == Aspect_IS_POINT ? GL_POINT : GL_FILL);
870 // =======================================================================
871 // function : setDrawMode
873 // =======================================================================
874 void OpenGl_PrimitiveArray::setDrawMode (const Graphic3d_TypeOfPrimitiveArray theType)
876 if (myAttribs.IsNull())
878 myDrawMode = DRAW_MODE_NONE;
884 case Graphic3d_TOPA_POINTS:
885 myDrawMode = GL_POINTS;
887 case Graphic3d_TOPA_POLYLINES:
888 myDrawMode = GL_LINE_STRIP;
890 case Graphic3d_TOPA_SEGMENTS:
891 myDrawMode = GL_LINES;
893 case Graphic3d_TOPA_TRIANGLES:
894 myDrawMode = GL_TRIANGLES;
896 case Graphic3d_TOPA_TRIANGLESTRIPS:
897 myDrawMode = GL_TRIANGLE_STRIP;
899 case Graphic3d_TOPA_TRIANGLEFANS:
900 myDrawMode = GL_TRIANGLE_FAN;
902 #if !defined(GL_ES_VERSION_2_0)
903 case Graphic3d_TOPA_POLYGONS:
904 myDrawMode = GL_POLYGON;
906 case Graphic3d_TOPA_QUADRANGLES:
907 myDrawMode = GL_QUADS;
909 case Graphic3d_TOPA_QUADRANGLESTRIPS:
910 myDrawMode = GL_QUAD_STRIP;
913 case Graphic3d_TOPA_POLYGONS:
914 case Graphic3d_TOPA_QUADRANGLES:
915 case Graphic3d_TOPA_QUADRANGLESTRIPS:
917 case Graphic3d_TOPA_UNDEFINED:
922 // =======================================================================
923 // function : processIndices
925 // =======================================================================
926 Standard_Boolean OpenGl_PrimitiveArray::processIndices (const Handle(OpenGl_Context)& theContext) const
928 if (myIndices.IsNull()
929 || myAttribs.IsNull()
930 || theContext->hasUintIndex)
932 return Standard_True;
935 if (myAttribs->NbElements > std::numeric_limits<GLushort>::max())
937 Handle(Graphic3d_Buffer) anAttribs = new Graphic3d_Buffer (new NCollection_AlignedAllocator (16));
938 if (!anAttribs->Init (myIndices->NbElements, myAttribs->AttributesArray(), myAttribs->NbAttributes))
940 return Standard_False; // failed to initialize attribute array
943 for (Standard_Integer anIdxIdx = 0; anIdxIdx < myIndices->NbElements; ++anIdxIdx)
945 const Standard_Integer anIndex = myIndices->Index (anIdxIdx);
946 memcpy (anAttribs->ChangeData() + myAttribs->Stride * anIdxIdx,
947 myAttribs->Data() + myAttribs->Stride * anIndex,
952 myAttribs = anAttribs;
955 return Standard_True;
958 // =======================================================================
959 // function : InitBuffers
961 // =======================================================================
962 void OpenGl_PrimitiveArray::InitBuffers (const Handle(OpenGl_Context)& theContext,
963 const Graphic3d_TypeOfPrimitiveArray theType,
964 const Handle(Graphic3d_IndexBuffer)& theIndices,
965 const Handle(Graphic3d_Buffer)& theAttribs,
966 const Handle(Graphic3d_BoundBuffer)& theBounds)
968 // Release old graphic resources
969 Release (theContext.operator->());
971 myIndices = theIndices;
972 myAttribs = theAttribs;
973 myBounds = theBounds;
974 #if defined(GL_ES_VERSION_2_0)
975 processIndices (theContext);
978 setDrawMode (theType);