| 1 | // Created on: 2011-08-01 |
| 2 | // Created by: Sergey ZERCHANINOV |
| 3 | // Copyright (c) 2011-2012 OPEN CASCADE SAS |
| 4 | // |
| 5 | // The content of this file is subject to the Open CASCADE Technology Public |
| 6 | // License Version 6.5 (the "License"). You may not use the content of this file |
| 7 | // except in compliance with the License. Please obtain a copy of the License |
| 8 | // at http://www.opencascade.org and read it completely before using this file. |
| 9 | // |
| 10 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
| 11 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
| 12 | // |
| 13 | // The Original Code and all software distributed under the License is |
| 14 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
| 15 | // Initial Developer hereby disclaims all such warranties, including without |
| 16 | // limitation, any warranties of merchantability, fitness for a particular |
| 17 | // purpose or non-infringement. Please see the License for the specific terms |
| 18 | // and conditions governing the rights and limitations under the License. |
| 19 | |
| 20 | |
| 21 | #include <OpenGl_GlCore11.hxx> |
| 22 | |
| 23 | #include <OpenGl_Structure.hxx> |
| 24 | |
| 25 | #include <OpenGl_Polyline.hxx> |
| 26 | #include <OpenGl_Workspace.hxx> |
| 27 | #include <OpenGl_View.hxx> |
| 28 | |
| 29 | #include <OpenGl_telem_util.hxx> |
| 30 | |
| 31 | |
| 32 | /*----------------------------------------------------------------------*/ |
| 33 | |
| 34 | static void call_util_transpose_mat (float tmat[16], float mat[4][4]) |
| 35 | { |
| 36 | int i, j; |
| 37 | |
| 38 | for (i=0; i<4; i++) |
| 39 | for (j=0; j<4; j++) |
| 40 | tmat[j*4+i] = mat[i][j]; |
| 41 | } |
| 42 | |
| 43 | /*----------------------------------------------------------------------*/ |
| 44 | |
| 45 | OpenGl_Structure::OpenGl_Structure () |
| 46 | : myTransformation(NULL), |
| 47 | myTransPers(NULL), |
| 48 | myAspectLine(NULL), |
| 49 | myAspectFace(NULL), |
| 50 | myAspectMarker(NULL), |
| 51 | myAspectText(NULL), |
| 52 | myHighlightBox(NULL), |
| 53 | myHighlightColor(NULL), |
| 54 | myNamedStatus(0), |
| 55 | myZLayer(0) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | /*----------------------------------------------------------------------*/ |
| 60 | |
| 61 | OpenGl_Structure::~OpenGl_Structure() |
| 62 | { |
| 63 | Release (Handle(OpenGl_Context)()); |
| 64 | delete myTransformation; myTransformation = NULL; |
| 65 | delete myTransPers; myTransPers = NULL; |
| 66 | } |
| 67 | |
| 68 | /*----------------------------------------------------------------------*/ |
| 69 | |
| 70 | void OpenGl_Structure::SetTransformation(const float *AMatrix) |
| 71 | { |
| 72 | if (!myTransformation) |
| 73 | myTransformation = new OpenGl_Matrix(); |
| 74 | |
| 75 | matcpy( myTransformation->mat, AMatrix ); |
| 76 | } |
| 77 | |
| 78 | /*----------------------------------------------------------------------*/ |
| 79 | |
| 80 | void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers) |
| 81 | { |
| 82 | if (!myTransPers) |
| 83 | myTransPers = new TEL_TRANSFORM_PERSISTENCE; |
| 84 | |
| 85 | myTransPers->mode = ATransPers.Flag; |
| 86 | myTransPers->pointX = ATransPers.Point.x; |
| 87 | myTransPers->pointY = ATransPers.Point.y; |
| 88 | myTransPers->pointZ = ATransPers.Point.z; |
| 89 | } |
| 90 | |
| 91 | /*----------------------------------------------------------------------*/ |
| 92 | |
| 93 | void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &AContext) |
| 94 | { |
| 95 | if (!myAspectLine) |
| 96 | myAspectLine = new OpenGl_AspectLine(); |
| 97 | myAspectLine->SetContext( AContext ); |
| 98 | } |
| 99 | |
| 100 | /*----------------------------------------------------------------------*/ |
| 101 | |
| 102 | void OpenGl_Structure::SetAspectFace (const Handle(OpenGl_Context)& theCtx, |
| 103 | const CALL_DEF_CONTEXTFILLAREA& theAspect) |
| 104 | { |
| 105 | if (!myAspectFace) |
| 106 | { |
| 107 | myAspectFace = new OpenGl_AspectFace(); |
| 108 | } |
| 109 | myAspectFace->Init (theCtx, theAspect); |
| 110 | } |
| 111 | |
| 112 | /*----------------------------------------------------------------------*/ |
| 113 | |
| 114 | void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER &AContext) |
| 115 | { |
| 116 | if (!myAspectMarker) |
| 117 | myAspectMarker = new OpenGl_AspectMarker(); |
| 118 | myAspectMarker->SetContext( AContext ); |
| 119 | } |
| 120 | |
| 121 | /*----------------------------------------------------------------------*/ |
| 122 | |
| 123 | void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &AContext) |
| 124 | { |
| 125 | if (!myAspectText) |
| 126 | myAspectText = new OpenGl_AspectText(); |
| 127 | myAspectText->SetContext( AContext ); |
| 128 | } |
| 129 | |
| 130 | /*----------------------------------------------------------------------*/ |
| 131 | |
| 132 | void OpenGl_Structure::SetHighlightBox (const Handle(OpenGl_Context)& theGlCtx, |
| 133 | const CALL_DEF_BOUNDBOX& theBoundBox) |
| 134 | { |
| 135 | if (myHighlightBox != NULL) |
| 136 | { |
| 137 | myHighlightBox->Release (theGlCtx); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | myHighlightBox = new OpenGl_Group(); |
| 142 | } |
| 143 | |
| 144 | CALL_DEF_CONTEXTLINE aContextLine; |
| 145 | aContextLine.Color = theBoundBox.Color; |
| 146 | aContextLine.LineType = Aspect_TOL_SOLID; |
| 147 | aContextLine.Width = 1.0f; |
| 148 | myHighlightBox->SetAspectLine (aContextLine); |
| 149 | |
| 150 | #define CALL_MAX_BOUNDBOXSIZE 16 |
| 151 | |
| 152 | Graphic3d_Array1OfVertex aPoints (1, CALL_MAX_BOUNDBOXSIZE); |
| 153 | const float Xm = theBoundBox.Pmin.x; |
| 154 | const float Ym = theBoundBox.Pmin.y; |
| 155 | const float Zm = theBoundBox.Pmin.z; |
| 156 | const float XM = theBoundBox.Pmax.x; |
| 157 | const float YM = theBoundBox.Pmax.y; |
| 158 | const float ZM = theBoundBox.Pmax.z; |
| 159 | aPoints( 1).SetCoord (Xm, Ym, Zm); |
| 160 | aPoints( 2).SetCoord (Xm, Ym, ZM); |
| 161 | aPoints( 3).SetCoord (Xm, YM, ZM); |
| 162 | aPoints( 4).SetCoord (Xm, YM, Zm); |
| 163 | aPoints( 5).SetCoord (Xm, Ym, Zm); |
| 164 | aPoints( 6).SetCoord (XM, Ym, Zm); |
| 165 | aPoints( 7).SetCoord (XM, Ym, ZM); |
| 166 | aPoints( 8).SetCoord (XM, YM, ZM); |
| 167 | aPoints( 9).SetCoord (XM, YM, Zm); |
| 168 | aPoints(10).SetCoord (XM, Ym, Zm); |
| 169 | aPoints(11).SetCoord (XM, YM, Zm); |
| 170 | aPoints(12).SetCoord (Xm, YM, Zm); |
| 171 | aPoints(13).SetCoord (Xm, YM, ZM); |
| 172 | aPoints(14).SetCoord (XM, YM, ZM); |
| 173 | aPoints(15).SetCoord (XM, Ym, ZM); |
| 174 | aPoints(16).SetCoord (Xm, Ym, ZM); |
| 175 | |
| 176 | OpenGl_Polyline* aPolyline = new OpenGl_Polyline (aPoints); |
| 177 | myHighlightBox->AddElement (TelPolyline, aPolyline); |
| 178 | } |
| 179 | |
| 180 | /*----------------------------------------------------------------------*/ |
| 181 | |
| 182 | void OpenGl_Structure::ClearHighlightBox (const Handle(OpenGl_Context)& theGlCtx) |
| 183 | { |
| 184 | if (myHighlightBox != NULL) |
| 185 | { |
| 186 | OpenGl_Element::Destroy (theGlCtx, myHighlightBox); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /*----------------------------------------------------------------------*/ |
| 191 | |
| 192 | void OpenGl_Structure::SetHighlightColor (const Handle(OpenGl_Context)& theGlCtx, |
| 193 | const Standard_ShortReal R, |
| 194 | const Standard_ShortReal G, |
| 195 | const Standard_ShortReal B) |
| 196 | { |
| 197 | ClearHighlightBox (theGlCtx); |
| 198 | if (myHighlightColor == NULL) |
| 199 | { |
| 200 | myHighlightColor = new TEL_COLOUR(); |
| 201 | } |
| 202 | |
| 203 | myHighlightColor->rgb[0] = R; |
| 204 | myHighlightColor->rgb[1] = G; |
| 205 | myHighlightColor->rgb[2] = B; |
| 206 | myHighlightColor->rgb[3] = 1.F; |
| 207 | } |
| 208 | |
| 209 | /*----------------------------------------------------------------------*/ |
| 210 | |
| 211 | void OpenGl_Structure::ClearHighlightColor (const Handle(OpenGl_Context)& theGlCtx) |
| 212 | { |
| 213 | ClearHighlightBox(theGlCtx); |
| 214 | delete myHighlightColor; |
| 215 | myHighlightColor = NULL; |
| 216 | } |
| 217 | |
| 218 | /*----------------------------------------------------------------------*/ |
| 219 | |
| 220 | void OpenGl_Structure::Connect (const OpenGl_Structure *AStructure) |
| 221 | { |
| 222 | Disconnect (AStructure); |
| 223 | myConnected.Append(AStructure); |
| 224 | } |
| 225 | |
| 226 | /*----------------------------------------------------------------------*/ |
| 227 | |
| 228 | void OpenGl_Structure::Disconnect (const OpenGl_Structure *AStructure) |
| 229 | { |
| 230 | OpenGl_ListOfStructure::Iterator its(myConnected); |
| 231 | while (its.More()) |
| 232 | { |
| 233 | // Check for the given structure |
| 234 | if (its.Value() == AStructure) |
| 235 | { |
| 236 | myConnected.Remove(its); |
| 237 | return; |
| 238 | } |
| 239 | its.Next(); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /*----------------------------------------------------------------------*/ |
| 244 | |
| 245 | OpenGl_Group * OpenGl_Structure::AddGroup () |
| 246 | { |
| 247 | // Create new group |
| 248 | OpenGl_Group *g = new OpenGl_Group; |
| 249 | myGroups.Append(g); |
| 250 | return g; |
| 251 | } |
| 252 | |
| 253 | /*----------------------------------------------------------------------*/ |
| 254 | |
| 255 | void OpenGl_Structure::RemoveGroup (const Handle(OpenGl_Context)& theGlCtx, |
| 256 | const OpenGl_Group* theGroup) |
| 257 | { |
| 258 | for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next()) |
| 259 | { |
| 260 | // Check for the given group |
| 261 | if (anIter.Value() == theGroup) |
| 262 | { |
| 263 | // Delete object |
| 264 | OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue())); |
| 265 | myGroups.Remove (anIter); |
| 266 | return; |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /*----------------------------------------------------------------------*/ |
| 272 | |
| 273 | void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx) |
| 274 | { |
| 275 | // Release groups |
| 276 | for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next()) |
| 277 | { |
| 278 | // Delete objects |
| 279 | OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue())); |
| 280 | } |
| 281 | myGroups.Clear(); |
| 282 | } |
| 283 | |
| 284 | /*----------------------------------------------------------------------*/ |
| 285 | |
| 286 | void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const |
| 287 | { |
| 288 | // Process the structure only if visible |
| 289 | if ( myNamedStatus & OPENGL_NS_HIDE ) |
| 290 | return; |
| 291 | |
| 292 | // Render named status |
| 293 | const Standard_Integer named_status = AWorkspace->NamedStatus; |
| 294 | AWorkspace->NamedStatus |= myNamedStatus; |
| 295 | |
| 296 | // Is rendering in ADD or IMMEDIATE mode? |
| 297 | const Standard_Boolean isImmediate = (AWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0; |
| 298 | |
| 299 | // Apply local transformation |
| 300 | GLint matrix_mode = 0; |
| 301 | const OpenGl_Matrix *local_trsf = NULL; |
| 302 | if (myTransformation) |
| 303 | { |
| 304 | if (isImmediate) |
| 305 | { |
| 306 | float mat16[16]; |
| 307 | call_util_transpose_mat (mat16, myTransformation->mat); |
| 308 | glGetIntegerv (GL_MATRIX_MODE, &matrix_mode); |
| 309 | glMatrixMode (GL_MODELVIEW); |
| 310 | glPushMatrix (); |
| 311 | glScalef (1.F, 1.F, 1.F); |
| 312 | glMultMatrixf (mat16); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | glMatrixMode (GL_MODELVIEW); |
| 317 | glPushMatrix(); |
| 318 | |
| 319 | local_trsf = AWorkspace->SetStructureMatrix(myTransformation); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // Apply transform persistence |
| 324 | const TEL_TRANSFORM_PERSISTENCE *trans_pers = NULL; |
| 325 | if ( myTransPers && myTransPers->mode != 0 ) |
| 326 | { |
| 327 | trans_pers = AWorkspace->ActiveView()->BeginTransformPersistence( myTransPers ); |
| 328 | } |
| 329 | |
| 330 | // Apply aspects |
| 331 | const OpenGl_AspectLine *aspect_line = AWorkspace->AspectLine(Standard_False); |
| 332 | const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace(Standard_False); |
| 333 | const OpenGl_AspectMarker *aspect_marker = AWorkspace->AspectMarker(Standard_False); |
| 334 | const OpenGl_AspectText *aspect_text = AWorkspace->AspectText(Standard_False); |
| 335 | if (myAspectLine) |
| 336 | AWorkspace->SetAspectLine(myAspectLine); |
| 337 | if (myAspectFace) |
| 338 | AWorkspace->SetAspectFace(myAspectFace); |
| 339 | if (myAspectMarker) |
| 340 | AWorkspace->SetAspectMarker(myAspectMarker); |
| 341 | if (myAspectText) |
| 342 | AWorkspace->SetAspectText(myAspectText); |
| 343 | |
| 344 | // Apply highlight box |
| 345 | if (myHighlightBox) |
| 346 | myHighlightBox->Render( AWorkspace ); |
| 347 | |
| 348 | // Apply highlight color |
| 349 | const TEL_COLOUR *highlight_color = AWorkspace->HighlightColor; |
| 350 | if (myHighlightColor) |
| 351 | AWorkspace->HighlightColor = myHighlightColor; |
| 352 | |
| 353 | // Render connected structures |
| 354 | OpenGl_ListOfStructure::Iterator its(myConnected); |
| 355 | while (its.More()) |
| 356 | { |
| 357 | its.Value()->Render(AWorkspace); |
| 358 | its.Next(); |
| 359 | } |
| 360 | |
| 361 | // Render groups |
| 362 | OpenGl_ListOfGroup::Iterator itg(myGroups); |
| 363 | while (itg.More()) |
| 364 | { |
| 365 | itg.Value()->Render(AWorkspace); |
| 366 | itg.Next(); |
| 367 | } |
| 368 | |
| 369 | // Restore highlight color |
| 370 | AWorkspace->HighlightColor = highlight_color; |
| 371 | |
| 372 | // Restore aspects |
| 373 | AWorkspace->SetAspectLine(aspect_line); |
| 374 | AWorkspace->SetAspectFace(aspect_face); |
| 375 | AWorkspace->SetAspectMarker(aspect_marker); |
| 376 | AWorkspace->SetAspectText(aspect_text); |
| 377 | |
| 378 | // Restore transform persistence |
| 379 | if ( myTransPers && myTransPers->mode != 0 ) |
| 380 | { |
| 381 | AWorkspace->ActiveView()->BeginTransformPersistence( trans_pers ); |
| 382 | } |
| 383 | |
| 384 | // Restore local transformation |
| 385 | if (myTransformation) |
| 386 | { |
| 387 | if (isImmediate) |
| 388 | { |
| 389 | glPopMatrix (); |
| 390 | glMatrixMode (matrix_mode); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | AWorkspace->SetStructureMatrix(local_trsf); |
| 395 | |
| 396 | glMatrixMode (GL_MODELVIEW); |
| 397 | glPopMatrix(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | // Restore named status |
| 402 | AWorkspace->NamedStatus = named_status; |
| 403 | } |
| 404 | |
| 405 | // ======================================================================= |
| 406 | // function : Release |
| 407 | // purpose : |
| 408 | // ======================================================================= |
| 409 | void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx) |
| 410 | { |
| 411 | // Release groups |
| 412 | Clear (theGlCtx); |
| 413 | OpenGl_Element::Destroy (theGlCtx, myAspectLine); |
| 414 | OpenGl_Element::Destroy (theGlCtx, myAspectFace); |
| 415 | OpenGl_Element::Destroy (theGlCtx, myAspectMarker); |
| 416 | OpenGl_Element::Destroy (theGlCtx, myAspectText); |
| 417 | ClearHighlightColor (theGlCtx); |
| 418 | } |
| 419 | |
| 420 | // ======================================================================= |
| 421 | // function : ReleaseGlResources |
| 422 | // purpose : |
| 423 | // ======================================================================= |
| 424 | void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx) |
| 425 | { |
| 426 | for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next()) |
| 427 | { |
| 428 | OpenGl_Group* aGroup = const_cast<OpenGl_Group*& > (anIter.ChangeValue()); |
| 429 | if (aGroup != NULL) |
| 430 | { |
| 431 | aGroup->Release (theGlCtx); |
| 432 | } |
| 433 | } |
| 434 | if (myAspectLine != NULL) |
| 435 | { |
| 436 | myAspectLine->Release (theGlCtx); |
| 437 | } |
| 438 | if (myAspectFace != NULL) |
| 439 | { |
| 440 | myAspectFace->Release (theGlCtx); |
| 441 | } |
| 442 | if (myAspectMarker != NULL) |
| 443 | { |
| 444 | myAspectMarker->Release (theGlCtx); |
| 445 | } |
| 446 | if (myAspectText != NULL) |
| 447 | { |
| 448 | myAspectText->Release (theGlCtx); |
| 449 | } |
| 450 | if (myHighlightBox != NULL) |
| 451 | { |
| 452 | myHighlightBox->Release (theGlCtx); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | //======================================================================= |
| 457 | //function : SetZLayer |
| 458 | //purpose : |
| 459 | //======================================================================= |
| 460 | |
| 461 | void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex) |
| 462 | { |
| 463 | myZLayer = theLayerIndex; |
| 464 | } |
| 465 | |
| 466 | //======================================================================= |
| 467 | //function : GetZLayer |
| 468 | //purpose : |
| 469 | //======================================================================= |
| 470 | |
| 471 | Standard_Integer OpenGl_Structure::GetZLayer () const |
| 472 | { |
| 473 | return myZLayer; |
| 474 | } |