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