| 1 | // Created on: 2011-09-20 |
| 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 | #include <OpenGl_GlCore11.hxx> |
| 21 | |
| 22 | #include <OpenGl_View.hxx> |
| 23 | #include <OpenGl_Context.hxx> |
| 24 | #include <OpenGl_Workspace.hxx> |
| 25 | #include <OpenGl_Display.hxx> |
| 26 | |
| 27 | #include <OpenGl_Texture.hxx> |
| 28 | #include <OpenGl_Trihedron.hxx> |
| 29 | #include <OpenGl_GraduatedTrihedron.hxx> |
| 30 | |
| 31 | #include <OpenGl_transform_persistence.hxx> |
| 32 | |
| 33 | #include <Graphic3d_TextureEnv.hxx> |
| 34 | |
| 35 | IMPLEMENT_STANDARD_HANDLE(OpenGl_View,MMgt_TShared) |
| 36 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,MMgt_TShared) |
| 37 | |
| 38 | /*----------------------------------------------------------------------*/ |
| 39 | |
| 40 | static const OPENGL_BG_TEXTURE myDefaultBgTexture = { 0, 0, 0, Aspect_FM_CENTERED }; |
| 41 | static const OPENGL_BG_GRADIENT myDefaultBgGradient = { {{ 0.F, 0.F, 0.F, 1.F }}, {{ 0.F, 0.F, 0.F, 1.F }}, Aspect_GFM_NONE }; |
| 42 | static const Tmatrix3 myDefaultMatrix = { { 1.F, 0.F, 0.F, 0.F }, { 0.F, 1.F, 0.F, 0.F }, { 0.F, 0.F, 1.F, 0.F }, { 0.F, 0.F, 0.F, 1.F } }; |
| 43 | static const OPENGL_ZCLIP myDefaultZClip = { { Standard_True, 0.F }, { Standard_True, 1.F } }; |
| 44 | static const OPENGL_EXTRA_REP myDefaultExtra = |
| 45 | { |
| 46 | //vrp |
| 47 | { 0.F, 0.F, 0.F }, |
| 48 | //vpn |
| 49 | { 0.F, 0.F, 1.F }, |
| 50 | //vup |
| 51 | { 0.F, 1.F, 0.F }, |
| 52 | //map |
| 53 | { |
| 54 | //window |
| 55 | { 0.F, 0.F, 1.F, 1.F }, |
| 56 | //viewport |
| 57 | { 0.F, 0.F, 0.F, 1.F, 1.F, 1.F }, |
| 58 | //proj |
| 59 | TelParallel, |
| 60 | //prp |
| 61 | { 0.F, 0.F, 0.F }, |
| 62 | //vpd |
| 63 | 0.F, |
| 64 | //fpd |
| 65 | 0.F, |
| 66 | //bpd |
| 67 | -1.F |
| 68 | }, |
| 69 | //scaleFactors |
| 70 | { 1.F, 1.F, 1.F } |
| 71 | }; |
| 72 | |
| 73 | static const OPENGL_FOG myDefaultFog = { Standard_False, 0.F, 1.F, { { 0.F, 0.F, 0.F, 1.F } } }; |
| 74 | static const TEL_TRANSFORM_PERSISTENCE myDefaultTransPers = { 0, 0.F, 0.F, 0.F }; |
| 75 | static const GLdouble THE_IDENTITY_MATRIX[4][4] = |
| 76 | { |
| 77 | {1.0, 0.0, 0.0, 0.0}, |
| 78 | {0.0, 1.0, 0.0, 0.0}, |
| 79 | {0.0, 0.0, 1.0, 0.0}, |
| 80 | {0.0, 0.0, 0.0, 1.0} |
| 81 | }; |
| 82 | |
| 83 | /*----------------------------------------------------------------------*/ |
| 84 | |
| 85 | OpenGl_View::OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext) |
| 86 | : mySurfaceDetail(Visual3d_TOD_NONE), |
| 87 | myBackfacing(0), |
| 88 | myBgTexture(myDefaultBgTexture), |
| 89 | myBgGradient(myDefaultBgGradient), |
| 90 | //myOrientationMatrix(myDefaultMatrix), |
| 91 | //myMappingMatrix(myDefaultMatrix), |
| 92 | //shield_indicator = TOn, |
| 93 | //shield_colour = { { 0.F, 0.F, 0.F, 1.F } }, |
| 94 | //border_indicator = TOff, |
| 95 | //border_colour = { { 0.F, 0.F, 0.F, 1.F } }, |
| 96 | //active_status = TOn, |
| 97 | myZClip(myDefaultZClip), |
| 98 | myExtra(myDefaultExtra), |
| 99 | myFog(myDefaultFog), |
| 100 | myVisualization(AContext.Visualization), |
| 101 | myIntShadingMethod(TEL_SM_GOURAUD), |
| 102 | myAntiAliasing(Standard_False), |
| 103 | myTransPers(&myDefaultTransPers), |
| 104 | myTrihedron(NULL), |
| 105 | myIsTransPers(Standard_False), |
| 106 | myGraduatedTrihedron(NULL) |
| 107 | { |
| 108 | // Initialize matrices |
| 109 | memcpy(myOrientationMatrix,myDefaultMatrix,sizeof(Tmatrix3)); |
| 110 | memcpy(myMappingMatrix,myDefaultMatrix,sizeof(Tmatrix3)); |
| 111 | |
| 112 | // Shading method |
| 113 | switch (AContext.Model) |
| 114 | { |
| 115 | case 1 : /* VISUAL3D_TOM_INTERP_COLOR */ |
| 116 | case 3 : /* VISUAL3D_TOM_VERTEX */ |
| 117 | myIntShadingMethod = TEL_SM_GOURAUD; |
| 118 | break; |
| 119 | default : |
| 120 | myIntShadingMethod = TEL_SM_FLAT; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /*----------------------------------------------------------------------*/ |
| 126 | |
| 127 | OpenGl_View::~OpenGl_View () |
| 128 | { |
| 129 | ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context |
| 130 | } |
| 131 | |
| 132 | void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx) |
| 133 | { |
| 134 | OpenGl_Element::Destroy (theCtx, myTrihedron); |
| 135 | OpenGl_Element::Destroy (theCtx, myGraduatedTrihedron); |
| 136 | |
| 137 | if (!myTextureEnv.IsNull()) |
| 138 | { |
| 139 | theCtx->DelayedRelease (myTextureEnv); |
| 140 | myTextureEnv.Nullify(); |
| 141 | } |
| 142 | if (myBgTexture.TexId != 0) |
| 143 | { |
| 144 | glDeleteTextures (1, (GLuint*)&(myBgTexture.TexId)); |
| 145 | myBgTexture.TexId = 0; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void OpenGl_View::SetTextureEnv (const Handle(OpenGl_Context)& theCtx, |
| 150 | const Handle(Graphic3d_TextureEnv)& theTexture) |
| 151 | { |
| 152 | if (!myTextureEnv.IsNull()) |
| 153 | { |
| 154 | theCtx->DelayedRelease (myTextureEnv); |
| 155 | myTextureEnv.Nullify(); |
| 156 | } |
| 157 | |
| 158 | if (theTexture.IsNull()) |
| 159 | { |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | myTextureEnv = new OpenGl_Texture (theTexture->GetParams()); |
| 164 | Handle(Image_PixMap) anImage = theTexture->GetImage(); |
| 165 | if( !anImage.IsNull()) |
| 166 | myTextureEnv->Init (theCtx, *anImage.operator->(), theTexture->Type()); |
| 167 | } |
| 168 | |
| 169 | /*----------------------------------------------------------------------*/ |
| 170 | |
| 171 | void OpenGl_View::SetBackfacing (const Standard_Integer theMode) |
| 172 | { |
| 173 | myBackfacing = theMode; |
| 174 | } |
| 175 | |
| 176 | /*----------------------------------------------------------------------*/ |
| 177 | |
| 178 | //call_togl_setlight |
| 179 | void OpenGl_View::SetLights (const CALL_DEF_VIEWCONTEXT &AContext) |
| 180 | { |
| 181 | myLights.Clear(); |
| 182 | |
| 183 | const int nb_lights = AContext.NbActiveLight; |
| 184 | |
| 185 | int i = 0; |
| 186 | const CALL_DEF_LIGHT *alight = &(AContext.ActiveLight[0]); |
| 187 | for ( ; i < nb_lights; i++, alight++ ) |
| 188 | { |
| 189 | OpenGl_Light rep; |
| 190 | memset(&rep,0,sizeof(rep)); |
| 191 | |
| 192 | switch( alight->LightType ) |
| 193 | { |
| 194 | case 0 : /* TOLS_AMBIENT */ |
| 195 | rep.type = TLightAmbient; |
| 196 | rep.col.rgb[0] = alight->Color.r; |
| 197 | rep.col.rgb[1] = alight->Color.g; |
| 198 | rep.col.rgb[2] = alight->Color.b; |
| 199 | break; |
| 200 | |
| 201 | case 1 : /* TOLS_DIRECTIONAL */ |
| 202 | rep.type = TLightDirectional; |
| 203 | rep.col.rgb[0] = alight->Color.r; |
| 204 | rep.col.rgb[1] = alight->Color.g; |
| 205 | rep.col.rgb[2] = alight->Color.b; |
| 206 | rep.dir[0] = alight->Direction.x; |
| 207 | rep.dir[1] = alight->Direction.y; |
| 208 | rep.dir[2] = alight->Direction.z; |
| 209 | break; |
| 210 | |
| 211 | case 2 : /* TOLS_POSITIONAL */ |
| 212 | rep.type = TLightPositional; |
| 213 | rep.col.rgb[0] = alight->Color.r; |
| 214 | rep.col.rgb[1] = alight->Color.g; |
| 215 | rep.col.rgb[2] = alight->Color.b; |
| 216 | rep.pos[0] = alight->Position.x; |
| 217 | rep.pos[1] = alight->Position.y; |
| 218 | rep.pos[2] = alight->Position.z; |
| 219 | rep.atten[0] = alight->Attenuation[0]; |
| 220 | rep.atten[1] = alight->Attenuation[1]; |
| 221 | break; |
| 222 | |
| 223 | case 3 : /* TOLS_SPOT */ |
| 224 | rep.type = TLightSpot; |
| 225 | rep.col.rgb[0] = alight->Color.r; |
| 226 | rep.col.rgb[1] = alight->Color.g; |
| 227 | rep.col.rgb[2] = alight->Color.b; |
| 228 | rep.pos[0] = alight->Position.x; |
| 229 | rep.pos[1] = alight->Position.y; |
| 230 | rep.pos[2] = alight->Position.z; |
| 231 | rep.dir[0] = alight->Direction.x; |
| 232 | rep.dir[1] = alight->Direction.y; |
| 233 | rep.dir[2] = alight->Direction.z; |
| 234 | rep.shine = alight->Concentration; |
| 235 | rep.atten[0] = alight->Attenuation[0]; |
| 236 | rep.atten[1] = alight->Attenuation[1]; |
| 237 | rep.angle = alight->Angle; |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | rep.HeadLight = alight->Headlight; |
| 242 | |
| 243 | myLights.Append(rep); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /*----------------------------------------------------------------------*/ |
| 248 | |
| 249 | //call_togl_setvisualisation |
| 250 | void OpenGl_View::SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext) |
| 251 | { |
| 252 | myVisualization = AContext.Visualization; |
| 253 | // Shading method |
| 254 | switch (AContext.Model) |
| 255 | { |
| 256 | case 1 : /* VISUAL3D_TOM_INTERP_COLOR */ |
| 257 | case 3 : /* VISUAL3D_TOM_VERTEX */ |
| 258 | myIntShadingMethod = TEL_SM_GOURAUD; |
| 259 | break; |
| 260 | default : |
| 261 | myIntShadingMethod = TEL_SM_FLAT; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | /*----------------------------------------------------------------------*/ |
| 267 | |
| 268 | //call_togl_cliplimit |
| 269 | void OpenGl_View::SetClipLimit (const Graphic3d_CView& theCView) |
| 270 | { |
| 271 | myZClip.Back.Limit = |
| 272 | (theCView.Context.ZClipBackPlane - theCView.Mapping.BackPlaneDistance) / |
| 273 | (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance); |
| 274 | myZClip.Front.Limit = |
| 275 | (theCView.Context.ZClipFrontPlane - theCView.Mapping.BackPlaneDistance) / |
| 276 | (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance); |
| 277 | if (myZClip.Back.Limit < 0.0f) |
| 278 | myZClip.Back.Limit = 0.0f; |
| 279 | if (myZClip.Front.Limit > 1.0f) |
| 280 | myZClip.Front.Limit = 1.0f; |
| 281 | if (myZClip.Back.Limit > myZClip.Front.Limit) |
| 282 | { |
| 283 | myZClip.Back.Limit = 0.0f; |
| 284 | myZClip.Front.Limit = 1.0f; |
| 285 | } |
| 286 | |
| 287 | myZClip.Back.IsOn = (theCView.Context.BackZClipping != 0); |
| 288 | myZClip.Front.IsOn = (theCView.Context.FrontZClipping != 0); |
| 289 | } |
| 290 | |
| 291 | /*----------------------------------------------------------------------*/ |
| 292 | |
| 293 | //call_togl_viewmapping |
| 294 | void OpenGl_View::SetMapping (const Graphic3d_CView& theCView) |
| 295 | { |
| 296 | const float ratio = theCView.DefWindow.dy / theCView.DefWindow.dx; |
| 297 | const float r_ratio = theCView.DefWindow.dx / theCView.DefWindow.dy; |
| 298 | |
| 299 | TEL_VIEW_MAPPING Map; |
| 300 | |
| 301 | Map.window.xmin = theCView.Mapping.WindowLimit.um; |
| 302 | Map.window.ymin = theCView.Mapping.WindowLimit.vm; |
| 303 | Map.window.xmax = theCView.Mapping.WindowLimit.uM; |
| 304 | Map.window.ymax = theCView.Mapping.WindowLimit.vM; |
| 305 | |
| 306 | Map.viewport.xmin = 0.F; |
| 307 | Map.viewport.xmax = ( 1.F < r_ratio ? 1.F : r_ratio ); |
| 308 | Map.viewport.ymin = 0.F; |
| 309 | Map.viewport.ymax = ( 1.F < ratio ? 1.F : ratio ); |
| 310 | Map.viewport.zmin = 0.F; |
| 311 | Map.viewport.zmax = 1.F; |
| 312 | |
| 313 | // projection type |
| 314 | switch (theCView.Mapping.Projection) |
| 315 | { |
| 316 | case 0 : |
| 317 | Map.proj = TelPerspective; |
| 318 | break; |
| 319 | case 1 : |
| 320 | Map.proj = TelParallel; |
| 321 | break; |
| 322 | } |
| 323 | |
| 324 | // projection reference point |
| 325 | Map.prp[0] = theCView.Mapping.ProjectionReferencePoint.x; |
| 326 | Map.prp[1] = theCView.Mapping.ProjectionReferencePoint.y; |
| 327 | Map.prp[2] = theCView.Mapping.ProjectionReferencePoint.z; |
| 328 | if (!openglDisplay.IsNull() && !openglDisplay->Walkthrough()) |
| 329 | Map.prp[2] += theCView.Mapping.FrontPlaneDistance; |
| 330 | |
| 331 | // view plane distance |
| 332 | Map.vpd = theCView.Mapping.ViewPlaneDistance; |
| 333 | |
| 334 | // back plane distance |
| 335 | Map.bpd = theCView.Mapping.BackPlaneDistance; |
| 336 | |
| 337 | // front plane distance |
| 338 | Map.fpd = theCView.Mapping.FrontPlaneDistance; |
| 339 | |
| 340 | Tint err_ind = 0; |
| 341 | |
| 342 | // use user-defined matrix |
| 343 | if (theCView.Mapping.IsCustomMatrix) |
| 344 | { |
| 345 | int i, j; |
| 346 | for( i = 0; i < 4; i++ ) |
| 347 | for( j = 0; j < 4; j++ ) |
| 348 | myMappingMatrix[i][j] = theCView.Mapping.ProjectionMatrix[i][j]; |
| 349 | } |
| 350 | else |
| 351 | TelEvalViewMappingMatrix( &Map, &err_ind, myMappingMatrix ); |
| 352 | |
| 353 | if (!err_ind) |
| 354 | myExtra.map = Map; |
| 355 | } |
| 356 | |
| 357 | /*----------------------------------------------------------------------*/ |
| 358 | |
| 359 | //call_togl_vieworientation |
| 360 | void OpenGl_View::SetOrientation (const Graphic3d_CView& theCView) |
| 361 | { |
| 362 | Tfloat Vrp[3]; |
| 363 | Tfloat Vpn[3]; |
| 364 | Tfloat Vup[3]; |
| 365 | Tfloat ScaleFactors[3]; |
| 366 | |
| 367 | Vrp[0] = theCView.Orientation.ViewReferencePoint.x; |
| 368 | Vrp[1] = theCView.Orientation.ViewReferencePoint.y; |
| 369 | Vrp[2] = theCView.Orientation.ViewReferencePoint.z; |
| 370 | |
| 371 | Vpn[0] = theCView.Orientation.ViewReferencePlane.x; |
| 372 | Vpn[1] = theCView.Orientation.ViewReferencePlane.y; |
| 373 | Vpn[2] = theCView.Orientation.ViewReferencePlane.z; |
| 374 | |
| 375 | Vup[0] = theCView.Orientation.ViewReferenceUp.x; |
| 376 | Vup[1] = theCView.Orientation.ViewReferenceUp.y; |
| 377 | Vup[2] = theCView.Orientation.ViewReferenceUp.z; |
| 378 | |
| 379 | ScaleFactors[0] = theCView.Orientation.ViewScaleX; |
| 380 | ScaleFactors[1] = theCView.Orientation.ViewScaleY; |
| 381 | ScaleFactors[2] = theCView.Orientation.ViewScaleZ; |
| 382 | |
| 383 | Tint err_ind = 0; |
| 384 | |
| 385 | // use user-defined matrix |
| 386 | if (theCView.Orientation.IsCustomMatrix) |
| 387 | { |
| 388 | int i, j; |
| 389 | for( i = 0; i < 4; i++ ) |
| 390 | for( j = 0; j < 4; j++ ) |
| 391 | myOrientationMatrix[i][j] = theCView.Orientation.ModelViewMatrix[i][j]; |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | TelEvalViewOrientationMatrix (Vrp, Vpn, Vup, ScaleFactors, &err_ind, myOrientationMatrix); |
| 396 | } |
| 397 | |
| 398 | if (!err_ind) |
| 399 | { |
| 400 | myExtra.vrp[0] = Vrp[0]; |
| 401 | myExtra.vrp[1] = Vrp[1]; |
| 402 | myExtra.vrp[2] = Vrp[2]; |
| 403 | |
| 404 | myExtra.vpn[0] = Vpn[0]; |
| 405 | myExtra.vpn[1] = Vpn[1]; |
| 406 | myExtra.vpn[2] = Vpn[2]; |
| 407 | |
| 408 | myExtra.vup[0] = Vup[0]; |
| 409 | myExtra.vup[1] = Vup[1]; |
| 410 | myExtra.vup[2] = Vup[2]; |
| 411 | |
| 412 | myExtra.scaleFactors[0] = ScaleFactors[0], |
| 413 | myExtra.scaleFactors[1] = ScaleFactors[1], |
| 414 | myExtra.scaleFactors[2] = ScaleFactors[2]; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /*----------------------------------------------------------------------*/ |
| 419 | |
| 420 | void OpenGl_View::SetFog (const Graphic3d_CView& theCView, |
| 421 | const Standard_Boolean theFlag) |
| 422 | { |
| 423 | if (!theFlag) |
| 424 | { |
| 425 | myFog.IsOn = Standard_False; |
| 426 | } |
| 427 | else |
| 428 | { |
| 429 | myFog.IsOn = Standard_True; |
| 430 | |
| 431 | myFog.Front = |
| 432 | (theCView.Context.DepthFrontPlane - theCView.Mapping.BackPlaneDistance) / |
| 433 | (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance); |
| 434 | |
| 435 | myFog.Back = |
| 436 | (theCView.Context.DepthBackPlane - theCView.Mapping.BackPlaneDistance) / |
| 437 | (theCView.Mapping.FrontPlaneDistance - theCView.Mapping.BackPlaneDistance); |
| 438 | |
| 439 | if (myFog.Front < 0.F) |
| 440 | myFog.Front = 0.F; |
| 441 | else if (myFog.Front > 1.F) |
| 442 | myFog.Front = 1.F; |
| 443 | |
| 444 | if (myFog.Back < 0.F) |
| 445 | myFog.Back = 0.F; |
| 446 | else if (myFog.Back > 1.F) |
| 447 | myFog.Back = 1.F; |
| 448 | |
| 449 | if (myFog.Back > myFog.Front) |
| 450 | { |
| 451 | myFog.Front = 1.F; |
| 452 | myFog.Back = 0.F; |
| 453 | } |
| 454 | |
| 455 | myFog.Color.rgb[0] = theCView.DefWindow.Background.r; |
| 456 | myFog.Color.rgb[1] = theCView.DefWindow.Background.g; |
| 457 | myFog.Color.rgb[2] = theCView.DefWindow.Background.b; |
| 458 | myFog.Color.rgb[3] = 1.0f; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /*----------------------------------------------------------------------*/ |
| 463 | |
| 464 | void OpenGl_View::TriedronDisplay (const Handle(OpenGl_Context)& theCtx, |
| 465 | const Aspect_TypeOfTriedronPosition thePosition, |
| 466 | const Quantity_NameOfColor theColor, |
| 467 | const Standard_Real theScale, |
| 468 | const Standard_Boolean theAsWireframe) |
| 469 | { |
| 470 | OpenGl_Element::Destroy (theCtx, myTrihedron); |
| 471 | myTrihedron = new OpenGl_Trihedron (thePosition, theColor, theScale, theAsWireframe); |
| 472 | } |
| 473 | |
| 474 | /*----------------------------------------------------------------------*/ |
| 475 | |
| 476 | void OpenGl_View::TriedronErase (const Handle(OpenGl_Context)& theCtx) |
| 477 | { |
| 478 | OpenGl_Element::Destroy (theCtx, myTrihedron); |
| 479 | } |
| 480 | |
| 481 | /*----------------------------------------------------------------------*/ |
| 482 | |
| 483 | void OpenGl_View::GraduatedTrihedronDisplay (const Handle(OpenGl_Context)& theCtx, |
| 484 | const Graphic3d_CGraduatedTrihedron& theData) |
| 485 | { |
| 486 | OpenGl_Element::Destroy (theCtx, myGraduatedTrihedron); |
| 487 | myGraduatedTrihedron = new OpenGl_GraduatedTrihedron (theData); |
| 488 | } |
| 489 | |
| 490 | /*----------------------------------------------------------------------*/ |
| 491 | |
| 492 | void OpenGl_View::GraduatedTrihedronErase (const Handle(OpenGl_Context)& theCtx) |
| 493 | { |
| 494 | OpenGl_Element::Destroy (theCtx, myGraduatedTrihedron); |
| 495 | } |
| 496 | |
| 497 | /*----------------------------------------------------------------------*/ |
| 498 | |
| 499 | //transform_persistence_end |
| 500 | void OpenGl_View::EndTransformPersistence() |
| 501 | { |
| 502 | if (myIsTransPers) |
| 503 | { |
| 504 | // restore matrix |
| 505 | glMatrixMode (GL_PROJECTION); |
| 506 | glPopMatrix(); |
| 507 | glMatrixMode (GL_MODELVIEW); |
| 508 | glPopMatrix(); |
| 509 | myIsTransPers = Standard_False; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /*----------------------------------------------------------------------*/ |
| 514 | |
| 515 | //transform_persistence_begin |
| 516 | const TEL_TRANSFORM_PERSISTENCE* OpenGl_View::BeginTransformPersistence (const TEL_TRANSFORM_PERSISTENCE* theTransPers) |
| 517 | { |
| 518 | const TEL_TRANSFORM_PERSISTENCE* aTransPersPrev = myTransPers; |
| 519 | myTransPers = theTransPers; |
| 520 | if (theTransPers->mode == 0) |
| 521 | { |
| 522 | EndTransformPersistence(); |
| 523 | return aTransPersPrev; |
| 524 | } |
| 525 | |
| 526 | GLint aViewport[4]; |
| 527 | GLdouble aModelMatrix[4][4]; |
| 528 | GLdouble aProjMatrix[4][4]; |
| 529 | glGetIntegerv (GL_VIEWPORT, aViewport); |
| 530 | glGetDoublev (GL_MODELVIEW_MATRIX, (GLdouble* )aModelMatrix); |
| 531 | glGetDoublev (GL_PROJECTION_MATRIX, (GLdouble *)aProjMatrix); |
| 532 | const GLdouble aViewportW = (GLdouble )aViewport[2]; |
| 533 | const GLdouble aViewportH = (GLdouble )aViewport[3]; |
| 534 | |
| 535 | if (myIsTransPers) |
| 536 | { |
| 537 | // pop matrix stack - it will be overridden later |
| 538 | glMatrixMode (GL_PROJECTION); |
| 539 | glPopMatrix(); |
| 540 | glMatrixMode (GL_MODELVIEW); |
| 541 | glPopMatrix(); |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | myIsTransPers = Standard_True; |
| 546 | } |
| 547 | |
| 548 | // push matrices into stack and reset them |
| 549 | glMatrixMode (GL_MODELVIEW); |
| 550 | glPushMatrix(); |
| 551 | glLoadIdentity(); |
| 552 | |
| 553 | glMatrixMode (GL_PROJECTION); |
| 554 | glPushMatrix(); |
| 555 | glLoadIdentity(); |
| 556 | |
| 557 | // get the window's (fixed) coordinates for theTransPers->point before matrixes modifications |
| 558 | GLdouble aWinX = 0.0, aWinY = 0.0, aWinZ = 0.0; |
| 559 | if ((theTransPers->mode & TPF_PAN) != TPF_PAN) |
| 560 | { |
| 561 | gluProject (theTransPers->pointX, theTransPers->pointY, theTransPers->pointZ, |
| 562 | (GLdouble* )aModelMatrix, (GLdouble* )aProjMatrix, aViewport, |
| 563 | &aWinX, &aWinY, &aWinZ); |
| 564 | } |
| 565 | |
| 566 | // prevent zooming |
| 567 | if ((theTransPers->mode & TPF_ZOOM) |
| 568 | || (theTransPers->mode == TPF_TRIEDRON)) |
| 569 | { |
| 570 | // compute fixed-zoom multiplier |
| 571 | // actually function works ugly with TelPerspective! |
| 572 | const GLdouble aDet2 = 0.002 / (aViewportW > aViewportH ? aProjMatrix[1][1] : aProjMatrix[0][0]); |
| 573 | aProjMatrix[0][0] *= aDet2; |
| 574 | aProjMatrix[1][1] *= aDet2; |
| 575 | aProjMatrix[2][2] *= aDet2; |
| 576 | } |
| 577 | |
| 578 | // prevent translation - annulate translate matrix |
| 579 | if ((theTransPers->mode & TPF_PAN) |
| 580 | || (theTransPers->mode == TPF_TRIEDRON)) |
| 581 | { |
| 582 | aModelMatrix[3][0] = 0.0; |
| 583 | aModelMatrix[3][1] = 0.0; |
| 584 | aModelMatrix[3][2] = 0.0; |
| 585 | aProjMatrix [3][0] = 0.0; |
| 586 | aProjMatrix [3][1] = 0.0; |
| 587 | aProjMatrix [3][2] = 0.0; |
| 588 | } |
| 589 | |
| 590 | // prevent scaling-on-axis |
| 591 | if (theTransPers->mode & TPF_ZOOM) |
| 592 | { |
| 593 | const double aScaleX = myExtra.scaleFactors[0]; |
| 594 | const double aScaleY = myExtra.scaleFactors[1]; |
| 595 | const double aScaleZ = myExtra.scaleFactors[2]; |
| 596 | for (int i = 0; i < 3; ++i) |
| 597 | { |
| 598 | aModelMatrix[0][i] /= aScaleX; |
| 599 | aModelMatrix[1][i] /= aScaleY; |
| 600 | aModelMatrix[2][i] /= aScaleZ; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | // prevent rotating - annulate rotate matrix |
| 605 | if (theTransPers->mode & TPF_ROTATE) |
| 606 | { |
| 607 | aModelMatrix[0][0] = 1.0; |
| 608 | aModelMatrix[1][1] = 1.0; |
| 609 | aModelMatrix[2][2] = 1.0; |
| 610 | |
| 611 | aModelMatrix[1][0] = 0.0; |
| 612 | aModelMatrix[2][0] = 0.0; |
| 613 | aModelMatrix[0][1] = 0.0; |
| 614 | aModelMatrix[2][1] = 0.0; |
| 615 | aModelMatrix[0][2] = 0.0; |
| 616 | aModelMatrix[1][2] = 0.0; |
| 617 | } |
| 618 | |
| 619 | // load computed matrices |
| 620 | glMatrixMode (GL_MODELVIEW); |
| 621 | glMultMatrixd ((GLdouble* )aModelMatrix); |
| 622 | |
| 623 | glMatrixMode (GL_PROJECTION); |
| 624 | glMultMatrixd ((GLdouble* )aProjMatrix); |
| 625 | |
| 626 | if (theTransPers->mode == TPF_TRIEDRON) |
| 627 | { |
| 628 | // move to the window corner |
| 629 | if (theTransPers->pointX != 0.0 |
| 630 | && theTransPers->pointY != 0.0) |
| 631 | { |
| 632 | GLdouble aW1, aH1, aW2, aH2, aDummy; |
| 633 | glMatrixMode (GL_PROJECTION); |
| 634 | gluUnProject ( 0.5 * aViewportW, 0.5 * aViewportH, 0.0, |
| 635 | (GLdouble* )THE_IDENTITY_MATRIX, (GLdouble* )aProjMatrix, aViewport, |
| 636 | &aW1, &aH1, &aDummy); |
| 637 | gluUnProject (-0.5 * aViewportW, -0.5 * aViewportH, 0.0, |
| 638 | (GLdouble* )THE_IDENTITY_MATRIX, (GLdouble* )aProjMatrix, aViewport, |
| 639 | &aW2, &aH2, &aDummy); |
| 640 | GLdouble aMoveX = 0.5 * (aW1 - aW2 - theTransPers->pointZ); |
| 641 | GLdouble aMoveY = 0.5 * (aH1 - aH2 - theTransPers->pointZ); |
| 642 | aMoveX = (theTransPers->pointX > 0.0) ? aMoveX : -aMoveX; |
| 643 | aMoveY = (theTransPers->pointY > 0.0) ? aMoveY : -aMoveY; |
| 644 | glTranslated (aMoveX, aMoveY, 0.0); |
| 645 | } |
| 646 | } |
| 647 | else if ((theTransPers->mode & TPF_PAN) != TPF_PAN) |
| 648 | { |
| 649 | // move to thePoint using saved win-coordinates ('marker-behaviour') |
| 650 | GLdouble aMoveX, aMoveY, aMoveZ; |
| 651 | glGetDoublev (GL_MODELVIEW_MATRIX, (GLdouble* )aModelMatrix); |
| 652 | glGetDoublev (GL_PROJECTION_MATRIX, (GLdouble* )aProjMatrix); |
| 653 | gluUnProject (aWinX, aWinY, aWinZ, |
| 654 | (GLdouble* )aModelMatrix, (GLdouble* )aProjMatrix, aViewport, |
| 655 | &aMoveX, &aMoveY, &aMoveZ); |
| 656 | |
| 657 | glMatrixMode (GL_MODELVIEW); |
| 658 | glTranslated (aMoveX, aMoveY, aMoveZ); |
| 659 | } |
| 660 | |
| 661 | return aTransPersPrev; |
| 662 | } |