0026016: BRepPrimAPI_MakeRevol crash
[occt.git] / src / OpenGl / OpenGl_PrimitiveArray.cxx
CommitLineData
b311480e 1// Created on: 2011-07-13
2// Created by: Sergey ZERCHANINOV
de75ed09 3// Copyright (c) 2011-2013 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
2166f0fa 16#include <OpenGl_AspectFace.hxx>
30f0ad28 17#include <OpenGl_Context.hxx>
2166f0fa 18#include <OpenGl_GraphicDriver.hxx>
30f0ad28 19#include <OpenGl_IndexBuffer.hxx>
a577aaab 20#include <OpenGl_PointSprite.hxx>
30f0ad28 21#include <OpenGl_PrimitiveArray.hxx>
22#include <OpenGl_ShaderManager.hxx>
23#include <OpenGl_ShaderProgram.hxx>
2166f0fa 24#include <OpenGl_Structure.hxx>
7d3e64ef 25#include <OpenGl_VertexBufferCompat.hxx>
bf75be98 26#include <OpenGl_Workspace.hxx>
6c6aadb1 27#include <Graphic3d_TextureParams.hxx>
2166f0fa 28
30f0ad28 29namespace
30{
871fa103 31 //! Convert index data type from size
32 inline GLenum toGlIndexType (const Standard_Integer theStride)
33 {
34 switch (theStride)
35 {
36 case 2: return GL_UNSIGNED_SHORT;
37 case 4: return GL_UNSIGNED_INT;
38 default: return GL_NONE;
39 }
40 }
41
42 //! Convert data type to GL info
43 inline GLenum toGlDataType (const Graphic3d_TypeOfData theType,
44 GLint& theNbComp)
45 {
46 switch (theType)
47 {
48 case Graphic3d_TOD_USHORT:
49 theNbComp = 1;
50 return GL_UNSIGNED_SHORT;
51 case Graphic3d_TOD_UINT:
52 theNbComp = 1;
53 return GL_UNSIGNED_INT;
54 case Graphic3d_TOD_VEC2:
55 theNbComp = 2;
56 return GL_FLOAT;
57 case Graphic3d_TOD_VEC3:
58 theNbComp = 3;
59 return GL_FLOAT;
60 case Graphic3d_TOD_VEC4:
61 theNbComp = 4;
62 return GL_FLOAT;
63 case Graphic3d_TOD_VEC4UB:
64 theNbComp = 4;
65 return GL_UNSIGNED_BYTE;
66 }
67 theNbComp = 0;
68 return GL_NONE;
69 }
70
30f0ad28 71}
72
871fa103 73//! Auxiliary template for VBO with interleaved attributes.
7d3e64ef 74template<class TheBaseClass, int NbAttributes>
75class OpenGl_VertexBufferT : public TheBaseClass
7fd59977 76{
871fa103 77
78public:
79
80 //! Create uninitialized VBO.
81 OpenGl_VertexBufferT (const Graphic3d_Attribute* theAttribs,
82 const Standard_Integer theStride)
83 : Stride (theStride)
84 {
85 memcpy (Attribs, theAttribs, sizeof(Graphic3d_Attribute) * NbAttributes);
86 }
87
88 //! Create uninitialized VBO.
89 OpenGl_VertexBufferT (const Graphic3d_Buffer& theAttribs)
90 : Stride (theAttribs.Stride)
91 {
92 memcpy (Attribs, theAttribs.AttributesArray(), sizeof(Graphic3d_Attribute) * NbAttributes);
93 }
94
95 virtual bool HasColorAttribute() const
96 {
97 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
98 {
99 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
100 if (anAttrib.Id == Graphic3d_TOA_COLOR)
101 {
102 return true;
103 }
104 }
105 return false;
106 }
107
7d3e64ef 108 virtual bool HasNormalAttribute() const
109 {
110 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
111 {
112 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
113 if (anAttrib.Id == Graphic3d_TOA_NORM)
114 {
115 return true;
116 }
117 }
118 return false;
119 }
120
121 virtual void BindPositionAttribute (const Handle(OpenGl_Context)& theGlCtx) const
871fa103 122 {
7d3e64ef 123 if (!TheBaseClass::IsValid())
871fa103 124 {
125 return;
126 }
127
7d3e64ef 128 TheBaseClass::Bind (theGlCtx);
871fa103 129 GLint aNbComp;
7d3e64ef 130 const GLubyte* anOffset = TheBaseClass::myOffset;
871fa103 131 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
132 {
133 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
134 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
135 if (aDataType == GL_NONE)
136 {
137 continue;
138 }
139 else if (anAttrib.Id == Graphic3d_TOA_POS)
140 {
7d3e64ef 141 TheBaseClass::bindAttribute (theGlCtx, Graphic3d_TOA_POS, aNbComp, aDataType, Stride, anOffset);
871fa103 142 break;
143 }
144
145 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
146 }
147 }
148
7d3e64ef 149 virtual void BindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
871fa103 150 {
7d3e64ef 151 if (!TheBaseClass::IsValid())
871fa103 152 {
153 return;
154 }
155
7d3e64ef 156 TheBaseClass::Bind (theGlCtx);
871fa103 157 GLint aNbComp;
7d3e64ef 158 const GLubyte* anOffset = TheBaseClass::myOffset;
871fa103 159 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
160 {
161 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
162 const GLenum aDataType = toGlDataType (anAttrib.DataType, aNbComp);
163 if (aDataType == GL_NONE)
164 {
165 continue;
166 }
167
7d3e64ef 168 TheBaseClass::bindAttribute (theGlCtx, anAttrib.Id, aNbComp, aDataType, Stride, anOffset);
871fa103 169 anOffset += Graphic3d_Attribute::Stride (anAttrib.DataType);
170 }
171 }
172
7d3e64ef 173 virtual void UnbindAllAttributes (const Handle(OpenGl_Context)& theGlCtx) const
871fa103 174 {
7d3e64ef 175 if (!TheBaseClass::IsValid())
871fa103 176 {
177 return;
178 }
7d3e64ef 179 TheBaseClass::Unbind (theGlCtx);
871fa103 180
181 for (Standard_Integer anAttribIter = 0; anAttribIter < NbAttributes; ++anAttribIter)
182 {
183 const Graphic3d_Attribute& anAttrib = Attribs[anAttribIter];
7d3e64ef 184 TheBaseClass::unbindAttribute (theGlCtx, anAttrib.Id);
871fa103 185 }
186 }
187
188public:
189
190 Graphic3d_Attribute Attribs[NbAttributes];
191 Standard_Integer Stride;
192
193};
7fd59977 194
2166f0fa
SK
195// =======================================================================
196// function : clearMemoryGL
197// purpose :
198// =======================================================================
5e27df78 199void OpenGl_PrimitiveArray::clearMemoryGL (const Handle(OpenGl_Context)& theGlCtx) const
7fd59977 200{
871fa103 201 if (!myVboIndices.IsNull())
2166f0fa 202 {
871fa103 203 myVboIndices->Release (theGlCtx.operator->());
204 myVboIndices.Nullify();
205 }
206 if (!myVboAttribs.IsNull())
207 {
208 myVboAttribs->Release (theGlCtx.operator->());
209 myVboAttribs.Nullify();
2166f0fa 210 }
7fd59977 211}
212
2166f0fa 213// =======================================================================
7d3e64ef 214// function : initNormalVbo
2166f0fa
SK
215// purpose :
216// =======================================================================
7d3e64ef 217Standard_Boolean OpenGl_PrimitiveArray::initNormalVbo (const Handle(OpenGl_Context)& theCtx) const
7fd59977 218{
7d3e64ef 219 switch (myAttribs->NbAttributes)
2166f0fa 220 {
7d3e64ef 221 case 1: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 1> (*myAttribs); break;
222 case 2: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 2> (*myAttribs); break;
223 case 3: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 3> (*myAttribs); break;
224 case 4: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 4> (*myAttribs); break;
225 case 5: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 5> (*myAttribs); break;
226 case 6: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 6> (*myAttribs); break;
227 case 7: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 7> (*myAttribs); break;
228 case 8: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 8> (*myAttribs); break;
229 case 9: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 9> (*myAttribs); break;
230 case 10: myVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBuffer, 10>(*myAttribs); break;
231 }
232
233 if (!myVboAttribs->init (theCtx, 0, myAttribs->NbElements, myAttribs->Data(), GL_NONE, myAttribs->Stride))
234 {
235 TCollection_ExtendedString aMsg;
236 aMsg += "VBO creation for Primitive Array has failed for ";
237 aMsg += myAttribs->NbElements;
238 aMsg += " vertices. Out of memory?";
239 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_PERFORMANCE_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMsg);
240
241 clearMemoryGL (theCtx);
5e27df78 242 return Standard_False;
2166f0fa 243 }
7d3e64ef 244 else if (myIndices.IsNull())
245 {
246 return Standard_True;
247 }
7fd59977 248
7d3e64ef 249 myVboIndices = new OpenGl_IndexBuffer();
250 bool isOk = false;
251 switch (myIndices->Stride)
252 {
253 case 2:
254 {
255 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLushort*> (myIndices->Data()));
256 break;
257 }
258 case 4:
259 {
260 isOk = myVboIndices->Init (theCtx, 1, myIndices->NbElements, reinterpret_cast<const GLuint*> (myIndices->Data()));
261 break;
262 }
263 default:
264 {
265 clearMemoryGL (theCtx);
266 return Standard_False;
267 }
268 }
269 if (!isOk)
2166f0fa 270 {
7d3e64ef 271 TCollection_ExtendedString aMsg;
272 aMsg += "VBO creation for Primitive Array has failed for ";
273 aMsg += myIndices->NbElements;
274 aMsg += " indices. Out of memory?";
275 theCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, GL_DEBUG_TYPE_PERFORMANCE_ARB, 0, GL_DEBUG_SEVERITY_LOW_ARB, aMsg);
276 clearMemoryGL (theCtx);
277 return Standard_False;
7fd59977 278 }
7d3e64ef 279 return Standard_True;
280}
871fa103 281
7d3e64ef 282// =======================================================================
283// function : buildVBO
284// purpose :
285// =======================================================================
286Standard_Boolean OpenGl_PrimitiveArray::buildVBO (const Handle(OpenGl_Context)& theCtx,
287 const Standard_Boolean theToKeepData) const
288{
289 bool isNormalMode = theCtx->ToUseVbo();
290 if (myAttribs.IsNull()
291 || myAttribs->IsEmpty()
292 || myAttribs->NbElements < 1
293 || myAttribs->NbAttributes < 1
294 || myAttribs->NbAttributes > 10)
2166f0fa 295 {
7d3e64ef 296 // vertices should be always defined - others are optional
871fa103 297 return Standard_False;
7fd59977 298 }
871fa103 299
7d3e64ef 300 if (isNormalMode
301 && initNormalVbo (theCtx))
302 {
303 if (!theCtx->caps->keepArrayData
304 && !theToKeepData)
305 {
306 myIndices.Nullify();
307 myAttribs.Nullify();
308 }
309 return Standard_True;
310 }
311
312 Handle(OpenGl_VertexBufferCompat) aVboAttribs;
313 switch (myAttribs->NbAttributes)
314 {
315 case 1: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 1> (*myAttribs); break;
316 case 2: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 2> (*myAttribs); break;
317 case 3: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 3> (*myAttribs); break;
318 case 4: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 4> (*myAttribs); break;
319 case 5: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 5> (*myAttribs); break;
320 case 6: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 6> (*myAttribs); break;
321 case 7: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 7> (*myAttribs); break;
322 case 8: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 8> (*myAttribs); break;
323 case 9: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 9> (*myAttribs); break;
324 case 10: aVboAttribs = new OpenGl_VertexBufferT<OpenGl_VertexBufferCompat, 10>(*myAttribs); break;
325 }
326 aVboAttribs->initLink (myAttribs, 0, myAttribs->NbElements, GL_NONE);
871fa103 327 if (!myIndices.IsNull())
2166f0fa 328 {
7d3e64ef 329 Handle(OpenGl_VertexBufferCompat) aVboIndices = new OpenGl_VertexBufferCompat();
871fa103 330 switch (myIndices->Stride)
5e27df78 331 {
871fa103 332 case 2:
333 {
7d3e64ef 334 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_SHORT);
871fa103 335 break;
336 }
337 case 4:
338 {
7d3e64ef 339 aVboIndices->initLink (myIndices, 1, myIndices->NbElements, GL_UNSIGNED_INT);
871fa103 340 break;
341 }
7d3e64ef 342 default:
343 {
344 return Standard_False;
345 }
5e27df78 346 }
7d3e64ef 347 myVboIndices = aVboIndices;
2166f0fa 348 }
7d3e64ef 349 myVboAttribs = aVboAttribs;
350 if (!theCtx->caps->keepArrayData
351 && !theToKeepData)
e276548b 352 {
7d3e64ef 353 // does not make sense for compatibility mode
354 //myIndices.Nullify();
355 //myAttribs.Nullify();
e276548b 356 }
7d3e64ef 357
2166f0fa 358 return Standard_True;
7fd59977 359}
360
2166f0fa 361// =======================================================================
7d3e64ef 362// function : drawArray
2166f0fa
SK
363// purpose :
364// =======================================================================
7d3e64ef 365void OpenGl_PrimitiveArray::drawArray (const Handle(OpenGl_Workspace)& theWorkspace,
8625ef7e 366 const Graphic3d_Vec4* theFaceColors,
367 const Standard_Boolean theHasVertColor) const
7fd59977 368{
871fa103 369 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
871fa103 370 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
8625ef7e 371 bool hasVColors = theHasVertColor && !toHilight;
7d3e64ef 372 if (myVboAttribs.IsNull())
2166f0fa 373 {
ca3c13d1 374 #if !defined(GL_ES_VERSION_2_0)
7d3e64ef 375 if (myDrawMode == GL_POINTS)
376 {
377 // extreme compatibility mode - without sprites but with markers
378 drawMarkers (theWorkspace);
379 }
ca3c13d1 380 #endif
7d3e64ef 381 return;
7fd59977 382 }
383
7d3e64ef 384 myVboAttribs->BindAllAttributes (aGlContext);
8625ef7e 385 if (theHasVertColor && toHilight)
2166f0fa 386 {
8625ef7e 387 // disable per-vertex color
388 OpenGl_VertexBuffer::unbindAttribute (aGlContext, Graphic3d_TOA_COLOR);
7d3e64ef 389 }
390 if (!myVboIndices.IsNull())
391 {
392 myVboIndices->Bind (aGlContext);
393 GLubyte* anOffset = myVboIndices->GetDataOffset();
394 if (!myBounds.IsNull())
2166f0fa 395 {
7d3e64ef 396 // draw primitives by vertex count with the indices
397 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
398 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
2166f0fa 399 {
7d3e64ef 400 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
8625ef7e 401 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
7d3e64ef 402 glDrawElements (myDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
403 anOffset += aStride * aNbElemsInGroup;
762aacae 404 }
bf75be98 405 }
2166f0fa
SK
406 else
407 {
7d3e64ef 408 // draw one (or sequential) primitive by the indices
409 glDrawElements (myDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
5e27df78 410 }
7d3e64ef 411 myVboIndices->Unbind (aGlContext);
7fd59977 412 }
7d3e64ef 413 else if (!myBounds.IsNull())
871fa103 414 {
7d3e64ef 415 GLint aFirstElem = 0;
416 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
417 {
418 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
8625ef7e 419 if (theFaceColors != NULL) aGlContext->SetColor4fv (theFaceColors[aGroupIter]);
7d3e64ef 420 glDrawArrays (myDrawMode, aFirstElem, aNbElemsInGroup);
421 aFirstElem += aNbElemsInGroup;
422 }
871fa103 423 }
7d3e64ef 424 else
2166f0fa 425 {
7d3e64ef 426 if (myDrawMode == GL_POINTS)
427 {
428 drawMarkers (theWorkspace);
429 }
430 else
431 {
432 glDrawArrays (myDrawMode, 0, myVboAttribs->GetElemsNb());
433 }
2166f0fa
SK
434 }
435
7d3e64ef 436 // bind with 0
437 myVboAttribs->UnbindAllAttributes (aGlContext);
438
439 if (hasVColors)
440 {
441 theWorkspace->NamedStatus |= OPENGL_NS_RESMAT;
442 }
7fd59977 443}
444
2166f0fa 445// =======================================================================
7d3e64ef 446// function : drawEdges
2166f0fa
SK
447// purpose :
448// =======================================================================
7d3e64ef 449void OpenGl_PrimitiveArray::drawEdges (const TEL_COLOUR* theEdgeColour,
2166f0fa 450 const Handle(OpenGl_Workspace)& theWorkspace) const
7fd59977 451{
65360da3 452 const Handle(OpenGl_Context)& aGlContext = theWorkspace->GetGlContext();
7d3e64ef 453 if (myVboAttribs.IsNull())
454 {
455 return;
456 }
457
ca3c13d1 458#if !defined(GL_ES_VERSION_2_0)
65360da3 459 if (aGlContext->core11 != NULL)
460 {
461 glDisable (GL_LIGHTING);
462 }
ca3c13d1 463#endif
7fd59977 464
2166f0fa 465 const OpenGl_AspectLine* anAspectLineOld = NULL;
7fd59977 466
7d3e64ef 467 anAspectLineOld = theWorkspace->SetAspectLine (theWorkspace->AspectFace (Standard_True)->AspectEdge());
468 const OpenGl_AspectLine* anAspect = theWorkspace->AspectLine (Standard_True);
30f0ad28 469
ca3c13d1 470#if !defined(GL_ES_VERSION_2_0)
7d3e64ef 471 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
ca3c13d1 472#endif
7d3e64ef 473
4e1523ef 474 if (aGlContext->core20fwd != NULL)
7d3e64ef 475 {
8625ef7e 476 aGlContext->ShaderManager()->BindProgram (anAspect, NULL, Standard_False, Standard_False, anAspect->ShaderProgramRes (aGlContext));
2166f0fa 477 }
7fd59977 478
5e27df78 479 /// OCC22236 NOTE: draw edges for all situations:
871fa103 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.
5e27df78 482 /// 3) draw primitive's edges by vertexes if no edges and bounds array is specified
7d3e64ef 483 myVboAttribs->BindPositionAttribute (aGlContext);
8625ef7e 484 aGlContext->SetColor4fv (*(const OpenGl_Vec4* )theEdgeColour->rgb);
7d3e64ef 485 if (!myVboIndices.IsNull())
bf75be98 486 {
7d3e64ef 487 myVboIndices->Bind (aGlContext);
488 GLubyte* anOffset = myVboIndices->GetDataOffset();
2166f0fa 489
7d3e64ef 490 // draw primitives by vertex count with the indices
491 if (!myBounds.IsNull())
2166f0fa 492 {
7d3e64ef 493 const size_t aStride = myVboIndices->GetDataType() == GL_UNSIGNED_SHORT ? sizeof(unsigned short) : sizeof(unsigned int);
871fa103 494 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
2166f0fa 495 {
871fa103 496 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
7d3e64ef 497 glDrawElements (myDrawMode, aNbElemsInGroup, myVboIndices->GetDataType(), anOffset);
498 anOffset += aStride * aNbElemsInGroup;
7fd59977 499 }
500 }
7d3e64ef 501 // draw one (or sequential) primitive by the indices
2166f0fa
SK
502 else
503 {
7d3e64ef 504 glDrawElements (myDrawMode, myVboIndices->GetElemsNb(), myVboIndices->GetDataType(), anOffset);
762aacae 505 }
7d3e64ef 506 myVboIndices->Unbind (aGlContext);
2166f0fa 507 }
7d3e64ef 508 else if (!myBounds.IsNull())
2166f0fa 509 {
7d3e64ef 510 GLint aFirstElem = 0;
511 for (Standard_Integer aGroupIter = 0; aGroupIter < myBounds->NbBounds; ++aGroupIter)
2166f0fa 512 {
7d3e64ef 513 const GLint aNbElemsInGroup = myBounds->Bounds[aGroupIter];
514 glDrawArrays (myDrawMode, aFirstElem, aNbElemsInGroup);
515 aFirstElem += aNbElemsInGroup;
7fd59977 516 }
2166f0fa 517 }
7d3e64ef 518 else
519 {
520 glDrawArrays (myDrawMode, 0, myAttribs->NbElements);
521 }
522
523 // unbind buffers
524 myVboAttribs->UnbindAttribute (aGlContext, Graphic3d_TOA_POS);
2166f0fa 525
8625ef7e 526 // restore line context
8625ef7e 527 theWorkspace->SetAspectLine (anAspectLineOld);
7fd59977 528}
529
a577aaab 530// =======================================================================
7d3e64ef 531// function : drawMarkers
a577aaab 532// purpose :
533// =======================================================================
7d3e64ef 534void OpenGl_PrimitiveArray::drawMarkers (const Handle(OpenGl_Workspace)& theWorkspace) const
a577aaab 535{
536 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker (Standard_True);
537 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
8625ef7e 538 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (aCtx);
539 if (!aSpriteNorm.IsNull()
540 && !aSpriteNorm->IsDisplayList())
a577aaab 541 {
542 // Textured markers will be drawn with the point sprites
8625ef7e 543 aCtx->SetPointSize (anAspectMarker->MarkerSize());
ca3c13d1 544 #if !defined(GL_ES_VERSION_2_0)
4e1523ef 545 if (aCtx->core11 != NULL)
546 {
547 aCtx->core11fwd->glEnable (GL_ALPHA_TEST);
548 aCtx->core11fwd->glAlphaFunc (GL_GEQUAL, 0.1f);
549 }
ca3c13d1 550 #endif
a577aaab 551
8625ef7e 552 aCtx->core11fwd->glEnable (GL_BLEND);
553 aCtx->core11fwd->glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
a577aaab 554
8625ef7e 555 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
a577aaab 556
8625ef7e 557 aCtx->core11fwd->glDisable (GL_BLEND);
ca3c13d1 558 #if !defined(GL_ES_VERSION_2_0)
4e1523ef 559 if (aCtx->core11 != NULL)
560 {
561 aCtx->core11fwd->glDisable (GL_ALPHA_TEST);
562 }
ca3c13d1 563 #endif
8625ef7e 564 aCtx->SetPointSize (1.0f);
a577aaab 565 return;
566 }
8625ef7e 567 else if (anAspectMarker->Type() == Aspect_TOM_POINT)
a577aaab 568 {
8625ef7e 569 aCtx->SetPointSize (anAspectMarker->MarkerSize());
570 aCtx->core11fwd->glDrawArrays (myDrawMode, 0, !myVboAttribs.IsNull() ? myVboAttribs->GetElemsNb() : myAttribs->NbElements);
571 aCtx->SetPointSize (1.0f);
a577aaab 572 }
ca3c13d1 573#if !defined(GL_ES_VERSION_2_0)
8625ef7e 574 // Textured markers will be drawn with the glBitmap
575 else if (anAspectMarker->Type() != Aspect_TOM_POINT
576 && !aSpriteNorm.IsNull())
a577aaab 577 {
871fa103 578 /**if (!isHilight && (myPArray->vcolours != NULL))
a577aaab 579 {
871fa103 580 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
a577aaab 581 {
871fa103 582 glColor4ubv (myPArray->vcolours[anIter].GetData());
583 glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
a577aaab 584 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
585 }
586 }
871fa103 587 else*/
a577aaab 588 {
871fa103 589 for (Standard_Integer anIter = 0; anIter < myAttribs->NbElements; anIter++)
a577aaab 590 {
8625ef7e 591 aCtx->core11->glRasterPos3fv (myAttribs->Value<Graphic3d_Vec3> (anIter).GetData());
a577aaab 592 aSpriteNorm->DrawBitmap (theWorkspace->GetGlContext());
593 }
594 }
595 }
ca3c13d1 596#endif
a577aaab 597}
598
e1c659da 599// =======================================================================
600// function : OpenGl_PrimitiveArray
601// purpose :
602// =======================================================================
603OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver)
604
605: myDrawMode (DRAW_MODE_NONE),
606 myIsVboInit (Standard_False)
607{
608 if (theDriver != NULL)
609 {
610 myUID = theDriver->GetNextPrimitiveArrayUID();
611 }
612}
613
2166f0fa
SK
614// =======================================================================
615// function : OpenGl_PrimitiveArray
616// purpose :
617// =======================================================================
8d3f219f 618OpenGl_PrimitiveArray::OpenGl_PrimitiveArray (const OpenGl_GraphicDriver* theDriver,
619 const Graphic3d_TypeOfPrimitiveArray theType,
871fa103 620 const Handle(Graphic3d_IndexBuffer)& theIndices,
621 const Handle(Graphic3d_Buffer)& theAttribs,
622 const Handle(Graphic3d_BoundBuffer)& theBounds)
8d3f219f 623
871fa103 624: myIndices (theIndices),
625 myAttribs (theAttribs),
626 myBounds (theBounds),
627 myDrawMode (DRAW_MODE_NONE),
c827ea3a 628 myIsVboInit (Standard_False)
2166f0fa 629{
c827ea3a 630 if (theDriver != NULL)
631 {
632 myUID = theDriver->GetNextPrimitiveArrayUID();
633 }
634
871fa103 635 if (!myIndices.IsNull()
636 && myIndices->NbElements < 1)
637 {
638 // dummy index buffer?
639 myIndices.Nullify();
640 }
871fa103 641
a79f67f8 642 setDrawMode (theType);
2166f0fa
SK
643}
644
645// =======================================================================
646// function : ~OpenGl_PrimitiveArray
647// purpose :
648// =======================================================================
5e27df78 649OpenGl_PrimitiveArray::~OpenGl_PrimitiveArray()
2166f0fa 650{
5e27df78 651 //
652}
2166f0fa 653
5e27df78 654// =======================================================================
655// function : Release
656// purpose :
657// =======================================================================
10b9c7df 658void OpenGl_PrimitiveArray::Release (OpenGl_Context* theContext)
5e27df78 659{
c827ea3a 660 myIsVboInit = Standard_False;
871fa103 661 if (!myVboIndices.IsNull())
2166f0fa 662 {
10b9c7df 663 if (theContext)
5e27df78 664 {
871fa103 665 theContext->DelayedRelease (myVboIndices);
666 }
667 myVboIndices.Nullify();
668 }
669 if (!myVboAttribs.IsNull())
670 {
10b9c7df 671 if (theContext)
871fa103 672 {
673 theContext->DelayedRelease (myVboAttribs);
5e27df78 674 }
871fa103 675 myVboAttribs.Nullify();
2166f0fa
SK
676 }
677}
678
679// =======================================================================
680// function : Render
681// purpose :
682// =======================================================================
683void OpenGl_PrimitiveArray::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
684{
871fa103 685 if (myDrawMode == DRAW_MODE_NONE)
bf75be98 686 {
2166f0fa 687 return;
bf75be98 688 }
2166f0fa 689
a577aaab 690 const OpenGl_AspectFace* anAspectFace = theWorkspace->AspectFace (Standard_True);
691 const OpenGl_AspectLine* anAspectLine = theWorkspace->AspectLine (Standard_True);
692 const OpenGl_AspectMarker* anAspectMarker = theWorkspace->AspectMarker (myDrawMode == GL_POINTS);
693
2166f0fa 694 // create VBOs on first render call
58655684 695 const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext();
7d3e64ef 696 if (!myIsVboInit)
2166f0fa 697 {
7d3e64ef 698 // compatibility - keep data to draw markers using display lists
699 const Standard_Boolean toKeepData = myDrawMode == GL_POINTS
8625ef7e 700 && !anAspectMarker->SpriteRes (aCtx).IsNull()
701 && anAspectMarker->SpriteRes (aCtx)->IsDisplayList();
7d3e64ef 702 buildVBO (aCtx, toKeepData);
5e27df78 703 myIsVboInit = Standard_True;
2166f0fa
SK
704 }
705
fd4a6963 706 Tint aFrontLightingModel = anAspectFace->IntFront().color_mask;
707 const TEL_COLOUR* anInteriorColor = &anAspectFace->IntFront().matcol;
2166f0fa 708 const TEL_COLOUR* anEdgeColor = &anAspectFace->AspectEdge()->Color();
871fa103 709 const TEL_COLOUR* aLineColor = myDrawMode == GL_POINTS ? &anAspectMarker->Color() : &anAspectLine->Color();
2166f0fa
SK
710
711 // Use highlight colors
712 if (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT)
bf75be98 713 {
2166f0fa
SK
714 anEdgeColor = anInteriorColor = aLineColor = theWorkspace->HighlightColor;
715 aFrontLightingModel = 0;
716 }
717
8625ef7e 718 const Standard_Boolean hasColorAttrib = !myVboAttribs.IsNull()
719 && myVboAttribs->HasColorAttribute();
720 const Standard_Boolean isLightOn = aFrontLightingModel != 0
721 && !myVboAttribs.IsNull()
722 && myVboAttribs->HasNormalAttribute();
ca3c13d1 723#if !defined(GL_ES_VERSION_2_0)
7d3e64ef 724 // manage FFP lighting
4e1523ef 725 if (aCtx->core11 != NULL)
7d3e64ef 726 {
4e1523ef 727 if (!isLightOn)
728 {
729 glDisable (GL_LIGHTING);
730 }
731 else
732 {
733 glEnable (GL_LIGHTING);
734 }
7d3e64ef 735 }
ca3c13d1 736#endif
8625ef7e 737 // Temporarily disable environment mapping
738 Handle(OpenGl_Texture) aTextureBack;
739 if (myDrawMode <= GL_LINE_STRIP)
740 {
741 aTextureBack = theWorkspace->DisableTexture();
742 }
7d3e64ef 743
744 if ((myDrawMode > GL_LINE_STRIP && anAspectFace->InteriorStyle() != Aspect_IS_EMPTY) ||
745 (myDrawMode <= GL_LINE_STRIP))
746 {
747 const bool toHilight = (theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) != 0;
748 const Graphic3d_Vec4* aFaceColors = !myBounds.IsNull() && !toHilight && anAspectFace->InteriorStyle() != Aspect_IS_HIDDENLINE
749 ? myBounds->Colors
750 : NULL;
8625ef7e 751 const Standard_Boolean hasVertColor = hasColorAttrib && !toHilight;
4e1523ef 752 if (aCtx->core20fwd != NULL)
8625ef7e 753 {
754 switch (myDrawMode)
755 {
756 case GL_POINTS:
757 {
758 const Handle(OpenGl_PointSprite)& aSpriteNorm = anAspectMarker->SpriteRes (aCtx);
759 if (!aSpriteNorm.IsNull()
760 && !aSpriteNorm->IsDisplayList())
761 {
762 const Handle(OpenGl_PointSprite)& aSprite = (toHilight && anAspectMarker->SpriteHighlightRes (aCtx)->IsValid())
763 ? anAspectMarker->SpriteHighlightRes (aCtx)
764 : aSpriteNorm;
765 theWorkspace->EnableTexture (aSprite);
766 aCtx->ShaderManager()->BindProgram (anAspectMarker, aSprite, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
767 }
768 else
769 {
770 aCtx->ShaderManager()->BindProgram (anAspectMarker, NULL, isLightOn, hasVertColor, anAspectMarker->ShaderProgramRes (aCtx));
771 }
772 break;
773 }
774 case GL_LINES:
775 case GL_LINE_STRIP:
776 {
777 aCtx->ShaderManager()->BindProgram (anAspectLine, NULL, isLightOn, hasVertColor, anAspectLine->ShaderProgramRes (aCtx));
778 break;
779 }
780 default:
781 {
6c6aadb1 782 const Standard_Boolean isLightOnFace = isLightOn
783 && (theWorkspace->ActiveTexture().IsNull()
784 || theWorkspace->ActiveTexture()->GetParams()->IsModulate());
785 aCtx->ShaderManager()->BindProgram (anAspectFace, theWorkspace->ActiveTexture(), isLightOnFace, hasVertColor, anAspectFace->ShaderProgramRes (aCtx));
8625ef7e 786 break;
787 }
788 }
789 }
790
791 aCtx->SetColor4fv (*(const OpenGl_Vec4* )(myDrawMode <= GL_LINE_STRIP ? aLineColor->rgb : anInteriorColor->rgb));
7d3e64ef 792
8625ef7e 793 drawArray (theWorkspace, aFaceColors, hasColorAttrib);
7d3e64ef 794 }
795
8625ef7e 796 if (myDrawMode <= GL_LINE_STRIP)
797 {
798 theWorkspace->EnableTexture (aTextureBack);
799 }
800 else
7d3e64ef 801 {
802 if (anAspectFace->Edge()
803 || anAspectFace->InteriorStyle() == Aspect_IS_HIDDENLINE)
30f0ad28 804 {
7d3e64ef 805 drawEdges (anEdgeColor, theWorkspace);
b34efb62 806
b34efb62 807 // restore OpenGL polygon mode if needed
65360da3 808 #if !defined(GL_ES_VERSION_2_0)
b34efb62 809 if (anAspectFace->InteriorStyle() >= Aspect_IS_HATCH)
810 {
811 glPolygonMode (GL_FRONT_AND_BACK,
812 anAspectFace->InteriorStyle() == Aspect_IS_POINT ? GL_POINT : GL_FILL);
813 }
814 #endif
30f0ad28 815 }
816 }
817
7d3e64ef 818 aCtx->BindProgram (NULL);
2166f0fa 819}
a79f67f8 820
821// =======================================================================
822// function : setDrawMode
823// purpose :
824// =======================================================================
825void OpenGl_PrimitiveArray::setDrawMode (const Graphic3d_TypeOfPrimitiveArray theType)
826{
827 if (myAttribs.IsNull())
828 {
829 myDrawMode = DRAW_MODE_NONE;
830 return;
831 }
832
833 switch (theType)
834 {
835 case Graphic3d_TOPA_POINTS:
836 myDrawMode = GL_POINTS;
837 break;
838 case Graphic3d_TOPA_POLYLINES:
839 myDrawMode = GL_LINE_STRIP;
840 break;
841 case Graphic3d_TOPA_SEGMENTS:
842 myDrawMode = GL_LINES;
843 break;
844 case Graphic3d_TOPA_TRIANGLES:
845 myDrawMode = GL_TRIANGLES;
846 break;
847 case Graphic3d_TOPA_TRIANGLESTRIPS:
848 myDrawMode = GL_TRIANGLE_STRIP;
849 break;
850 case Graphic3d_TOPA_TRIANGLEFANS:
851 myDrawMode = GL_TRIANGLE_FAN;
852 break;
853 #if !defined(GL_ES_VERSION_2_0)
854 case Graphic3d_TOPA_POLYGONS:
855 myDrawMode = GL_POLYGON;
856 break;
857 case Graphic3d_TOPA_QUADRANGLES:
858 myDrawMode = GL_QUADS;
859 break;
860 case Graphic3d_TOPA_QUADRANGLESTRIPS:
861 myDrawMode = GL_QUAD_STRIP;
862 break;
863 #else
864 case Graphic3d_TOPA_POLYGONS:
865 case Graphic3d_TOPA_QUADRANGLES:
866 case Graphic3d_TOPA_QUADRANGLESTRIPS:
867 #endif
868 case Graphic3d_TOPA_UNDEFINED:
869 break;
870 }
871}
872
873// =======================================================================
874// function : InitBuffers
875// purpose :
876// =======================================================================
877void OpenGl_PrimitiveArray::InitBuffers (const Handle(OpenGl_Context)& theContext,
878 const Graphic3d_TypeOfPrimitiveArray theType,
879 const Handle(Graphic3d_IndexBuffer)& theIndices,
880 const Handle(Graphic3d_Buffer)& theAttribs,
881 const Handle(Graphic3d_BoundBuffer)& theBounds)
882{
883 // Release old graphic resources
884 Release (theContext.operator->());
885
886 myIndices = theIndices;
887 myAttribs = theAttribs;
888 myBounds = theBounds;
889
890 setDrawMode (theType);
891}