Commit | Line | Data |
---|---|---|
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 | // |
d5f74e42 | 7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published | |
973c2be1 | 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 | |
a577aaab | 16 | #include <OpenGl_GlCore15.hxx> |
b86bb3df | 17 | #include <OpenGl_ArbFBO.hxx> |
5f8b738e | 18 | |
2166f0fa SK |
19 | #include <InterfaceGraphic.hxx> |
20 | ||
2166f0fa SK |
21 | #include <OpenGl_AspectLine.hxx> |
22 | #include <OpenGl_AspectFace.hxx> | |
23 | #include <OpenGl_AspectMarker.hxx> | |
24 | #include <OpenGl_AspectText.hxx> | |
bf75be98 | 25 | #include <OpenGl_Context.hxx> |
679ecdee | 26 | #include <OpenGl_Element.hxx> |
a174a3c5 | 27 | #include <OpenGl_FrameBuffer.hxx> |
679ecdee | 28 | #include <OpenGl_Structure.hxx> |
25ef750e | 29 | #include <OpenGl_Sampler.hxx> |
b86bb3df | 30 | #include <OpenGl_ShaderManager.hxx> |
bf75be98 | 31 | #include <OpenGl_Texture.hxx> |
c827ea3a | 32 | #include <OpenGl_Utils.hxx> |
e276548b | 33 | #include <OpenGl_View.hxx> |
a174a3c5 | 34 | #include <OpenGl_Workspace.hxx> |
2166f0fa | 35 | |
bf75be98 | 36 | #include <Graphic3d_TextureParams.hxx> |
2166f0fa | 37 | |
58655684 | 38 | #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE) |
a174a3c5 | 39 | #include <OpenGl_AVIWriter.hxx> |
40 | #endif | |
41 | ||
2166f0fa SK |
42 | |
43 | namespace | |
44 | { | |
0adbd30f | 45 | static const TEL_COLOUR THE_WHITE_COLOR = { { 1.0f, 1.0f, 1.0f, 1.0f } }; |
46 | static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f); | |
2166f0fa SK |
47 | |
48 | static const OpenGl_AspectLine myDefaultAspectLine; | |
49 | static const OpenGl_AspectFace myDefaultAspectFace; | |
50 | static const OpenGl_AspectMarker myDefaultAspectMarker; | |
51 | static const OpenGl_AspectText myDefaultAspectText; | |
52 | ||
53 | static const OpenGl_TextParam myDefaultTextParam = | |
54 | { | |
55 | 16, Graphic3d_HTA_LEFT, Graphic3d_VTA_BOTTOM | |
56 | }; | |
57 | ||
58 | static const OpenGl_Matrix myDefaultMatrix = | |
59 | { | |
60 | {{ 1.0F, 0.0F, 0.0F, 0.0F }, | |
61 | { 0.0F, 1.0F, 0.0F, 0.0F }, | |
62 | { 0.0F, 0.0F, 1.0F, 0.0F }, | |
63 | { 0.0F, 0.0F, 0.0F, 1.0F }} | |
64 | }; | |
bf75be98 | 65 | |
2166f0fa SK |
66 | }; |
67 | ||
0adbd30f | 68 | // ======================================================================= |
69 | // function : Init | |
70 | // purpose : | |
71 | // ======================================================================= | |
72 | void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp) | |
73 | { | |
74 | // ambient component | |
75 | if (theProp.color_mask & OPENGL_AMBIENT_MASK) | |
76 | { | |
77 | const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb; | |
78 | Ambient = OpenGl_Vec4 (aSrcAmb[0] * theProp.amb, | |
79 | aSrcAmb[1] * theProp.amb, | |
80 | aSrcAmb[2] * theProp.amb, | |
81 | 1.0f); | |
82 | } | |
83 | else | |
84 | { | |
85 | Ambient = THE_BLACK_COLOR; | |
86 | } | |
87 | ||
88 | // diffusion component | |
89 | if (theProp.color_mask & OPENGL_DIFFUSE_MASK) | |
90 | { | |
91 | const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb; | |
92 | Diffuse = OpenGl_Vec4 (aSrcDif[0] * theProp.diff, | |
93 | aSrcDif[1] * theProp.diff, | |
94 | aSrcDif[2] * theProp.diff, | |
95 | 1.0f); | |
96 | } | |
97 | else | |
98 | { | |
99 | Diffuse = THE_BLACK_COLOR; | |
100 | } | |
101 | ||
102 | // specular component | |
103 | if (theProp.color_mask & OPENGL_SPECULAR_MASK) | |
104 | { | |
105 | const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : THE_WHITE_COLOR.rgb; | |
106 | Specular = OpenGl_Vec4 (aSrcSpe[0] * theProp.spec, | |
107 | aSrcSpe[1] * theProp.spec, | |
108 | aSrcSpe[2] * theProp.spec, | |
109 | 1.0f); | |
110 | } | |
111 | else | |
112 | { | |
113 | Specular = THE_BLACK_COLOR; | |
114 | } | |
115 | ||
116 | // emission component | |
117 | if (theProp.color_mask & OPENGL_EMISSIVE_MASK) | |
118 | { | |
119 | const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb; | |
120 | Emission = OpenGl_Vec4 (aSrcEms[0] * theProp.emsv, | |
121 | aSrcEms[1] * theProp.emsv, | |
122 | aSrcEms[2] * theProp.emsv, | |
123 | 1.0f); | |
124 | } | |
125 | else | |
126 | { | |
127 | Emission = THE_BLACK_COLOR; | |
128 | } | |
129 | ||
130 | ChangeShine() = theProp.shine; | |
131 | ChangeTransparency() = theProp.trans; | |
132 | } | |
133 | ||
2166f0fa SK |
134 | // ======================================================================= |
135 | // function : OpenGl_Workspace | |
136 | // purpose : | |
137 | // ======================================================================= | |
25b97fac | 138 | OpenGl_Workspace::OpenGl_Workspace (const Handle(OpenGl_GraphicDriver)& theDriver, |
2166f0fa | 139 | const CALL_DEF_WINDOW& theCWindow, |
5e27df78 | 140 | Aspect_RenderingContext theGContext, |
58655684 | 141 | const Handle(OpenGl_Caps)& theCaps, |
5e27df78 | 142 | const Handle(OpenGl_Context)& theShareCtx) |
25b97fac | 143 | : OpenGl_Window (theDriver, theCWindow, theGContext, theCaps, theShareCtx), |
a174a3c5 | 144 | NamedStatus (0), |
0adbd30f | 145 | HighlightColor (&THE_WHITE_COLOR), |
a174a3c5 | 146 | // |
ee51a9fe | 147 | myHasFboBlit (Standard_True), |
fc73a202 | 148 | // |
a272ed94 | 149 | myViewId (-1), |
73192b37 | 150 | myAntiAliasingMode (3), |
a174a3c5 | 151 | myTransientDrawToFront (Standard_True), |
679ecdee | 152 | myBackBufferRestored (Standard_False), |
153 | myIsImmediateDrawn (Standard_False), | |
eae454e3 | 154 | myUseZBuffer (Standard_True), |
155 | myUseDepthWrite (Standard_True), | |
2166f0fa | 156 | myUseGLLight (Standard_True), |
b7cd4ba7 | 157 | myIsCullingEnabled (Standard_False), |
a89742cf | 158 | myFrameCounter (0), |
2166f0fa | 159 | // |
2166f0fa SK |
160 | AspectLine_set (&myDefaultAspectLine), |
161 | AspectLine_applied (NULL), | |
162 | AspectFace_set (&myDefaultAspectFace), | |
163 | AspectFace_applied (NULL), | |
164 | AspectMarker_set (&myDefaultAspectMarker), | |
165 | AspectMarker_applied (NULL), | |
166 | AspectText_set (&myDefaultAspectText), | |
167 | AspectText_applied (NULL), | |
168 | TextParam_set (&myDefaultTextParam), | |
169 | TextParam_applied (NULL), | |
170 | ViewMatrix_applied (&myDefaultMatrix), | |
171 | StructureMatrix_applied (&myDefaultMatrix), | |
3b1817a9 | 172 | myCullingMode (TelCullUndefined), |
0f8c0fb8 | 173 | myModelViewMatrix (myDefaultMatrix), |
550f3b8b | 174 | PolygonOffset_applied (THE_DEFAULT_POFFSET) |
2166f0fa | 175 | { |
73192b37 | 176 | myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1); |
38a0206f | 177 | myMainSceneFbos[0] = new OpenGl_FrameBuffer(); |
178 | myMainSceneFbos[1] = new OpenGl_FrameBuffer(); | |
179 | myImmediateSceneFbos[0] = new OpenGl_FrameBuffer(); | |
180 | myImmediateSceneFbos[1] = new OpenGl_FrameBuffer(); | |
73192b37 | 181 | |
182 | if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs)) | |
183 | { | |
184 | // share and register for release once the resource is no longer used | |
185 | myLineAttribs = new OpenGl_LineAttributes(); | |
186 | myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs); | |
187 | myLineAttribs->Init (myGlContext); | |
188 | } | |
2166f0fa SK |
189 | |
190 | // General initialization of the context | |
191 | ||
ca3c13d1 | 192 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 193 | if (myGlContext->core11 != NULL) |
194 | { | |
195 | // Eviter d'avoir les faces mal orientees en noir. | |
196 | // Pourrait etre utiliser pour detecter les problemes d'orientation | |
197 | glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); | |
198 | ||
199 | // Optimisation pour le Fog et l'antialiasing | |
200 | glHint (GL_FOG_HINT, GL_FASTEST); | |
201 | glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST); | |
202 | } | |
2166f0fa | 203 | |
2166f0fa SK |
204 | glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST); |
205 | glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST); | |
ca3c13d1 | 206 | #endif |
2166f0fa | 207 | |
73192b37 | 208 | // AA mode |
209 | const char* anAaEnv = ::getenv ("CALL_OPENGL_ANTIALIASING_MODE"); | |
210 | if (anAaEnv != NULL) | |
211 | { | |
212 | int v; | |
213 | if (sscanf (anAaEnv, "%d", &v) > 0) myAntiAliasingMode = v; | |
214 | } | |
f8ae3605 | 215 | |
216 | myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter(); | |
217 | myNoneCulling.ChangeCullingMode() = TelCullNone; | |
218 | myNoneCulling.ChangeEdge() = 0; | |
219 | myFrontCulling.ChangeCullingMode() = TelCullBack; | |
220 | myFrontCulling.ChangeEdge() = 0; | |
2166f0fa SK |
221 | } |
222 | ||
1981cb22 | 223 | // ======================================================================= |
224 | // function : SetImmediateModeDrawToFront | |
225 | // purpose : | |
226 | // ======================================================================= | |
227 | Standard_Boolean OpenGl_Workspace::SetImmediateModeDrawToFront (const Standard_Boolean theDrawToFrontBuffer) | |
228 | { | |
229 | const Standard_Boolean aPrevMode = myTransientDrawToFront; | |
230 | myTransientDrawToFront = theDrawToFrontBuffer; | |
231 | return aPrevMode; | |
232 | } | |
233 | ||
38a0206f | 234 | inline void nullifyGlResource (Handle(OpenGl_Resource)& theResource, |
235 | const Handle(OpenGl_Context)& theCtx) | |
236 | { | |
237 | if (!theResource.IsNull()) | |
238 | { | |
239 | theResource->Release (theCtx.operator->()); | |
240 | theResource.Nullify(); | |
241 | } | |
242 | } | |
243 | ||
2166f0fa SK |
244 | // ======================================================================= |
245 | // function : ~OpenGl_Workspace | |
246 | // purpose : | |
247 | // ======================================================================= | |
248 | OpenGl_Workspace::~OpenGl_Workspace() | |
249 | { | |
73192b37 | 250 | if (!myLineAttribs.IsNull()) |
251 | { | |
252 | myLineAttribs.Nullify(); | |
253 | myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True); | |
254 | } | |
255 | ||
38a0206f | 256 | nullifyGlResource (myMainSceneFbos[0], myGlContext); |
257 | nullifyGlResource (myMainSceneFbos[1], myGlContext); | |
258 | nullifyGlResource (myImmediateSceneFbos[0], myGlContext); | |
259 | nullifyGlResource (myImmediateSceneFbos[1], myGlContext); | |
260 | ||
261 | myFullScreenQuad.Release (myGlContext.operator->()); | |
2166f0fa SK |
262 | } |
263 | ||
264 | // ======================================================================= | |
265 | // function : Activate | |
266 | // purpose : | |
267 | // ======================================================================= | |
268 | Standard_Boolean OpenGl_Workspace::Activate() | |
269 | { | |
270 | if (!OpenGl_Window::Activate()) | |
271 | return Standard_False; | |
272 | ||
2166f0fa SK |
273 | ViewMatrix_applied = &myDefaultMatrix; |
274 | StructureMatrix_applied = &myDefaultMatrix; | |
26395493 | 275 | |
276 | ResetAppliedAspect(); | |
277 | ||
278 | return Standard_True; | |
2166f0fa SK |
279 | } |
280 | ||
26395493 | 281 | //======================================================================= |
282 | //function : ResetAppliedAspect | |
283 | //purpose : Sets default values of GL parameters in accordance with default aspects | |
284 | //======================================================================= | |
285 | void OpenGl_Workspace::ResetAppliedAspect() | |
286 | { | |
4e1523ef | 287 | myGlContext->BindDefaultVao(); |
288 | ||
bf75be98 | 289 | NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0; |
0adbd30f | 290 | HighlightColor = &THE_WHITE_COLOR; |
26395493 | 291 | AspectLine_set = &myDefaultAspectLine; |
292 | AspectLine_applied = NULL; | |
293 | AspectFace_set = &myDefaultAspectFace; | |
294 | AspectFace_applied = NULL; | |
295 | AspectMarker_set = &myDefaultAspectMarker; | |
296 | AspectMarker_applied = NULL; | |
297 | AspectText_set = &myDefaultAspectText; | |
298 | AspectText_applied = NULL; | |
299 | TextParam_set = &myDefaultTextParam; | |
300 | TextParam_applied = NULL; | |
550f3b8b | 301 | PolygonOffset_applied = THE_DEFAULT_POFFSET; |
3b1817a9 | 302 | myCullingMode = TelCullUndefined; |
26395493 | 303 | |
304 | AspectLine(Standard_True); | |
305 | AspectFace(Standard_True); | |
306 | AspectMarker(Standard_True); | |
307 | AspectText(Standard_True); | |
ac116c22 | 308 | |
309 | myGlContext->SetTypeOfLine (myDefaultAspectLine.Type()); | |
310 | myGlContext->SetLineWidth (myDefaultAspectLine.Width()); | |
26395493 | 311 | } |
bf75be98 | 312 | |
313 | // ======================================================================= | |
314 | // function : DisableTexture | |
315 | // purpose : | |
316 | // ======================================================================= | |
317 | Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture() | |
318 | { | |
319 | if (myTextureBound.IsNull()) | |
320 | { | |
321 | return myTextureBound; | |
322 | } | |
323 | ||
e3414ada | 324 | const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler(); |
325 | if (!aSampler.IsNull()) | |
326 | { | |
327 | aSampler->Unbind (*myGlContext); | |
328 | } | |
329 | ||
ca3c13d1 | 330 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 331 | // reset texture matrix because some code may expect it is identity |
4e1523ef | 332 | if (myGlContext->core11 != NULL) |
333 | { | |
334 | GLint aMatrixMode = GL_TEXTURE; | |
335 | glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode); | |
336 | glMatrixMode (GL_TEXTURE); | |
337 | glLoadIdentity(); | |
338 | glMatrixMode (aMatrixMode); | |
339 | } | |
ca3c13d1 | 340 | #endif |
bf75be98 | 341 | |
342 | myTextureBound->Unbind (myGlContext); | |
343 | switch (myTextureBound->GetTarget()) | |
344 | { | |
ca3c13d1 | 345 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 346 | case GL_TEXTURE_1D: |
347 | { | |
4e1523ef | 348 | if (myGlContext->core11 != NULL) |
bf75be98 | 349 | { |
4e1523ef | 350 | if (myTextureBound->GetParams()->GenMode() != GL_NONE) |
351 | { | |
352 | glDisable (GL_TEXTURE_GEN_S); | |
353 | } | |
354 | glDisable (GL_TEXTURE_1D); | |
bf75be98 | 355 | } |
bf75be98 | 356 | break; |
357 | } | |
ca3c13d1 | 358 | #endif |
bf75be98 | 359 | case GL_TEXTURE_2D: |
360 | { | |
ca3c13d1 | 361 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 362 | if (myGlContext->core11 != NULL) |
bf75be98 | 363 | { |
4e1523ef | 364 | if (myTextureBound->GetParams()->GenMode() != GL_NONE) |
a577aaab | 365 | { |
4e1523ef | 366 | glDisable (GL_TEXTURE_GEN_S); |
367 | glDisable (GL_TEXTURE_GEN_T); | |
368 | if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE) | |
369 | { | |
370 | glDisable (GL_POINT_SPRITE); | |
371 | } | |
a577aaab | 372 | } |
4e1523ef | 373 | glDisable (GL_TEXTURE_2D); |
bf75be98 | 374 | } |
05e2200b | 375 | #endif |
bf75be98 | 376 | break; |
377 | } | |
378 | default: break; | |
379 | } | |
380 | ||
381 | Handle(OpenGl_Texture) aPrevTexture = myTextureBound; | |
382 | myTextureBound.Nullify(); | |
383 | return aPrevTexture; | |
384 | } | |
385 | ||
386 | // ======================================================================= | |
387 | // function : setTextureParams | |
388 | // purpose : | |
389 | // ======================================================================= | |
390 | void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture, | |
391 | const Handle(Graphic3d_TextureParams)& theParams) | |
392 | { | |
393 | const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams; | |
394 | if (aParams.IsNull()) | |
395 | { | |
396 | return; | |
397 | } | |
398 | ||
ca3c13d1 | 399 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 400 | GLint aMatrixMode = GL_TEXTURE; |
4e1523ef | 401 | if (myGlContext->core11 != NULL) |
402 | { | |
403 | glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode); | |
404 | ||
405 | // setup texture matrix | |
406 | glMatrixMode (GL_TEXTURE); | |
407 | OpenGl_Mat4 aTextureMat; | |
408 | const Graphic3d_Vec2& aScale = aParams->Scale(); | |
409 | const Graphic3d_Vec2& aTrans = aParams->Translation(); | |
410 | OpenGl_Utils::Scale (aTextureMat, aScale.x(), aScale.y(), 1.0f); | |
411 | OpenGl_Utils::Translate (aTextureMat, -aTrans.x(), -aTrans.y(), 0.0f); | |
412 | OpenGl_Utils::Rotate (aTextureMat, -aParams->Rotation(), 0.0f, 0.0f, 1.0f); | |
413 | glLoadMatrixf (aTextureMat); | |
414 | ||
415 | GLint anEnvMode = GL_MODULATE; // lighting mode | |
416 | if (!aParams->IsModulate()) | |
076ca35c | 417 | { |
4e1523ef | 418 | anEnvMode = GL_DECAL; |
419 | if (theTexture->GetFormat() == GL_ALPHA | |
420 | || theTexture->GetFormat() == GL_LUMINANCE) | |
421 | { | |
422 | anEnvMode = GL_REPLACE; | |
423 | } | |
076ca35c | 424 | } |
076ca35c | 425 | |
4e1523ef | 426 | // setup generation of texture coordinates |
427 | switch (aParams->GenMode()) | |
bf75be98 | 428 | { |
4e1523ef | 429 | case Graphic3d_TOTM_OBJECT: |
bf75be98 | 430 | { |
4e1523ef | 431 | glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
432 | glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData()); | |
433 | if (theTexture->GetTarget() != GL_TEXTURE_1D) | |
434 | { | |
435 | glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); | |
436 | glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData()); | |
437 | } | |
438 | break; | |
bf75be98 | 439 | } |
4e1523ef | 440 | case Graphic3d_TOTM_SPHERE: |
bf75be98 | 441 | { |
4e1523ef | 442 | glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
443 | if (theTexture->GetTarget() != GL_TEXTURE_1D) | |
444 | { | |
445 | glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); | |
446 | } | |
447 | break; | |
bf75be98 | 448 | } |
4e1523ef | 449 | case Graphic3d_TOTM_EYE: |
450 | { | |
451 | myGlContext->WorldViewState.Push(); | |
c827ea3a | 452 | |
4e1523ef | 453 | myGlContext->WorldViewState.SetIdentity(); |
454 | myGlContext->ApplyWorldViewMatrix(); | |
bf75be98 | 455 | |
4e1523ef | 456 | glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
457 | glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData()); | |
bf75be98 | 458 | |
4e1523ef | 459 | if (theTexture->GetTarget() != GL_TEXTURE_1D) |
460 | { | |
461 | glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); | |
462 | glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData()); | |
463 | } | |
c827ea3a | 464 | |
4e1523ef | 465 | myGlContext->WorldViewState.Pop(); |
c827ea3a | 466 | |
4e1523ef | 467 | break; |
468 | } | |
469 | case Graphic3d_TOTM_SPRITE: | |
a577aaab | 470 | { |
4e1523ef | 471 | if (GetGlContext()->core20fwd != NULL) |
472 | { | |
473 | glEnable (GL_POINT_SPRITE); | |
474 | glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); | |
475 | anEnvMode = GL_REPLACE; | |
476 | GetGlContext()->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); | |
477 | } | |
478 | break; | |
a577aaab | 479 | } |
4e1523ef | 480 | case Graphic3d_TOTM_MANUAL: |
481 | default: break; | |
a577aaab | 482 | } |
bf75be98 | 483 | |
4e1523ef | 484 | // setup lighting |
485 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode); | |
486 | } | |
ca3c13d1 | 487 | #endif |
bf75be98 | 488 | |
25ef750e | 489 | // get active sampler object to override default texture parameters |
490 | const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler(); | |
491 | ||
bf75be98 | 492 | // setup texture filtering and wrapping |
493 | //if (theTexture->GetParams() != theParams) | |
494 | const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR; | |
ca3c13d1 | 495 | const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp(); |
bf75be98 | 496 | switch (theTexture->GetTarget()) |
497 | { | |
ca3c13d1 | 498 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 499 | case GL_TEXTURE_1D: |
500 | { | |
25ef750e | 501 | if (aSampler.IsNull() || !aSampler->IsValid()) |
502 | { | |
503 | glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter); | |
504 | glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter); | |
505 | glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode); | |
506 | } | |
507 | else | |
508 | { | |
509 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter); | |
510 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter); | |
511 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode); | |
512 | } | |
513 | ||
bf75be98 | 514 | break; |
515 | } | |
ca3c13d1 | 516 | #endif |
bf75be98 | 517 | case GL_TEXTURE_2D: |
518 | { | |
519 | GLenum aFilterMin = aFilter; | |
520 | if (theTexture->HasMipmaps()) | |
521 | { | |
522 | aFilterMin = GL_NEAREST_MIPMAP_NEAREST; | |
523 | if (aParams->Filter() == Graphic3d_TOTF_BILINEAR) | |
524 | { | |
525 | aFilterMin = GL_LINEAR_MIPMAP_NEAREST; | |
526 | } | |
527 | else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR) | |
528 | { | |
529 | aFilterMin = GL_LINEAR_MIPMAP_LINEAR; | |
530 | } | |
531 | ||
532 | if (myGlContext->extAnis) | |
533 | { | |
534 | // setup degree of anisotropy filter | |
535 | const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy(); | |
25ef750e | 536 | GLint aDegree; |
bf75be98 | 537 | switch (aParams->AnisoFilter()) |
538 | { | |
539 | case Graphic3d_LOTA_QUALITY: | |
540 | { | |
25ef750e | 541 | aDegree = aMaxDegree; |
bf75be98 | 542 | break; |
543 | } | |
544 | case Graphic3d_LOTA_MIDDLE: | |
545 | { | |
25ef750e | 546 | aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2); |
bf75be98 | 547 | break; |
548 | } | |
549 | case Graphic3d_LOTA_FAST: | |
550 | { | |
25ef750e | 551 | aDegree = 2; |
bf75be98 | 552 | break; |
553 | } | |
554 | case Graphic3d_LOTA_OFF: | |
555 | default: | |
556 | { | |
25ef750e | 557 | aDegree = 1; |
bf75be98 | 558 | break; |
559 | } | |
560 | } | |
25ef750e | 561 | |
562 | if (aSampler.IsNull() || !aSampler->IsValid()) | |
563 | { | |
564 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree); | |
565 | } | |
566 | else | |
567 | { | |
568 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree); | |
569 | } | |
bf75be98 | 570 | } |
571 | } | |
25ef750e | 572 | |
573 | if (aSampler.IsNull() || !aSampler->IsValid()) | |
574 | { | |
575 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin); | |
576 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter); | |
577 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode); | |
578 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode); | |
579 | } | |
580 | else | |
581 | { | |
582 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin); | |
583 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter); | |
584 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode); | |
585 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode); | |
586 | } | |
587 | ||
bf75be98 | 588 | break; |
589 | } | |
590 | default: break; | |
591 | } | |
592 | ||
593 | switch (theTexture->GetTarget()) | |
594 | { | |
ca3c13d1 | 595 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 596 | case GL_TEXTURE_1D: |
597 | { | |
4e1523ef | 598 | if (myGlContext->core11 != NULL) |
bf75be98 | 599 | { |
4e1523ef | 600 | if (aParams->GenMode() != Graphic3d_TOTM_MANUAL) |
601 | { | |
602 | glEnable (GL_TEXTURE_GEN_S); | |
603 | } | |
604 | glEnable (GL_TEXTURE_1D); | |
bf75be98 | 605 | } |
bf75be98 | 606 | break; |
607 | } | |
ca3c13d1 | 608 | #endif |
bf75be98 | 609 | case GL_TEXTURE_2D: |
610 | { | |
ca3c13d1 | 611 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 612 | if (myGlContext->core11 != NULL) |
bf75be98 | 613 | { |
4e1523ef | 614 | if (aParams->GenMode() != Graphic3d_TOTM_MANUAL) |
615 | { | |
616 | glEnable (GL_TEXTURE_GEN_S); | |
617 | glEnable (GL_TEXTURE_GEN_T); | |
618 | } | |
619 | glEnable (GL_TEXTURE_2D); | |
bf75be98 | 620 | } |
05e2200b | 621 | #endif |
bf75be98 | 622 | break; |
623 | } | |
624 | default: break; | |
625 | } | |
626 | ||
ca3c13d1 | 627 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 628 | if (myGlContext->core11 != NULL) |
629 | { | |
630 | glMatrixMode (aMatrixMode); // turn back active matrix | |
631 | } | |
ca3c13d1 | 632 | #endif |
bf75be98 | 633 | theTexture->SetParams (aParams); |
634 | } | |
635 | ||
636 | // ======================================================================= | |
637 | // function : EnableTexture | |
638 | // purpose : | |
639 | // ======================================================================= | |
640 | Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture, | |
641 | const Handle(Graphic3d_TextureParams)& theParams) | |
642 | { | |
643 | if (theTexture.IsNull() || !theTexture->IsValid()) | |
644 | { | |
645 | return DisableTexture(); | |
646 | } | |
647 | ||
bca1d6e2 | 648 | if (myTextureBound == theTexture |
649 | && (theParams.IsNull() || theParams == theTexture->GetParams())) | |
bf75be98 | 650 | { |
bca1d6e2 | 651 | // already bound |
652 | return myTextureBound; | |
bf75be98 | 653 | } |
654 | ||
655 | Handle(OpenGl_Texture) aPrevTexture = DisableTexture(); | |
656 | myTextureBound = theTexture; | |
657 | myTextureBound->Bind (myGlContext); | |
658 | setTextureParams (myTextureBound, theParams); | |
659 | ||
25ef750e | 660 | // If custom sampler object is available it will be |
661 | // used for overriding default texture parameters | |
662 | const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler(); | |
663 | ||
664 | if (!aSampler.IsNull() && aSampler->IsValid()) | |
665 | { | |
666 | aSampler->Bind (*myGlContext); | |
667 | } | |
668 | ||
bf75be98 | 669 | return aPrevTexture; |
670 | } | |
a174a3c5 | 671 | |
38a0206f | 672 | // ======================================================================= |
673 | // function : bindDefaultFbo | |
674 | // purpose : | |
675 | // ======================================================================= | |
676 | void OpenGl_Workspace::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo) | |
677 | { | |
678 | OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid()) | |
679 | ? theCustomFbo | |
680 | : (!myGlContext->DefaultFrameBuffer().IsNull() | |
681 | && myGlContext->DefaultFrameBuffer()->IsValid() | |
682 | ? myGlContext->DefaultFrameBuffer().operator->() | |
683 | : NULL); | |
684 | if (anFbo != NULL) | |
685 | { | |
686 | anFbo->BindBuffer (myGlContext); | |
687 | } | |
688 | else | |
689 | { | |
690 | #if !defined(GL_ES_VERSION_2_0) | |
691 | myGlContext->SetReadDrawBuffer (GL_BACK); | |
692 | #else | |
693 | if (myGlContext->arbFBO != NULL) | |
694 | { | |
695 | myGlContext->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); | |
696 | } | |
697 | #endif | |
698 | } | |
699 | myGlContext->core11fwd->glViewport (0, 0, myWidth, myHeight); | |
700 | } | |
701 | ||
702 | // ======================================================================= | |
703 | // function : blitBuffers | |
704 | // purpose : | |
705 | // ======================================================================= | |
706 | bool OpenGl_Workspace::blitBuffers (OpenGl_FrameBuffer* theReadFbo, | |
707 | OpenGl_FrameBuffer* theDrawFbo) | |
708 | { | |
709 | if (theReadFbo == NULL) | |
710 | { | |
711 | return false; | |
712 | } | |
f978241f | 713 | else if (theReadFbo == theDrawFbo) |
714 | { | |
715 | return true; | |
716 | } | |
38a0206f | 717 | |
718 | // clear destination before blitting | |
719 | if (theDrawFbo != NULL | |
720 | && theDrawFbo->IsValid()) | |
721 | { | |
722 | theDrawFbo->BindBuffer (myGlContext); | |
723 | } | |
724 | else | |
725 | { | |
726 | myGlContext->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); | |
727 | } | |
728 | #if !defined(GL_ES_VERSION_2_0) | |
729 | myGlContext->core20fwd->glClearDepth (1.0); | |
730 | #else | |
731 | myGlContext->core20fwd->glClearDepthf (1.0f); | |
732 | #endif | |
733 | myGlContext->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); | |
734 | ||
735 | /*#if !defined(GL_ES_VERSION_2_0) | |
736 | if (myGlContext->arbFBOBlit != NULL) | |
737 | { | |
738 | theReadFbo->BindReadBuffer (myGlContext); | |
739 | if (theDrawFbo != NULL | |
740 | && theDrawFbo->IsValid()) | |
741 | { | |
742 | theDrawFbo->BindDrawBuffer (myGlContext); | |
743 | } | |
744 | else | |
745 | { | |
746 | myGlContext->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); | |
747 | } | |
748 | // we don't copy stencil buffer here... does it matter for performance? | |
749 | myGlContext->arbFBOBlit->glBlitFramebuffer (0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(), | |
750 | 0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(), | |
751 | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST); | |
752 | ||
753 | if (theDrawFbo != NULL | |
754 | && theDrawFbo->IsValid()) | |
755 | { | |
756 | theDrawFbo->BindBuffer (myGlContext); | |
757 | } | |
758 | else | |
759 | { | |
760 | myGlContext->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); | |
761 | } | |
762 | } | |
763 | else | |
764 | #endif*/ | |
765 | { | |
766 | myGlContext->core20fwd->glDepthFunc (GL_ALWAYS); | |
767 | myGlContext->core20fwd->glDepthMask (GL_TRUE); | |
768 | myGlContext->core20fwd->glEnable (GL_DEPTH_TEST); | |
769 | ||
770 | DisableTexture(); | |
771 | if (!myFullScreenQuad.IsValid()) | |
772 | { | |
773 | OpenGl_Vec4 aQuad[4] = | |
774 | { | |
775 | OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f), | |
776 | OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f), | |
777 | OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f), | |
778 | OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f) | |
779 | }; | |
780 | myFullScreenQuad.Init (myGlContext, 4, 4, aQuad[0].GetData()); | |
781 | } | |
782 | ||
783 | const Handle(OpenGl_ShaderManager)& aManager = myGlContext->ShaderManager(); | |
784 | if (myFullScreenQuad.IsValid() | |
785 | && aManager->BindFboBlitProgram()) | |
786 | { | |
787 | theReadFbo->ColorTexture() ->Bind (myGlContext, GL_TEXTURE0 + 0); | |
788 | theReadFbo->DepthStencilTexture()->Bind (myGlContext, GL_TEXTURE0 + 1); | |
fe3a29bc | 789 | myFullScreenQuad.BindVertexAttrib (myGlContext, Graphic3d_TOA_POS); |
38a0206f | 790 | |
791 | myGlContext->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4); | |
792 | ||
fe3a29bc | 793 | myFullScreenQuad.UnbindVertexAttrib (myGlContext, Graphic3d_TOA_POS); |
38a0206f | 794 | theReadFbo->DepthStencilTexture()->Unbind (myGlContext, GL_TEXTURE0 + 1); |
795 | theReadFbo->ColorTexture() ->Unbind (myGlContext, GL_TEXTURE0 + 0); | |
796 | } | |
797 | else | |
798 | { | |
799 | TCollection_ExtendedString aMsg = TCollection_ExtendedString() | |
800 | + "Error! FBO blitting has failed"; | |
801 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, | |
802 | GL_DEBUG_TYPE_ERROR_ARB, | |
803 | 0, | |
804 | GL_DEBUG_SEVERITY_HIGH_ARB, | |
805 | aMsg); | |
806 | myHasFboBlit = Standard_False; | |
807 | theReadFbo->Release (myGlContext.operator->()); | |
808 | return true; | |
809 | } | |
810 | } | |
811 | return true; | |
812 | } | |
813 | ||
814 | // ======================================================================= | |
815 | // function : drawStereoPair | |
816 | // purpose : | |
817 | // ======================================================================= | |
f978241f | 818 | void OpenGl_Workspace::drawStereoPair (const Graphic3d_CView& theCView) |
38a0206f | 819 | { |
820 | OpenGl_FrameBuffer* aPair[2] = | |
821 | { | |
822 | myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL, | |
823 | myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL | |
824 | }; | |
825 | if (aPair[0] == NULL | |
f978241f | 826 | || aPair[1] == NULL |
827 | || !myTransientDrawToFront) | |
38a0206f | 828 | { |
829 | aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL; | |
830 | aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL; | |
831 | } | |
832 | ||
833 | if (aPair[0] == NULL | |
834 | || aPair[1] == NULL) | |
835 | { | |
836 | return; | |
837 | } | |
838 | ||
f978241f | 839 | Standard_Boolean toReverse = theCView.RenderParams.ToReverseStereo; |
840 | const Standard_Boolean isOddY = (theCView.DefWindow.top + theCView.DefWindow.dy) % 2 == 1; | |
841 | const Standard_Boolean isOddX = theCView.DefWindow.left % 2 == 1; | |
842 | if (isOddY | |
843 | && (theCView.RenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced | |
844 | || theCView.RenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard)) | |
845 | { | |
846 | toReverse = !toReverse; | |
847 | } | |
848 | if (isOddX | |
849 | && (theCView.RenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced | |
850 | || theCView.RenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard)) | |
851 | { | |
852 | toReverse = !toReverse; | |
853 | } | |
854 | ||
855 | if (toReverse) | |
856 | { | |
857 | std::swap (aPair[0], aPair[1]); | |
858 | } | |
859 | ||
38a0206f | 860 | myGlContext->core20fwd->glDepthFunc (GL_ALWAYS); |
861 | myGlContext->core20fwd->glDepthMask (GL_TRUE); | |
862 | myGlContext->core20fwd->glEnable (GL_DEPTH_TEST); | |
863 | ||
864 | DisableTexture(); | |
865 | if (!myFullScreenQuad.IsValid()) | |
866 | { | |
867 | OpenGl_Vec4 aQuad[4] = | |
868 | { | |
869 | OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f), | |
870 | OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f), | |
871 | OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f), | |
872 | OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f) | |
873 | }; | |
874 | myFullScreenQuad.Init (myGlContext, 4, 4, aQuad[0].GetData()); | |
875 | } | |
876 | ||
877 | const Handle(OpenGl_ShaderManager)& aManager = myGlContext->ShaderManager(); | |
878 | if (myFullScreenQuad.IsValid() | |
f978241f | 879 | && aManager->BindStereoProgram (theCView.RenderParams.StereoMode)) |
38a0206f | 880 | { |
f978241f | 881 | if (theCView.RenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph) |
882 | { | |
883 | OpenGl_Mat4 aFilterL, aFilterR; | |
884 | aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f)); | |
885 | aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f)); | |
886 | switch (theCView.RenderParams.AnaglyphFilter) | |
887 | { | |
888 | case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple: | |
889 | { | |
890 | aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f)); | |
891 | aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f)); | |
892 | aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f)); | |
893 | break; | |
894 | } | |
895 | case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized: | |
896 | { | |
897 | aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f)); | |
898 | aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f)); | |
899 | aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f)); | |
900 | aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); | |
901 | aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f)); | |
902 | aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f)); | |
903 | aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f)); | |
904 | aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); | |
905 | break; | |
906 | } | |
907 | case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple: | |
908 | { | |
909 | aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f)); | |
910 | aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f)); | |
911 | aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f)); | |
912 | break; | |
913 | } | |
914 | case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized: | |
915 | { | |
916 | aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f)); | |
917 | aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f)); | |
918 | aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f)); | |
919 | aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); | |
920 | aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f)); | |
921 | aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f)); | |
922 | aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f)); | |
923 | aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); | |
924 | break; | |
925 | } | |
926 | case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple: | |
927 | { | |
928 | aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f)); | |
929 | aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f)); | |
930 | aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f)); | |
931 | break; | |
932 | } | |
933 | case Graphic3d_RenderingParams::Anaglyph_UserDefined: | |
934 | { | |
935 | aFilterL = theCView.RenderParams.AnaglyphLeft; | |
936 | aFilterR = theCView.RenderParams.AnaglyphRight; | |
937 | break; | |
938 | } | |
939 | } | |
940 | myGlContext->ActiveProgram()->SetUniform (myGlContext, "uMultL", aFilterL); | |
941 | myGlContext->ActiveProgram()->SetUniform (myGlContext, "uMultR", aFilterR); | |
942 | } | |
943 | ||
38a0206f | 944 | aPair[0]->ColorTexture()->Bind (myGlContext, GL_TEXTURE0 + 0); |
945 | aPair[1]->ColorTexture()->Bind (myGlContext, GL_TEXTURE0 + 1); | |
946 | myFullScreenQuad.BindVertexAttrib (myGlContext, 0); | |
947 | ||
948 | myGlContext->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4); | |
949 | ||
950 | myFullScreenQuad.UnbindVertexAttrib (myGlContext, 0); | |
951 | aPair[1]->ColorTexture()->Unbind (myGlContext, GL_TEXTURE0 + 1); | |
952 | aPair[0]->ColorTexture()->Unbind (myGlContext, GL_TEXTURE0 + 0); | |
953 | } | |
954 | else | |
955 | { | |
956 | TCollection_ExtendedString aMsg = TCollection_ExtendedString() | |
957 | + "Error! Anaglyph has failed"; | |
958 | myGlContext->PushMessage (GL_DEBUG_SOURCE_APPLICATION_ARB, | |
959 | GL_DEBUG_TYPE_ERROR_ARB, | |
960 | 0, | |
961 | GL_DEBUG_SEVERITY_HIGH_ARB, | |
962 | aMsg); | |
963 | } | |
964 | } | |
965 | ||
a174a3c5 | 966 | // ======================================================================= |
967 | // function : Redraw | |
968 | // purpose : | |
969 | // ======================================================================= | |
970 | void OpenGl_Workspace::Redraw (const Graphic3d_CView& theCView, | |
971 | const Aspect_CLayer2d& theCUnderLayer, | |
972 | const Aspect_CLayer2d& theCOverLayer) | |
973 | { | |
974 | if (!Activate()) | |
975 | { | |
976 | return; | |
977 | } | |
978 | ||
f978241f | 979 | if (mySwapInterval != myGlContext->caps->swapInterval) |
980 | { | |
981 | mySwapInterval = myGlContext->caps->swapInterval; | |
982 | myGlContext->SetSwapInterval (mySwapInterval); | |
983 | } | |
984 | ||
a89742cf | 985 | ++myFrameCounter; |
b7cd4ba7 | 986 | myIsCullingEnabled = theCView.IsCullingEnabled; |
f978241f | 987 | const Graphic3d_StereoMode aStereoMode = theCView.RenderParams.StereoMode; |
988 | const Handle(Graphic3d_Camera)& aCamera = myView->Camera(); | |
989 | Graphic3d_Camera::Projection aProjectType = aCamera->ProjectionType(); | |
b7cd4ba7 | 990 | |
a174a3c5 | 991 | // release pending GL resources |
ee51a9fe | 992 | myGlContext->ReleaseDelayed(); |
a174a3c5 | 993 | |
b5ac8292 | 994 | // fetch OpenGl context state |
ee51a9fe | 995 | myGlContext->FetchState(); |
a174a3c5 | 996 | |
a174a3c5 | 997 | OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO; |
ee51a9fe | 998 | bool toSwap = myGlContext->IsRender() |
999 | && !myGlContext->caps->buffersNoSwap | |
1000 | && aFrameBuffer == NULL; | |
a174a3c5 | 1001 | |
b86bb3df | 1002 | Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWidth; |
1003 | Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myHeight; | |
1004 | ||
38a0206f | 1005 | if ( aFrameBuffer == NULL |
1006 | && !myGlContext->DefaultFrameBuffer().IsNull() | |
1007 | && myGlContext->DefaultFrameBuffer()->IsValid()) | |
a2e4f780 | 1008 | { |
38a0206f | 1009 | aFrameBuffer = myGlContext->DefaultFrameBuffer().operator->(); |
a2e4f780 | 1010 | } |
1011 | ||
ee51a9fe | 1012 | if (myHasFboBlit |
f978241f | 1013 | && (myTransientDrawToFront || aProjectType == Graphic3d_Camera::Projection_Stereo)) |
b86bb3df | 1014 | { |
38a0206f | 1015 | if (myMainSceneFbos[0]->GetVPSizeX() != aSizeX |
1016 | || myMainSceneFbos[0]->GetVPSizeY() != aSizeY) | |
760c21c2 | 1017 | { |
ee51a9fe | 1018 | // prepare FBOs containing main scene |
1019 | // for further blitting and rendering immediate presentations on top | |
1020 | if (myGlContext->core20fwd != NULL) | |
1021 | { | |
38a0206f | 1022 | myMainSceneFbos[0]->Init (myGlContext, aSizeX, aSizeY); |
ee51a9fe | 1023 | } |
1024 | } | |
b86bb3df | 1025 | } |
ee51a9fe | 1026 | else |
b86bb3df | 1027 | { |
38a0206f | 1028 | myMainSceneFbos [0]->Release (myGlContext.operator->()); |
1029 | myMainSceneFbos [1]->Release (myGlContext.operator->()); | |
1030 | myImmediateSceneFbos[0]->Release (myGlContext.operator->()); | |
1031 | myImmediateSceneFbos[1]->Release (myGlContext.operator->()); | |
1032 | myMainSceneFbos [0]->ChangeViewport (0, 0); | |
1033 | myMainSceneFbos [1]->ChangeViewport (0, 0); | |
1034 | myImmediateSceneFbos[0]->ChangeViewport (0, 0); | |
1035 | myImmediateSceneFbos[1]->ChangeViewport (0, 0); | |
b86bb3df | 1036 | } |
1037 | ||
f978241f | 1038 | if (aProjectType == Graphic3d_Camera::Projection_Stereo |
1039 | && myMainSceneFbos[0]->IsValid()) | |
a89742cf | 1040 | { |
f978241f | 1041 | myMainSceneFbos[1]->InitLazy (myGlContext, aSizeX, aSizeY); |
1042 | if (!myMainSceneFbos[1]->IsValid()) | |
38a0206f | 1043 | { |
f978241f | 1044 | // no enough memory? |
38a0206f | 1045 | aProjectType = Graphic3d_Camera::Projection_Perspective; |
1046 | } | |
f978241f | 1047 | else if (!myTransientDrawToFront) |
38a0206f | 1048 | { |
f978241f | 1049 | // |
1050 | } | |
1051 | else if (!myGlContext->HasStereoBuffers() | |
1052 | || aStereoMode != Graphic3d_StereoMode_QuadBuffer) | |
1053 | { | |
1054 | myImmediateSceneFbos[0]->InitLazy (myGlContext, aSizeX, aSizeY); | |
1055 | myImmediateSceneFbos[1]->InitLazy (myGlContext, aSizeX, aSizeY); | |
1056 | if (!myImmediateSceneFbos[0]->IsValid() | |
1057 | || !myImmediateSceneFbos[1]->IsValid()) | |
38a0206f | 1058 | { |
38a0206f | 1059 | aProjectType = Graphic3d_Camera::Projection_Perspective; |
1060 | } | |
38a0206f | 1061 | } |
91c60b57 | 1062 | } |
e276548b | 1063 | |
38a0206f | 1064 | if (aProjectType == Graphic3d_Camera::Projection_Stereo) |
91c60b57 | 1065 | { |
38a0206f | 1066 | OpenGl_FrameBuffer* aMainFbos[2] = |
1067 | { | |
1068 | myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL, | |
1069 | myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL | |
1070 | }; | |
1071 | OpenGl_FrameBuffer* anImmFbos[2] = | |
1072 | { | |
1073 | myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL, | |
1074 | myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL | |
1075 | }; | |
a174a3c5 | 1076 | |
f978241f | 1077 | if (!myTransientDrawToFront) |
1078 | { | |
1079 | anImmFbos[0] = aMainFbos[0]; | |
1080 | anImmFbos[1] = aMainFbos[1]; | |
1081 | } | |
1082 | else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip | |
1083 | || aStereoMode == Graphic3d_StereoMode_QuadBuffer) | |
1084 | { | |
1085 | anImmFbos[0] = NULL; | |
1086 | anImmFbos[1] = NULL; | |
1087 | } | |
1088 | ||
38a0206f | 1089 | #if !defined(GL_ES_VERSION_2_0) |
f978241f | 1090 | myGlContext->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK); |
38a0206f | 1091 | #endif |
1092 | redraw1 (theCView, theCUnderLayer, theCOverLayer, | |
1093 | aMainFbos[0], Graphic3d_Camera::Projection_MonoLeftEye); | |
1094 | myBackBufferRestored = Standard_True; | |
1095 | myIsImmediateDrawn = Standard_False; | |
1096 | #if !defined(GL_ES_VERSION_2_0) | |
f978241f | 1097 | myGlContext->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK); |
38a0206f | 1098 | #endif |
1099 | if (!redrawImmediate (theCView, theCOverLayer, theCUnderLayer, aMainFbos[0], aProjectType, anImmFbos[0])) | |
1100 | { | |
1101 | toSwap = false; | |
1102 | } | |
f978241f | 1103 | else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip |
1104 | && toSwap) | |
1105 | { | |
1106 | myGlContext->SwapBuffers(); | |
1107 | } | |
38a0206f | 1108 | |
1109 | #if !defined(GL_ES_VERSION_2_0) | |
f978241f | 1110 | myGlContext->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK); |
38a0206f | 1111 | #endif |
1112 | redraw1 (theCView, theCUnderLayer, theCOverLayer, | |
1113 | aMainFbos[1], Graphic3d_Camera::Projection_MonoRightEye); | |
1114 | myBackBufferRestored = Standard_True; | |
1115 | myIsImmediateDrawn = Standard_False; | |
1116 | if (!redrawImmediate (theCView, theCOverLayer, theCUnderLayer, aMainFbos[1], aProjectType, anImmFbos[1])) | |
1117 | { | |
1118 | toSwap = false; | |
1119 | } | |
1120 | ||
1121 | if (anImmFbos[0] != NULL) | |
1122 | { | |
1123 | bindDefaultFbo (aFrameBuffer); | |
f978241f | 1124 | drawStereoPair (theCView); |
38a0206f | 1125 | } |
1126 | } | |
1127 | else | |
a174a3c5 | 1128 | { |
38a0206f | 1129 | OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL; |
1130 | #if !defined(GL_ES_VERSION_2_0) | |
1131 | if (aMainFbo == NULL | |
1132 | && aFrameBuffer == NULL) | |
1133 | { | |
1134 | myGlContext->SetReadDrawBuffer (GL_BACK); | |
1135 | } | |
1136 | #endif | |
1137 | redraw1 (theCView, theCUnderLayer, theCOverLayer, | |
1138 | aMainFbo != NULL ? aMainFbo : aFrameBuffer, aProjectType); | |
1139 | myBackBufferRestored = Standard_True; | |
1140 | myIsImmediateDrawn = Standard_False; | |
1141 | if (!redrawImmediate (theCView, theCOverLayer, theCUnderLayer, aMainFbo, aProjectType, aFrameBuffer)) | |
1142 | { | |
1143 | toSwap = false; | |
1144 | } | |
a174a3c5 | 1145 | } |
1146 | ||
679ecdee | 1147 | #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE) |
a174a3c5 | 1148 | if (OpenGl_AVIWriter_AllowWriting (theCView.DefWindow.XWindow)) |
1149 | { | |
1150 | GLint params[4]; | |
1151 | glGetIntegerv (GL_VIEWPORT, params); | |
1152 | int nWidth = params[2] & ~0x7; | |
1153 | int nHeight = params[3] & ~0x7; | |
1154 | ||
1155 | const int nBitsPerPixel = 24; | |
1156 | GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8]; | |
1157 | ||
1158 | glPixelStorei (GL_PACK_ALIGNMENT, 1); | |
1159 | glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData); | |
1160 | OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel); | |
1161 | delete[] aDumpData; | |
1162 | } | |
1163 | #endif | |
1164 | ||
38a0206f | 1165 | // bind default FBO |
1166 | bindDefaultFbo(); | |
1167 | ||
ee51a9fe | 1168 | // Swap the buffers |
1169 | if (toSwap) | |
1170 | { | |
1171 | GetGlContext()->SwapBuffers(); | |
38a0206f | 1172 | if (!myMainSceneFbos[0]->IsValid()) |
ee51a9fe | 1173 | { |
1174 | myBackBufferRestored = Standard_False; | |
1175 | } | |
1176 | } | |
1177 | else | |
1178 | { | |
1179 | myGlContext->core11fwd->glFlush(); | |
1180 | } | |
1181 | ||
a174a3c5 | 1182 | // reset render mode state |
ee51a9fe | 1183 | myGlContext->FetchState(); |
a174a3c5 | 1184 | } |
679ecdee | 1185 | |
1186 | // ======================================================================= | |
1187 | // function : redraw1 | |
1188 | // purpose : | |
1189 | // ======================================================================= | |
38a0206f | 1190 | void OpenGl_Workspace::redraw1 (const Graphic3d_CView& theCView, |
1191 | const Aspect_CLayer2d& theCUnderLayer, | |
1192 | const Aspect_CLayer2d& theCOverLayer, | |
1193 | OpenGl_FrameBuffer* theReadDrawFbo, | |
1194 | const Graphic3d_Camera::Projection theProjection) | |
679ecdee | 1195 | { |
73192b37 | 1196 | if (myView.IsNull()) |
679ecdee | 1197 | { |
1198 | return; | |
1199 | } | |
1200 | ||
38a0206f | 1201 | if (theReadDrawFbo != NULL) |
1202 | { | |
1203 | theReadDrawFbo->BindBuffer (myGlContext); | |
1204 | theReadDrawFbo->SetupViewport (myGlContext); | |
1205 | } | |
1206 | else | |
1207 | { | |
1208 | myGlContext->core11fwd->glViewport (0, 0, myWidth, myHeight); | |
1209 | } | |
1210 | ||
679ecdee | 1211 | // request reset of material |
1212 | NamedStatus |= OPENGL_NS_RESMAT; | |
1213 | ||
eae454e3 | 1214 | myUseZBuffer = Standard_True; |
1215 | myUseDepthWrite = Standard_True; | |
1216 | GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; | |
1217 | glDepthFunc (GL_LEQUAL); | |
1218 | glDepthMask (GL_TRUE); | |
1219 | glEnable (GL_DEPTH_TEST); | |
679ecdee | 1220 | |
eae454e3 | 1221 | #if !defined(GL_ES_VERSION_2_0) |
1222 | glClearDepth (1.0); | |
1223 | #else | |
1224 | glClearDepthf (1.0f); | |
1225 | #endif | |
679ecdee | 1226 | |
91c60b57 | 1227 | if (NamedStatus & OPENGL_NS_WHITEBACK) |
679ecdee | 1228 | { |
91c60b57 | 1229 | // set background to white |
1230 | glClearColor (1.0f, 1.0f, 1.0f, 1.0f); | |
679ecdee | 1231 | } |
1232 | else | |
1233 | { | |
91c60b57 | 1234 | glClearColor (myBgColor.rgb[0], myBgColor.rgb[1], myBgColor.rgb[2], 0.0f); |
679ecdee | 1235 | } |
91c60b57 | 1236 | |
679ecdee | 1237 | glClear (toClear); |
1238 | ||
1239 | Handle(OpenGl_Workspace) aWS (this); | |
38a0206f | 1240 | myView->Render (myPrintContext, aWS, theReadDrawFbo, theProjection, theCView, theCUnderLayer, theCOverLayer, Standard_False); |
679ecdee | 1241 | } |
1242 | ||
1243 | // ======================================================================= | |
1244 | // function : copyBackToFront | |
1245 | // purpose : | |
1246 | // ======================================================================= | |
1247 | void OpenGl_Workspace::copyBackToFront() | |
1248 | { | |
ca3c13d1 | 1249 | #if !defined(GL_ES_VERSION_2_0) |
ca3c13d1 | 1250 | |
c827ea3a | 1251 | OpenGl_Mat4 aProjectMat; |
1252 | OpenGl_Utils::Ortho2D<Standard_ShortReal> (aProjectMat, | |
1253 | 0.f, static_cast<GLfloat> (myWidth), 0.f, static_cast<GLfloat> (myHeight)); | |
1254 | ||
1255 | myGlContext->WorldViewState.Push(); | |
1256 | myGlContext->ProjectionState.Push(); | |
1257 | ||
1258 | myGlContext->WorldViewState.SetIdentity(); | |
1259 | myGlContext->ProjectionState.SetCurrent (aProjectMat); | |
1260 | ||
1261 | myGlContext->ApplyProjectionMatrix(); | |
1262 | myGlContext->ApplyWorldViewMatrix(); | |
679ecdee | 1263 | |
1264 | DisableFeatures(); | |
1265 | ||
38a0206f | 1266 | switch (myGlContext->DrawBuffer()) |
1267 | { | |
1268 | case GL_BACK_LEFT: | |
1269 | { | |
1270 | myGlContext->SetReadBuffer (GL_BACK_LEFT); | |
1271 | myGlContext->SetDrawBuffer (GL_FRONT_LEFT); | |
1272 | break; | |
1273 | } | |
1274 | case GL_BACK_RIGHT: | |
1275 | { | |
1276 | myGlContext->SetReadBuffer (GL_BACK_RIGHT); | |
1277 | myGlContext->SetDrawBuffer (GL_FRONT_RIGHT); | |
1278 | break; | |
1279 | } | |
1280 | default: | |
1281 | { | |
1282 | myGlContext->SetReadBuffer (GL_BACK); | |
1283 | myGlContext->SetDrawBuffer (GL_FRONT); | |
1284 | break; | |
1285 | } | |
1286 | } | |
679ecdee | 1287 | |
1288 | glRasterPos2i (0, 0); | |
1289 | glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_COLOR); | |
a1954302 | 1290 | //glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH); |
679ecdee | 1291 | |
1292 | EnableFeatures(); | |
1293 | ||
c827ea3a | 1294 | myGlContext->WorldViewState.Pop(); |
1295 | myGlContext->ProjectionState.Pop(); | |
1296 | myGlContext->ApplyProjectionMatrix(); | |
679ecdee | 1297 | |
38a0206f | 1298 | // read/write from front buffer now |
1299 | myGlContext->SetReadBuffer (myGlContext->DrawBuffer()); | |
ca3c13d1 | 1300 | #endif |
679ecdee | 1301 | myIsImmediateDrawn = Standard_False; |
1302 | } | |
1303 | ||
1304 | // ======================================================================= | |
1305 | // function : DisplayCallback | |
1306 | // purpose : | |
1307 | // ======================================================================= | |
1308 | void OpenGl_Workspace::DisplayCallback (const Graphic3d_CView& theCView, | |
1309 | Standard_Integer theReason) | |
1310 | { | |
1311 | if (theCView.GDisplayCB == NULL) | |
1312 | { | |
1313 | return; | |
1314 | } | |
1315 | ||
1316 | Aspect_GraphicCallbackStruct aCallData; | |
1317 | aCallData.reason = theReason; | |
4e1523ef | 1318 | aCallData.glContext = myGlContext; |
679ecdee | 1319 | aCallData.wsID = theCView.WsId; |
1320 | aCallData.viewID = theCView.ViewId; | |
4e1523ef | 1321 | aCallData.IsCoreProfile = (myGlContext->core11 == NULL); |
679ecdee | 1322 | theCView.GDisplayCB (theCView.DefWindow.XWindow, theCView.GClientData, &aCallData); |
1323 | } | |
1324 | ||
1325 | // ======================================================================= | |
1326 | // function : RedrawImmediate | |
1327 | // purpose : | |
1328 | // ======================================================================= | |
1329 | void OpenGl_Workspace::RedrawImmediate (const Graphic3d_CView& theCView, | |
1330 | const Aspect_CLayer2d& theCUnderLayer, | |
ee51a9fe | 1331 | const Aspect_CLayer2d& theCOverLayer) |
679ecdee | 1332 | { |
38a0206f | 1333 | const Handle(Graphic3d_Camera)& aCamera = myView->Camera(); |
1334 | Graphic3d_Camera::Projection aProjectType = aCamera->ProjectionType(); | |
1335 | OpenGl_FrameBuffer* aFrameBuffer = (OpenGl_FrameBuffer* )theCView.ptrFBO; | |
1336 | if ( aFrameBuffer == NULL | |
1337 | && !myGlContext->DefaultFrameBuffer().IsNull() | |
1338 | && myGlContext->DefaultFrameBuffer()->IsValid()) | |
1339 | { | |
1340 | aFrameBuffer = myGlContext->DefaultFrameBuffer().operator->(); | |
1341 | } | |
1342 | ||
f978241f | 1343 | const Graphic3d_StereoMode aStereoMode = theCView.RenderParams.StereoMode; |
38a0206f | 1344 | if (aProjectType == Graphic3d_Camera::Projection_Stereo) |
1345 | { | |
1346 | if (aFrameBuffer != NULL) | |
1347 | { | |
1348 | // implicitly switch to mono camera for image dump | |
1349 | aProjectType = Graphic3d_Camera::Projection_Perspective; | |
1350 | } | |
1351 | else if (myMainSceneFbos[0]->IsValid() | |
1352 | && !myMainSceneFbos[1]->IsValid()) | |
1353 | { | |
1354 | aProjectType = Graphic3d_Camera::Projection_Perspective; | |
1355 | } | |
1356 | } | |
1357 | ||
ee51a9fe | 1358 | if (!myTransientDrawToFront |
1359 | || !myBackBufferRestored | |
38a0206f | 1360 | || (myGlContext->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid())) |
679ecdee | 1361 | { |
ee51a9fe | 1362 | Redraw (theCView, theCUnderLayer, theCOverLayer); |
679ecdee | 1363 | return; |
1364 | } | |
ee51a9fe | 1365 | else if (!Activate()) |
679ecdee | 1366 | { |
679ecdee | 1367 | return; |
1368 | } | |
1369 | ||
38a0206f | 1370 | bool toSwap = false; |
1371 | if (aProjectType == Graphic3d_Camera::Projection_Stereo) | |
1372 | { | |
1373 | OpenGl_FrameBuffer* aMainFbos[2] = | |
1374 | { | |
1375 | myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL, | |
1376 | myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL | |
1377 | }; | |
1378 | OpenGl_FrameBuffer* anImmFbos[2] = | |
1379 | { | |
1380 | myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL, | |
1381 | myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL | |
1382 | }; | |
f978241f | 1383 | if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip |
1384 | || aStereoMode == Graphic3d_StereoMode_QuadBuffer) | |
1385 | { | |
1386 | anImmFbos[0] = NULL; | |
1387 | anImmFbos[1] = NULL; | |
1388 | } | |
38a0206f | 1389 | |
1390 | if (myGlContext->arbFBO != NULL) | |
1391 | { | |
1392 | myGlContext->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); | |
1393 | } | |
1394 | #if !defined(GL_ES_VERSION_2_0) | |
1395 | if (anImmFbos[0] == NULL) | |
1396 | { | |
f978241f | 1397 | myGlContext->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK); |
38a0206f | 1398 | } |
1399 | #endif | |
1400 | toSwap = redrawImmediate (theCView, theCUnderLayer, theCOverLayer, | |
1401 | aMainFbos[0], | |
1402 | Graphic3d_Camera::Projection_MonoLeftEye, | |
1403 | anImmFbos[0], | |
1404 | Standard_True) || toSwap; | |
f978241f | 1405 | if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip |
1406 | && toSwap | |
1407 | && !myGlContext->caps->buffersNoSwap) | |
1408 | { | |
1409 | myGlContext->SwapBuffers(); | |
1410 | } | |
38a0206f | 1411 | |
1412 | if (myGlContext->arbFBO != NULL) | |
1413 | { | |
1414 | myGlContext->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); | |
1415 | } | |
1416 | #if !defined(GL_ES_VERSION_2_0) | |
1417 | if (anImmFbos[1] == NULL) | |
1418 | { | |
f978241f | 1419 | myGlContext->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK); |
38a0206f | 1420 | } |
1421 | #endif | |
1422 | toSwap = redrawImmediate (theCView, theCUnderLayer, theCOverLayer, | |
1423 | aMainFbos[1], | |
1424 | Graphic3d_Camera::Projection_MonoRightEye, | |
1425 | anImmFbos[1], | |
1426 | Standard_True) || toSwap; | |
1427 | if (anImmFbos[0] != NULL) | |
1428 | { | |
1429 | bindDefaultFbo (aFrameBuffer); | |
f978241f | 1430 | drawStereoPair (theCView); |
38a0206f | 1431 | } |
1432 | } | |
1433 | else | |
a2e4f780 | 1434 | { |
38a0206f | 1435 | OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL; |
1436 | #if !defined(GL_ES_VERSION_2_0) | |
1437 | if (aMainFbo == NULL) | |
1438 | { | |
1439 | myGlContext->SetReadDrawBuffer (GL_BACK); | |
1440 | } | |
1441 | #endif | |
1442 | toSwap = redrawImmediate (theCView, theCUnderLayer, theCOverLayer, | |
1443 | aMainFbo, | |
1444 | aProjectType, | |
1445 | aFrameBuffer, | |
1446 | Standard_True) || toSwap; | |
a2e4f780 | 1447 | } |
1448 | ||
38a0206f | 1449 | // bind default FBO |
1450 | bindDefaultFbo(); | |
1451 | ||
1452 | if (toSwap | |
ef0bfc15 | 1453 | && !myGlContext->caps->buffersNoSwap) |
b86bb3df | 1454 | { |
ee51a9fe | 1455 | myGlContext->SwapBuffers(); |
1456 | } | |
1457 | else | |
1458 | { | |
1459 | myGlContext->core11fwd->glFlush(); | |
ee51a9fe | 1460 | } |
1461 | } | |
b86bb3df | 1462 | |
ee51a9fe | 1463 | // ======================================================================= |
1464 | // function : redrawImmediate | |
1465 | // purpose : | |
1466 | // ======================================================================= | |
1467 | bool OpenGl_Workspace::redrawImmediate (const Graphic3d_CView& theCView, | |
1468 | const Aspect_CLayer2d& theCUnderLayer, | |
1469 | const Aspect_CLayer2d& theCOverLayer, | |
38a0206f | 1470 | OpenGl_FrameBuffer* theReadFbo, |
1471 | const Graphic3d_Camera::Projection theProjection, | |
1472 | OpenGl_FrameBuffer* theDrawFbo, | |
ee51a9fe | 1473 | const Standard_Boolean theIsPartialUpdate) |
1474 | { | |
1475 | GLboolean toCopyBackToFront = GL_FALSE; | |
1476 | if (!myTransientDrawToFront) | |
1477 | { | |
1478 | myBackBufferRestored = Standard_False; | |
1479 | } | |
38a0206f | 1480 | else if (theReadFbo != NULL |
1481 | && theReadFbo->IsValid() | |
ee51a9fe | 1482 | && myGlContext->IsRender()) |
1483 | { | |
38a0206f | 1484 | if (!blitBuffers (theReadFbo, theDrawFbo)) |
b86bb3df | 1485 | { |
38a0206f | 1486 | return true; |
b86bb3df | 1487 | } |
1488 | } | |
38a0206f | 1489 | else if (theDrawFbo == NULL) |
679ecdee | 1490 | { |
ee51a9fe | 1491 | #if !defined(GL_ES_VERSION_2_0) |
1492 | myGlContext->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront); | |
1493 | #endif | |
1494 | if (toCopyBackToFront) | |
679ecdee | 1495 | { |
ee51a9fe | 1496 | if (!myView->HasImmediateStructures() |
1497 | && !theIsPartialUpdate) | |
1498 | { | |
1499 | // prefer Swap Buffers within Redraw in compatibility mode (without FBO) | |
1500 | return true; | |
1501 | } | |
1502 | copyBackToFront(); | |
ee51a9fe | 1503 | } |
1504 | else | |
1505 | { | |
1506 | myBackBufferRestored = Standard_False; | |
679ecdee | 1507 | } |
679ecdee | 1508 | } |
1509 | else | |
1510 | { | |
1511 | myBackBufferRestored = Standard_False; | |
1512 | } | |
1513 | myIsImmediateDrawn = Standard_True; | |
1514 | ||
679ecdee | 1515 | Handle(OpenGl_Workspace) aWS (this); |
a1954302 | 1516 | |
eae454e3 | 1517 | myUseZBuffer = Standard_True; |
1518 | myUseDepthWrite = Standard_True; | |
1519 | glDepthFunc (GL_LEQUAL); | |
1520 | glDepthMask (GL_TRUE); | |
1521 | glEnable (GL_DEPTH_TEST); | |
1522 | #if !defined(GL_ES_VERSION_2_0) | |
1523 | glClearDepth (1.0); | |
1524 | #else | |
1525 | glClearDepthf (1.0f); | |
1526 | #endif | |
a1954302 | 1527 | |
38a0206f | 1528 | myView->Render (myPrintContext, aWS, theDrawFbo, theProjection, |
1529 | theCView, theCUnderLayer, theCOverLayer, Standard_True); | |
a1954302 | 1530 | if (!myView->ImmediateStructures().IsEmpty()) |
1531 | { | |
eae454e3 | 1532 | myUseZBuffer = Standard_False; |
a1954302 | 1533 | glDisable (GL_DEPTH_TEST); |
1534 | } | |
9f112210 | 1535 | for (OpenGl_IndexedMapOfStructure::Iterator anIter (myView->ImmediateStructures()); anIter.More(); anIter.Next()) |
679ecdee | 1536 | { |
1537 | const OpenGl_Structure* aStructure = anIter.Value(); | |
a1954302 | 1538 | if (!aStructure->IsVisible()) |
1539 | { | |
1540 | continue; | |
1541 | } | |
1542 | ||
679ecdee | 1543 | aStructure->Render (aWS); |
1544 | } | |
1545 | ||
38a0206f | 1546 | return !toCopyBackToFront; |
679ecdee | 1547 | } |
91c60b57 | 1548 | |
91c60b57 | 1549 | |
1550 | // ======================================================================= | |
1551 | // function : CanRender | |
1552 | // purpose : | |
1553 | // ======================================================================= | |
1554 | Standard_Boolean OpenGl_RaytraceFilter::CanRender (const OpenGl_Element* theElement) | |
1555 | { | |
1556 | Standard_Boolean aPrevFilterResult = Standard_True; | |
1557 | if (!myPrevRenderFilter.IsNull()) | |
1558 | { | |
1559 | aPrevFilterResult = myPrevRenderFilter->CanRender (theElement); | |
1560 | } | |
1561 | return aPrevFilterResult && | |
1562 | !OpenGl_Raytrace::IsRaytracedElement (theElement); | |
1563 | } |