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 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
364 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
365 bool hasVColors = theHasVertColor && !toHilight;
366 if (myVboAttribs.IsNull())
368 #if !defined(GL_ES_VERSION_2_0)
369 if (myDrawMode == GL_POINTS)
371 // extreme compatibility mode - without sprites but with markers
372 drawMarkers (theWorkspace);
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);
435 theWorkspace->NamedStatus |= OPENGL_NS_RESMAT;
439 // =======================================================================
440 // function : drawEdges
442 // =======================================================================
443 void OpenGl_PrimitiveArray::drawEdges (const TEL_COLOUR* theEdgeColour,
444 const Handle(OpenGl_Workspace)& theWorkspace) const
446 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
447 if (myVboAttribs.IsNull())
452 #if !defined(GL_ES_VERSION_2_0)
453 if (aGlContext->core11 != NULL)
455 glDisable (GL_LIGHTING);
459 const OpenGl_AspectLine* anAspectLineOld = NULL;
461 anAspectLineOld = theWorkspace->SetAspectLine (theWorkspace->AspectFace (Standard_True)->AspectEdge());
462 const OpenGl_AspectLine* anAspect = theWorkspace->AspectLine (Standard_True);
464 #if !defined(GL_ES_VERSION_2_0)
465 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
468 if (aGlContext->core20fwd != NULL)
470 aGlContext->ShaderManager()->BindLineProgram (NULL, anAspect->Type() != Aspect_TOL_SOLID, Standard_False, Standard_False, anAspect->ShaderProgramRes (aGlContext));
473 /// OCC22236 NOTE: draw edges for all situations:
474 /// 1) draw elements with GL_LINE style as edges from myPArray->bufferVBO[VBOEdges] indices array
475 /// 2) draw elements from vertex array, when bounds defines count of primitive's vertices.
476 /// 3) draw primitive's edges by vertexes if no edges and bounds array is specified
477 myVboAttribs->BindPositionAttribute (aGlContext);
479 aGlContext->SetColor4fv (*(const OpenGl_Vec4* )theEdgeColour->rgb);
480 aGlContext->SetTypeOfLine (anAspect->Type());
481 aGlContext->SetLineWidth (anAspect->Width());
483 if (!myVboIndices.IsNull())
485 myVboIndices->Bind (aGlContext);
486 GLubyte* anOffset = myVboIndices->GetDataOffset();
488 // draw primitives by vertex count with the indices
489 if (!myBounds.IsNull())
491 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
492 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
494 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
495 glDrawElements (myDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
496 anOffset += aStride * aNbElemsInGroup;
499 // draw one (or sequential) primitive by the indices
502 glDrawElements (myDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
504 myVboIndices->Unbind (aGlContext);
506 else if (!myBounds.IsNull())
508 GLint aFirstElem = 0;
509 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
511 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
512 glDrawArrays (myDrawMode, aFirstElem, aNbElemsInGroup);
513 aFirstElem += aNbElemsInGroup;
518 glDrawArrays (myDrawMode, 0, myAttribs->NbElements);
522 myVboAttribs->UnbindAttribute (aGlContext, Graphic3d_TOA_POS);
524 // restore line context
525 theWorkspace->SetAspectLine (anAspectLineOld);
528 // =======================================================================
529 // function : drawMarkers
531 // =======================================================================
532 void OpenGl_PrimitiveArray::drawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const
534 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker (Standard_True);
535 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
536 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (aCtx);
537 if (!aSpriteNorm.IsNull()
538 && !aSpriteNorm->IsDisplayList())
540 // Textured markers will be drawn with the point sprites
541 aCtx->SetPointSize (anAspectMarker->MarkerSize());
542 #if !defined(GL_ES_VERSION_2_0)
543 if (aCtx->core11 != NULL)
545 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
546 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
550 aCtx->core11fwd->glEnable (GL_BLEND);
551 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
553 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
555 aCtx->core11fwd->glDisable (GL_BLEND);
556 #if !defined(GL_ES_VERSION_2_0)
557 if (aCtx->core11 != NULL)
559 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
562 aCtx->SetPointSize (1.0f);
565 else if (anAspectMarker->Type() == Aspect_TOM_POINT)
567 aCtx->SetPointSize (anAspectMarker->MarkerSize());
568 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
569 aCtx->SetPointSize (1.0f);
571 #if !defined(GL_ES_VERSION_2_0)
572 // Textured markers will be drawn with the glBitmap
573 else if (anAspectMarker->Type() != Aspect_TOM_POINT
574 && !aSpriteNorm.IsNull())
576 /**if (!isHilight && (myPArray->vcolours != NULL))
578 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
580 glColor4ubv (myPArray->vcolours[anIter].GetData());
581 glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
582 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
587 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
589 aCtx->core11->glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
590 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
597 // =======================================================================
598 // function : OpenGl_PrimitiveArray
600 // =======================================================================
601 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver)
603 : myDrawMode (DRAW_MODE_NONE),
604 myIsVboInit (Standard_False)
606 if (theDriver != NULL)
608 myUID = theDriver->GetNextPrimitiveArrayUID();
612 // =======================================================================
613 // function : OpenGl_PrimitiveArray
615 // =======================================================================
616 OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver,
617 const Graphic3d_TypeOfPrimitiveArray theType,
618 const Handle(Graphic3d_IndexBuffer)& theIndices,
619 const Handle(Graphic3d_Buffer)& theAttribs,
620 const Handle(Graphic3d_BoundBuffer)& theBounds)
622 : myIndices (theIndices),
623 myAttribs (theAttribs),
624 myBounds (theBounds),
625 myDrawMode (DRAW_MODE_NONE),
626 myIsVboInit (Standard_False)
628 if (!myIndices.IsNull()
629 && myIndices->NbElements < 1)
631 // dummy index buffer?
635 if (theDriver != NULL)
637 myUID = theDriver->GetNextPrimitiveArrayUID();
638 #if defined (GL_ES_VERSION_2_0)
639 const Handle(OpenGl_Context)& aCtx = theDriver->GetSharedContext();
642 processIndices (aCtx);
647 setDrawMode (theType);
650 // =======================================================================
651 // function : ~OpenGl_PrimitiveArray
653 // =======================================================================
654 OpenGl_PrimitiveArray::~OpenGl_PrimitiveArray()
659 // =======================================================================
660 // function : Release
662 // =======================================================================
663 void OpenGl_PrimitiveArray::Release (OpenGl_Context* theContext)
665 myIsVboInit = Standard_False;
666 if (!myVboIndices.IsNull())
670 theContext->DelayedRelease (myVboIndices);
672 myVboIndices.Nullify();
674 if (!myVboAttribs.IsNull())
678 theContext->DelayedRelease (myVboAttribs);
680 myVboAttribs.Nullify();
684 // =======================================================================
687 // =======================================================================
688 void OpenGl_PrimitiveArray::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
690 if (myDrawMode == DRAW_MODE_NONE)
695 const OpenGl_AspectFace* anAspectFace = theWorkspace->AspectFace (Standard_True);
696 const OpenGl_AspectLine* anAspectLine = theWorkspace->AspectLine (Standard_True);
697 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker (myDrawMode == GL_POINTS);
699 // create VBOs on first render call
700 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
703 // compatibility - keep data to draw markers using display lists
704 const Standard_Boolean toKeepData = myDrawMode == GL_POINTS
705 && !anAspectMarker->SpriteRes (aCtx).IsNull()
706 && anAspectMarker->SpriteRes (aCtx)->IsDisplayList();
707 #if defined (GL_ES_VERSION_2_0)
708 processIndices (aCtx);
710 buildVBO (aCtx, toKeepData);
711 myIsVboInit = Standard_True;
714 Tint aFrontLightingModel = anAspectFace->IntFront().color_mask;
715 const TEL_COLOUR* anInteriorColor = &anAspectFace->IntFront().matcol;
716 const TEL_COLOUR* anEdgeColor = &anAspectFace->AspectEdge()->Color();
717 const TEL_COLOUR* aLineColor = myDrawMode == GL_POINTS ? &anAspectMarker->Color() : &anAspectLine->Color();
719 // Use highlight colors
720 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
722 anEdgeColor = anInteriorColor = aLineColor = theWorkspace->HighlightColor;
723 aFrontLightingModel = 0;
726 const Standard_Boolean hasColorAttrib = !myVboAttribs.IsNull()
727 && myVboAttribs->HasColorAttribute();
728 const Standard_Boolean isLightOn = aFrontLightingModel != 0
729 && !myVboAttribs.IsNull()
730 && myVboAttribs->HasNormalAttribute();
731 #if !defined(GL_ES_VERSION_2_0)
732 // manage FFP lighting
733 if (aCtx->core11 != NULL)
737 glDisable (GL_LIGHTING);
741 glEnable (GL_LIGHTING);
745 // Temporarily disable environment mapping
746 Handle(OpenGl_Texture) aTextureBack;
747 if (myDrawMode <= GL_LINE_STRIP)
749 aTextureBack = theWorkspace->DisableTexture();
752 if ((myDrawMode > GL_LINE_STRIP && anAspectFace->InteriorStyle() != Aspect_IS_EMPTY) ||
753 (myDrawMode <= GL_LINE_STRIP))
755 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
756 const Graphic3d_Vec4* aFaceColors = !myBounds.IsNull() && !toHilight && anAspectFace->InteriorStyle() != Aspect_IS_HIDDENLINE
759 const Standard_Boolean hasVertColor = hasColorAttrib && !toHilight;
760 if (aCtx->core20fwd != NULL)
766 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (aCtx);
767 if (!aSpriteNorm.IsNull()
768 && !aSpriteNorm->IsDisplayList())
770 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes (aCtx)->IsValid())
771 ? anAspectMarker->SpriteHighlightRes (aCtx)
773 theWorkspace->EnableTexture (aSprite);
774 aCtx->ShaderManager()->BindMarkerProgram (aSprite, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
778 aCtx->ShaderManager()->BindMarkerProgram (NULL, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
785 aCtx->ShaderManager()->BindLineProgram (NULL, anAspectLine->Type() != Aspect_TOL_SOLID, isLightOn, hasVertColor, anAspectLine->ShaderProgramRes (aCtx));
790 const Handle(OpenGl_Texture)& aTexture = theWorkspace->ActiveTexture();
791 const Standard_Boolean isLightOnFace = isLightOn
792 && (aTexture.IsNull()
793 || aTexture->GetParams()->IsModulate());
794 const Standard_Boolean toEnableEnvMap = (!aTexture.IsNull() && (aTexture == theWorkspace->EnvironmentTexture()));
795 aCtx->ShaderManager()->BindFaceProgram (aTexture,
799 anAspectFace->ShaderProgramRes (aCtx));
805 // All primitives should gather material properties from the AspectFace in shading mode
808 aCtx->SetShadingMaterial (anAspectFace);
811 if (!theWorkspace->ActiveTexture().IsNull()
812 && myDrawMode != GL_POINTS) // transformation is not supported within point sprites
814 aCtx->SetTextureMatrix (theWorkspace->ActiveTexture()->GetParams());
817 aCtx->SetColor4fv (*(const OpenGl_Vec4* )(myDrawMode <= GL_LINE_STRIP ? aLineColor->rgb : anInteriorColor->rgb));
818 if (myDrawMode == GL_LINES
819 || myDrawMode == GL_LINE_STRIP)
821 aCtx->SetTypeOfLine (anAspectLine->Type());
822 aCtx->SetLineWidth (anAspectLine->Width());
825 drawArray (theWorkspace, aFaceColors, hasColorAttrib);
828 if (myDrawMode <= GL_LINE_STRIP)
830 theWorkspace->EnableTexture (aTextureBack);
834 if (anAspectFace->Edge()
835 || anAspectFace->InteriorStyle() == Aspect_IS_HIDDENLINE)
837 drawEdges (anEdgeColor, theWorkspace);
839 // restore OpenGL polygon mode if needed
840 #if !defined(GL_ES_VERSION_2_0)
841 if (anAspectFace->InteriorStyle() >= Aspect_IS_HATCH)
843 glPolygonMode (GL_FRONT_AND_BACK,
844 anAspectFace->InteriorStyle() == Aspect_IS_POINT ? GL_POINT : GL_FILL);
850 aCtx->BindProgram (NULL);
853 // =======================================================================
854 // function : setDrawMode
856 // =======================================================================
857 void OpenGl_PrimitiveArray::setDrawMode (const Graphic3d_TypeOfPrimitiveArray theType)
859 if (myAttribs.IsNull())
861 myDrawMode = DRAW_MODE_NONE;
867 case Graphic3d_TOPA_POINTS:
868 myDrawMode = GL_POINTS;
870 case Graphic3d_TOPA_POLYLINES:
871 myDrawMode = GL_LINE_STRIP;
873 case Graphic3d_TOPA_SEGMENTS:
874 myDrawMode = GL_LINES;
876 case Graphic3d_TOPA_TRIANGLES:
877 myDrawMode = GL_TRIANGLES;
879 case Graphic3d_TOPA_TRIANGLESTRIPS:
880 myDrawMode = GL_TRIANGLE_STRIP;
882 case Graphic3d_TOPA_TRIANGLEFANS:
883 myDrawMode = GL_TRIANGLE_FAN;
885 #if !defined(GL_ES_VERSION_2_0)
886 case Graphic3d_TOPA_POLYGONS:
887 myDrawMode = GL_POLYGON;
889 case Graphic3d_TOPA_QUADRANGLES:
890 myDrawMode = GL_QUADS;
892 case Graphic3d_TOPA_QUADRANGLESTRIPS:
893 myDrawMode = GL_QUAD_STRIP;
896 case Graphic3d_TOPA_POLYGONS:
897 case Graphic3d_TOPA_QUADRANGLES:
898 case Graphic3d_TOPA_QUADRANGLESTRIPS:
900 case Graphic3d_TOPA_UNDEFINED:
905 // =======================================================================
906 // function : processIndices
908 // =======================================================================
909 Standard_Boolean OpenGl_PrimitiveArray::processIndices (const Handle(OpenGl_Context)& theContext) const
911 if (myIndices.IsNull()
912 || theContext->hasUintIndex)
914 return Standard_True;
917 if (myIndices->NbElements > std::numeric_limits<GLushort>::max())
919 Handle(Graphic3d_Buffer) anAttribs = new Graphic3d_Buffer (new NCollection_AlignedAllocator (16));
920 if (!anAttribs->Init (myIndices->NbElements, myAttribs->AttributesArray(), myAttribs->NbAttributes))
922 return Standard_False; // failed to initialize attribute array
925 for (Standard_Integer anIdxIdx = 0; anIdxIdx < myIndices->NbElements; ++anIdxIdx)
927 const Standard_Integer anIndex = myIndices->Index (anIdxIdx);
928 memcpy (anAttribs->ChangeData() + myAttribs->Stride * anIdxIdx,
929 myAttribs->Data() + myAttribs->Stride * anIndex,
934 myAttribs = anAttribs;
937 return Standard_True;
940 // =======================================================================
941 // function : InitBuffers
943 // =======================================================================
944 void OpenGl_PrimitiveArray::InitBuffers (const Handle(OpenGl_Context)& theContext,
945 const Graphic3d_TypeOfPrimitiveArray theType,
946 const Handle(Graphic3d_IndexBuffer)& theIndices,
947 const Handle(Graphic3d_Buffer)& theAttribs,
948 const Handle(Graphic3d_BoundBuffer)& theBounds)
950 // Release old graphic resources
951 Release (theContext.operator->());
953 myIndices = theIndices;
954 myAttribs = theAttribs;
955 myBounds = theBounds;
956 #if defined(GL_ES_VERSION_2_0)
957 processIndices (theContext);
960 setDrawMode (theType);