c357e426 |
1 | // Created on: 2011-09-20 |
2 | // Created by: Sergey ZERCHANINOV |
3 | // Copyright (c) 2011-2014 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
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 |
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. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #include <stdio.h> |
17 | #include <stdlib.h> |
18 | |
19 | #include <OpenGl_GlCore11.hxx> |
c357e426 |
20 | |
21 | #include <Graphic3d_GraphicDriver.hxx> |
22 | #include <Graphic3d_TextureParams.hxx> |
23 | #include <Graphic3d_Texture2Dmanual.hxx> |
24 | #include <Graphic3d_TransformUtils.hxx> |
25 | #include <Image_AlienPixMap.hxx> |
26 | |
27 | #include <NCollection_Mat4.hxx> |
28 | |
29 | #include <OpenGl_AspectLine.hxx> |
30 | #include <OpenGl_Context.hxx> |
31 | #include <OpenGl_Matrix.hxx> |
32 | #include <OpenGl_Workspace.hxx> |
33 | #include <OpenGl_View.hxx> |
34 | #include <OpenGl_Trihedron.hxx> |
35 | #include <OpenGl_GraduatedTrihedron.hxx> |
36 | #include <OpenGl_PrimitiveArray.hxx> |
c357e426 |
37 | #include <OpenGl_ShaderManager.hxx> |
38 | #include <OpenGl_ShaderProgram.hxx> |
39 | #include <OpenGl_Structure.hxx> |
40 | #include <OpenGl_ArbFBO.hxx> |
41 | |
42 | #define EPSI 0.0001 |
43 | |
44 | namespace |
45 | { |
46 | static const GLfloat THE_DEFAULT_AMBIENT[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; |
47 | static const GLfloat THE_DEFAULT_SPOT_DIR[3] = { 0.0f, 0.0f, -1.0f }; |
48 | static const GLfloat THE_DEFAULT_SPOT_EXPONENT = 0.0f; |
49 | static const GLfloat THE_DEFAULT_SPOT_CUTOFF = 180.0f; |
50 | } |
51 | |
52 | extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx |
53 | |
54 | #if !defined(GL_ES_VERSION_2_0) |
55 | |
56 | //======================================================================= |
57 | //function : bindLight |
58 | //purpose : |
59 | //======================================================================= |
60 | static void bindLight (const OpenGl_Light& theLight, |
61 | GLenum& theLightGlId, |
62 | Graphic3d_Vec4& theAmbientColor, |
63 | const Handle(OpenGl_Workspace)& theWorkspace) |
64 | { |
65 | // Only 8 lights in OpenGL... |
66 | if (theLightGlId > GL_LIGHT7) |
67 | { |
68 | return; |
69 | } |
70 | |
71 | if (theLight.Type == Graphic3d_TOLS_AMBIENT) |
72 | { |
73 | // add RGBA intensity of the ambient light |
74 | theAmbientColor += theLight.Color; |
75 | return; |
76 | } |
77 | |
78 | const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext(); |
79 | |
80 | // the light is a headlight? |
81 | if (theLight.IsHeadlight) |
82 | { |
83 | aContext->WorldViewState.Push(); |
84 | aContext->WorldViewState.SetIdentity(); |
85 | |
86 | aContext->ApplyWorldViewMatrix(); |
87 | } |
88 | |
89 | // setup light type |
90 | switch (theLight.Type) |
91 | { |
92 | case Graphic3d_TOLS_AMBIENT : break; // handled by separate if-clause at beginning of method |
93 | case Graphic3d_TOLS_DIRECTIONAL: |
94 | { |
95 | // if the last parameter of GL_POSITION, is zero, the corresponding light source is a Directional one |
96 | const OpenGl_Vec4 anInfDir = -theLight.Direction; |
97 | |
98 | // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE. |
99 | glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT); |
100 | glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData()); |
101 | glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData()); |
102 | glLightfv (theLightGlId, GL_POSITION, anInfDir.GetData()); |
103 | glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR); |
104 | glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT); |
105 | glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF); |
106 | break; |
107 | } |
108 | case Graphic3d_TOLS_POSITIONAL: |
109 | { |
110 | // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE |
111 | glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT); |
112 | glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData()); |
113 | glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData()); |
114 | glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData()); |
115 | glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR); |
116 | glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT); |
117 | glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF); |
118 | glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation()); |
119 | glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation()); |
120 | glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0); |
121 | break; |
122 | } |
123 | case Graphic3d_TOLS_SPOT: |
124 | { |
125 | glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT); |
126 | glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData()); |
127 | glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData()); |
128 | glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData()); |
129 | glLightfv (theLightGlId, GL_SPOT_DIRECTION, theLight.Direction.GetData()); |
130 | glLightf (theLightGlId, GL_SPOT_EXPONENT, theLight.Concentration() * 128.0f); |
131 | glLightf (theLightGlId, GL_SPOT_CUTOFF, (theLight.Angle() * 180.0f) / GLfloat(M_PI)); |
132 | glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation()); |
133 | glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation()); |
134 | glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0f); |
135 | break; |
136 | } |
137 | } |
138 | |
139 | // restore matrix in case of headlight |
140 | if (theLight.IsHeadlight) |
141 | { |
142 | aContext->WorldViewState.Pop(); |
143 | } |
144 | |
145 | glEnable (theLightGlId++); |
146 | } |
147 | #endif |
148 | |
149 | //======================================================================= |
a521d90d |
150 | //function : drawBackground |
c357e426 |
151 | //purpose : |
152 | //======================================================================= |
a521d90d |
153 | void OpenGl_View::drawBackground (const Handle(OpenGl_Workspace)& theWorkspace) |
c357e426 |
154 | { |
155 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); |
156 | |
157 | if ((theWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) != 0 // no background |
158 | || (!myBgTextureArray->IsDefined() // no texture |
159 | && !myBgGradientArray->IsDefined())) // no gradient |
160 | { |
161 | return; |
162 | } |
163 | |
164 | const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False); |
165 | if (wasUsedZBuffer) |
166 | { |
167 | aCtx->core11fwd->glDisable (GL_DEPTH_TEST); |
168 | } |
169 | |
170 | aCtx->ProjectionState.Push(); |
171 | aCtx->WorldViewState.Push(); |
172 | aCtx->ModelWorldState.Push(); |
173 | aCtx->ProjectionState.SetIdentity(); |
174 | aCtx->WorldViewState.SetIdentity(); |
175 | aCtx->ModelWorldState.SetIdentity(); |
176 | aCtx->ApplyProjectionMatrix(); |
177 | aCtx->ApplyModelViewMatrix(); |
178 | |
179 | // Drawing background gradient if: |
180 | // - gradient fill type is not Aspect_GFM_NONE and |
181 | // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode |
182 | if (myBgGradientArray->IsDefined() |
b6472664 |
183 | && (!myTextureParams->Aspect()->ToMapTexture() |
c357e426 |
184 | || myBgTextureArray->TextureFillMethod() == Aspect_FM_CENTERED |
185 | || myBgTextureArray->TextureFillMethod() == Aspect_FM_NONE)) |
186 | { |
187 | #if !defined(GL_ES_VERSION_2_0) |
188 | GLint aShadingModelOld = GL_SMOOTH; |
189 | if (aCtx->core11 != NULL) |
190 | { |
191 | aCtx->core11fwd->glDisable (GL_LIGHTING); |
192 | aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld); |
193 | aCtx->core11->glShadeModel (GL_SMOOTH); |
194 | } |
195 | #endif |
196 | |
197 | if (myBgGradientArray->IsDataChanged()) |
198 | { |
199 | myBgGradientArray->Init (theWorkspace); |
200 | } |
201 | |
202 | myBgGradientArray->Render (theWorkspace); |
203 | |
204 | #if !defined(GL_ES_VERSION_2_0) |
205 | if (aCtx->core11 != NULL) |
206 | { |
207 | aCtx->core11->glShadeModel (aShadingModelOld); |
208 | } |
209 | #endif |
210 | } |
211 | |
212 | // Drawing background image if it is defined |
213 | // (texture is defined and fill type is not Aspect_FM_NONE) |
214 | if (myBgTextureArray->IsDefined() |
b6472664 |
215 | && myTextureParams->Aspect()->ToMapTexture()) |
c357e426 |
216 | { |
217 | aCtx->core11fwd->glDisable (GL_BLEND); |
218 | |
219 | const OpenGl_AspectFace* anOldAspectFace = theWorkspace->SetAspectFace (myTextureParams); |
220 | |
221 | if (myBgTextureArray->IsDataChanged() |
222 | || myBgTextureArray->IsViewSizeChanged (theWorkspace)) |
223 | { |
224 | myBgTextureArray->Init (theWorkspace); |
225 | } |
226 | |
227 | myBgTextureArray->Render (theWorkspace); |
228 | |
229 | // restore aspects |
230 | theWorkspace->SetAspectFace (anOldAspectFace); |
231 | } |
232 | |
233 | aCtx->ModelWorldState.Pop(); |
234 | aCtx->WorldViewState.Pop(); |
235 | aCtx->ProjectionState.Pop(); |
236 | aCtx->ApplyProjectionMatrix(); |
237 | aCtx->ApplyModelViewMatrix(); |
238 | |
239 | if (wasUsedZBuffer) |
240 | { |
241 | theWorkspace->SetUseZBuffer (Standard_True); |
242 | aCtx->core11fwd->glEnable (GL_DEPTH_TEST); |
243 | } |
244 | } |
245 | |
246 | //======================================================================= |
247 | //function : Redraw |
248 | //purpose : |
249 | //======================================================================= |
250 | void OpenGl_View::Redraw() |
251 | { |
7ccf8676 |
252 | const Standard_Boolean wasDisabledMSAA = myToDisableMSAA; |
253 | const Standard_Boolean hadFboBlit = myHasFboBlit; |
c357e426 |
254 | if (myRenderParams.Method == Graphic3d_RM_RAYTRACING |
255 | && !myCaps->vboDisable |
256 | && !myCaps->keepArrayData) |
257 | { |
258 | if (myWasRedrawnGL) |
259 | { |
260 | myDeviceLostFlag = Standard_True; |
261 | } |
262 | |
263 | myCaps->keepArrayData = Standard_True; |
264 | } |
265 | |
266 | if (!myWorkspace->Activate()) |
267 | { |
268 | return; |
269 | } |
270 | |
271 | myWindow->SetSwapInterval(); |
272 | |
273 | ++myFrameCounter; |
274 | const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode; |
275 | Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType(); |
276 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
277 | |
278 | // release pending GL resources |
279 | aCtx->ReleaseDelayed(); |
280 | |
281 | // fetch OpenGl context state |
282 | aCtx->FetchState(); |
283 | |
75c262a9 |
284 | // set resolution ratio |
285 | aCtx->SetResolutionRatio (RenderingParams().ResolutionRatio()); |
286 | |
b128c892 |
287 | OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->(); |
c357e426 |
288 | bool toSwap = aCtx->IsRender() |
289 | && !aCtx->caps->buffersNoSwap |
290 | && aFrameBuffer == NULL; |
291 | |
292 | Standard_Integer aSizeX = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeX() : myWindow->Width(); |
293 | Standard_Integer aSizeY = aFrameBuffer != NULL ? aFrameBuffer->GetVPSizeY() : myWindow->Height(); |
294 | |
3c4b62a4 |
295 | // determine multisampling parameters |
7ccf8676 |
296 | Standard_Integer aNbSamples = !myToDisableMSAA |
297 | ? Max (Min (myRenderParams.NbMsaaSamples, aCtx->MaxMsaaSamples()), 0) |
298 | : 0; |
3c4b62a4 |
299 | if (aNbSamples != 0) |
300 | { |
301 | aNbSamples = OpenGl_Context::GetPowerOfTwo (aNbSamples, aCtx->MaxMsaaSamples()); |
302 | } |
303 | |
c357e426 |
304 | if ( aFrameBuffer == NULL |
305 | && !aCtx->DefaultFrameBuffer().IsNull() |
306 | && aCtx->DefaultFrameBuffer()->IsValid()) |
307 | { |
308 | aFrameBuffer = aCtx->DefaultFrameBuffer().operator->(); |
309 | } |
310 | |
311 | if (myHasFboBlit |
3c4b62a4 |
312 | && (myTransientDrawToFront |
313 | || aProjectType == Graphic3d_Camera::Projection_Stereo |
314 | || aNbSamples != 0)) |
c357e426 |
315 | { |
316 | if (myMainSceneFbos[0]->GetVPSizeX() != aSizeX |
3c4b62a4 |
317 | || myMainSceneFbos[0]->GetVPSizeY() != aSizeY |
318 | || myMainSceneFbos[0]->NbSamples() != aNbSamples) |
c357e426 |
319 | { |
521b0d7f |
320 | if (!myTransientDrawToFront) |
321 | { |
322 | myImmediateSceneFbos[0]->Release (aCtx.operator->()); |
323 | myImmediateSceneFbos[1]->Release (aCtx.operator->()); |
324 | myImmediateSceneFbos[0]->ChangeViewport (0, 0); |
325 | myImmediateSceneFbos[1]->ChangeViewport (0, 0); |
326 | } |
327 | |
c357e426 |
328 | // prepare FBOs containing main scene |
329 | // for further blitting and rendering immediate presentations on top |
330 | if (aCtx->core20fwd != NULL) |
331 | { |
3c4b62a4 |
332 | myMainSceneFbos[0]->Init (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, aNbSamples); |
c357e426 |
333 | } |
521b0d7f |
334 | if (myTransientDrawToFront |
335 | && !aCtx->caps->useSystemBuffer |
336 | && myMainSceneFbos[0]->IsValid()) |
c357e426 |
337 | { |
3c4b62a4 |
338 | myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]); |
c357e426 |
339 | } |
340 | } |
341 | } |
342 | else |
343 | { |
344 | myMainSceneFbos [0]->Release (aCtx.operator->()); |
345 | myMainSceneFbos [1]->Release (aCtx.operator->()); |
346 | myImmediateSceneFbos[0]->Release (aCtx.operator->()); |
347 | myImmediateSceneFbos[1]->Release (aCtx.operator->()); |
348 | myMainSceneFbos [0]->ChangeViewport (0, 0); |
349 | myMainSceneFbos [1]->ChangeViewport (0, 0); |
350 | myImmediateSceneFbos[0]->ChangeViewport (0, 0); |
351 | myImmediateSceneFbos[1]->ChangeViewport (0, 0); |
352 | } |
353 | |
354 | if (aProjectType == Graphic3d_Camera::Projection_Stereo |
355 | && myMainSceneFbos[0]->IsValid()) |
356 | { |
3c4b62a4 |
357 | myMainSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]); |
c357e426 |
358 | if (!myMainSceneFbos[1]->IsValid()) |
359 | { |
360 | // no enough memory? |
361 | aProjectType = Graphic3d_Camera::Projection_Perspective; |
362 | } |
363 | else if (!myTransientDrawToFront) |
364 | { |
365 | // |
366 | } |
367 | else if (!aCtx->HasStereoBuffers() || aStereoMode != Graphic3d_StereoMode_QuadBuffer) |
368 | { |
3c4b62a4 |
369 | myImmediateSceneFbos[0]->InitLazy (aCtx, *myMainSceneFbos[0]); |
370 | myImmediateSceneFbos[1]->InitLazy (aCtx, *myMainSceneFbos[0]); |
c357e426 |
371 | if (!myImmediateSceneFbos[0]->IsValid() |
372 | || !myImmediateSceneFbos[1]->IsValid()) |
373 | { |
374 | aProjectType = Graphic3d_Camera::Projection_Perspective; |
375 | } |
376 | } |
377 | } |
378 | |
379 | if (aProjectType == Graphic3d_Camera::Projection_Stereo) |
380 | { |
381 | OpenGl_FrameBuffer* aMainFbos[2] = |
382 | { |
383 | myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL, |
384 | myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL |
385 | }; |
386 | OpenGl_FrameBuffer* anImmFbos[2] = |
387 | { |
388 | myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL, |
389 | myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL |
390 | }; |
391 | |
392 | if (!myTransientDrawToFront) |
393 | { |
394 | anImmFbos[0] = aMainFbos[0]; |
395 | anImmFbos[1] = aMainFbos[1]; |
396 | } |
397 | else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip |
398 | || aStereoMode == Graphic3d_StereoMode_QuadBuffer) |
399 | { |
400 | anImmFbos[0] = NULL; |
401 | anImmFbos[1] = NULL; |
402 | } |
403 | |
404 | #if !defined(GL_ES_VERSION_2_0) |
405 | aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK); |
406 | #endif |
407 | redraw (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0]); |
408 | myBackBufferRestored = Standard_True; |
409 | myIsImmediateDrawn = Standard_False; |
410 | #if !defined(GL_ES_VERSION_2_0) |
411 | aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK); |
412 | #endif |
b0dc79bc |
413 | if (!redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, aMainFbos[0], anImmFbos[0])) |
c357e426 |
414 | { |
415 | toSwap = false; |
416 | } |
417 | else if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip && toSwap) |
418 | { |
419 | aCtx->SwapBuffers(); |
420 | } |
421 | |
422 | #if !defined(GL_ES_VERSION_2_0) |
423 | aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK); |
424 | #endif |
425 | redraw (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1]); |
426 | myBackBufferRestored = Standard_True; |
427 | myIsImmediateDrawn = Standard_False; |
b0dc79bc |
428 | if (!redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, aMainFbos[1], anImmFbos[1])) |
c357e426 |
429 | { |
430 | toSwap = false; |
431 | } |
432 | |
433 | if (anImmFbos[0] != NULL) |
434 | { |
3c4b62a4 |
435 | drawStereoPair (aFrameBuffer); |
c357e426 |
436 | } |
437 | } |
438 | else |
439 | { |
440 | OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL; |
441 | OpenGl_FrameBuffer* anImmFbo = aFrameBuffer; |
442 | if (!aCtx->caps->useSystemBuffer && myImmediateSceneFbos[0]->IsValid()) |
443 | { |
444 | anImmFbo = myImmediateSceneFbos[0].operator->(); |
445 | } |
521b0d7f |
446 | if (!myTransientDrawToFront) |
447 | { |
448 | anImmFbo = aMainFbo; |
449 | } |
c357e426 |
450 | |
451 | #if !defined(GL_ES_VERSION_2_0) |
452 | if (aMainFbo == NULL |
453 | && aFrameBuffer == NULL) |
454 | { |
455 | aCtx->SetReadDrawBuffer (GL_BACK); |
456 | } |
457 | #endif |
458 | redraw (aProjectType, aMainFbo != NULL ? aMainFbo : aFrameBuffer); |
459 | myBackBufferRestored = Standard_True; |
460 | myIsImmediateDrawn = Standard_False; |
461 | if (!redrawImmediate (aProjectType, aMainFbo, anImmFbo)) |
462 | { |
463 | toSwap = false; |
464 | } |
465 | |
466 | if (anImmFbo != NULL |
467 | && anImmFbo != aFrameBuffer) |
468 | { |
469 | blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput); |
470 | } |
471 | } |
472 | |
473 | #if defined(_WIN32) && defined(HAVE_VIDEOCAPTURE) |
474 | if (OpenGl_AVIWriter_AllowWriting (myWindow->PlatformWindow()->NativeHandle())) |
475 | { |
476 | GLint params[4]; |
477 | glGetIntegerv (GL_VIEWPORT, params); |
478 | int nWidth = params[2] & ~0x7; |
479 | int nHeight = params[3] & ~0x7; |
480 | |
481 | const int nBitsPerPixel = 24; |
482 | GLubyte* aDumpData = new GLubyte[nWidth * nHeight * nBitsPerPixel / 8]; |
483 | |
484 | glPixelStorei (GL_PACK_ALIGNMENT, 1); |
485 | glReadPixels (0, 0, nWidth, nHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, aDumpData); |
486 | OpenGl_AVIWriter_AVIWriter (aDumpData, nWidth, nHeight, nBitsPerPixel); |
487 | delete[] aDumpData; |
488 | } |
489 | #endif |
490 | |
bf02aa7d |
491 | if (myRenderParams.Method == Graphic3d_RM_RAYTRACING |
492 | && myRenderParams.IsGlobalIlluminationEnabled) |
493 | { |
494 | myAccumFrames++; |
495 | } |
496 | |
c357e426 |
497 | // bind default FBO |
498 | bindDefaultFbo(); |
499 | |
7ccf8676 |
500 | if (wasDisabledMSAA != myToDisableMSAA |
501 | || hadFboBlit != myHasFboBlit) |
502 | { |
503 | // retry on error |
504 | Redraw(); |
505 | } |
506 | |
c357e426 |
507 | // Swap the buffers |
508 | if (toSwap) |
509 | { |
510 | aCtx->SwapBuffers(); |
511 | if (!myMainSceneFbos[0]->IsValid()) |
512 | { |
513 | myBackBufferRestored = Standard_False; |
514 | } |
515 | } |
516 | else |
517 | { |
518 | aCtx->core11fwd->glFlush(); |
519 | } |
520 | |
521 | // reset render mode state |
522 | aCtx->FetchState(); |
523 | |
524 | myWasRedrawnGL = Standard_True; |
525 | } |
526 | |
527 | // ======================================================================= |
528 | // function : RedrawImmediate |
529 | // purpose : |
530 | // ======================================================================= |
531 | void OpenGl_View::RedrawImmediate() |
532 | { |
533 | if (!myWorkspace->Activate()) |
534 | return; |
535 | |
536 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
537 | if (!myTransientDrawToFront |
538 | || !myBackBufferRestored |
539 | || (aCtx->caps->buffersNoSwap && !myMainSceneFbos[0]->IsValid())) |
540 | { |
541 | Redraw(); |
542 | return; |
543 | } |
544 | |
545 | const Graphic3d_StereoMode aStereoMode = myRenderParams.StereoMode; |
546 | Graphic3d_Camera::Projection aProjectType = myCamera->ProjectionType(); |
b128c892 |
547 | OpenGl_FrameBuffer* aFrameBuffer = myFBO.operator->(); |
c357e426 |
548 | |
549 | if ( aFrameBuffer == NULL |
550 | && !aCtx->DefaultFrameBuffer().IsNull() |
551 | && aCtx->DefaultFrameBuffer()->IsValid()) |
552 | { |
553 | aFrameBuffer = aCtx->DefaultFrameBuffer().operator->(); |
554 | } |
555 | |
556 | if (aProjectType == Graphic3d_Camera::Projection_Stereo) |
557 | { |
558 | if (myMainSceneFbos[0]->IsValid() |
559 | && !myMainSceneFbos[1]->IsValid()) |
560 | { |
561 | aProjectType = Graphic3d_Camera::Projection_Perspective; |
562 | } |
563 | } |
564 | |
565 | bool toSwap = false; |
566 | if (aProjectType == Graphic3d_Camera::Projection_Stereo) |
567 | { |
568 | OpenGl_FrameBuffer* aMainFbos[2] = |
569 | { |
570 | myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL, |
571 | myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL |
572 | }; |
573 | OpenGl_FrameBuffer* anImmFbos[2] = |
574 | { |
575 | myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL, |
576 | myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL |
577 | }; |
578 | if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip |
579 | || aStereoMode == Graphic3d_StereoMode_QuadBuffer) |
580 | { |
581 | anImmFbos[0] = NULL; |
582 | anImmFbos[1] = NULL; |
583 | } |
584 | |
585 | if (aCtx->arbFBO != NULL) |
586 | { |
587 | aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
588 | } |
589 | #if !defined(GL_ES_VERSION_2_0) |
590 | if (anImmFbos[0] == NULL) |
591 | { |
592 | aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_LEFT : GL_BACK); |
593 | } |
594 | #endif |
595 | toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoLeftEye, |
596 | aMainFbos[0], |
597 | anImmFbos[0], |
598 | Standard_True) || toSwap; |
599 | if (aStereoMode == Graphic3d_StereoMode_SoftPageFlip |
600 | && toSwap |
601 | && !aCtx->caps->buffersNoSwap) |
602 | { |
603 | aCtx->SwapBuffers(); |
604 | } |
605 | |
606 | if (aCtx->arbFBO != NULL) |
607 | { |
608 | aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
609 | } |
610 | #if !defined(GL_ES_VERSION_2_0) |
611 | if (anImmFbos[1] == NULL) |
612 | { |
613 | aCtx->SetReadDrawBuffer (aStereoMode == Graphic3d_StereoMode_QuadBuffer ? GL_BACK_RIGHT : GL_BACK); |
614 | } |
615 | #endif |
616 | toSwap = redrawImmediate (Graphic3d_Camera::Projection_MonoRightEye, |
617 | aMainFbos[1], |
618 | anImmFbos[1], |
619 | Standard_True) || toSwap; |
620 | if (anImmFbos[0] != NULL) |
621 | { |
3c4b62a4 |
622 | drawStereoPair (aFrameBuffer); |
c357e426 |
623 | } |
624 | } |
625 | else |
626 | { |
627 | OpenGl_FrameBuffer* aMainFbo = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL; |
628 | OpenGl_FrameBuffer* anImmFbo = aFrameBuffer; |
629 | if (!aCtx->caps->useSystemBuffer && myImmediateSceneFbos[0]->IsValid()) |
630 | { |
631 | anImmFbo = myImmediateSceneFbos[0].operator->(); |
632 | } |
633 | #if !defined(GL_ES_VERSION_2_0) |
634 | if (aMainFbo == NULL) |
635 | { |
636 | aCtx->SetReadDrawBuffer (GL_BACK); |
637 | } |
638 | #endif |
639 | toSwap = redrawImmediate (aProjectType, |
640 | aMainFbo, |
641 | anImmFbo, |
642 | Standard_True) || toSwap; |
643 | if (anImmFbo != NULL |
644 | && anImmFbo != aFrameBuffer) |
645 | { |
646 | blitBuffers (anImmFbo, aFrameBuffer, myToFlipOutput); |
647 | } |
648 | } |
649 | |
650 | // bind default FBO |
651 | bindDefaultFbo(); |
652 | |
653 | if (toSwap && !aCtx->caps->buffersNoSwap) |
654 | { |
655 | aCtx->SwapBuffers(); |
656 | } |
657 | else |
658 | { |
659 | aCtx->core11fwd->glFlush(); |
660 | } |
661 | |
662 | myWasRedrawnGL = Standard_True; |
663 | } |
664 | |
665 | // ======================================================================= |
666 | // function : redraw |
667 | // purpose : |
668 | // ======================================================================= |
669 | void OpenGl_View::redraw (const Graphic3d_Camera::Projection theProjection, OpenGl_FrameBuffer* theReadDrawFbo) |
670 | { |
671 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
672 | if (theReadDrawFbo != NULL) |
673 | { |
674 | theReadDrawFbo->BindBuffer (aCtx); |
675 | theReadDrawFbo->SetupViewport (aCtx); |
676 | } |
677 | else |
678 | { |
679 | aCtx->core11fwd->glViewport (0, 0, myWindow->Width(), myWindow->Height()); |
680 | } |
681 | |
682 | // request reset of material |
683 | myWorkspace->NamedStatus |= OPENGL_NS_RESMAT; |
684 | myWorkspace->UseZBuffer() = Standard_True; |
685 | myWorkspace->UseDepthWrite() = Standard_True; |
686 | GLbitfield toClear = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; |
687 | glDepthFunc (GL_LEQUAL); |
688 | glDepthMask (GL_TRUE); |
689 | glEnable (GL_DEPTH_TEST); |
690 | |
691 | #if !defined(GL_ES_VERSION_2_0) |
692 | glClearDepth (1.0); |
693 | #else |
694 | glClearDepthf (1.0f); |
695 | #endif |
696 | |
697 | if (myWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) |
698 | { |
699 | // set background to white |
700 | glClearColor (1.0f, 1.0f, 1.0f, 1.0f); |
701 | } |
702 | else |
703 | { |
b6472664 |
704 | const OpenGl_Vec4& aBgColor = myBgColor; |
705 | glClearColor (aBgColor.r(), aBgColor.g(), aBgColor.b(), 0.0f); |
c357e426 |
706 | } |
707 | |
708 | glClear (toClear); |
709 | |
710 | render (theProjection, theReadDrawFbo, Standard_False); |
711 | } |
712 | |
713 | // ======================================================================= |
714 | // function : redrawMonoImmediate |
715 | // purpose : |
716 | // ======================================================================= |
717 | bool OpenGl_View::redrawImmediate (const Graphic3d_Camera::Projection theProjection, |
718 | OpenGl_FrameBuffer* theReadFbo, |
719 | OpenGl_FrameBuffer* theDrawFbo, |
720 | const Standard_Boolean theIsPartialUpdate) |
721 | { |
722 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
723 | GLboolean toCopyBackToFront = GL_FALSE; |
521b0d7f |
724 | if (theDrawFbo == theReadFbo |
725 | && theDrawFbo != NULL) |
c357e426 |
726 | { |
727 | myBackBufferRestored = Standard_False; |
728 | } |
729 | else if (theReadFbo != NULL |
730 | && theReadFbo->IsValid() |
731 | && aCtx->IsRender()) |
732 | { |
733 | if (!blitBuffers (theReadFbo, theDrawFbo)) |
734 | { |
735 | return true; |
736 | } |
737 | } |
738 | else if (theDrawFbo == NULL) |
739 | { |
740 | #if !defined(GL_ES_VERSION_2_0) |
741 | aCtx->core11fwd->glGetBooleanv (GL_DOUBLEBUFFER, &toCopyBackToFront); |
742 | #endif |
743 | if (toCopyBackToFront) |
744 | { |
745 | if (!HasImmediateStructures() |
746 | && !theIsPartialUpdate) |
747 | { |
748 | // prefer Swap Buffers within Redraw in compatibility mode (without FBO) |
749 | return true; |
750 | } |
751 | copyBackToFront(); |
752 | } |
753 | else |
754 | { |
755 | myBackBufferRestored = Standard_False; |
756 | } |
757 | } |
758 | else |
759 | { |
760 | myBackBufferRestored = Standard_False; |
761 | } |
762 | myIsImmediateDrawn = Standard_True; |
763 | |
764 | myWorkspace->UseZBuffer() = Standard_True; |
765 | myWorkspace->UseDepthWrite() = Standard_True; |
766 | glDepthFunc (GL_LEQUAL); |
767 | glDepthMask (GL_TRUE); |
768 | glEnable (GL_DEPTH_TEST); |
769 | #if !defined(GL_ES_VERSION_2_0) |
770 | glClearDepth (1.0); |
771 | #else |
772 | glClearDepthf (1.0f); |
773 | #endif |
774 | |
775 | render (theProjection, theDrawFbo, Standard_True); |
776 | |
777 | return !toCopyBackToFront; |
778 | } |
779 | |
780 | //======================================================================= |
781 | //function : Render |
782 | //purpose : |
783 | //======================================================================= |
784 | void OpenGl_View::render (Graphic3d_Camera::Projection theProjection, |
785 | OpenGl_FrameBuffer* theOutputFBO, |
786 | const Standard_Boolean theToDrawImmediate) |
787 | { |
788 | // ================================== |
789 | // Step 1: Prepare for render |
790 | // ================================== |
791 | |
792 | const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext(); |
793 | |
794 | #if !defined(GL_ES_VERSION_2_0) |
795 | // Disable current clipping planes |
796 | if (aContext->core11 != NULL) |
797 | { |
798 | const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes(); |
799 | for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId) |
800 | { |
801 | aContext->core11fwd->glDisable (aClipPlaneId); |
802 | } |
803 | } |
804 | #endif |
805 | |
806 | // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm). |
807 | myBVHSelector.SetViewVolume (myCamera); |
91d96372 |
808 | myBVHSelector.SetViewportSize (myWindow->Width(), myWindow->Height()); |
c357e426 |
809 | |
810 | const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager(); |
811 | if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState) |
812 | { |
016e5959 |
813 | aManager->UpdateLightSourceStateTo (myShadingModel == Graphic3d_TOSM_NONE ? &myNoShadingLight : &myLights); |
c357e426 |
814 | myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()); |
815 | } |
816 | |
817 | // Update matrices if camera has changed. |
818 | Graphic3d_WorldViewProjState aWVPState = myCamera->WorldViewProjState(); |
819 | const Standard_Boolean isCameraChanged = myWorldViewProjState != aWVPState; |
820 | const Standard_Boolean isSameView = aManager->IsSameView (this); |
821 | if (isCameraChanged) |
822 | { |
823 | aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF()); |
824 | aContext->WorldViewState .SetCurrent (myCamera->OrientationMatrixF()); |
bf02aa7d |
825 | myAccumFrames = 0; |
c357e426 |
826 | } |
827 | |
828 | // Apply new matrix state if camera has changed or this view differs from the one |
829 | // that was previously used for configuring matrices of shader manager |
830 | // (ApplyProjectionMatrix and ApplyWorldViewMatrix will affect the manager). |
831 | if (isCameraChanged || !isSameView) |
832 | { |
833 | aContext->ApplyProjectionMatrix(); |
834 | aContext->ApplyWorldViewMatrix(); |
835 | } |
836 | |
837 | if (aManager->ModelWorldState().Index() == 0) |
838 | { |
839 | aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4()); |
840 | } |
841 | |
842 | myWorldViewProjState = aWVPState; |
843 | |
844 | // ==================================== |
845 | // Step 2: Redraw background |
846 | // ==================================== |
847 | |
848 | // Render background |
849 | if (!theToDrawImmediate) |
850 | { |
a521d90d |
851 | drawBackground (myWorkspace); |
c357e426 |
852 | } |
853 | |
854 | #if !defined(GL_ES_VERSION_2_0) |
855 | // Switch off lighting by default |
856 | if (aContext->core11 != NULL) |
857 | { |
858 | glDisable(GL_LIGHTING); |
859 | } |
860 | #endif |
861 | |
862 | // ================================= |
863 | // Step 3: Redraw main plane |
864 | // ================================= |
865 | |
866 | // Setup face culling |
867 | GLboolean isCullFace = GL_FALSE; |
868 | if (myBackfacing != Graphic3d_TOBM_AUTOMATIC) |
869 | { |
870 | isCullFace = glIsEnabled (GL_CULL_FACE); |
871 | if (myBackfacing == Graphic3d_TOBM_DISABLE) |
872 | { |
873 | glEnable (GL_CULL_FACE); |
874 | glCullFace (GL_BACK); |
875 | } |
876 | else |
877 | glDisable (GL_CULL_FACE); |
878 | } |
879 | |
880 | #if !defined(GL_ES_VERSION_2_0) |
881 | // if the view is scaled normal vectors are scaled to unit |
882 | // length for correct displaying of shaded objects |
883 | const gp_Pnt anAxialScale = myCamera->AxialScale(); |
884 | if (anAxialScale.X() != 1.F || |
885 | anAxialScale.Y() != 1.F || |
886 | anAxialScale.Z() != 1.F) |
887 | { |
888 | aContext->SetGlNormalizeEnabled (Standard_True); |
889 | } |
890 | else |
891 | { |
892 | aContext->SetGlNormalizeEnabled (Standard_False); |
893 | } |
894 | |
c357e426 |
895 | // Apply InteriorShadingMethod |
896 | if (aContext->core11 != NULL) |
897 | { |
898 | aContext->core11->glShadeModel (myShadingModel == Graphic3d_TOSM_FACET |
899 | || myShadingModel == Graphic3d_TOSM_NONE ? GL_FLAT : GL_SMOOTH); |
900 | } |
901 | #endif |
902 | |
903 | aManager->SetShadingModel (myShadingModel); |
c357e426 |
904 | |
905 | // Redraw 3d scene |
906 | if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye) |
907 | { |
908 | aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF()); |
909 | aContext->ApplyProjectionMatrix(); |
910 | } |
911 | else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye) |
912 | { |
913 | aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF()); |
914 | aContext->ApplyProjectionMatrix(); |
915 | } |
83da37b1 |
916 | |
917 | myWorkspace->SetEnvironmentTexture (myTextureEnv); |
918 | |
bf02aa7d |
919 | renderScene (theProjection, theOutputFBO, theToDrawImmediate); |
c357e426 |
920 | |
83da37b1 |
921 | myWorkspace->SetEnvironmentTexture (Handle(OpenGl_Texture)()); |
922 | |
c357e426 |
923 | // =============================== |
924 | // Step 4: Trihedron |
925 | // =============================== |
926 | |
927 | // Resetting GL parameters according to the default aspects |
928 | // in order to synchronize GL state with the graphic driver state |
929 | // before drawing auxiliary stuff (trihedrons, overlayer) |
c357e426 |
930 | myWorkspace->ResetAppliedAspect(); |
931 | |
c357e426 |
932 | |
deb02f86 |
933 | // We need to disable (unbind) all shaders programs to ensure |
934 | // that all objects without specified aspect will be drawn |
935 | // correctly (such as background) |
936 | aContext->BindProgram (NULL); |
c357e426 |
937 | |
938 | // Render trihedron |
939 | if (!theToDrawImmediate) |
940 | { |
941 | renderTrihedron (myWorkspace); |
942 | |
943 | // Restore face culling |
944 | if (myBackfacing != Graphic3d_TOBM_AUTOMATIC) |
945 | { |
946 | if (isCullFace) |
947 | { |
948 | glEnable (GL_CULL_FACE); |
949 | glCullFace (GL_BACK); |
950 | } |
951 | else |
952 | glDisable (GL_CULL_FACE); |
953 | } |
954 | } |
955 | |
c357e426 |
956 | // ============================================================== |
957 | // Step 6: Keep shader manager informed about last View |
958 | // ============================================================== |
959 | |
960 | if (!aManager.IsNull()) |
961 | { |
962 | aManager->SetLastView (this); |
963 | } |
964 | } |
965 | |
966 | // ======================================================================= |
967 | // function : InvalidateBVHData |
968 | // purpose : |
969 | // ======================================================================= |
970 | void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId) |
971 | { |
972 | myZLayers.InvalidateBVHData (theLayerId); |
973 | } |
974 | |
975 | //======================================================================= |
976 | //function : renderStructs |
977 | //purpose : |
978 | //======================================================================= |
bf02aa7d |
979 | void OpenGl_View::renderStructs (Graphic3d_Camera::Projection theProjection, |
980 | OpenGl_FrameBuffer* theReadDrawFbo, |
981 | const Standard_Boolean theToDrawImmediate) |
c357e426 |
982 | { |
983 | if ( myZLayers.NbStructures() <= 0 ) |
984 | return; |
985 | |
986 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
c357e426 |
987 | Standard_Boolean toRenderGL = theToDrawImmediate || |
988 | myRenderParams.Method != Graphic3d_RM_RAYTRACING || |
989 | myRaytraceInitStatus == OpenGl_RT_FAIL || |
990 | aCtx->IsFeedback(); |
991 | |
992 | if (!toRenderGL) |
993 | { |
994 | toRenderGL = !initRaytraceResources (aCtx) || |
995 | !updateRaytraceGeometry (OpenGl_GUM_CHECK, myId, aCtx); |
996 | |
997 | toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL |
998 | |
999 | if (!toRenderGL) |
1000 | { |
1001 | const Standard_Integer aSizeX = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeX() : myWindow->Width(); |
1002 | const Standard_Integer aSizeY = theReadDrawFbo != NULL ? theReadDrawFbo->GetVPSizeY() : myWindow->Height(); |
3c4b62a4 |
1003 | myOpenGlFBO ->InitLazy (aCtx, aSizeX, aSizeY, myFboColorFormat, myFboDepthFormat, 0); |
c357e426 |
1004 | |
1005 | if (myRaytraceFilter.IsNull()) |
1006 | myRaytraceFilter = new OpenGl_RaytraceFilter; |
1007 | |
1008 | myRaytraceFilter->SetPrevRenderFilter (myWorkspace->GetRenderFilter()); |
1009 | |
1010 | if (theReadDrawFbo != NULL) |
1011 | theReadDrawFbo->UnbindBuffer (aCtx); |
1012 | |
1013 | // Prepare preliminary OpenGL output |
1014 | if (aCtx->arbFBOBlit != NULL) |
1015 | { |
1016 | // Render bottom OSD layer |
1017 | myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Bottom); |
1018 | |
1019 | myWorkspace->SetRenderFilter (myRaytraceFilter); |
1020 | { |
1021 | if (theReadDrawFbo != NULL) |
1022 | { |
1023 | theReadDrawFbo->BindReadBuffer (aCtx); |
1024 | } |
1025 | else |
1026 | { |
1027 | aCtx->arbFBO->glBindFramebuffer (GL_READ_FRAMEBUFFER, 0); |
1028 | } |
1029 | |
1030 | myOpenGlFBO->BindDrawBuffer (aCtx); |
1031 | |
1032 | aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aSizeX, aSizeY, |
1033 | 0, 0, aSizeX, aSizeY, |
1034 | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, |
1035 | GL_NEAREST); |
1036 | |
1037 | // Render non-polygonal elements in default layer |
1038 | myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Default); |
1039 | } |
1040 | myWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter()); |
1041 | } |
1042 | |
1043 | if (theReadDrawFbo != NULL) |
1044 | { |
1045 | theReadDrawFbo->BindBuffer (aCtx); |
1046 | } |
1047 | else |
1048 | { |
1049 | aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0); |
1050 | } |
1051 | |
f55ba97f |
1052 | // Reset OpenGl aspects state to default to avoid enabling of |
1053 | // backface culling which is not supported in ray-tracing. |
1054 | myWorkspace->ResetAppliedAspect(); |
1055 | |
c357e426 |
1056 | // Ray-tracing polygonal primitive arrays |
bf02aa7d |
1057 | raytrace (aSizeX, aSizeY, theProjection, theReadDrawFbo, aCtx); |
c357e426 |
1058 | |
1059 | // Render upper (top and topmost) OpenGL layers |
1060 | myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_Upper); |
1061 | } |
1062 | } |
1063 | |
1064 | // Redraw 3D scene using OpenGL in standard |
1065 | // mode or in case of ray-tracing failure |
1066 | if (toRenderGL) |
1067 | { |
1068 | myZLayers.Render (myWorkspace, theToDrawImmediate, OpenGl_LF_All); |
1069 | |
1070 | // Set flag that scene was redrawn by standard pipeline |
1071 | myWasRedrawnGL = Standard_True; |
1072 | } |
1073 | } |
1074 | |
1075 | //======================================================================= |
1076 | //function : renderTrihedron |
1077 | //purpose : |
1078 | //======================================================================= |
1079 | void OpenGl_View::renderTrihedron (const Handle(OpenGl_Workspace) &theWorkspace) |
1080 | { |
1081 | // display global trihedron |
1082 | if (myToShowTrihedron) |
1083 | { |
83da37b1 |
1084 | // disable environment texture |
1085 | Handle(OpenGl_Texture) anEnvironmentTexture = theWorkspace->EnvironmentTexture(); |
1086 | theWorkspace->SetEnvironmentTexture (Handle(OpenGl_Texture)()); |
1087 | |
c357e426 |
1088 | myTrihedron.Render (theWorkspace); |
83da37b1 |
1089 | |
1090 | // restore environment texture |
1091 | theWorkspace->SetEnvironmentTexture (anEnvironmentTexture); |
c357e426 |
1092 | } |
1093 | if (myToShowGradTrihedron) |
1094 | { |
1095 | myGraduatedTrihedron.Render (theWorkspace); |
1096 | } |
1097 | } |
1098 | |
1099 | // ======================================================================= |
1100 | // function : Invalidate |
1101 | // purpose : |
1102 | // ======================================================================= |
1103 | void OpenGl_View::Invalidate() |
1104 | { |
1105 | myBackBufferRestored = Standard_False; |
1106 | } |
1107 | |
1108 | //======================================================================= |
1109 | //function : renderScene |
1110 | //purpose : |
1111 | //======================================================================= |
bf02aa7d |
1112 | void OpenGl_View::renderScene (Graphic3d_Camera::Projection theProjection, |
1113 | OpenGl_FrameBuffer* theReadDrawFbo, |
1114 | const Standard_Boolean theToDrawImmediate) |
c357e426 |
1115 | { |
1116 | const Handle(OpenGl_Context)& aContext = myWorkspace->GetGlContext(); |
1117 | |
c357e426 |
1118 | // Specify clipping planes in view transformation space |
89a929ea |
1119 | aContext->ChangeClipping().RemoveAll (aContext); |
c357e426 |
1120 | if (!myClipPlanes.IsEmpty()) |
1121 | { |
1122 | Graphic3d_SequenceOfHClipPlane aUserPlanes; |
1123 | Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes); |
1124 | for (; aClippingIt.More(); aClippingIt.Next()) |
1125 | { |
1126 | const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value(); |
1127 | if (aClipPlane->IsOn()) |
1128 | { |
1129 | aUserPlanes.Append (aClipPlane); |
1130 | } |
1131 | } |
1132 | |
1133 | if (!aUserPlanes.IsEmpty()) |
1134 | { |
deb02f86 |
1135 | aContext->ChangeClipping().AddWorld (aContext, aUserPlanes); |
c357e426 |
1136 | } |
1137 | |
deb02f86 |
1138 | aContext->ShaderManager()->UpdateClippingState(); |
c357e426 |
1139 | } |
1140 | |
1141 | #if !defined(GL_ES_VERSION_2_0) |
1142 | // Apply Lights |
1143 | if (aContext->core11 != NULL) |
1144 | { |
1145 | // setup lights |
1146 | Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0], |
1147 | THE_DEFAULT_AMBIENT[1], |
1148 | THE_DEFAULT_AMBIENT[2], |
1149 | THE_DEFAULT_AMBIENT[3]); |
1150 | GLenum aLightGlId = GL_LIGHT0; |
1151 | |
016e5959 |
1152 | OpenGl_ListOfLight::Iterator aLightIt (myShadingModel == Graphic3d_TOSM_NONE ? myNoShadingLight : myLights); |
c357e426 |
1153 | for (; aLightIt.More(); aLightIt.Next()) |
1154 | { |
1155 | bindLight (aLightIt.Value(), aLightGlId, anAmbientColor, myWorkspace); |
1156 | } |
1157 | |
1158 | // apply accumulated ambient color |
1159 | anAmbientColor.a() = 1.0f; |
1160 | glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData()); |
1161 | |
1162 | if (aLightGlId != GL_LIGHT0) |
1163 | { |
1164 | glEnable (GL_LIGHTING); |
1165 | } |
1166 | // switch off unused lights |
1167 | for (; aLightGlId <= GL_LIGHT7; ++aLightGlId) |
1168 | { |
1169 | glDisable (aLightGlId); |
1170 | } |
1171 | } |
1172 | #endif |
1173 | |
1174 | // Clear status bitfields |
1175 | myWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO); |
1176 | |
83da37b1 |
1177 | // First pass |
1178 | renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate); |
1179 | myWorkspace->DisableTexture(); |
c357e426 |
1180 | |
83da37b1 |
1181 | // Second pass |
1182 | if (myWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) |
c357e426 |
1183 | { |
83da37b1 |
1184 | myWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO; |
c357e426 |
1185 | |
83da37b1 |
1186 | // Remember OpenGl properties |
1187 | GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA; |
1188 | GLint aSaveZbuffFunc; |
1189 | GLboolean aSaveZbuffWrite; |
1190 | glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite); |
1191 | glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc); |
1192 | #if !defined(GL_ES_VERSION_2_0) |
1193 | glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst); |
1194 | glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc); |
1195 | #endif |
1196 | GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST); |
1197 | GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND); |
c357e426 |
1198 | |
83da37b1 |
1199 | // Change the properties for second rendering pass |
1200 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
1201 | glEnable (GL_BLEND); |
c357e426 |
1202 | |
83da37b1 |
1203 | glDepthFunc (GL_EQUAL); |
1204 | glDepthMask (GL_FALSE); |
1205 | glEnable (GL_DEPTH_TEST); |
c357e426 |
1206 | |
83da37b1 |
1207 | // Render the view |
1208 | renderStructs (theProjection, theReadDrawFbo, theToDrawImmediate); |
1209 | myWorkspace->DisableTexture(); |
1210 | |
1211 | // Restore properties back |
1212 | glBlendFunc (aSaveBlendSrc, aSaveBlendDst); |
1213 | if (!wasBlendEnabled) |
1214 | glDisable (GL_BLEND); |
1215 | |
1216 | glDepthFunc (aSaveZbuffFunc); |
1217 | glDepthMask (aSaveZbuffWrite); |
1218 | if (!wasZbuffEnabled) |
1219 | glDisable (GL_DEPTH_FUNC); |
c357e426 |
1220 | } |
1221 | |
1222 | // Apply restored view matrix. |
1223 | aContext->ApplyWorldViewMatrix(); |
1224 | |
deb02f86 |
1225 | aContext->ChangeClipping().RemoveAll (aContext); |
1226 | if (!myClipPlanes.IsEmpty()) |
1227 | { |
1228 | aContext->ShaderManager()->RevertClippingState(); |
deb02f86 |
1229 | } |
c357e426 |
1230 | } |
1231 | |
1232 | // ======================================================================= |
1233 | // function : bindDefaultFbo |
1234 | // purpose : |
1235 | // ======================================================================= |
1236 | void OpenGl_View::bindDefaultFbo (OpenGl_FrameBuffer* theCustomFbo) |
1237 | { |
1238 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
1239 | OpenGl_FrameBuffer* anFbo = (theCustomFbo != NULL && theCustomFbo->IsValid()) |
1240 | ? theCustomFbo |
1241 | : (!aCtx->DefaultFrameBuffer().IsNull() |
1242 | && aCtx->DefaultFrameBuffer()->IsValid() |
1243 | ? aCtx->DefaultFrameBuffer().operator->() |
1244 | : NULL); |
1245 | if (anFbo != NULL) |
1246 | { |
1247 | anFbo->BindBuffer (aCtx); |
521b0d7f |
1248 | anFbo->SetupViewport (aCtx); |
c357e426 |
1249 | } |
1250 | else |
1251 | { |
1252 | #if !defined(GL_ES_VERSION_2_0) |
1253 | aCtx->SetReadDrawBuffer (GL_BACK); |
1254 | #else |
1255 | if (aCtx->arbFBO != NULL) |
1256 | { |
1257 | aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
1258 | } |
1259 | #endif |
521b0d7f |
1260 | aCtx->core11fwd->glViewport (0, 0, myWindow->Width(), myWindow->Height()); |
c357e426 |
1261 | } |
c357e426 |
1262 | } |
1263 | |
1264 | // ======================================================================= |
1265 | // function : initBlitQuad |
1266 | // purpose : |
1267 | // ======================================================================= |
1268 | OpenGl_VertexBuffer* OpenGl_View::initBlitQuad (const Standard_Boolean theToFlip) |
1269 | { |
1270 | OpenGl_VertexBuffer* aVerts = NULL; |
1271 | if (!theToFlip) |
1272 | { |
1273 | aVerts = &myFullScreenQuad; |
1274 | if (!aVerts->IsValid()) |
1275 | { |
1276 | OpenGl_Vec4 aQuad[4] = |
1277 | { |
1278 | OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 0.0f), |
1279 | OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 1.0f), |
1280 | OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 0.0f), |
1281 | OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 1.0f) |
1282 | }; |
1283 | aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData()); |
1284 | } |
1285 | } |
1286 | else |
1287 | { |
1288 | aVerts = &myFullScreenQuadFlip; |
1289 | if (!aVerts->IsValid()) |
1290 | { |
1291 | OpenGl_Vec4 aQuad[4] = |
1292 | { |
1293 | OpenGl_Vec4( 1.0f, -1.0f, 1.0f, 1.0f), |
1294 | OpenGl_Vec4( 1.0f, 1.0f, 1.0f, 0.0f), |
1295 | OpenGl_Vec4(-1.0f, -1.0f, 0.0f, 1.0f), |
1296 | OpenGl_Vec4(-1.0f, 1.0f, 0.0f, 0.0f) |
1297 | }; |
1298 | aVerts->Init (myWorkspace->GetGlContext(), 4, 4, aQuad[0].GetData()); |
1299 | } |
1300 | } |
1301 | return aVerts; |
1302 | } |
1303 | |
1304 | // ======================================================================= |
1305 | // function : blitBuffers |
1306 | // purpose : |
1307 | // ======================================================================= |
1308 | bool OpenGl_View::blitBuffers (OpenGl_FrameBuffer* theReadFbo, |
1309 | OpenGl_FrameBuffer* theDrawFbo, |
1310 | const Standard_Boolean theToFlip) |
1311 | { |
1312 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
1313 | if (theReadFbo == NULL || aCtx->IsFeedback()) |
1314 | { |
1315 | return false; |
1316 | } |
1317 | else if (theReadFbo == theDrawFbo) |
1318 | { |
1319 | return true; |
1320 | } |
1321 | |
1322 | // clear destination before blitting |
1323 | if (theDrawFbo != NULL |
1324 | && theDrawFbo->IsValid()) |
1325 | { |
1326 | theDrawFbo->BindBuffer (aCtx); |
1327 | } |
1328 | else |
1329 | { |
1330 | aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
1331 | } |
1332 | #if !defined(GL_ES_VERSION_2_0) |
1333 | aCtx->core20fwd->glClearDepth (1.0); |
1334 | #else |
1335 | aCtx->core20fwd->glClearDepthf (1.0f); |
1336 | #endif |
1337 | aCtx->core20fwd->glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
1338 | |
3c4b62a4 |
1339 | #if !defined(GL_ES_VERSION_2_0) |
1340 | if (aCtx->arbFBOBlit != NULL |
1341 | && theReadFbo->NbSamples() != 0) |
c357e426 |
1342 | { |
3c4b62a4 |
1343 | GLbitfield aCopyMask = 0; |
c357e426 |
1344 | theReadFbo->BindReadBuffer (aCtx); |
1345 | if (theDrawFbo != NULL |
1346 | && theDrawFbo->IsValid()) |
1347 | { |
1348 | theDrawFbo->BindDrawBuffer (aCtx); |
3c4b62a4 |
1349 | if (theDrawFbo->HasColor() |
1350 | && theReadFbo->HasColor()) |
1351 | { |
1352 | aCopyMask |= GL_COLOR_BUFFER_BIT; |
1353 | } |
1354 | if (theDrawFbo->HasDepth() |
1355 | && theReadFbo->HasDepth()) |
1356 | { |
1357 | aCopyMask |= GL_DEPTH_BUFFER_BIT; |
1358 | } |
c357e426 |
1359 | } |
1360 | else |
1361 | { |
3c4b62a4 |
1362 | if (theReadFbo->HasColor()) |
1363 | { |
1364 | aCopyMask |= GL_COLOR_BUFFER_BIT; |
1365 | } |
1366 | if (theReadFbo->HasDepth()) |
1367 | { |
1368 | aCopyMask |= GL_DEPTH_BUFFER_BIT; |
1369 | } |
c357e426 |
1370 | aCtx->arbFBO->glBindFramebuffer (GL_DRAW_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
1371 | } |
3c4b62a4 |
1372 | |
c357e426 |
1373 | // we don't copy stencil buffer here... does it matter for performance? |
1374 | aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(), |
1375 | 0, 0, theReadFbo->GetVPSizeX(), theReadFbo->GetVPSizeY(), |
3c4b62a4 |
1376 | aCopyMask, GL_NEAREST); |
7ccf8676 |
1377 | const int anErr = ::glGetError(); |
1378 | if (anErr != GL_NO_ERROR) |
1379 | { |
1380 | // glBlitFramebuffer() might fail in several cases: |
1381 | // - Both FBOs have MSAA and they are samples number does not match. |
1382 | // OCCT checks that this does not happen, |
1383 | // however some graphics drivers provide an option for overriding MSAA. |
1384 | // In this case window MSAA might be non-zero (and application can not check it) |
1385 | // and might not match MSAA of our offscreen FBOs. |
1386 | // - Pixel formats of FBOs do not match. |
1387 | // This also might happen with window has pixel format, |
1388 | // e.g. Mesa fails blitting RGBA8 -> RGB8 while other drivers support this conversion. |
1389 | TCollection_ExtendedString aMsg = TCollection_ExtendedString() + "FBO blitting has failed [Error #" + anErr + "]\n" |
1390 | + " Please check your graphics driver settings or try updating driver."; |
1391 | if (theReadFbo->NbSamples() != 0) |
1392 | { |
1393 | myToDisableMSAA = true; |
1394 | aMsg += "\n MSAA settings should not be overridden by driver!"; |
1395 | } |
1396 | aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1397 | GL_DEBUG_TYPE_ERROR, |
1398 | 0, |
1399 | GL_DEBUG_SEVERITY_HIGH, |
1400 | aMsg); |
1401 | } |
1402 | |
c357e426 |
1403 | if (theDrawFbo != NULL |
3c4b62a4 |
1404 | && theDrawFbo->IsValid()) |
c357e426 |
1405 | { |
1406 | theDrawFbo->BindBuffer (aCtx); |
1407 | } |
1408 | else |
1409 | { |
1410 | aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, OpenGl_FrameBuffer::NO_FRAMEBUFFER); |
1411 | } |
1412 | } |
1413 | else |
3c4b62a4 |
1414 | #endif |
c357e426 |
1415 | { |
1416 | aCtx->core20fwd->glDepthFunc (GL_ALWAYS); |
1417 | aCtx->core20fwd->glDepthMask (GL_TRUE); |
1418 | aCtx->core20fwd->glEnable (GL_DEPTH_TEST); |
1419 | |
1420 | myWorkspace->DisableTexture(); |
1421 | |
1422 | OpenGl_VertexBuffer* aVerts = initBlitQuad (theToFlip); |
1423 | const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager(); |
1424 | if (aVerts->IsValid() |
1425 | && aManager->BindFboBlitProgram()) |
1426 | { |
1427 | theReadFbo->ColorTexture() ->Bind (aCtx, GL_TEXTURE0 + 0); |
1428 | theReadFbo->DepthStencilTexture()->Bind (aCtx, GL_TEXTURE0 + 1); |
1429 | aVerts->BindVertexAttrib (aCtx, Graphic3d_TOA_POS); |
1430 | |
1431 | aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4); |
1432 | |
1433 | aVerts->UnbindVertexAttrib (aCtx, Graphic3d_TOA_POS); |
1434 | theReadFbo->DepthStencilTexture()->Unbind (aCtx, GL_TEXTURE0 + 1); |
1435 | theReadFbo->ColorTexture() ->Unbind (aCtx, GL_TEXTURE0 + 0); |
a521d90d |
1436 | aCtx->BindProgram (NULL); |
c357e426 |
1437 | } |
1438 | else |
1439 | { |
1440 | TCollection_ExtendedString aMsg = TCollection_ExtendedString() |
1441 | + "Error! FBO blitting has failed"; |
3b523c4c |
1442 | aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1443 | GL_DEBUG_TYPE_ERROR, |
c357e426 |
1444 | 0, |
3b523c4c |
1445 | GL_DEBUG_SEVERITY_HIGH, |
c357e426 |
1446 | aMsg); |
1447 | myHasFboBlit = Standard_False; |
1448 | theReadFbo->Release (aCtx.operator->()); |
1449 | return true; |
1450 | } |
1451 | } |
1452 | return true; |
1453 | } |
1454 | |
1455 | // ======================================================================= |
1456 | // function : drawStereoPair |
1457 | // purpose : |
1458 | // ======================================================================= |
3c4b62a4 |
1459 | void OpenGl_View::drawStereoPair (OpenGl_FrameBuffer* theDrawFbo) |
c357e426 |
1460 | { |
3c4b62a4 |
1461 | const Handle(OpenGl_Context)& aCtx = myWorkspace->GetGlContext(); |
1462 | bindDefaultFbo (theDrawFbo); |
c357e426 |
1463 | OpenGl_FrameBuffer* aPair[2] = |
1464 | { |
1465 | myImmediateSceneFbos[0]->IsValid() ? myImmediateSceneFbos[0].operator->() : NULL, |
1466 | myImmediateSceneFbos[1]->IsValid() ? myImmediateSceneFbos[1].operator->() : NULL |
1467 | }; |
1468 | if (aPair[0] == NULL |
1469 | || aPair[1] == NULL |
1470 | || !myTransientDrawToFront) |
1471 | { |
1472 | aPair[0] = myMainSceneFbos[0]->IsValid() ? myMainSceneFbos[0].operator->() : NULL; |
1473 | aPair[1] = myMainSceneFbos[1]->IsValid() ? myMainSceneFbos[1].operator->() : NULL; |
1474 | } |
1475 | |
1476 | if (aPair[0] == NULL |
1477 | || aPair[1] == NULL) |
1478 | { |
1479 | return; |
1480 | } |
1481 | |
3c4b62a4 |
1482 | if (aPair[0]->NbSamples() != 0) |
1483 | { |
1484 | // resolve MSAA buffers before drawing |
1485 | if (!myOpenGlFBO ->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, myFboDepthFormat, 0) |
1486 | || !myOpenGlFBO2->InitLazy (aCtx, aPair[0]->GetVPSizeX(), aPair[0]->GetVPSizeY(), myFboColorFormat, 0, 0)) |
1487 | { |
82f443b6 |
1488 | aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1489 | GL_DEBUG_TYPE_ERROR, |
3c4b62a4 |
1490 | 0, |
82f443b6 |
1491 | GL_DEBUG_SEVERITY_HIGH, |
3c4b62a4 |
1492 | "Error! Unable to allocate FBO for blitting stereo pair"); |
1493 | bindDefaultFbo (theDrawFbo); |
1494 | return; |
1495 | } |
1496 | |
1497 | if (!blitBuffers (aPair[0], myOpenGlFBO .operator->(), Standard_False) |
1498 | || !blitBuffers (aPair[1], myOpenGlFBO2.operator->(), Standard_False)) |
1499 | { |
1500 | bindDefaultFbo (theDrawFbo); |
1501 | return; |
1502 | } |
1503 | |
1504 | aPair[0] = myOpenGlFBO .operator->(); |
1505 | aPair[1] = myOpenGlFBO2.operator->(); |
1506 | bindDefaultFbo (theDrawFbo); |
1507 | } |
1508 | |
c357e426 |
1509 | struct |
1510 | { |
1511 | Standard_Integer left; |
1512 | Standard_Integer top; |
1513 | Standard_Integer right; |
1514 | Standard_Integer bottom; |
1515 | Standard_Integer dx() { return right - left; } |
1516 | Standard_Integer dy() { return bottom - top; } |
1517 | } aGeom; |
1518 | |
1519 | myWindow->PlatformWindow()->Position (aGeom.left, aGeom.top, aGeom.right, aGeom.bottom); |
1520 | |
1521 | Standard_Boolean toReverse = myRenderParams.ToReverseStereo; |
1522 | const Standard_Boolean isOddY = (aGeom.top + aGeom.dy()) % 2 == 1; |
1523 | const Standard_Boolean isOddX = aGeom.left % 2 == 1; |
1524 | if (isOddY |
1525 | && (myRenderParams.StereoMode == Graphic3d_StereoMode_RowInterlaced |
1526 | || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard)) |
1527 | { |
1528 | toReverse = !toReverse; |
1529 | } |
1530 | if (isOddX |
1531 | && (myRenderParams.StereoMode == Graphic3d_StereoMode_ColumnInterlaced |
1532 | || myRenderParams.StereoMode == Graphic3d_StereoMode_ChessBoard)) |
1533 | { |
1534 | toReverse = !toReverse; |
1535 | } |
1536 | |
1537 | if (toReverse) |
1538 | { |
1539 | std::swap (aPair[0], aPair[1]); |
1540 | } |
1541 | |
c357e426 |
1542 | aCtx->core20fwd->glDepthFunc (GL_ALWAYS); |
1543 | aCtx->core20fwd->glDepthMask (GL_TRUE); |
1544 | aCtx->core20fwd->glEnable (GL_DEPTH_TEST); |
1545 | |
1546 | myWorkspace->DisableTexture(); |
1547 | OpenGl_VertexBuffer* aVerts = initBlitQuad (myToFlipOutput); |
1548 | |
1549 | const Handle(OpenGl_ShaderManager)& aManager = aCtx->ShaderManager(); |
1550 | if (aVerts->IsValid() |
1551 | && aManager->BindStereoProgram (myRenderParams.StereoMode)) |
1552 | { |
1553 | if (myRenderParams.StereoMode == Graphic3d_StereoMode_Anaglyph) |
1554 | { |
1555 | OpenGl_Mat4 aFilterL, aFilterR; |
1556 | aFilterL.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f)); |
1557 | aFilterR.SetDiagonal (Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f)); |
1558 | switch (myRenderParams.AnaglyphFilter) |
1559 | { |
1560 | case Graphic3d_RenderingParams::Anaglyph_RedCyan_Simple: |
1561 | { |
1562 | aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f)); |
1563 | aFilterR.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f)); |
1564 | aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f)); |
1565 | break; |
1566 | } |
1567 | case Graphic3d_RenderingParams::Anaglyph_RedCyan_Optimized: |
1568 | { |
1569 | aFilterL.SetRow (0, Graphic3d_Vec4 ( 0.4154f, 0.4710f, 0.16666667f, 0.0f)); |
1570 | aFilterL.SetRow (1, Graphic3d_Vec4 (-0.0458f, -0.0484f, -0.0257f, 0.0f)); |
1571 | aFilterL.SetRow (2, Graphic3d_Vec4 (-0.0547f, -0.0615f, 0.0128f, 0.0f)); |
1572 | aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); |
1573 | aFilterR.SetRow (0, Graphic3d_Vec4 (-0.01090909f, -0.03636364f, -0.00606061f, 0.0f)); |
1574 | aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.37560000f, 0.73333333f, 0.01111111f, 0.0f)); |
1575 | aFilterR.SetRow (2, Graphic3d_Vec4 (-0.06510000f, -0.12870000f, 1.29710000f, 0.0f)); |
1576 | aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); |
1577 | break; |
1578 | } |
1579 | case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Simple: |
1580 | { |
1581 | aFilterL.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f)); |
1582 | aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f)); |
1583 | aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f)); |
1584 | break; |
1585 | } |
1586 | case Graphic3d_RenderingParams::Anaglyph_YellowBlue_Optimized: |
1587 | { |
1588 | aFilterL.SetRow (0, Graphic3d_Vec4 ( 1.062f, -0.205f, 0.299f, 0.0f)); |
1589 | aFilterL.SetRow (1, Graphic3d_Vec4 (-0.026f, 0.908f, 0.068f, 0.0f)); |
1590 | aFilterL.SetRow (2, Graphic3d_Vec4 (-0.038f, -0.173f, 0.022f, 0.0f)); |
1591 | aFilterL.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); |
1592 | aFilterR.SetRow (0, Graphic3d_Vec4 (-0.016f, -0.123f, -0.017f, 0.0f)); |
1593 | aFilterR.SetRow (1, Graphic3d_Vec4 ( 0.006f, 0.062f, -0.017f, 0.0f)); |
1594 | aFilterR.SetRow (2, Graphic3d_Vec4 ( 0.094f, 0.185f, 0.911f, 0.0f)); |
1595 | aFilterR.SetRow (3, Graphic3d_Vec4 ( 0.0f, 0.0f, 0.0f, 0.0f)); |
1596 | break; |
1597 | } |
1598 | case Graphic3d_RenderingParams::Anaglyph_GreenMagenta_Simple: |
1599 | { |
1600 | aFilterR.SetRow (0, Graphic3d_Vec4 (1.0f, 0.0f, 0.0f, 0.0f)); |
1601 | aFilterL.SetRow (1, Graphic3d_Vec4 (0.0f, 1.0f, 0.0f, 0.0f)); |
1602 | aFilterR.SetRow (2, Graphic3d_Vec4 (0.0f, 0.0f, 1.0f, 0.0f)); |
1603 | break; |
1604 | } |
1605 | case Graphic3d_RenderingParams::Anaglyph_UserDefined: |
1606 | { |
1607 | aFilterL = myRenderParams.AnaglyphLeft; |
1608 | aFilterR = myRenderParams.AnaglyphRight; |
1609 | break; |
1610 | } |
1611 | } |
1612 | aCtx->ActiveProgram()->SetUniform (aCtx, "uMultL", aFilterL); |
1613 | aCtx->ActiveProgram()->SetUniform (aCtx, "uMultR", aFilterR); |
1614 | } |
1615 | |
1616 | aPair[0]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 0); |
1617 | aPair[1]->ColorTexture()->Bind (aCtx, GL_TEXTURE0 + 1); |
1618 | aVerts->BindVertexAttrib (aCtx, 0); |
1619 | |
1620 | aCtx->core20fwd->glDrawArrays (GL_TRIANGLE_STRIP, 0, 4); |
1621 | |
1622 | aVerts->UnbindVertexAttrib (aCtx, 0); |
1623 | aPair[1]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 1); |
1624 | aPair[0]->ColorTexture()->Unbind (aCtx, GL_TEXTURE0 + 0); |
1625 | } |
1626 | else |
1627 | { |
1628 | TCollection_ExtendedString aMsg = TCollection_ExtendedString() |
1629 | + "Error! Anaglyph has failed"; |
3b523c4c |
1630 | aCtx->PushMessage (GL_DEBUG_SOURCE_APPLICATION, |
1631 | GL_DEBUG_TYPE_ERROR, |
c357e426 |
1632 | 0, |
3b523c4c |
1633 | GL_DEBUG_SEVERITY_HIGH, |
c357e426 |
1634 | aMsg); |
1635 | } |
1636 | } |
1637 | |
1638 | // ======================================================================= |
1639 | // function : copyBackToFront |
1640 | // purpose : |
1641 | // ======================================================================= |
1642 | void OpenGl_View::copyBackToFront() |
1643 | { |
1644 | #if !defined(GL_ES_VERSION_2_0) |
1645 | |
1646 | OpenGl_Mat4 aProjectMat; |
1647 | Graphic3d_TransformUtils::Ortho2D (aProjectMat, |
1648 | 0.f, static_cast<GLfloat> (myWindow->Width()), 0.f, static_cast<GLfloat> (myWindow->Height())); |
1649 | |
1650 | Handle(OpenGl_Context) aCtx = myWorkspace->GetGlContext(); |
1651 | aCtx->WorldViewState.Push(); |
1652 | aCtx->ProjectionState.Push(); |
1653 | |
1654 | aCtx->WorldViewState.SetIdentity(); |
1655 | aCtx->ProjectionState.SetCurrent (aProjectMat); |
1656 | |
1657 | aCtx->ApplyProjectionMatrix(); |
1658 | aCtx->ApplyWorldViewMatrix(); |
1659 | |
1660 | aCtx->DisableFeatures(); |
1661 | |
1662 | switch (aCtx->DrawBuffer()) |
1663 | { |
1664 | case GL_BACK_LEFT: |
1665 | { |
1666 | aCtx->SetReadBuffer (GL_BACK_LEFT); |
1667 | aCtx->SetDrawBuffer (GL_FRONT_LEFT); |
1668 | break; |
1669 | } |
1670 | case GL_BACK_RIGHT: |
1671 | { |
1672 | aCtx->SetReadBuffer (GL_BACK_RIGHT); |
1673 | aCtx->SetDrawBuffer (GL_FRONT_RIGHT); |
1674 | break; |
1675 | } |
1676 | default: |
1677 | { |
1678 | aCtx->SetReadBuffer (GL_BACK); |
1679 | aCtx->SetDrawBuffer (GL_FRONT); |
1680 | break; |
1681 | } |
1682 | } |
1683 | |
1684 | glRasterPos2i (0, 0); |
1685 | glCopyPixels (0, 0, myWindow->Width() + 1, myWindow->Height() + 1, GL_COLOR); |
1686 | //glCopyPixels (0, 0, myWidth + 1, myHeight + 1, GL_DEPTH); |
1687 | |
1688 | aCtx->EnableFeatures(); |
1689 | |
1690 | aCtx->WorldViewState.Pop(); |
1691 | aCtx->ProjectionState.Pop(); |
1692 | aCtx->ApplyProjectionMatrix(); |
1693 | |
1694 | // read/write from front buffer now |
1695 | aCtx->SetReadBuffer (aCtx->DrawBuffer()); |
1696 | #endif |
1697 | myIsImmediateDrawn = Standard_False; |
1698 | } |