0024530: TKMesh - remove unused package IntPoly
[occt.git] / src / OpenGl / OpenGl_View.cxx
CommitLineData
b311480e 1// Created on: 2011-09-20
2// Created by: Sergey ZERCHANINOV
973c2be1 3// Copyright (c) 2011-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
973c2be1 7// This library is free software; you can redistribute it and / or modify it
8// under the terms of the GNU Lesser General Public 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
e276548b 16#ifdef HAVE_CONFIG_H
17 #include <config.h>
18#endif
19
30f0ad28 20#include <NCollection_Mat4.hxx>
5f8b738e 21
bf75be98 22#include <OpenGl_Context.hxx>
2166f0fa 23#include <OpenGl_Display.hxx>
30f0ad28 24#include <OpenGl_GlCore11.hxx>
25#include <OpenGl_GraduatedTrihedron.hxx>
392ac980 26#include <OpenGl_GraphicDriver.hxx>
30f0ad28 27#include <OpenGl_ShaderManager.hxx>
bf75be98 28#include <OpenGl_Texture.hxx>
2166f0fa 29#include <OpenGl_Trihedron.hxx>
2166f0fa 30#include <OpenGl_transform_persistence.hxx>
30f0ad28 31#include <OpenGl_View.hxx>
32#include <OpenGl_Workspace.hxx>
2166f0fa 33
bf75be98 34#include <Graphic3d_TextureEnv.hxx>
35
2166f0fa
SK
36IMPLEMENT_STANDARD_HANDLE(OpenGl_View,MMgt_TShared)
37IMPLEMENT_STANDARD_RTTIEXT(OpenGl_View,MMgt_TShared)
38
39/*----------------------------------------------------------------------*/
40
41static const OPENGL_BG_TEXTURE myDefaultBgTexture = { 0, 0, 0, Aspect_FM_CENTERED };
42static const OPENGL_BG_GRADIENT myDefaultBgGradient = { {{ 0.F, 0.F, 0.F, 1.F }}, {{ 0.F, 0.F, 0.F, 1.F }}, Aspect_GFM_NONE };
43static 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 } };
44static const OPENGL_ZCLIP myDefaultZClip = { { Standard_True, 0.F }, { Standard_True, 1.F } };
2166f0fa
SK
45
46static const OPENGL_FOG myDefaultFog = { Standard_False, 0.F, 1.F, { { 0.F, 0.F, 0.F, 1.F } } };
47static const TEL_TRANSFORM_PERSISTENCE myDefaultTransPers = { 0, 0.F, 0.F, 0.F };
c34dba32 48static const GLdouble THE_IDENTITY_MATRIX[4][4] =
49{
50 {1.0, 0.0, 0.0, 0.0},
51 {0.0, 1.0, 0.0, 0.0},
52 {0.0, 0.0, 1.0, 0.0},
53 {0.0, 0.0, 0.0, 1.0}
54};
2166f0fa
SK
55
56/*----------------------------------------------------------------------*/
57
392ac980 58OpenGl_View::OpenGl_View (const CALL_DEF_VIEWCONTEXT &AContext,
59 OpenGl_StateCounter* theCounter)
bf75be98 60: mySurfaceDetail(Visual3d_TOD_NONE),
2166f0fa
SK
61 myBackfacing(0),
62 myBgTexture(myDefaultBgTexture),
63 myBgGradient(myDefaultBgGradient),
2166f0fa
SK
64 //shield_indicator = TOn,
65 //shield_colour = { { 0.F, 0.F, 0.F, 1.F } },
66 //border_indicator = TOff,
67 //border_colour = { { 0.F, 0.F, 0.F, 1.F } },
68 //active_status = TOn,
69 myZClip(myDefaultZClip),
b5ac8292 70 myCamera(AContext.Camera),
2166f0fa 71 myFog(myDefaultFog),
2f6cb3ac 72 myTrihedron(NULL),
73 myGraduatedTrihedron(NULL),
2166f0fa
SK
74 myVisualization(AContext.Visualization),
75 myIntShadingMethod(TEL_SM_GOURAUD),
76 myAntiAliasing(Standard_False),
2166f0fa 77 myTransPers(&myDefaultTransPers),
30f0ad28 78 myIsTransPers(Standard_False),
b5ac8292 79 myProjectionState (0),
80 myModelViewState (0),
392ac980 81 myStateCounter (theCounter),
392ac980 82 myLastLightSourceState (0, 0)
2166f0fa 83{
2166f0fa
SK
84
85 // Shading method
86 switch (AContext.Model)
87 {
88 case 1 : /* VISUAL3D_TOM_INTERP_COLOR */
89 case 3 : /* VISUAL3D_TOM_VERTEX */
90 myIntShadingMethod = TEL_SM_GOURAUD;
91 break;
92 default :
93 myIntShadingMethod = TEL_SM_FLAT;
94 break;
95 }
e276548b 96
392ac980 97 myCurrLightSourceState = myStateCounter->Increment();
98
e276548b 99#ifdef HAVE_OPENCL
100 myModificationState = 1; // initial state
101#endif
2166f0fa
SK
102}
103
104/*----------------------------------------------------------------------*/
105
106OpenGl_View::~OpenGl_View ()
107{
bf75be98 108 ReleaseGlResources (NULL); // ensure ReleaseGlResources() was called within valid context
109}
110
111void OpenGl_View::ReleaseGlResources (const Handle(OpenGl_Context)& theCtx)
112{
a174a3c5 113 OpenGl_Element::Destroy (theCtx, myTrihedron);
114 OpenGl_Element::Destroy (theCtx, myGraduatedTrihedron);
30f0ad28 115
bf75be98 116 if (!myTextureEnv.IsNull())
117 {
118 theCtx->DelayedRelease (myTextureEnv);
119 myTextureEnv.Nullify();
120 }
121 if (myBgTexture.TexId != 0)
122 {
123 glDeleteTextures (1, (GLuint*)&(myBgTexture.TexId));
124 myBgTexture.TexId = 0;
125 }
bf75be98 126}
2166f0fa 127
bf75be98 128void OpenGl_View::SetTextureEnv (const Handle(OpenGl_Context)& theCtx,
129 const Handle(Graphic3d_TextureEnv)& theTexture)
130{
131 if (!myTextureEnv.IsNull())
132 {
133 theCtx->DelayedRelease (myTextureEnv);
134 myTextureEnv.Nullify();
135 }
136
137 if (theTexture.IsNull())
138 {
139 return;
140 }
141
142 myTextureEnv = new OpenGl_Texture (theTexture->GetParams());
143 Handle(Image_PixMap) anImage = theTexture->GetImage();
e276548b 144 if (!anImage.IsNull())
da0e82aa 145 myTextureEnv->Init (theCtx, *anImage.operator->(), theTexture->Type());
e276548b 146
147#ifdef HAVE_OPENCL
148 myModificationState++;
149#endif
150}
151
152void OpenGl_View::SetSurfaceDetail (const Visual3d_TypeOfSurfaceDetail theMode)
153{
154 mySurfaceDetail = theMode;
155
156#ifdef HAVE_OPENCL
157 myModificationState++;
158#endif
2166f0fa
SK
159}
160
12381341 161// =======================================================================
162// function : SetBackfacing
163// purpose :
164// =======================================================================
de75ed09 165void OpenGl_View::SetBackfacing (const Standard_Integer theMode)
2166f0fa 166{
de75ed09 167 myBackfacing = theMode;
2166f0fa
SK
168}
169
12381341 170// =======================================================================
171// function : SetLights
172// purpose :
173// =======================================================================
174void OpenGl_View::SetLights (const CALL_DEF_VIEWCONTEXT& theViewCtx)
2166f0fa
SK
175{
176 myLights.Clear();
12381341 177 for (Standard_Integer aLightIt = 0; aLightIt < theViewCtx.NbActiveLight; ++aLightIt)
2166f0fa 178 {
12381341 179 myLights.Append (theViewCtx.ActiveLight[aLightIt]);
2166f0fa 180 }
392ac980 181 myCurrLightSourceState = myStateCounter->Increment();
2166f0fa
SK
182}
183
184/*----------------------------------------------------------------------*/
185
2166f0fa
SK
186//call_togl_setvisualisation
187void OpenGl_View::SetVisualisation (const CALL_DEF_VIEWCONTEXT &AContext)
188{
189 myVisualization = AContext.Visualization;
190 // Shading method
191 switch (AContext.Model)
192 {
193 case 1 : /* VISUAL3D_TOM_INTERP_COLOR */
194 case 3 : /* VISUAL3D_TOM_VERTEX */
195 myIntShadingMethod = TEL_SM_GOURAUD;
196 break;
197 default :
198 myIntShadingMethod = TEL_SM_FLAT;
199 break;
200 }
201}
202
203/*----------------------------------------------------------------------*/
204
205//call_togl_cliplimit
bf75be98 206void OpenGl_View::SetClipLimit (const Graphic3d_CView& theCView)
2166f0fa 207{
b5ac8292 208 myZClip.Back.Limit = theCView.Context.ZClipBackPlane;
209 myZClip.Front.Limit = theCView.Context.ZClipFrontPlane;
2166f0fa 210
bf75be98 211 myZClip.Back.IsOn = (theCView.Context.BackZClipping != 0);
212 myZClip.Front.IsOn = (theCView.Context.FrontZClipping != 0);
2166f0fa
SK
213}
214
215/*----------------------------------------------------------------------*/
216
bf75be98 217void OpenGl_View::SetFog (const Graphic3d_CView& theCView,
218 const Standard_Boolean theFlag)
2166f0fa 219{
bf75be98 220 if (!theFlag)
2166f0fa
SK
221 {
222 myFog.IsOn = Standard_False;
223 }
224 else
225 {
226 myFog.IsOn = Standard_True;
227
b5ac8292 228 myFog.Front = theCView.Context.DepthFrontPlane;
229 myFog.Back = theCView.Context.DepthBackPlane;
2166f0fa 230
bf75be98 231 myFog.Color.rgb[0] = theCView.DefWindow.Background.r;
232 myFog.Color.rgb[1] = theCView.DefWindow.Background.g;
233 myFog.Color.rgb[2] = theCView.DefWindow.Background.b;
234 myFog.Color.rgb[3] = 1.0f;
2166f0fa
SK
235 }
236}
237
238/*----------------------------------------------------------------------*/
239
a174a3c5 240void OpenGl_View::TriedronDisplay (const Handle(OpenGl_Context)& theCtx,
241 const Aspect_TypeOfTriedronPosition thePosition,
242 const Quantity_NameOfColor theColor,
243 const Standard_Real theScale,
244 const Standard_Boolean theAsWireframe)
2166f0fa 245{
a174a3c5 246 OpenGl_Element::Destroy (theCtx, myTrihedron);
247 myTrihedron = new OpenGl_Trihedron (thePosition, theColor, theScale, theAsWireframe);
2166f0fa
SK
248}
249
250/*----------------------------------------------------------------------*/
251
a174a3c5 252void OpenGl_View::TriedronErase (const Handle(OpenGl_Context)& theCtx)
2166f0fa 253{
a174a3c5 254 OpenGl_Element::Destroy (theCtx, myTrihedron);
2166f0fa
SK
255}
256
257/*----------------------------------------------------------------------*/
258
a174a3c5 259void OpenGl_View::GraduatedTrihedronDisplay (const Handle(OpenGl_Context)& theCtx,
260 const Graphic3d_CGraduatedTrihedron& theData)
2166f0fa 261{
a174a3c5 262 OpenGl_Element::Destroy (theCtx, myGraduatedTrihedron);
263 myGraduatedTrihedron = new OpenGl_GraduatedTrihedron (theData);
2166f0fa
SK
264}
265
266/*----------------------------------------------------------------------*/
267
a174a3c5 268void OpenGl_View::GraduatedTrihedronErase (const Handle(OpenGl_Context)& theCtx)
2166f0fa 269{
a174a3c5 270 OpenGl_Element::Destroy (theCtx, myGraduatedTrihedron);
2166f0fa
SK
271}
272
273/*----------------------------------------------------------------------*/
274
275//transform_persistence_end
30f0ad28 276void OpenGl_View::EndTransformPersistence(const Handle(OpenGl_Context)& theCtx)
2166f0fa 277{
c34dba32 278 if (myIsTransPers)
2166f0fa 279 {
c34dba32 280 // restore matrix
2166f0fa 281 glMatrixMode (GL_PROJECTION);
c34dba32 282 glPopMatrix();
2166f0fa 283 glMatrixMode (GL_MODELVIEW);
c34dba32 284 glPopMatrix();
2166f0fa 285 myIsTransPers = Standard_False;
30f0ad28 286
287 // Note: the approach of accessing OpenGl matrices is used now since the matrix
288 // manipulation are made with help of OpenGl methods. This might be replaced by
289 // direct computation of matrices by OCC subroutines.
290 Tmatrix3 aResultWorldView;
291 glGetFloatv (GL_MODELVIEW_MATRIX, *aResultWorldView);
292
293 Tmatrix3 aResultProjection;
294 glGetFloatv (GL_PROJECTION_MATRIX, *aResultProjection);
295
296 // Set OCCT state uniform variables
b5ac8292 297 theCtx->ShaderManager()->RevertWorldViewStateTo (&aResultWorldView);
298 theCtx->ShaderManager()->RevertProjectionStateTo (&aResultProjection);
2166f0fa 299 }
bf75be98 300}
2166f0fa
SK
301
302/*----------------------------------------------------------------------*/
303
304//transform_persistence_begin
30f0ad28 305const TEL_TRANSFORM_PERSISTENCE* OpenGl_View::BeginTransformPersistence (const Handle(OpenGl_Context)& theCtx,
306 const TEL_TRANSFORM_PERSISTENCE* theTransPers)
2166f0fa 307{
c34dba32 308 const TEL_TRANSFORM_PERSISTENCE* aTransPersPrev = myTransPers;
309 myTransPers = theTransPers;
310 if (theTransPers->mode == 0)
2166f0fa 311 {
30f0ad28 312 EndTransformPersistence (theCtx);
c34dba32 313 return aTransPersPrev;
2166f0fa
SK
314 }
315
c34dba32 316 GLint aViewport[4];
317 GLdouble aModelMatrix[4][4];
318 GLdouble aProjMatrix[4][4];
319 glGetIntegerv (GL_VIEWPORT, aViewport);
320 glGetDoublev (GL_MODELVIEW_MATRIX, (GLdouble* )aModelMatrix);
321 glGetDoublev (GL_PROJECTION_MATRIX, (GLdouble *)aProjMatrix);
322 const GLdouble aViewportW = (GLdouble )aViewport[2];
323 const GLdouble aViewportH = (GLdouble )aViewport[3];
2166f0fa 324
c34dba32 325 if (myIsTransPers)
2166f0fa 326 {
c34dba32 327 // pop matrix stack - it will be overridden later
2166f0fa 328 glMatrixMode (GL_PROJECTION);
c34dba32 329 glPopMatrix();
2166f0fa 330 glMatrixMode (GL_MODELVIEW);
c34dba32 331 glPopMatrix();
2166f0fa
SK
332 }
333 else
c34dba32 334 {
2166f0fa 335 myIsTransPers = Standard_True;
c34dba32 336 }
2166f0fa 337
c34dba32 338 // push matrices into stack and reset them
339 glMatrixMode (GL_MODELVIEW);
2166f0fa
SK
340 glPushMatrix();
341 glLoadIdentity();
342
c34dba32 343 glMatrixMode (GL_PROJECTION);
2166f0fa
SK
344 glPushMatrix();
345 glLoadIdentity();
346
c34dba32 347 // get the window's (fixed) coordinates for theTransPers->point before matrixes modifications
348 GLdouble aWinX = 0.0, aWinY = 0.0, aWinZ = 0.0;
349 if ((theTransPers->mode & TPF_PAN) != TPF_PAN)
2166f0fa 350 {
c34dba32 351 gluProject (theTransPers->pointX, theTransPers->pointY, theTransPers->pointZ,
352 (GLdouble* )aModelMatrix, (GLdouble* )aProjMatrix, aViewport,
353 &aWinX, &aWinY, &aWinZ);
2166f0fa
SK
354 }
355
c34dba32 356 // prevent zooming
357 if ((theTransPers->mode & TPF_ZOOM)
358 || (theTransPers->mode == TPF_TRIEDRON))
2166f0fa 359 {
c34dba32 360 // compute fixed-zoom multiplier
361 // actually function works ugly with TelPerspective!
362 const GLdouble aDet2 = 0.002 / (aViewportW > aViewportH ? aProjMatrix[1][1] : aProjMatrix[0][0]);
363 aProjMatrix[0][0] *= aDet2;
364 aProjMatrix[1][1] *= aDet2;
365 aProjMatrix[2][2] *= aDet2;
2166f0fa
SK
366 }
367
c34dba32 368 // prevent translation - annulate translate matrix
369 if ((theTransPers->mode & TPF_PAN)
370 || (theTransPers->mode == TPF_TRIEDRON))
2166f0fa 371 {
c34dba32 372 aModelMatrix[3][0] = 0.0;
373 aModelMatrix[3][1] = 0.0;
374 aModelMatrix[3][2] = 0.0;
375 aProjMatrix [3][0] = 0.0;
376 aProjMatrix [3][1] = 0.0;
377 aProjMatrix [3][2] = 0.0;
2166f0fa 378 }
2166f0fa 379
c34dba32 380 // prevent scaling-on-axis
381 if (theTransPers->mode & TPF_ZOOM)
382 {
b5ac8292 383 const gp_Pnt anAxialScale = myCamera->AxialScale();
384 const double aScaleX = anAxialScale.X();
385 const double aScaleY = anAxialScale.Y();
386 const double aScaleZ = anAxialScale.Z();
c34dba32 387 for (int i = 0; i < 3; ++i)
388 {
389 aModelMatrix[0][i] /= aScaleX;
390 aModelMatrix[1][i] /= aScaleY;
391 aModelMatrix[2][i] /= aScaleZ;
392 }
2166f0fa
SK
393 }
394
c34dba32 395 // prevent rotating - annulate rotate matrix
396 if (theTransPers->mode & TPF_ROTATE)
2166f0fa 397 {
c34dba32 398 aModelMatrix[0][0] = 1.0;
399 aModelMatrix[1][1] = 1.0;
400 aModelMatrix[2][2] = 1.0;
401
402 aModelMatrix[1][0] = 0.0;
403 aModelMatrix[2][0] = 0.0;
404 aModelMatrix[0][1] = 0.0;
405 aModelMatrix[2][1] = 0.0;
406 aModelMatrix[0][2] = 0.0;
407 aModelMatrix[1][2] = 0.0;
2166f0fa
SK
408 }
409
c34dba32 410 // load computed matrices
2166f0fa 411 glMatrixMode (GL_MODELVIEW);
c34dba32 412 glMultMatrixd ((GLdouble* )aModelMatrix);
2166f0fa
SK
413
414 glMatrixMode (GL_PROJECTION);
c34dba32 415 glMultMatrixd ((GLdouble* )aProjMatrix);
2166f0fa 416
c34dba32 417 if (theTransPers->mode == TPF_TRIEDRON)
2166f0fa 418 {
c34dba32 419 // move to the window corner
420 if (theTransPers->pointX != 0.0
421 && theTransPers->pointY != 0.0)
2166f0fa 422 {
c34dba32 423 GLdouble aW1, aH1, aW2, aH2, aDummy;
424 glMatrixMode (GL_PROJECTION);
425 gluUnProject ( 0.5 * aViewportW, 0.5 * aViewportH, 0.0,
426 (GLdouble* )THE_IDENTITY_MATRIX, (GLdouble* )aProjMatrix, aViewport,
427 &aW1, &aH1, &aDummy);
428 gluUnProject (-0.5 * aViewportW, -0.5 * aViewportH, 0.0,
429 (GLdouble* )THE_IDENTITY_MATRIX, (GLdouble* )aProjMatrix, aViewport,
430 &aW2, &aH2, &aDummy);
431 GLdouble aMoveX = 0.5 * (aW1 - aW2 - theTransPers->pointZ);
432 GLdouble aMoveY = 0.5 * (aH1 - aH2 - theTransPers->pointZ);
433 aMoveX = (theTransPers->pointX > 0.0) ? aMoveX : -aMoveX;
434 aMoveY = (theTransPers->pointY > 0.0) ? aMoveY : -aMoveY;
435 glTranslated (aMoveX, aMoveY, 0.0);
2166f0fa
SK
436 }
437 }
c34dba32 438 else if ((theTransPers->mode & TPF_PAN) != TPF_PAN)
439 {
440 // move to thePoint using saved win-coordinates ('marker-behaviour')
441 GLdouble aMoveX, aMoveY, aMoveZ;
442 glGetDoublev (GL_MODELVIEW_MATRIX, (GLdouble* )aModelMatrix);
443 glGetDoublev (GL_PROJECTION_MATRIX, (GLdouble* )aProjMatrix);
444 gluUnProject (aWinX, aWinY, aWinZ,
445 (GLdouble* )aModelMatrix, (GLdouble* )aProjMatrix, aViewport,
446 &aMoveX, &aMoveY, &aMoveZ);
2166f0fa 447
c34dba32 448 glMatrixMode (GL_MODELVIEW);
449 glTranslated (aMoveX, aMoveY, aMoveZ);
450 }
2166f0fa 451
30f0ad28 452 // Note: the approach of accessing OpenGl matrices is used now since the matrix
453 // manipulation are made with help of OpenGl methods. This might be replaced by
454 // direct computation of matrices by OCC subroutines.
455 Tmatrix3 aResultWorldView;
456 glGetFloatv (GL_MODELVIEW_MATRIX, *aResultWorldView);
457
458 Tmatrix3 aResultProjection;
459 glGetFloatv (GL_PROJECTION_MATRIX, *aResultProjection);
460
461 // Set OCCT state uniform variables
b5ac8292 462 theCtx->ShaderManager()->UpdateWorldViewStateTo (&aResultWorldView);
463 theCtx->ShaderManager()->UpdateProjectionStateTo (&aResultProjection);
30f0ad28 464
c34dba32 465 return aTransPersPrev;
466}
b5ac8292 467
468/*----------------------------------------------------------------------*/
469
470void OpenGl_View::GetMatrices (TColStd_Array2OfReal& theMatOrient,
471 TColStd_Array2OfReal& theMatMapping) const
472{
473 const OpenGl_Matrix* aProj = (const OpenGl_Matrix*) &myCamera->ProjectionMatrix();
474 const OpenGl_Matrix* aOrient = (const OpenGl_Matrix*) &myCamera->OrientationMatrix();
475
476 int i, j;
477 for (i = 0; i < 4; ++i)
478 {
479 for (j = 0; j < 4; ++j)
480 {
481 theMatOrient (i, j) = aOrient->mat[j][i];
482 theMatMapping (i, j) = aProj-> mat[j][i];
483 }
484 }
485}
486/*----------------------------------------------------------------------*/