0024138: Exception during projection of the point on the face
[occt.git] / src / OpenGl / OpenGl_Workspace.cxx
CommitLineData
b311480e 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
bf75be98 20#include <OpenGl_GlCore12.hxx>
5f8b738e 21
2166f0fa
SK
22#include <InterfaceGraphic.hxx>
23
2166f0fa
SK
24#include <OpenGl_AspectLine.hxx>
25#include <OpenGl_AspectFace.hxx>
26#include <OpenGl_AspectMarker.hxx>
27#include <OpenGl_AspectText.hxx>
bf75be98 28#include <OpenGl_Context.hxx>
a174a3c5 29#include <OpenGl_FrameBuffer.hxx>
bf75be98 30#include <OpenGl_Texture.hxx>
a174a3c5 31#include <OpenGl_Workspace.hxx>
2166f0fa 32
bf75be98 33#include <Graphic3d_TextureParams.hxx>
2166f0fa 34
58655684 35#if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE)
a174a3c5 36 #include <OpenGl_AVIWriter.hxx>
37#endif
38
2166f0fa
SK
39IMPLEMENT_STANDARD_HANDLE(OpenGl_Workspace,OpenGl_Window)
40IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,OpenGl_Window)
41
42namespace
43{
44 static const TEL_COLOUR myDefaultHighlightColor = { { 1.F, 1.F, 1.F, 1.F } };
45
46 static const OpenGl_AspectLine myDefaultAspectLine;
47 static const OpenGl_AspectFace myDefaultAspectFace;
48 static const OpenGl_AspectMarker myDefaultAspectMarker;
49 static const OpenGl_AspectText myDefaultAspectText;
50
51 static const OpenGl_TextParam myDefaultTextParam =
52 {
53 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM
54 };
55
56 static const OpenGl_Matrix myDefaultMatrix =
57 {
58 {{ 1.0F, 0.0F, 0.0F, 0.0F },
59 { 0.0F, 1.0F, 0.0F, 0.0F },
60 { 0.0F, 0.0F, 1.0F, 0.0F },
61 { 0.0F, 0.0F, 0.0F, 1.0F }}
62 };
bf75be98 63
2166f0fa
SK
64};
65
66// =======================================================================
67// function : OpenGl_Workspace
68// purpose :
69// =======================================================================
70OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_Display)& theDisplay,
71 const CALL_DEF_WINDOW& theCWindow,
5e27df78 72 Aspect_RenderingContext theGContext,
58655684 73 const Handle(OpenGl_Caps)& theCaps,
5e27df78 74 const Handle(OpenGl_Context)& theShareCtx)
58655684 75: OpenGl_Window (theDisplay, theCWindow, theGContext, theCaps, theShareCtx),
a174a3c5 76 NamedStatus (0),
a174a3c5 77 HighlightColor (&myDefaultHighlightColor),
78 //
2166f0fa
SK
79 myIsTransientOpen (Standard_False),
80 myRetainMode (Standard_False),
a174a3c5 81 myTransientDrawToFront (Standard_True),
2166f0fa
SK
82 myUseTransparency (Standard_False),
83 myUseZBuffer (Standard_False),
84 myUseDepthTest (Standard_True),
85 myUseGLLight (Standard_True),
86 myBackBufferRestored (Standard_False),
87 //
2166f0fa
SK
88 AspectLine_set (&myDefaultAspectLine),
89 AspectLine_applied (NULL),
90 AspectFace_set (&myDefaultAspectFace),
91 AspectFace_applied (NULL),
92 AspectMarker_set (&myDefaultAspectMarker),
93 AspectMarker_applied (NULL),
94 AspectText_set (&myDefaultAspectText),
95 AspectText_applied (NULL),
96 TextParam_set (&myDefaultTextParam),
97 TextParam_applied (NULL),
98 ViewMatrix_applied (&myDefaultMatrix),
99 StructureMatrix_applied (&myDefaultMatrix),
100 PolygonOffset_applied (NULL)
101{
102 theDisplay->InitAttributes();
103
104 // General initialization of the context
105
106 // Eviter d'avoir les faces mal orientees en noir.
107 // Pourrait etre utiliser pour detecter les problemes d'orientation
bf75be98 108 glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
2166f0fa
SK
109
110 // Optimisation pour le Fog et l'antialiasing
111 glHint (GL_FOG_HINT, GL_FASTEST);
112 glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST);
113 glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST);
114 glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
115
116 // Polygon Offset
117 EnablePolygonOffset();
118}
119
1981cb22 120// =======================================================================
121// function : SetImmediateModeDrawToFront
122// purpose :
123// =======================================================================
124Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer)
125{
126 const Standard_Boolean aPrevMode = myTransientDrawToFront;
127 myTransientDrawToFront = theDrawToFrontBuffer;
128 return aPrevMode;
129}
130
2166f0fa
SK
131// =======================================================================
132// function : ~OpenGl_Workspace
133// purpose :
134// =======================================================================
135OpenGl_Workspace::~OpenGl_Workspace()
136{
137}
138
139// =======================================================================
140// function : Activate
141// purpose :
142// =======================================================================
143Standard_Boolean OpenGl_Workspace::Activate()
144{
145 if (!OpenGl_Window::Activate())
146 return Standard_False;
147
2166f0fa
SK
148 ViewMatrix_applied = &myDefaultMatrix;
149 StructureMatrix_applied = &myDefaultMatrix;
26395493 150
151 ResetAppliedAspect();
152
153 return Standard_True;
2166f0fa
SK
154}
155
156// =======================================================================
157// function : UseTransparency
158// purpose : call_togl_transparency
159// =======================================================================
160void OpenGl_Workspace::UseTransparency (const Standard_Boolean theFlag)
161{
de75ed09 162 myUseTransparency = theFlag;
2166f0fa 163}
26395493 164
165//=======================================================================
166//function : ResetAppliedAspect
167//purpose : Sets default values of GL parameters in accordance with default aspects
168//=======================================================================
169void OpenGl_Workspace::ResetAppliedAspect()
170{
bf75be98 171 NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0;
26395493 172 HighlightColor = &myDefaultHighlightColor;
173 AspectLine_set = &myDefaultAspectLine;
174 AspectLine_applied = NULL;
175 AspectFace_set = &myDefaultAspectFace;
176 AspectFace_applied = NULL;
177 AspectMarker_set = &myDefaultAspectMarker;
178 AspectMarker_applied = NULL;
179 AspectText_set = &myDefaultAspectText;
180 AspectText_applied = NULL;
181 TextParam_set = &myDefaultTextParam;
182 TextParam_applied = NULL;
183 PolygonOffset_applied = NULL;
184
185 AspectLine(Standard_True);
186 AspectFace(Standard_True);
187 AspectMarker(Standard_True);
188 AspectText(Standard_True);
189}
bf75be98 190
191// =======================================================================
192// function : DisableTexture
193// purpose :
194// =======================================================================
195Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture()
196{
197 if (myTextureBound.IsNull())
198 {
199 return myTextureBound;
200 }
201
202 // reset texture matrix because some code may expect it is identity
203 GLint aMatrixMode = GL_TEXTURE;
204 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
205 glMatrixMode (GL_TEXTURE);
206 glLoadIdentity();
207 glMatrixMode (aMatrixMode);
208
209 myTextureBound->Unbind (myGlContext);
210 switch (myTextureBound->GetTarget())
211 {
212 case GL_TEXTURE_1D:
213 {
214 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
215 {
216 glDisable (GL_TEXTURE_GEN_S);
217 }
218 glDisable (GL_TEXTURE_1D);
219 break;
220 }
221 case GL_TEXTURE_2D:
222 {
223 if (myTextureBound->GetParams()->GenMode() != GL_NONE)
224 {
225 glDisable (GL_TEXTURE_GEN_S);
226 glDisable (GL_TEXTURE_GEN_T);
227 }
228 glDisable (GL_TEXTURE_2D);
229 break;
230 }
231 default: break;
232 }
233
234 Handle(OpenGl_Texture) aPrevTexture = myTextureBound;
235 myTextureBound.Nullify();
236 return aPrevTexture;
237}
238
239// =======================================================================
240// function : setTextureParams
241// purpose :
242// =======================================================================
243void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture,
244 const Handle(Graphic3d_TextureParams)& theParams)
245{
246 const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams;
247 if (aParams.IsNull())
248 {
249 return;
250 }
251
252 GLint aMatrixMode = GL_TEXTURE;
253 glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode);
254
255 // setup texture matrix
256 glMatrixMode (GL_TEXTURE);
257 glLoadIdentity();
258 const Graphic3d_Vec2& aScale = aParams->Scale();
259 const Graphic3d_Vec2& aTrans = aParams->Translation();
260 glScalef ( aScale.x(), aScale.y(), 1.0f);
261 glTranslatef (-aTrans.x(), -aTrans.y(), 0.0f);
262 glRotatef (-aParams->Rotation(), 0.0f, 0.0f, 1.0f);
263
264 // setup generation of texture coordinates
265 switch (aParams->GenMode())
266 {
267 case Graphic3d_TOTM_OBJECT:
268 {
269 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
270 glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData());
271 if (theTexture->GetTarget() != GL_TEXTURE_1D)
272 {
273 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
274 glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData());
275 }
276 break;
277 }
278 case Graphic3d_TOTM_SPHERE:
279 {
280 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
281 if (theTexture->GetTarget() != GL_TEXTURE_1D)
282 {
283 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
284 }
285 break;
286 }
287 case Graphic3d_TOTM_EYE:
288 {
289 glMatrixMode (GL_MODELVIEW);
290 glPushMatrix();
291 glLoadIdentity();
292
293 glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
294 glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData());
295
296 if (theTexture->GetTarget() != GL_TEXTURE_1D)
297 {
298 glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
299 glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData());
300 }
301 glPopMatrix();
302 break;
303 }
304 case Graphic3d_TOTM_MANUAL:
305 default: break;
306 }
307
308 // setup lighting
309 glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, aParams->IsModulate() ? GL_MODULATE : GL_DECAL);
310
311 // setup texture filtering and wrapping
312 //if (theTexture->GetParams() != theParams)
313 const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR;
314 const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : GL_CLAMP;
315 switch (theTexture->GetTarget())
316 {
317 case GL_TEXTURE_1D:
318 {
319 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter);
320 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter);
321 glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode);
322 break;
323 }
324 case GL_TEXTURE_2D:
325 {
326 GLenum aFilterMin = aFilter;
327 if (theTexture->HasMipmaps())
328 {
329 aFilterMin = GL_NEAREST_MIPMAP_NEAREST;
330 if (aParams->Filter() == Graphic3d_TOTF_BILINEAR)
331 {
332 aFilterMin = GL_LINEAR_MIPMAP_NEAREST;
333 }
334 else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR)
335 {
336 aFilterMin = GL_LINEAR_MIPMAP_LINEAR;
337 }
338
339 if (myGlContext->extAnis)
340 {
341 // setup degree of anisotropy filter
342 const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy();
343 switch (aParams->AnisoFilter())
344 {
345 case Graphic3d_LOTA_QUALITY:
346 {
347 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aMaxDegree);
348 break;
349 }
350 case Graphic3d_LOTA_MIDDLE:
351 {
352
353 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2));
354 break;
355 }
356 case Graphic3d_LOTA_FAST:
357 {
358 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2);
359 break;
360 }
361 case Graphic3d_LOTA_OFF:
362 default:
363 {
364 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1);
365 break;
366 }
367 }
368 }
369 }
370 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin);
371 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter);
372 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode);
373 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode);
374 break;
375 }
376 default: break;
377 }
378
379 switch (theTexture->GetTarget())
380 {
381 case GL_TEXTURE_1D:
382 {
383 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
384 {
385 glEnable (GL_TEXTURE_GEN_S);
386 }
387 glEnable (GL_TEXTURE_1D);
388 break;
389 }
390 case GL_TEXTURE_2D:
391 {
392 if (aParams->GenMode() != Graphic3d_TOTM_MANUAL)
393 {
394 glEnable (GL_TEXTURE_GEN_S);
395 glEnable (GL_TEXTURE_GEN_T);
396 }
397 glEnable (GL_TEXTURE_2D);
398 break;
399 }
400 default: break;
401 }
402
403 glMatrixMode (aMatrixMode); // turn back active matrix
404 theTexture->SetParams (aParams);
405}
406
407// =======================================================================
408// function : EnableTexture
409// purpose :
410// =======================================================================
411Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture,
412 const Handle(Graphic3d_TextureParams)& theParams)
413{
414 if (theTexture.IsNull() || !theTexture->IsValid())
415 {
416 return DisableTexture();
417 }
418
bca1d6e2 419 if (myTextureBound == theTexture
420 && (theParams.IsNull() || theParams == theTexture->GetParams()))
bf75be98 421 {
bca1d6e2 422 // already bound
423 return myTextureBound;
bf75be98 424 }
425
426 Handle(OpenGl_Texture) aPrevTexture = DisableTexture();
427 myTextureBound = theTexture;
428 myTextureBound->Bind (myGlContext);
429 setTextureParams (myTextureBound, theParams);
430
431 return aPrevTexture;
432}
a174a3c5 433
434// =======================================================================
435// function : Redraw
436// purpose :
437// =======================================================================
438void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView,
439 const Aspect_CLayer2d& theCUnderLayer,
440 const Aspect_CLayer2d& theCOverLayer)
441{
442 if (!Activate())
443 {
444 return;
445 }
446
447 // release pending GL resources
448 Handle(OpenGl_Context) aGlCtx = GetGlContext();
449 aGlCtx->ReleaseDelayed();
450
451 // cache render mode state
452 GLint aRendMode = GL_RENDER;
453 glGetIntegerv (GL_RENDER_MODE, &aRendMode);
454 aGlCtx->SetFeedback (aRendMode == GL_FEEDBACK);
455
456 Tint toSwap = (aRendMode == GL_RENDER); // swap buffers
457 GLint aViewPortBack[4];
458 OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO;
459 if (aFrameBuffer != NULL)
460 {
461 glGetIntegerv (GL_VIEWPORT, aViewPortBack);
462 aFrameBuffer->SetupViewport();
463 aFrameBuffer->BindBuffer (aGlCtx);
464 toSwap = 0; // no need to swap buffers
465 }
466
467 Redraw1 (theCView, theCUnderLayer, theCOverLayer, toSwap);
468 if (aFrameBuffer == NULL || !myTransientDrawToFront)
469 {
470 RedrawImmediatMode();
471 }
472
473 if (aFrameBuffer != NULL)
474 {
475 aFrameBuffer->UnbindBuffer (aGlCtx);
476 // move back original viewport
477 glViewport (aViewPortBack[0], aViewPortBack[1], aViewPortBack[2], aViewPortBack[3]);
478 }
479
480#if (defined(_WIN32) || defined(__WIN32__)) && defined(HAVE_VIDEOCAPTURE)
481 if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow))
482 {
483 GLint params[4];
484 glGetIntegerv (GL_VIEWPORT, params);
485 int nWidth = params[2] & ~0x7;
486 int nHeight = params[3] & ~0x7;
487
488 const int nBitsPerPixel = 24;
489 GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8];
490
491 glPixelStorei (GL_PACK_ALIGNMENT, 1);
492 glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData);
493 OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel);
494 delete[] aDumpData;
495 }
496#endif
497
498 // reset render mode state
499 aGlCtx->SetFeedback (Standard_False);
500}