| 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 : call_util_transpose_mat |
| 122 | // purpose : |
| 123 | // ======================================================================= |
| 124 | static void call_util_transpose_mat (float tmat[16], float mat[4][4]) |
| 125 | { |
| 126 | int i, j; |
| 127 | |
| 128 | for (i=0; i<4; i++) |
| 129 | for (j=0; j<4; j++) |
| 130 | tmat[j*4+i] = mat[i][j]; |
| 131 | } |
| 132 | |
| 133 | // ======================================================================= |
| 134 | // function : OpenGl_Structure |
| 135 | // purpose : |
| 136 | // ======================================================================= |
| 137 | OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager) |
| 138 | : Graphic3d_CStructure (theManager), |
| 139 | myTransformation(NULL), |
| 140 | myTransPers(NULL), |
| 141 | myAspectLine(NULL), |
| 142 | myAspectFace(NULL), |
| 143 | myAspectMarker(NULL), |
| 144 | myAspectText(NULL), |
| 145 | myHighlightColor(NULL), |
| 146 | myNamedStatus(0), |
| 147 | myZLayer(0), |
| 148 | myIsRaytracable (Standard_False), |
| 149 | myModificationState (0), |
| 150 | myIsCulled (Standard_True), |
| 151 | myIsMirrored (Standard_False) |
| 152 | { |
| 153 | UpdateNamedStatus(); |
| 154 | } |
| 155 | |
| 156 | // ======================================================================= |
| 157 | // function : ~OpenGl_Structure |
| 158 | // purpose : |
| 159 | // ======================================================================= |
| 160 | OpenGl_Structure::~OpenGl_Structure() |
| 161 | { |
| 162 | Release (Handle(OpenGl_Context)()); |
| 163 | delete myTransformation; myTransformation = NULL; |
| 164 | delete myTransPers; myTransPers = NULL; |
| 165 | } |
| 166 | |
| 167 | // ======================================================================= |
| 168 | // function : UpdateAspects |
| 169 | // purpose : |
| 170 | // ======================================================================= |
| 171 | void OpenGl_Structure::UpdateAspects() |
| 172 | { |
| 173 | SetTransformPersistence (TransformPersistence); |
| 174 | |
| 175 | if (ContextLine.IsDef) |
| 176 | SetAspectLine (ContextLine); |
| 177 | |
| 178 | if (ContextFillArea.IsDef) |
| 179 | SetAspectFace (ContextFillArea); |
| 180 | |
| 181 | if (ContextMarker.IsDef) |
| 182 | SetAspectMarker (ContextMarker); |
| 183 | |
| 184 | if (ContextText.IsDef) |
| 185 | SetAspectText (ContextText); |
| 186 | } |
| 187 | |
| 188 | // ======================================================================= |
| 189 | // function : UpdateTransformation |
| 190 | // purpose : |
| 191 | // ======================================================================= |
| 192 | void OpenGl_Structure::UpdateTransformation() |
| 193 | { |
| 194 | if (myTransformation == NULL) |
| 195 | { |
| 196 | myTransformation = new OpenGl_Matrix(); |
| 197 | } |
| 198 | |
| 199 | Standard_ShortReal (*aMat)[4] = Graphic3d_CStructure::Transformation; |
| 200 | |
| 201 | Standard_ShortReal aDet = |
| 202 | aMat[0][0] * (aMat[1][1] * aMat[2][2] - aMat[2][1] * aMat[1][2]) - |
| 203 | aMat[0][1] * (aMat[1][0] * aMat[2][2] - aMat[2][0] * aMat[1][2]) + |
| 204 | aMat[0][2] * (aMat[1][0] * aMat[2][1] - aMat[2][0] * aMat[1][1]); |
| 205 | |
| 206 | // Determinant of transform matrix less then 0 means that mirror transform applied. |
| 207 | myIsMirrored = aDet < 0.0f; |
| 208 | |
| 209 | matcpy (myTransformation->mat, &Graphic3d_CStructure::Transformation[0][0]); |
| 210 | |
| 211 | if (myIsRaytracable) |
| 212 | { |
| 213 | UpdateStateWithAncestorStructures(); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // ======================================================================= |
| 218 | // function : SetTransformPersistence |
| 219 | // purpose : |
| 220 | // ======================================================================= |
| 221 | void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers) |
| 222 | { |
| 223 | if (!myTransPers) |
| 224 | myTransPers = new TEL_TRANSFORM_PERSISTENCE; |
| 225 | |
| 226 | myTransPers->mode = ATransPers.Flag; |
| 227 | myTransPers->pointX = ATransPers.Point.x; |
| 228 | myTransPers->pointY = ATransPers.Point.y; |
| 229 | myTransPers->pointZ = ATransPers.Point.z; |
| 230 | MarkAsNotCulled(); |
| 231 | } |
| 232 | |
| 233 | // ======================================================================= |
| 234 | // function : SetAspectLine |
| 235 | // purpose : |
| 236 | // ======================================================================= |
| 237 | void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &theAspect) |
| 238 | { |
| 239 | if (!myAspectLine) |
| 240 | { |
| 241 | myAspectLine = new OpenGl_AspectLine(); |
| 242 | } |
| 243 | myAspectLine->SetAspect (theAspect); |
| 244 | } |
| 245 | |
| 246 | // ======================================================================= |
| 247 | // function : SetAspectFace |
| 248 | // purpose : |
| 249 | // ======================================================================= |
| 250 | void OpenGl_Structure::SetAspectFace (const CALL_DEF_CONTEXTFILLAREA& theAspect) |
| 251 | { |
| 252 | if (!myAspectFace) |
| 253 | { |
| 254 | myAspectFace = new OpenGl_AspectFace(); |
| 255 | } |
| 256 | myAspectFace->SetAspect (theAspect); |
| 257 | |
| 258 | if (myIsRaytracable) |
| 259 | { |
| 260 | UpdateStateWithAncestorStructures(); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // ======================================================================= |
| 265 | // function : SetAspectMarker |
| 266 | // purpose : |
| 267 | // ======================================================================= |
| 268 | void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER& theAspect) |
| 269 | { |
| 270 | if (!myAspectMarker) |
| 271 | { |
| 272 | myAspectMarker = new OpenGl_AspectMarker(); |
| 273 | } |
| 274 | myAspectMarker->SetAspect (theAspect); |
| 275 | } |
| 276 | |
| 277 | // ======================================================================= |
| 278 | // function : SetAspectText |
| 279 | // purpose : |
| 280 | // ======================================================================= |
| 281 | void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &theAspect) |
| 282 | { |
| 283 | if (!myAspectText) |
| 284 | { |
| 285 | myAspectText = new OpenGl_AspectText(); |
| 286 | } |
| 287 | myAspectText->SetAspect (theAspect); |
| 288 | } |
| 289 | |
| 290 | // ======================================================================= |
| 291 | // function : clearHighlightBox |
| 292 | // purpose : |
| 293 | // ======================================================================= |
| 294 | void OpenGl_Structure::clearHighlightBox (const Handle(OpenGl_Context)& theGlCtx) |
| 295 | { |
| 296 | if (!myHighlightBox.IsNull()) |
| 297 | { |
| 298 | myHighlightBox->Release (theGlCtx); |
| 299 | myHighlightBox.Nullify(); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // ======================================================================= |
| 304 | // function : HighlightWithColor |
| 305 | // purpose : |
| 306 | // ======================================================================= |
| 307 | void OpenGl_Structure::HighlightWithColor (const Graphic3d_Vec3& theColor, |
| 308 | const Standard_Boolean theToCreate) |
| 309 | { |
| 310 | const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext(); |
| 311 | if (theToCreate) |
| 312 | setHighlightColor (aContext, theColor); |
| 313 | else |
| 314 | clearHighlightColor (aContext); |
| 315 | } |
| 316 | |
| 317 | // ======================================================================= |
| 318 | // function : HighlightWithBndBox |
| 319 | // purpose : |
| 320 | // ======================================================================= |
| 321 | void OpenGl_Structure::HighlightWithBndBox (const Handle(Graphic3d_Structure)& theStruct, |
| 322 | const Standard_Boolean theToCreate) |
| 323 | { |
| 324 | const Handle(OpenGl_Context)& aContext = GlDriver()->GetSharedContext(); |
| 325 | if (!theToCreate) |
| 326 | { |
| 327 | clearHighlightBox (aContext); |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | if (!myHighlightBox.IsNull()) |
| 332 | { |
| 333 | myHighlightBox->Release (aContext); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | myHighlightBox = new OpenGl_Group (theStruct); |
| 338 | } |
| 339 | |
| 340 | CALL_DEF_CONTEXTLINE& aContextLine = myHighlightBox->ChangeContextLine(); |
| 341 | aContextLine.IsDef = 1; |
| 342 | aContextLine.Color = HighlightColor; |
| 343 | aContextLine.LineType = Aspect_TOL_SOLID; |
| 344 | aContextLine.Width = 1.0f; |
| 345 | myHighlightBox->UpdateAspectLine (Standard_True); |
| 346 | |
| 347 | OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (myBndBox); |
| 348 | myHighlightBox->AddElement (aBndBoxPrs); |
| 349 | } |
| 350 | |
| 351 | // ======================================================================= |
| 352 | // function : setHighlightColor |
| 353 | // purpose : |
| 354 | // ======================================================================= |
| 355 | void OpenGl_Structure::setHighlightColor (const Handle(OpenGl_Context)& theGlCtx, |
| 356 | const Graphic3d_Vec3& theColor) |
| 357 | { |
| 358 | clearHighlightBox (theGlCtx); |
| 359 | if (myHighlightColor == NULL) |
| 360 | { |
| 361 | myHighlightColor = new TEL_COLOUR(); |
| 362 | } |
| 363 | |
| 364 | myHighlightColor->rgb[0] = theColor.r(); |
| 365 | myHighlightColor->rgb[1] = theColor.g(); |
| 366 | myHighlightColor->rgb[2] = theColor.b(); |
| 367 | myHighlightColor->rgb[3] = 1.F; |
| 368 | } |
| 369 | |
| 370 | // ======================================================================= |
| 371 | // function : clearHighlightColor |
| 372 | // purpose : |
| 373 | // ======================================================================= |
| 374 | void OpenGl_Structure::clearHighlightColor (const Handle(OpenGl_Context)& theGlCtx) |
| 375 | { |
| 376 | clearHighlightBox(theGlCtx); |
| 377 | delete myHighlightColor; |
| 378 | myHighlightColor = NULL; |
| 379 | } |
| 380 | |
| 381 | // ======================================================================= |
| 382 | // function : UpdateNamedStatus |
| 383 | // purpose : |
| 384 | // ======================================================================= |
| 385 | void OpenGl_Structure::UpdateNamedStatus() |
| 386 | { |
| 387 | myNamedStatus = 0; |
| 388 | if (highlight) myNamedStatus |= OPENGL_NS_HIGHLIGHT; |
| 389 | if (!visible) myNamedStatus |= OPENGL_NS_HIDE; |
| 390 | |
| 391 | if (myIsRaytracable) |
| 392 | { |
| 393 | UpdateStateWithAncestorStructures(); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | // ======================================================================= |
| 398 | // function : RegisterAncestorStructure |
| 399 | // purpose : |
| 400 | // ======================================================================= |
| 401 | void OpenGl_Structure::RegisterAncestorStructure (const OpenGl_Structure* theStructure) const |
| 402 | { |
| 403 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 404 | { |
| 405 | if (anIt.Value() == theStructure) |
| 406 | { |
| 407 | return; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | myAncestorStructures.Append (theStructure); |
| 412 | } |
| 413 | |
| 414 | // ======================================================================= |
| 415 | // function : UnregisterAncestorStructure |
| 416 | // purpose : |
| 417 | // ======================================================================= |
| 418 | void OpenGl_Structure::UnregisterAncestorStructure (const OpenGl_Structure* theStructure) const |
| 419 | { |
| 420 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 421 | { |
| 422 | if (anIt.Value() == theStructure) |
| 423 | { |
| 424 | myAncestorStructures.Remove (anIt); |
| 425 | return; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // ======================================================================= |
| 431 | // function : UnregisterFromAncestorStructure |
| 432 | // purpose : |
| 433 | // ======================================================================= |
| 434 | void OpenGl_Structure::UnregisterFromAncestorStructure() const |
| 435 | { |
| 436 | for (OpenGl_ListOfStructure::Iterator anIta (myAncestorStructures); anIta.More(); anIta.Next()) |
| 437 | { |
| 438 | OpenGl_Structure* anAncestor = const_cast<OpenGl_Structure*> (anIta.ChangeValue()); |
| 439 | |
| 440 | for (OpenGl_ListOfStructure::Iterator anIts (anAncestor->myConnected); anIts.More(); anIts.Next()) |
| 441 | { |
| 442 | if (anIts.Value() == this) |
| 443 | { |
| 444 | anAncestor->myConnected.Remove (anIts); |
| 445 | return; |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | // ======================================================================= |
| 452 | // function : UpdateStateWithAncestorStructures |
| 453 | // purpose : |
| 454 | // ======================================================================= |
| 455 | void OpenGl_Structure::UpdateStateWithAncestorStructures() const |
| 456 | { |
| 457 | myModificationState++; |
| 458 | |
| 459 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 460 | { |
| 461 | anIt.Value()->UpdateStateWithAncestorStructures(); |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | // ======================================================================= |
| 466 | // function : UpdateRaytracableWithAncestorStructures |
| 467 | // purpose : |
| 468 | // ======================================================================= |
| 469 | void OpenGl_Structure::UpdateRaytracableWithAncestorStructures() const |
| 470 | { |
| 471 | myIsRaytracable = OpenGl_Raytrace::IsRaytracedStructure (this); |
| 472 | |
| 473 | if (!myIsRaytracable) |
| 474 | { |
| 475 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 476 | { |
| 477 | anIt.Value()->UpdateRaytracableWithAncestorStructures(); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | // ======================================================================= |
| 483 | // function : SetRaytracableWithAncestorStructures |
| 484 | // purpose : |
| 485 | // ======================================================================= |
| 486 | void OpenGl_Structure::SetRaytracableWithAncestorStructures() const |
| 487 | { |
| 488 | myIsRaytracable = Standard_True; |
| 489 | |
| 490 | for (OpenGl_ListOfStructure::Iterator anIt (myAncestorStructures); anIt.More(); anIt.Next()) |
| 491 | { |
| 492 | if (!anIt.Value()->IsRaytracable()) |
| 493 | { |
| 494 | anIt.Value()->SetRaytracableWithAncestorStructures(); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | // ======================================================================= |
| 500 | // function : Connect |
| 501 | // purpose : |
| 502 | // ======================================================================= |
| 503 | void OpenGl_Structure::Connect (Graphic3d_CStructure& theStructure) |
| 504 | { |
| 505 | OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure; |
| 506 | Disconnect (theStructure); |
| 507 | myConnected.Append (aStruct); |
| 508 | |
| 509 | if (aStruct->IsRaytracable()) |
| 510 | { |
| 511 | UpdateStateWithAncestorStructures(); |
| 512 | SetRaytracableWithAncestorStructures(); |
| 513 | } |
| 514 | |
| 515 | aStruct->RegisterAncestorStructure (this); |
| 516 | } |
| 517 | |
| 518 | // ======================================================================= |
| 519 | // function : Disconnect |
| 520 | // purpose : |
| 521 | // ======================================================================= |
| 522 | void OpenGl_Structure::Disconnect (Graphic3d_CStructure& theStructure) |
| 523 | { |
| 524 | OpenGl_Structure* aStruct = (OpenGl_Structure* )&theStructure; |
| 525 | for (OpenGl_ListOfStructure::Iterator anIter (myConnected); anIter.More(); anIter.Next()) |
| 526 | { |
| 527 | // Check for the given structure |
| 528 | if (anIter.Value() == aStruct) |
| 529 | { |
| 530 | myConnected.Remove (anIter); |
| 531 | |
| 532 | if (aStruct->IsRaytracable()) |
| 533 | { |
| 534 | UpdateStateWithAncestorStructures(); |
| 535 | UpdateRaytracableWithAncestorStructures(); |
| 536 | } |
| 537 | |
| 538 | aStruct->UnregisterAncestorStructure (this); |
| 539 | return; |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | // ======================================================================= |
| 545 | // function : NewGroup |
| 546 | // purpose : |
| 547 | // ======================================================================= |
| 548 | Handle(Graphic3d_Group) OpenGl_Structure::NewGroup (const Handle(Graphic3d_Structure)& theStruct) |
| 549 | { |
| 550 | Handle(OpenGl_Group) aGroup = new OpenGl_Group (theStruct); |
| 551 | myGroups.Append (aGroup); |
| 552 | return aGroup; |
| 553 | } |
| 554 | |
| 555 | // ======================================================================= |
| 556 | // function : RemoveGroup |
| 557 | // purpose : |
| 558 | // ======================================================================= |
| 559 | void OpenGl_Structure::RemoveGroup (const Handle(Graphic3d_Group)& theGroup) |
| 560 | { |
| 561 | if (theGroup.IsNull()) |
| 562 | { |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
| 567 | { |
| 568 | // Check for the given group |
| 569 | if (aGroupIter.Value() == theGroup) |
| 570 | { |
| 571 | theGroup->Clear (Standard_False); |
| 572 | |
| 573 | if (((OpenGl_Group* )theGroup.operator->())->IsRaytracable()) |
| 574 | { |
| 575 | UpdateStateWithAncestorStructures(); |
| 576 | UpdateRaytracableWithAncestorStructures(); |
| 577 | } |
| 578 | |
| 579 | myGroups.Remove (aGroupIter); |
| 580 | return; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // ======================================================================= |
| 586 | // function : Clear |
| 587 | // purpose : |
| 588 | // ======================================================================= |
| 589 | void OpenGl_Structure::Clear() |
| 590 | { |
| 591 | Clear (GlDriver()->GetSharedContext()); |
| 592 | } |
| 593 | |
| 594 | // ======================================================================= |
| 595 | // function : Clear |
| 596 | // purpose : |
| 597 | // ======================================================================= |
| 598 | void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx) |
| 599 | { |
| 600 | Standard_Boolean aRaytracableGroupDeleted (Standard_False); |
| 601 | |
| 602 | // Release groups |
| 603 | for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
| 604 | { |
| 605 | aRaytracableGroupDeleted |= aGroupIter.Value()->IsRaytracable(); |
| 606 | |
| 607 | // Delete objects |
| 608 | aGroupIter.ChangeValue()->Release (theGlCtx); |
| 609 | } |
| 610 | myGroups.Clear(); |
| 611 | |
| 612 | if (aRaytracableGroupDeleted) |
| 613 | { |
| 614 | UpdateStateWithAncestorStructures(); |
| 615 | UpdateRaytracableWithAncestorStructures(); |
| 616 | } |
| 617 | |
| 618 | Is2dText = Standard_False; |
| 619 | IsForHighlight = Standard_False; |
| 620 | } |
| 621 | |
| 622 | // ======================================================================= |
| 623 | // function : RenderGeometry |
| 624 | // purpose : |
| 625 | // ======================================================================= |
| 626 | void OpenGl_Structure::RenderGeometry (const Handle(OpenGl_Workspace) &theWorkspace) const |
| 627 | { |
| 628 | // Render groups |
| 629 | const Graphic3d_SequenceOfGroup& aGroups = DrawGroups(); |
| 630 | for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next()) |
| 631 | { |
| 632 | aGroupIter.Value()->Render (theWorkspace); |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | // ======================================================================= |
| 637 | // function : Render |
| 638 | // purpose : |
| 639 | // ======================================================================= |
| 640 | void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) const |
| 641 | { |
| 642 | // Process the structure only if visible |
| 643 | if (myNamedStatus & OPENGL_NS_HIDE) |
| 644 | { |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext(); |
| 649 | |
| 650 | // Render named status |
| 651 | const Standard_Integer aNamedStatus = theWorkspace->NamedStatus; |
| 652 | theWorkspace->NamedStatus |= myNamedStatus; |
| 653 | |
| 654 | // Is rendering in ADD or IMMEDIATE mode? |
| 655 | const Standard_Boolean isImmediate = (theWorkspace->NamedStatus & OPENGL_NS_IMMEDIATE) != 0; |
| 656 | |
| 657 | // Do we need to restore GL_NORMALIZE? |
| 658 | Standard_Boolean anOldGlNormalize = aContext->IsGlNormalizeEnabled(); |
| 659 | |
| 660 | // Apply local transformation |
| 661 | GLint aMatrixMode = 0; |
| 662 | const OpenGl_Matrix* aLocalTrsf = NULL; |
| 663 | if (myTransformation) |
| 664 | { |
| 665 | #if !defined(GL_ES_VERSION_2_0) |
| 666 | |
| 667 | Standard_ShortReal aScaleX = OpenGl_Vec3 (myTransformation->mat[0][0], |
| 668 | myTransformation->mat[0][1], |
| 669 | myTransformation->mat[0][2]).SquareModulus(); |
| 670 | // Scale transform detected. |
| 671 | if (Abs (aScaleX - 1.f) > Precision::Confusion()) |
| 672 | { |
| 673 | anOldGlNormalize = aContext->SetGlNormalizeEnabled (Standard_True); |
| 674 | } |
| 675 | |
| 676 | if (isImmediate) |
| 677 | { |
| 678 | Tmatrix3 aModelWorld; |
| 679 | call_util_transpose_mat (*aModelWorld, myTransformation->mat); |
| 680 | |
| 681 | glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode); |
| 682 | |
| 683 | if (!aContext->ShaderManager()->IsEmpty()) |
| 684 | { |
| 685 | Tmatrix3 aWorldView; |
| 686 | glGetFloatv (GL_MODELVIEW_MATRIX, *aWorldView); |
| 687 | |
| 688 | Tmatrix3 aProjection; |
| 689 | glGetFloatv (GL_PROJECTION_MATRIX, *aProjection); |
| 690 | |
| 691 | aContext->ShaderManager()->UpdateModelWorldStateTo (&aModelWorld); |
| 692 | aContext->ShaderManager()->UpdateWorldViewStateTo (&aWorldView); |
| 693 | aContext->ShaderManager()->UpdateProjectionStateTo (&aProjection); |
| 694 | } |
| 695 | |
| 696 | glMatrixMode (GL_MODELVIEW); |
| 697 | glPushMatrix (); |
| 698 | glScalef (1.0f, 1.0f, 1.0f); |
| 699 | glMultMatrixf (*aModelWorld); |
| 700 | } |
| 701 | else |
| 702 | { |
| 703 | glMatrixMode (GL_MODELVIEW); |
| 704 | glPushMatrix(); |
| 705 | |
| 706 | aLocalTrsf = theWorkspace->SetStructureMatrix (myTransformation); |
| 707 | } |
| 708 | #endif |
| 709 | } |
| 710 | |
| 711 | // Apply transform persistence |
| 712 | const TEL_TRANSFORM_PERSISTENCE *aTransPersistence = NULL; |
| 713 | if ( myTransPers && myTransPers->mode != 0 ) |
| 714 | { |
| 715 | aTransPersistence = theWorkspace->ActiveView()->BeginTransformPersistence (aContext, myTransPers); |
| 716 | } |
| 717 | |
| 718 | // Apply aspects |
| 719 | const OpenGl_AspectLine *anAspectLine = theWorkspace->AspectLine (Standard_False); |
| 720 | const OpenGl_AspectFace *anAspectFace = theWorkspace->AspectFace (Standard_False); |
| 721 | const OpenGl_AspectMarker *anAspectMarker = theWorkspace->AspectMarker (Standard_False); |
| 722 | const OpenGl_AspectText *anAspectText = theWorkspace->AspectText (Standard_False); |
| 723 | if (myAspectLine) |
| 724 | { |
| 725 | theWorkspace->SetAspectLine (myAspectLine); |
| 726 | } |
| 727 | if (myAspectFace) |
| 728 | { |
| 729 | theWorkspace->SetAspectFace (myAspectFace); |
| 730 | } |
| 731 | if (myAspectMarker) |
| 732 | { |
| 733 | theWorkspace->SetAspectMarker (myAspectMarker); |
| 734 | } |
| 735 | if (myAspectText) |
| 736 | { |
| 737 | theWorkspace->SetAspectText (myAspectText); |
| 738 | } |
| 739 | |
| 740 | // Apply correction for mirror transform |
| 741 | if (myIsMirrored) |
| 742 | { |
| 743 | glFrontFace (GL_CW); |
| 744 | } |
| 745 | |
| 746 | // Apply highlight color |
| 747 | const TEL_COLOUR *aHighlightColor = theWorkspace->HighlightColor; |
| 748 | if (myHighlightColor) |
| 749 | theWorkspace->HighlightColor = myHighlightColor; |
| 750 | |
| 751 | // Render connected structures |
| 752 | OpenGl_ListOfStructure::Iterator anIter (myConnected); |
| 753 | while (anIter.More()) |
| 754 | { |
| 755 | anIter.Value()->RenderGeometry (theWorkspace); |
| 756 | anIter.Next(); |
| 757 | } |
| 758 | |
| 759 | // Set up plane equations for non-structure transformed global model-view matrix |
| 760 | // List of planes to be applied to context state |
| 761 | Handle(Graphic3d_SequenceOfHClipPlane) aUserPlanes; |
| 762 | |
| 763 | // Collect clipping planes of structure scope |
| 764 | if (!myClipPlanes.IsEmpty()) |
| 765 | { |
| 766 | Graphic3d_SequenceOfHClipPlane::Iterator aClippingIter (myClipPlanes); |
| 767 | for (; aClippingIter.More(); aClippingIter.Next()) |
| 768 | { |
| 769 | const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIter.Value(); |
| 770 | if (!aClipPlane->IsOn()) |
| 771 | { |
| 772 | continue; |
| 773 | } |
| 774 | |
| 775 | if (aUserPlanes.IsNull()) |
| 776 | { |
| 777 | aUserPlanes = new Graphic3d_SequenceOfHClipPlane(); |
| 778 | } |
| 779 | |
| 780 | aUserPlanes->Append (aClipPlane); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty()) |
| 785 | { |
| 786 | // add planes at loaded view matrix state |
| 787 | aContext->ChangeClipping().AddWorld (*aUserPlanes, theWorkspace); |
| 788 | |
| 789 | // Set OCCT state uniform variables |
| 790 | if (!aContext->ShaderManager()->IsEmpty()) |
| 791 | { |
| 792 | aContext->ShaderManager()->UpdateClippingState(); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | // Render groups |
| 797 | const Graphic3d_SequenceOfGroup& aGroups = DrawGroups(); |
| 798 | for (OpenGl_Structure::GroupIterator aGroupIter (aGroups); aGroupIter.More(); aGroupIter.Next()) |
| 799 | { |
| 800 | aGroupIter.Value()->Render (theWorkspace); |
| 801 | } |
| 802 | |
| 803 | // Reset correction for mirror transform |
| 804 | if (myIsMirrored) |
| 805 | glFrontFace (GL_CCW); // default |
| 806 | |
| 807 | // Render capping for structure groups |
| 808 | if (!aContext->Clipping().Planes().IsEmpty()) |
| 809 | { |
| 810 | OpenGl_CappingAlgo::RenderCapping (theWorkspace, aGroups); |
| 811 | } |
| 812 | |
| 813 | // Revert structure clippings |
| 814 | if (!aUserPlanes.IsNull() && !aUserPlanes->IsEmpty()) |
| 815 | { |
| 816 | aContext->ChangeClipping().Remove (*aUserPlanes); |
| 817 | |
| 818 | // Set OCCT state uniform variables |
| 819 | if (!aContext->ShaderManager()->IsEmpty()) |
| 820 | { |
| 821 | aContext->ShaderManager()->RevertClippingState(); |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | // Restore highlight color |
| 826 | theWorkspace->HighlightColor = aHighlightColor; |
| 827 | |
| 828 | // Restore aspects |
| 829 | theWorkspace->SetAspectLine (anAspectLine); |
| 830 | theWorkspace->SetAspectFace (anAspectFace); |
| 831 | theWorkspace->SetAspectMarker (anAspectMarker); |
| 832 | theWorkspace->SetAspectText (anAspectText); |
| 833 | |
| 834 | // Restore transform persistence |
| 835 | if ( myTransPers && myTransPers->mode != 0 ) |
| 836 | { |
| 837 | theWorkspace->ActiveView()->BeginTransformPersistence (aContext, aTransPersistence); |
| 838 | } |
| 839 | |
| 840 | // Restore local transformation |
| 841 | if (myTransformation) |
| 842 | { |
| 843 | #if !defined(GL_ES_VERSION_2_0) |
| 844 | |
| 845 | aContext->SetGlNormalizeEnabled (anOldGlNormalize); |
| 846 | |
| 847 | if (isImmediate) |
| 848 | { |
| 849 | glPopMatrix (); |
| 850 | glMatrixMode (aMatrixMode); |
| 851 | |
| 852 | Tmatrix3 aModelWorldState = { { 1.f, 0.f, 0.f, 0.f }, |
| 853 | { 0.f, 1.f, 0.f, 0.f }, |
| 854 | { 0.f, 0.f, 1.f, 0.f }, |
| 855 | { 0.f, 0.f, 0.f, 1.f } }; |
| 856 | |
| 857 | aContext->ShaderManager()->RevertModelWorldStateTo (&aModelWorldState); |
| 858 | } |
| 859 | else |
| 860 | { |
| 861 | theWorkspace->SetStructureMatrix (aLocalTrsf, true); |
| 862 | |
| 863 | glMatrixMode (GL_MODELVIEW); |
| 864 | glPopMatrix(); |
| 865 | } |
| 866 | #endif |
| 867 | } |
| 868 | |
| 869 | // Apply highlight box |
| 870 | if (!myHighlightBox.IsNull()) |
| 871 | { |
| 872 | myHighlightBox->Render (theWorkspace); |
| 873 | } |
| 874 | |
| 875 | // Restore named status |
| 876 | theWorkspace->NamedStatus = aNamedStatus; |
| 877 | } |
| 878 | |
| 879 | // ======================================================================= |
| 880 | // function : Release |
| 881 | // purpose : |
| 882 | // ======================================================================= |
| 883 | void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx) |
| 884 | { |
| 885 | // Release groups |
| 886 | Clear (theGlCtx); |
| 887 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectLine); |
| 888 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectFace); |
| 889 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectMarker); |
| 890 | OpenGl_Element::Destroy (theGlCtx.operator->(), myAspectText); |
| 891 | clearHighlightColor (theGlCtx); |
| 892 | |
| 893 | // Remove from connected list of ancestor |
| 894 | UnregisterFromAncestorStructure(); |
| 895 | } |
| 896 | |
| 897 | // ======================================================================= |
| 898 | // function : ReleaseGlResources |
| 899 | // purpose : |
| 900 | // ======================================================================= |
| 901 | void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx) |
| 902 | { |
| 903 | for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
| 904 | { |
| 905 | aGroupIter.ChangeValue()->Release (theGlCtx); |
| 906 | } |
| 907 | if (myAspectLine != NULL) |
| 908 | { |
| 909 | myAspectLine->Release (theGlCtx.operator->()); |
| 910 | } |
| 911 | if (myAspectFace != NULL) |
| 912 | { |
| 913 | myAspectFace->Release (theGlCtx.operator->()); |
| 914 | } |
| 915 | if (myAspectMarker != NULL) |
| 916 | { |
| 917 | myAspectMarker->Release (theGlCtx.operator->()); |
| 918 | } |
| 919 | if (myAspectText != NULL) |
| 920 | { |
| 921 | myAspectText->Release (theGlCtx.operator->()); |
| 922 | } |
| 923 | if (!myHighlightBox.IsNull()) |
| 924 | { |
| 925 | myHighlightBox->Release (theGlCtx.operator->()); |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | //======================================================================= |
| 930 | //function : SetZLayer |
| 931 | //purpose : |
| 932 | //======================================================================= |
| 933 | void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex) |
| 934 | { |
| 935 | myZLayer = theLayerIndex; |
| 936 | } |
| 937 | |
| 938 | //======================================================================= |
| 939 | //function : GetZLayer |
| 940 | //purpose : |
| 941 | //======================================================================= |
| 942 | Standard_Integer OpenGl_Structure::GetZLayer () const |
| 943 | { |
| 944 | return myZLayer; |
| 945 | } |
| 946 | |
| 947 | //======================================================================= |
| 948 | //function : ShadowLink |
| 949 | //purpose : |
| 950 | //======================================================================= |
| 951 | Handle(Graphic3d_CStructure) OpenGl_Structure::ShadowLink (const Handle(Graphic3d_StructureManager)& theManager) const |
| 952 | { |
| 953 | return new OpenGl_StructureShadow (theManager, this); |
| 954 | } |