| 1 | // Created on: 2011-08-01 |
| 2 | // Created by: Sergey ZERCHANINOV |
| 3 | // Copyright (c) 2011-2014 OPEN CASCADE SAS |
| 4 | // |
| 5 | // This file is part of Open CASCADE Technology software library. |
| 6 | // |
| 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. |
| 12 | // |
| 13 | // Alternatively, this file may be used under the terms of Open CASCADE |
| 14 | // commercial license or contractual agreement. |
| 15 | |
| 16 | #include <OpenGl_CappingAlgo.hxx> |
| 17 | #include <OpenGl_Context.hxx> |
| 18 | #include <OpenGl_GlCore11.hxx> |
| 19 | #include <OpenGl_GraphicDriver.hxx> |
| 20 | #include <OpenGl_ShaderManager.hxx> |
| 21 | #include <OpenGl_ShaderProgram.hxx> |
| 22 | #include <OpenGl_StructureShadow.hxx> |
| 23 | #include <OpenGl_telem_util.hxx> |
| 24 | #include <OpenGl_Vec.hxx> |
| 25 | #include <OpenGl_View.hxx> |
| 26 | #include <OpenGl_Workspace.hxx> |
| 27 | |
| 28 | #include <Graphic3d_SequenceOfHClipPlane_Handle.hxx> |
| 29 | |
| 30 | IMPLEMENT_STANDARD_HANDLE (OpenGl_Structure, Graphic3d_CStructure) |
| 31 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Structure, Graphic3d_CStructure) |
| 32 | |
| 33 | //! Auxiliary class for bounding box presentation |
| 34 | class OpenGl_BndBoxPrs : public OpenGl_Element |
| 35 | { |
| 36 | |
| 37 | public: |
| 38 | |
| 39 | //! Main constructor |
| 40 | OpenGl_BndBoxPrs (const Graphic3d_BndBox4f& theBndBox) |
| 41 | { |
| 42 | const float Xm = theBndBox.CornerMin().x(); |
| 43 | const float Ym = theBndBox.CornerMin().y(); |
| 44 | const float Zm = theBndBox.CornerMin().z(); |
| 45 | const float XM = theBndBox.CornerMax().x(); |
| 46 | const float YM = theBndBox.CornerMax().y(); |
| 47 | const float ZM = theBndBox.CornerMax().z(); |
| 48 | |
| 49 | myVerts[0] = OpenGl_Vec3 (Xm, Ym, Zm); |
| 50 | myVerts[1] = OpenGl_Vec3 (Xm, Ym, ZM); |
| 51 | myVerts[2] = OpenGl_Vec3 (Xm, YM, ZM); |
| 52 | myVerts[3] = OpenGl_Vec3 (Xm, YM, Zm); |
| 53 | myVerts[4] = OpenGl_Vec3 (Xm, Ym, Zm); |
| 54 | myVerts[5] = OpenGl_Vec3 (XM, Ym, Zm); |
| 55 | myVerts[6] = OpenGl_Vec3 (XM, Ym, ZM); |
| 56 | myVerts[7] = OpenGl_Vec3 (XM, YM, ZM); |
| 57 | myVerts[8] = OpenGl_Vec3 (XM, YM, Zm); |
| 58 | myVerts[9] = OpenGl_Vec3 (XM, Ym, Zm); |
| 59 | myVerts[10] = OpenGl_Vec3 (XM, YM, Zm); |
| 60 | myVerts[11] = OpenGl_Vec3 (Xm, YM, Zm); |
| 61 | myVerts[12] = OpenGl_Vec3 (Xm, YM, ZM); |
| 62 | myVerts[13] = OpenGl_Vec3 (XM, YM, ZM); |
| 63 | myVerts[14] = OpenGl_Vec3 (XM, Ym, ZM); |
| 64 | myVerts[15] = OpenGl_Vec3 (Xm, Ym, ZM); |
| 65 | } |
| 66 | |
| 67 | //! Render presentation |
| 68 | virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const |
| 69 | { |
| 70 | #if !defined(GL_ES_VERSION_2_0) |
| 71 | // Apply line aspect |
| 72 | const OpenGl_AspectLine* anAspectLine = theWorkspace->AspectLine (Standard_True); |
| 73 | const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture(); |
| 74 | |
| 75 | glDisable (GL_LIGHTING); |
| 76 | if ((theWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0) |
| 77 | { |
| 78 | glDepthMask (GL_FALSE); |
| 79 | } |
| 80 | |
| 81 | // Use highlight colors |
| 82 | theWorkspace->GetGlContext()->core11->glColor3fv ((theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) ? theWorkspace->HighlightColor->rgb : anAspectLine->Color().rgb); |
| 83 | |
| 84 | glEnableClientState (GL_VERTEX_ARRAY); |
| 85 | glVertexPointer (3, GL_FLOAT, 0, (GLfloat* )&myVerts); |
| 86 | glDrawArrays (GL_LINE_STRIP, 0, 16); |
| 87 | glDisableClientState (GL_VERTEX_ARRAY); |
| 88 | |
| 89 | // restore aspects |
| 90 | if (!aPrevTexture.IsNull()) |
| 91 | { |
| 92 | theWorkspace->EnableTexture (aPrevTexture); |
| 93 | } |
| 94 | #endif |
| 95 | } |
| 96 | |
| 97 | //! Release graphical resources |
| 98 | virtual void Release (OpenGl_Context*) |
| 99 | { |
| 100 | // |
| 101 | } |
| 102 | |
| 103 | protected: |
| 104 | |
| 105 | //! Protected destructor |
| 106 | virtual ~OpenGl_BndBoxPrs() {} |
| 107 | |
| 108 | private: |
| 109 | |
| 110 | OpenGl_Vec3 myVerts[16]; //!< vertices array |
| 111 | |
| 112 | public: |
| 113 | |
| 114 | DEFINE_STANDARD_ALLOC |
| 115 | |
| 116 | }; |
| 117 | |
| 118 | /*----------------------------------------------------------------------*/ |
| 119 | |
| 120 | // ======================================================================= |
| 121 | // function : OpenGl_Structure |
| 122 | // purpose : |
| 123 | // ======================================================================= |
| 124 | OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager) |
| 125 | : Graphic3d_CStructure (theManager), |
| 126 | myTransformation(NULL), |
| 127 | myTransPers(NULL), |
| 128 | myAspectLine(NULL), |
| 129 | myAspectFace(NULL), |
| 130 | myAspectMarker(NULL), |
| 131 | myAspectText(NULL), |
| 132 | myHighlightColor(NULL), |
| 133 | myNamedStatus(0), |
| 134 | myZLayer(0), |
| 135 | myIsRaytracable (Standard_False), |
| 136 | myModificationState (0), |
| 137 | myIsCulled (Standard_True), |
| 138 | myIsMirrored (Standard_False) |
| 139 | { |
| 140 | UpdateNamedStatus(); |
| 141 | } |
| 142 | |
| 143 | // ======================================================================= |
| 144 | // function : ~OpenGl_Structure |
| 145 | // purpose : |
| 146 | // ======================================================================= |
| 147 | OpenGl_Structure::~OpenGl_Structure() |
| 148 | { |
| 149 | Release (Handle(OpenGl_Context)()); |
| 150 | delete myTransformation; myTransformation = NULL; |
| 151 | delete myTransPers; myTransPers = NULL; |
| 152 | } |
| 153 | |
| 154 | // ======================================================================= |
| 155 | // function : UpdateAspects |
| 156 | // purpose : |
| 157 | // ======================================================================= |
| 158 | void OpenGl_Structure::UpdateAspects() |
| 159 | { |
| 160 | SetTransformPersistence (TransformPersistence); |
| 161 | |
| 162 | if (ContextLine.IsDef) |
| 163 | SetAspectLine (ContextLine); |
| 164 | |
| 165 | if (ContextFillArea.IsDef) |
| 166 | SetAspectFace (ContextFillArea); |
| 167 | |
| 168 | if (ContextMarker.IsDef) |
| 169 | SetAspectMarker (ContextMarker); |
| 170 | |
| 171 | if (ContextText.IsDef) |
| 172 | SetAspectText (ContextText); |
| 173 | } |
| 174 | |
| 175 | // ======================================================================= |
| 176 | // function : UpdateTransformation |
| 177 | // purpose : |
| 178 | // ======================================================================= |
| 179 | void OpenGl_Structure::UpdateTransformation() |
| 180 | { |
| 181 | if (myTransformation == NULL) |
| 182 | { |
| 183 | myTransformation = new OpenGl_Matrix(); |
| 184 | } |
| 185 | |
| 186 | Standard_ShortReal (*aMat)[4] = Graphic3d_CStructure::Transformation; |
| 187 | |
| 188 | Standard_ShortReal aDet = |
| 189 | aMat[0][0] * (aMat[1][1] * aMat[2][2] - aMat[2][1] * aMat[1][2]) - |
| 190 | aMat[0][1] * (aMat[1][0] * aMat[2][2] - aMat[2][0] * aMat[1][2]) + |
| 191 | aMat[0][2] * (aMat[1][0] * aMat[2][1] - aMat[2][0] * aMat[1][1]); |
| 192 | |
| 193 | // Determinant of transform matrix less then 0 means that mirror transform applied. |
| 194 | myIsMirrored = aDet < 0.0f; |
| 195 | |
| 196 | matcpy (myTransformation->mat, &Graphic3d_CStructure::Transformation[0][0]); |
| 197 | |
| 198 | if (myIsRaytracable) |
| 199 | { |
| 200 | UpdateStateWithAncestorStructures(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // ======================================================================= |
| 205 | // function : SetTransformPersistence |
| 206 | // purpose : |
| 207 | // ======================================================================= |
| 208 | void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers) |
| 209 | { |
| 210 | if (!myTransPers) |
| 211 | myTransPers = new TEL_TRANSFORM_PERSISTENCE; |
| 212 | |
| 213 | myTransPers->mode = ATransPers.Flag; |
| 214 | myTransPers->pointX = ATransPers.Point.x; |
| 215 | myTransPers->pointY = ATransPers.Point.y; |
| 216 | myTransPers->pointZ = ATransPers.Point.z; |
| 217 | MarkAsNotCulled(); |
| 218 | } |
| 219 | |
| 220 | // ======================================================================= |
| 221 | // function : SetAspectLine |
| 222 | // purpose : |
| 223 | // ======================================================================= |
| 224 | void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &theAspect) |
| 225 | { |
| 226 | if (!myAspectLine) |
| 227 | { |
| 228 | myAspectLine = new OpenGl_AspectLine(); |
| 229 | } |
| 230 | myAspectLine->SetAspect (theAspect); |
| 231 | } |
| 232 | |
| 233 | // ======================================================================= |
| 234 | // function : SetAspectFace |
| 235 | // purpose : |
| 236 | // ======================================================================= |
| 237 | void OpenGl_Structure::SetAspectFace (const CALL_DEF_CONTEXTFILLAREA& theAspect) |
| 238 | { |
| 239 | if (!myAspectFace) |
| 240 | { |
| 241 | myAspectFace = new OpenGl_AspectFace(); |
| 242 | } |
| 243 | myAspectFace->SetAspect (theAspect); |
| 244 | |
| 245 | if (myIsRaytracable) |
| 246 | { |
| 247 | UpdateStateWithAncestorStructures(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // ======================================================================= |
| 252 | // function : SetAspectMarker |
| 253 | // purpose : |
| 254 | // ======================================================================= |
| 255 | void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER& theAspect) |
| 256 | { |
| 257 | if (!myAspectMarker) |
| 258 | { |
| 259 | myAspectMarker = new OpenGl_AspectMarker(); |
| 260 | } |
| 261 | myAspectMarker->SetAspect (theAspect); |
| 262 | } |
| 263 | |
| 264 | // ======================================================================= |
| 265 | // function : SetAspectText |
| 266 | // purpose : |
| 267 | // ======================================================================= |
| 268 | void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &theAspect) |
| 269 | { |
| 270 | if (!myAspectText) |
| 271 | { |
| 272 | myAspectText = new OpenGl_AspectText(); |
| 273 | } |
| 274 | myAspectText->SetAspect (theAspect); |
| 275 | } |
| 276 | |
| 277 | // ======================================================================= |
| 278 | // function : clearHighlightBox |
| 279 | // purpose : |
| 280 | // ======================================================================= |
| 281 | void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx) |
| 282 | { |
| 283 | if (!myHighlightBox.IsNull()) |
| 284 | { |
| 285 | myHighlightBox->Release (theGlCtx); |
| 286 | myHighlightBox.Nullify(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // ======================================================================= |
| 291 | // function : HighlightWithColor |
| 292 | // purpose : |
| 293 | // ======================================================================= |
| 294 | void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3& theColor, |
| 295 | const Standard_Boolean theToCreate) |
| 296 | { |
| 297 | const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext(); |
| 298 | if (theToCreate) |
| 299 | setHighlightColor (aContext, theColor); |
| 300 | else |
| 301 | clearHighlightColor (aContext); |
| 302 | } |
| 303 | |
| 304 | // ======================================================================= |
| 305 | // function : HighlightWithBndBox |
| 306 | // purpose : |
| 307 | // ======================================================================= |
| 308 | void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct, |
| 309 | const Standard_Boolean theToCreate) |
| 310 | { |
| 311 | const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext(); |
| 312 | if (!theToCreate) |
| 313 | { |
| 314 | clearHighlightBox (aContext); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | if (!myHighlightBox.IsNull()) |
| 319 | { |
| 320 | myHighlightBox->Release (aContext); |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | myHighlightBox = new OpenGl_Group (theStruct); |
| 325 | } |
| 326 | |
| 327 | CALL_DEF_CONTEXTLINE& aContextLine = myHighlightBox->ChangeContextLine(); |
| 328 | aContextLine.IsDef = 1; |
| 329 | aContextLine.Color = HighlightColor; |
| 330 | aContextLine.LineType = Aspect_TOL_SOLID; |
| 331 | aContextLine.Width = 1.0f; |
| 332 | myHighlightBox->UpdateAspectLine (Standard_True); |
| 333 | |
| 334 | OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox); |
| 335 | myHighlightBox->AddElement (aBndBoxPrs); |
| 336 | } |
| 337 | |
| 338 | // ======================================================================= |
| 339 | // function : setHighlightColor |
| 340 | // purpose : |
| 341 | // ======================================================================= |
| 342 | void OpenGl_Structure::setHighlightColor (const Handle(OpenGl_Context)& theGlCtx, |
| 343 | const Graphic3d_Vec3& theColor) |
| 344 | { |
| 345 | clearHighlightBox (theGlCtx); |
| 346 | if (myHighlightColor == NULL) |
| 347 | { |
| 348 | myHighlightColor = new TEL_COLOUR(); |
| 349 | } |
| 350 | |
| 351 | myHighlightColor->rgb[0] = theColor.r(); |
| 352 | myHighlightColor->rgb[1] = theColor.g(); |
| 353 | myHighlightColor->rgb[2] = theColor.b(); |
| 354 | myHighlightColor->rgb[3] = 1.F; |
| 355 | } |
| 356 | |
| 357 | // ======================================================================= |
| 358 | // function : clearHighlightColor |
| 359 | // purpose : |
| 360 | // ======================================================================= |
| 361 | void OpenGl_Structure::clearHighlightColor (const Handle(OpenGl_Context)& theGlCtx) |
| 362 | { |
| 363 | clearHighlightBox(theGlCtx); |
| 364 | delete myHighlightColor; |
| 365 | myHighlightColor = NULL; |
| 366 | } |
| 367 | |
| 368 | // ======================================================================= |
| 369 | // function : UpdateNamedStatus |
| 370 | // purpose : |
| 371 | // ======================================================================= |
| 372 | void OpenGl_Structure::UpdateNamedStatus() |
| 373 | { |
| 374 | myNamedStatus = 0; |
| 375 | if (highlight) myNamedStatus |= OPENGL_NS_HIGHLIGHT; |
| 376 | if (!visible) myNamedStatus |= OPENGL_NS_HIDE; |
| 377 | |
| 378 | if (myIsRaytracable) |
| 379 | { |
| 380 | UpdateStateWithAncestorStructures(); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // ======================================================================= |
| 385 | // function : RegisterAncestorStructure |
| 386 | // purpose : |
| 387 | // ======================================================================= |
| 388 | void OpenGl_Structure::RegisterAncestorStructure (const OpenGl_Structure* theStructure) const |
| 389 | { |
| 390 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 391 | { |
| 392 | if (anIt.Value() == theStructure) |
| 393 | { |
| 394 | return; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | myAncestorStructures.Append (theStructure); |
| 399 | } |
| 400 | |
| 401 | // ======================================================================= |
| 402 | // function : UnregisterAncestorStructure |
| 403 | // purpose : |
| 404 | // ======================================================================= |
| 405 | void OpenGl_Structure::UnregisterAncestorStructure (const OpenGl_Structure* theStructure) const |
| 406 | { |
| 407 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 408 | { |
| 409 | if (anIt.Value() == theStructure) |
| 410 | { |
| 411 | myAncestorStructures.Remove (anIt); |
| 412 | return; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // ======================================================================= |
| 418 | // function : UnregisterFromAncestorStructure |
| 419 | // purpose : |
| 420 | // ======================================================================= |
| 421 | void OpenGl_Structure::UnregisterFromAncestorStructure() const |
| 422 | { |
| 423 | for (OpenGl_ListOfStructure::Iterator anIta (myAncestorStructures); anIta.More(); anIta.Next()) |
| 424 | { |
| 425 | OpenGl_Structure* anAncestor = const_cast<OpenGl_Structure*> (anIta.ChangeValue()); |
| 426 | |
| 427 | for (OpenGl_ListOfStructure::Iterator anIts (anAncestor->myConnected); anIts.More(); anIts.Next()) |
| 428 | { |
| 429 | if (anIts.Value() == this) |
| 430 | { |
| 431 | anAncestor->myConnected.Remove (anIts); |
| 432 | return; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | // ======================================================================= |
| 439 | // function : UpdateStateWithAncestorStructures |
| 440 | // purpose : |
| 441 | // ======================================================================= |
| 442 | void OpenGl_Structure::UpdateStateWithAncestorStructures() const |
| 443 | { |
| 444 | myModificationState++; |
| 445 | |
| 446 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 447 | { |
| 448 | anIt.Value()->UpdateStateWithAncestorStructures(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // ======================================================================= |
| 453 | // function : UpdateRaytracableWithAncestorStructures |
| 454 | // purpose : |
| 455 | // ======================================================================= |
| 456 | void OpenGl_Structure::UpdateRaytracableWithAncestorStructures() const |
| 457 | { |
| 458 | myIsRaytracable = OpenGl_Raytrace::IsRaytracedStructure (this); |
| 459 | |
| 460 | if (!myIsRaytracable) |
| 461 | { |
| 462 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 463 | { |
| 464 | anIt.Value()->UpdateRaytracableWithAncestorStructures(); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // ======================================================================= |
| 470 | // function : SetRaytracableWithAncestorStructures |
| 471 | // purpose : |
| 472 | // ======================================================================= |
| 473 | void OpenGl_Structure::SetRaytracableWithAncestorStructures() const |
| 474 | { |
| 475 | myIsRaytracable = Standard_True; |
| 476 | |
| 477 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 478 | { |
| 479 | if (!anIt.Value()->IsRaytracable()) |
| 480 | { |
| 481 | anIt.Value()->SetRaytracableWithAncestorStructures(); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // ======================================================================= |
| 487 | // function : Connect |
| 488 | // purpose : |
| 489 | // ======================================================================= |
| 490 | void OpenGl_Structure::Connect (Graphic3d_CStructure& theStructure) |
| 491 | { |
| 492 | OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure; |
| 493 | Disconnect (theStructure); |
| 494 | myConnected.Append (aStruct); |
| 495 | |
| 496 | if (aStruct->IsRaytracable()) |
| 497 | { |
| 498 | UpdateStateWithAncestorStructures(); |
| 499 | SetRaytracableWithAncestorStructures(); |
| 500 | } |
| 501 | |
| 502 | aStruct->RegisterAncestorStructure (this); |
| 503 | } |
| 504 | |
| 505 | // ======================================================================= |
| 506 | // function : Disconnect |
| 507 | // purpose : |
| 508 | // ======================================================================= |
| 509 | void OpenGl_Structure::Disconnect (Graphic3d_CStructure& theStructure) |
| 510 | { |
| 511 | OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure; |
| 512 | for (OpenGl_ListOfStructure::Iterator anIter (myConnected); anIter.More(); anIter.Next()) |
| 513 | { |
| 514 | // Check for the given structure |
| 515 | if (anIter.Value() == aStruct) |
| 516 | { |
| 517 | myConnected.Remove (anIter); |
| 518 | |
| 519 | if (aStruct->IsRaytracable()) |
| 520 | { |
| 521 | UpdateStateWithAncestorStructures(); |
| 522 | UpdateRaytracableWithAncestorStructures(); |
| 523 | } |
| 524 | |
| 525 | aStruct->UnregisterAncestorStructure (this); |
| 526 | return; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // ======================================================================= |
| 532 | // function : NewGroup |
| 533 | // purpose : |
| 534 | // ======================================================================= |
| 535 | Handle(Graphic3d_Group) OpenGl_Structure::NewGroup (const Handle(Graphic3d_Structure)& theStruct) |
| 536 | { |
| 537 | Handle(OpenGl_Group) aGroup = new OpenGl_Group (theStruct); |
| 538 | myGroups.Append (aGroup); |
| 539 | return aGroup; |
| 540 | } |
| 541 | |
| 542 | // ======================================================================= |
| 543 | // function : RemoveGroup |
| 544 | // purpose : |
| 545 | // ======================================================================= |
| 546 | void OpenGl_Structure::RemoveGroup (const Handle(Graphic3d_Group)& theGroup) |
| 547 | { |
| 548 | if (theGroup.IsNull()) |
| 549 | { |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
| 554 | { |
| 555 | // Check for the given group |
| 556 | if (aGroupIter.Value() == theGroup) |
| 557 | { |
| 558 | theGroup->Clear (Standard_False); |
| 559 | |
| 560 | if (((OpenGl_Group* )theGroup.operator->())->IsRaytracable()) |
| 561 | { |
| 562 | UpdateStateWithAncestorStructures(); |
| 563 | UpdateRaytracableWithAncestorStructures(); |
| 564 | } |
| 565 | |
| 566 | myGroups.Remove (aGroupIter); |
| 567 | return; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // ======================================================================= |
| 573 | // function : Clear |
| 574 | // purpose : |
| 575 | // ======================================================================= |
| 576 | void OpenGl_Structure::Clear() |
| 577 | { |
| 578 | Clear (GlDriver()->GetSharedContext()); |
| 579 | } |
| 580 | |
| 581 | // ======================================================================= |
| 582 | // function : Clear |
| 583 | // purpose : |
| 584 | // ======================================================================= |
| 585 | void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx) |
| 586 | { |
| 587 | Standard_Boolean aRaytracableGroupDeleted (Standard_False); |
| 588 | |
| 589 | // Release groups |
| 590 | for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
| 591 | { |
| 592 | aRaytracableGroupDeleted |= aGroupIter.Value()->IsRaytracable(); |
| 593 | |
| 594 | // Delete objects |
| 595 | aGroupIter.ChangeValue()->Release (theGlCtx); |
| 596 | } |
| 597 | myGroups.Clear(); |
| 598 | |
| 599 | if (aRaytracableGroupDeleted) |
| 600 | { |
| 601 | UpdateStateWithAncestorStructures(); |
| 602 | UpdateRaytracableWithAncestorStructures(); |
| 603 | } |
| 604 | |
| 605 | Is2dText = Standard_False; |
| 606 | IsForHighlight = Standard_False; |
| 607 | } |
| 608 | |
| 609 | // ======================================================================= |
| 610 | // function : RenderGeometry |
| 611 | // purpose : |
| 612 | // ======================================================================= |
| 613 | void OpenGl_Structure::RenderGeometry (const Handle(OpenGl_Workspace) &theWorkspace) const |
| 614 | { |
| 615 | // Render groups |
| 616 | const Graphic3d_SequenceOfGroup& aGroups = DrawGroups(); |
| 617 | for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next()) |
| 618 | { |
| 619 | aGroupIter.Value()->Render (theWorkspace); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | // ======================================================================= |
| 624 | // function : Render |
| 625 | // purpose : |
| 626 | // ======================================================================= |
| 627 | void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) const |
| 628 | { |
| 629 | // Process the structure only if visible |
| 630 | if (myNamedStatus & OPENGL_NS_HIDE) |
| 631 | { |
| 632 | return; |
| 633 | } |
| 634 | |
| 635 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); |
| 636 | |
| 637 | // Render named status |
| 638 | const Standard_Integer aNamedStatus = theWorkspace->NamedStatus; |
| 639 | theWorkspace->NamedStatus |= myNamedStatus; |
| 640 | |
| 641 | // Do we need to restore GL_NORMALIZE? |
| 642 | const Standard_Boolean anOldGlNormalize = aCtx->IsGlNormalizeEnabled(); |
| 643 | |
| 644 | // Apply local transformation |
| 645 | if (myTransformation) |
| 646 | { |
| 647 | OpenGl_Matrix aModelWorld; |
| 648 | OpenGl_Transposemat3 (&aModelWorld, myTransformation); |
| 649 | aCtx->ModelWorldState.Push(); |
| 650 | aCtx->ModelWorldState.SetCurrent (OpenGl_Mat4::Map ((Standard_ShortReal* )aModelWorld.mat)); |
| 651 | |
| 652 | Standard_ShortReal aScaleX = OpenGl_Vec3 (myTransformation->mat[0][0], |
| 653 | myTransformation->mat[0][1], |
| 654 | myTransformation->mat[0][2]).SquareModulus(); |
| 655 | // Scale transform detected. |
| 656 | if (Abs (aScaleX - 1.f) > Precision::Confusion()) |
| 657 | { |
| 658 | aCtx->SetGlNormalizeEnabled (Standard_True); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | // Apply transform persistence |
| 663 | const TEL_TRANSFORM_PERSISTENCE *aTransPersistence = NULL; |
| 664 | if ( myTransPers && myTransPers->mode != 0 ) |
| 665 | { |
| 666 | aTransPersistence = theWorkspace->ActiveView()->BeginTransformPersistence (aCtx, myTransPers); |
| 667 | } |
| 668 | |
| 669 | // Take into account transform persistence |
| 670 | aCtx->ApplyModelViewMatrix(); |
| 671 | |
| 672 | // Apply aspects |
| 673 | const OpenGl_AspectLine *anAspectLine = theWorkspace->AspectLine (Standard_False); |
| 674 | const OpenGl_AspectFace *anAspectFace = theWorkspace->AspectFace (Standard_False); |
| 675 | const OpenGl_AspectMarker *anAspectMarker = theWorkspace->AspectMarker (Standard_False); |
| 676 | const OpenGl_AspectText *anAspectText = theWorkspace->AspectText (Standard_False); |
| 677 | if (myAspectLine) |
| 678 | { |
| 679 | theWorkspace->SetAspectLine (myAspectLine); |
| 680 | } |
| 681 | if (myAspectFace) |
| 682 | { |
| 683 | theWorkspace->SetAspectFace (myAspectFace); |
| 684 | } |
| 685 | if (myAspectMarker) |
| 686 | { |
| 687 | theWorkspace->SetAspectMarker (myAspectMarker); |
| 688 | } |
| 689 | if (myAspectText) |
| 690 | { |
| 691 | theWorkspace->SetAspectText (myAspectText); |
| 692 | } |
| 693 | |
| 694 | // Apply correction for mirror transform |
| 695 | if (myIsMirrored) |
| 696 | { |
| 697 | aCtx->core11fwd->glFrontFace (GL_CW); |
| 698 | } |
| 699 | |
| 700 | // Apply highlight color |
| 701 | const TEL_COLOUR *aHighlightColor = theWorkspace->HighlightColor; |
| 702 | if (myHighlightColor) |
| 703 | theWorkspace->HighlightColor = myHighlightColor; |
| 704 | |
| 705 | // Render connected structures |
| 706 | OpenGl_ListOfStructure::Iterator anIter (myConnected); |
| 707 | while (anIter.More()) |
| 708 | { |
| 709 | anIter.Value()->RenderGeometry (theWorkspace); |
| 710 | anIter.Next(); |
| 711 | } |
| 712 | |
| 713 | // Set up plane equations for non-structure transformed global model-view matrix |
| 714 | // List of planes to be applied to context state |
| 715 | Handle(Graphic3d_SequenceOfHClipPlane) aUserPlanes; |
| 716 | |
| 717 | // Collect clipping planes of structure scope |
| 718 | if (!myClipPlanes.IsEmpty()) |
| 719 | { |
| 720 | Graphic3d_SequenceOfHClipPlane::Iterator aClippingIter (myClipPlanes); |
| 721 | for (; aClippingIter.More(); aClippingIter.Next()) |
| 722 | { |
| 723 | const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIter.Value(); |
| 724 | if (!aClipPlane->IsOn()) |
| 725 | { |
| 726 | continue; |
| 727 | } |
| 728 | |
| 729 | if (aUserPlanes.IsNull()) |
| 730 | { |
| 731 | aUserPlanes = new Graphic3d_SequenceOfHClipPlane(); |
| 732 | } |
| 733 | |
| 734 | aUserPlanes->Append (aClipPlane); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty()) |
| 739 | { |
| 740 | // add planes at loaded view matrix state |
| 741 | aCtx->ChangeClipping().AddWorld (*aUserPlanes, theWorkspace); |
| 742 | |
| 743 | // Set OCCT state uniform variables |
| 744 | if (!aCtx->ShaderManager()->IsEmpty()) |
| 745 | { |
| 746 | aCtx->ShaderManager()->UpdateClippingState(); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | // Render groups |
| 751 | const Graphic3d_SequenceOfGroup& aGroups = DrawGroups(); |
| 752 | for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next()) |
| 753 | { |
| 754 | aGroupIter.Value()->Render (theWorkspace); |
| 755 | } |
| 756 | |
| 757 | // Reset correction for mirror transform |
| 758 | if (myIsMirrored) |
| 759 | { |
| 760 | aCtx->core11fwd->glFrontFace (GL_CCW); |
| 761 | } |
| 762 | |
| 763 | // Render capping for structure groups |
| 764 | if (!aCtx->Clipping().Planes().IsEmpty()) |
| 765 | { |
| 766 | OpenGl_CappingAlgo::RenderCapping (theWorkspace, aGroups); |
| 767 | } |
| 768 | |
| 769 | // Revert structure clippings |
| 770 | if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty()) |
| 771 | { |
| 772 | aCtx->ChangeClipping().Remove (*aUserPlanes); |
| 773 | |
| 774 | // Set OCCT state uniform variables |
| 775 | if (!aCtx->ShaderManager()->IsEmpty()) |
| 776 | { |
| 777 | aCtx->ShaderManager()->RevertClippingState(); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // Apply local transformation |
| 782 | if (myTransformation) |
| 783 | { |
| 784 | aCtx->ModelWorldState.Pop(); |
| 785 | aCtx->SetGlNormalizeEnabled (anOldGlNormalize); |
| 786 | } |
| 787 | |
| 788 | // Restore highlight color |
| 789 | theWorkspace->HighlightColor = aHighlightColor; |
| 790 | |
| 791 | // Restore aspects |
| 792 | theWorkspace->SetAspectLine (anAspectLine); |
| 793 | theWorkspace->SetAspectFace (anAspectFace); |
| 794 | theWorkspace->SetAspectMarker (anAspectMarker); |
| 795 | theWorkspace->SetAspectText (anAspectText); |
| 796 | |
| 797 | // Restore transform persistence |
| 798 | if ( myTransPers && myTransPers->mode != 0 ) |
| 799 | { |
| 800 | theWorkspace->ActiveView()->BeginTransformPersistence (aCtx, aTransPersistence); |
| 801 | } |
| 802 | |
| 803 | // Apply highlight box |
| 804 | if (!myHighlightBox.IsNull()) |
| 805 | { |
| 806 | myHighlightBox->Render (theWorkspace); |
| 807 | } |
| 808 | |
| 809 | // Restore named status |
| 810 | theWorkspace->NamedStatus = aNamedStatus; |
| 811 | } |
| 812 | |
| 813 | // ======================================================================= |
| 814 | // function : Release |
| 815 | // purpose : |
| 816 | // ======================================================================= |
| 817 | void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx) |
| 818 | { |
| 819 | // Release groups |
| 820 | Clear (theGlCtx); |
| 821 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectLine); |
| 822 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectFace); |
| 823 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectMarker); |
| 824 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectText); |
| 825 | clearHighlightColor (theGlCtx); |
| 826 | |
| 827 | // Remove from connected list of ancestor |
| 828 | UnregisterFromAncestorStructure(); |
| 829 | } |
| 830 | |
| 831 | // ======================================================================= |
| 832 | // function : ReleaseGlResources |
| 833 | // purpose : |
| 834 | // ======================================================================= |
| 835 | void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx) |
| 836 | { |
| 837 | for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
| 838 | { |
| 839 | aGroupIter.ChangeValue()->Release (theGlCtx); |
| 840 | } |
| 841 | if (myAspectLine != NULL) |
| 842 | { |
| 843 | myAspectLine->Release (theGlCtx.operator->()); |
| 844 | } |
| 845 | if (myAspectFace != NULL) |
| 846 | { |
| 847 | myAspectFace->Release (theGlCtx.operator->()); |
| 848 | } |
| 849 | if (myAspectMarker != NULL) |
| 850 | { |
| 851 | myAspectMarker->Release (theGlCtx.operator->()); |
| 852 | } |
| 853 | if (myAspectText != NULL) |
| 854 | { |
| 855 | myAspectText->Release (theGlCtx.operator->()); |
| 856 | } |
| 857 | if (!myHighlightBox.IsNull()) |
| 858 | { |
| 859 | myHighlightBox->Release (theGlCtx.operator->()); |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | //======================================================================= |
| 864 | //function : SetZLayer |
| 865 | //purpose : |
| 866 | //======================================================================= |
| 867 | void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex) |
| 868 | { |
| 869 | myZLayer = theLayerIndex; |
| 870 | } |
| 871 | |
| 872 | //======================================================================= |
| 873 | //function : GetZLayer |
| 874 | //purpose : |
| 875 | //======================================================================= |
| 876 | Standard_Integer OpenGl_Structure::GetZLayer () const |
| 877 | { |
| 878 | return myZLayer; |
| 879 | } |
| 880 | |
| 881 | //======================================================================= |
| 882 | //function : ShadowLink |
| 883 | //purpose : |
| 884 | //======================================================================= |
| 885 | Handle(Graphic3d_CStructure) OpenGl_Structure::ShadowLink (const Handle(Graphic3d_StructureManager)& theManager) const |
| 886 | { |
| 887 | return new OpenGl_StructureShadow (theManager, this); |
| 888 | } |