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 | |
2166f0fa SK |
16 | #include <stdio.h> |
17 | #include <stdlib.h> | |
18 | ||
5f8b738e | 19 | #include <OpenGl_GlCore11.hxx> |
2166f0fa | 20 | #include <OpenGl_tgl_funcs.hxx> |
2166f0fa | 21 | |
0b0320e7 | 22 | #include <Graphic3d_TextureParams.hxx> |
23 | #include <Graphic3d_Texture2Dmanual.hxx> | |
c04c30b3 | 24 | #include <Graphic3d_GraphicDriver.hxx> |
3c3131a0 | 25 | #include <Image_AlienPixMap.hxx> |
2166f0fa SK |
26 | #include <Visual3d_Layer.hxx> |
27 | ||
30f0ad28 | 28 | #include <NCollection_Mat4.hxx> |
29 | ||
2166f0fa | 30 | #include <OpenGl_AspectLine.hxx> |
30f0ad28 | 31 | #include <OpenGl_Context.hxx> |
30f0ad28 | 32 | #include <OpenGl_Matrix.hxx> |
2166f0fa SK |
33 | #include <OpenGl_Workspace.hxx> |
34 | #include <OpenGl_View.hxx> | |
35 | #include <OpenGl_Trihedron.hxx> | |
36 | #include <OpenGl_GraduatedTrihedron.hxx> | |
0b0320e7 | 37 | #include <OpenGl_PrimitiveArray.hxx> |
2166f0fa | 38 | #include <OpenGl_PrinterContext.hxx> |
30f0ad28 | 39 | #include <OpenGl_ShaderManager.hxx> |
40 | #include <OpenGl_ShaderProgram.hxx> | |
59f45b7c | 41 | #include <OpenGl_Structure.hxx> |
91c60b57 | 42 | #include <OpenGl_ArbFBO.hxx> |
2166f0fa | 43 | |
2166f0fa SK |
44 | #define EPSI 0.0001 |
45 | ||
12381341 | 46 | namespace |
47 | { | |
48 | ||
49 | static const GLfloat THE_DEFAULT_AMBIENT[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; | |
50 | static const GLfloat THE_DEFAULT_SPOT_DIR[3] = { 0.0f, 0.0f, -1.0f }; | |
51 | static const GLfloat THE_DEFAULT_SPOT_EXPONENT = 0.0f; | |
52 | static const GLfloat THE_DEFAULT_SPOT_CUTOFF = 180.0f; | |
53 | ||
54 | }; | |
2166f0fa | 55 | |
30f0ad28 | 56 | extern void InitLayerProp (const int theListId); //szvgl: defined in OpenGl_GraphicDriver_Layer.cxx |
2166f0fa | 57 | |
2166f0fa SK |
58 | /*----------------------------------------------------------------------*/ |
59 | /* | |
60 | * Fonctions privees | |
61 | */ | |
62 | ||
ca3c13d1 | 63 | #if !defined(GL_ES_VERSION_2_0) |
2166f0fa SK |
64 | /*-----------------------------------------------------------------*/ |
65 | /* | |
66 | * Set des lumieres | |
67 | */ | |
c827ea3a | 68 | static void bindLight (const OpenGl_Light& theLight, |
69 | GLenum& theLightGlId, | |
70 | Graphic3d_Vec4& theAmbientColor, | |
71 | const Handle(OpenGl_Workspace)& theWorkspace) | |
2166f0fa SK |
72 | { |
73 | // Only 8 lights in OpenGL... | |
12381341 | 74 | if (theLightGlId > GL_LIGHT7) |
2166f0fa | 75 | { |
12381341 | 76 | return; |
2166f0fa SK |
77 | } |
78 | ||
12381341 | 79 | if (theLight.Type == Visual3d_TOLS_AMBIENT) |
2166f0fa | 80 | { |
4fe9ad57 | 81 | // add RGBA intensity of the ambient light |
82 | theAmbientColor += theLight.Color; | |
12381341 | 83 | return; |
84 | } | |
2166f0fa | 85 | |
c827ea3a | 86 | const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext(); |
87 | ||
12381341 | 88 | // the light is a headlight? |
12381341 | 89 | if (theLight.IsHeadlight) |
90 | { | |
c827ea3a | 91 | |
92 | aContext->WorldViewState.Push(); | |
93 | aContext->WorldViewState.SetIdentity(); | |
94 | ||
95 | aContext->ApplyWorldViewMatrix(); | |
2166f0fa SK |
96 | } |
97 | ||
12381341 | 98 | // setup light type |
99 | switch (theLight.Type) | |
3c3131a0 | 100 | { |
12381341 | 101 | case Visual3d_TOLS_DIRECTIONAL: |
102 | { | |
103 | // if the last parameter of GL_POSITION, is zero, the corresponding light source is a Directional one | |
104 | const OpenGl_Vec4 anInfDir = -theLight.Direction; | |
105 | ||
106 | // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE. | |
107 | glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT); | |
108 | glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData()); | |
109 | glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData()); | |
110 | glLightfv (theLightGlId, GL_POSITION, anInfDir.GetData()); | |
111 | glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR); | |
112 | glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT); | |
113 | glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF); | |
114 | break; | |
115 | } | |
116 | case Visual3d_TOLS_POSITIONAL: | |
117 | { | |
118 | // to create a realistic effect, set the GL_SPECULAR parameter to the same value as the GL_DIFFUSE | |
119 | glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT); | |
120 | glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData()); | |
121 | glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData()); | |
122 | glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData()); | |
123 | glLightfv (theLightGlId, GL_SPOT_DIRECTION, THE_DEFAULT_SPOT_DIR); | |
124 | glLightf (theLightGlId, GL_SPOT_EXPONENT, THE_DEFAULT_SPOT_EXPONENT); | |
125 | glLightf (theLightGlId, GL_SPOT_CUTOFF, THE_DEFAULT_SPOT_CUTOFF); | |
126 | glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation()); | |
127 | glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation()); | |
128 | glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0); | |
129 | break; | |
130 | } | |
131 | case Visual3d_TOLS_SPOT: | |
132 | { | |
133 | glLightfv (theLightGlId, GL_AMBIENT, THE_DEFAULT_AMBIENT); | |
134 | glLightfv (theLightGlId, GL_DIFFUSE, theLight.Color.GetData()); | |
135 | glLightfv (theLightGlId, GL_SPECULAR, theLight.Color.GetData()); | |
136 | glLightfv (theLightGlId, GL_POSITION, theLight.Position.GetData()); | |
137 | glLightfv (theLightGlId, GL_SPOT_DIRECTION, theLight.Direction.GetData()); | |
138 | glLightf (theLightGlId, GL_SPOT_EXPONENT, theLight.Concentration() * 128.0f); | |
139 | glLightf (theLightGlId, GL_SPOT_CUTOFF, (theLight.Angle() * 180.0f) / GLfloat(M_PI)); | |
140 | glLightf (theLightGlId, GL_CONSTANT_ATTENUATION, theLight.ConstAttenuation()); | |
141 | glLightf (theLightGlId, GL_LINEAR_ATTENUATION, theLight.LinearAttenuation()); | |
142 | glLightf (theLightGlId, GL_QUADRATIC_ATTENUATION, 0.0f); | |
143 | break; | |
144 | } | |
2166f0fa SK |
145 | } |
146 | ||
12381341 | 147 | // restore matrix in case of headlight |
148 | if (theLight.IsHeadlight) | |
2166f0fa | 149 | { |
c827ea3a | 150 | aContext->WorldViewState.Pop(); |
2166f0fa | 151 | } |
12381341 | 152 | |
153 | glEnable (theLightGlId++); | |
2166f0fa | 154 | } |
ca3c13d1 | 155 | #endif |
2166f0fa | 156 | |
2166f0fa SK |
157 | /*----------------------------------------------------------------------*/ |
158 | ||
0b0320e7 | 159 | void OpenGl_View::DrawBackground (const Handle(OpenGl_Workspace)& theWorkspace) |
2166f0fa | 160 | { |
0b0320e7 | 161 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); |
2166f0fa | 162 | |
0b0320e7 | 163 | if ((theWorkspace->NamedStatus & OPENGL_NS_WHITEBACK) != 0 // no background |
164 | || (!myBgTextureArray->IsDefined() // no texture | |
165 | && !myBgGradientArray->IsDefined())) // no gradient | |
166 | { | |
167 | return; | |
168 | } | |
2166f0fa | 169 | |
eae454e3 | 170 | const Standard_Boolean wasUsedZBuffer = theWorkspace->SetUseZBuffer (Standard_False); |
171 | if (wasUsedZBuffer) | |
172 | { | |
173 | aCtx->core11fwd->glDisable (GL_DEPTH_TEST); | |
174 | } | |
c827ea3a | 175 | |
0b0320e7 | 176 | aCtx->WorldViewState.Push(); |
177 | aCtx->ProjectionState.Push(); | |
178 | aCtx->WorldViewState.SetIdentity(); | |
179 | aCtx->ProjectionState.SetIdentity(); | |
180 | aCtx->ApplyProjectionMatrix(); | |
181 | aCtx->ApplyWorldViewMatrix(); | |
c827ea3a | 182 | |
0b0320e7 | 183 | // Drawing background gradient if: |
184 | // - gradient fill type is not Aspect_GFM_NONE and | |
185 | // - either background texture is no specified or it is drawn in Aspect_FM_CENTERED mode | |
186 | if (myBgGradientArray->IsDefined() | |
d5846489 | 187 | && (!myTextureParams->DoTextureMap() |
0b0320e7 | 188 | || myBgTextureArray->TextureFillMethod() == Aspect_FM_CENTERED |
189 | || myBgTextureArray->TextureFillMethod() == Aspect_FM_NONE)) | |
190 | { | |
191 | #if !defined(GL_ES_VERSION_2_0) | |
192 | GLint aShadingModelOld = GL_SMOOTH; | |
193 | if (aCtx->core11 != NULL) | |
194 | { | |
195 | aCtx->core11fwd->glDisable (GL_LIGHTING); | |
196 | aCtx->core11fwd->glGetIntegerv (GL_SHADE_MODEL, &aShadingModelOld); | |
197 | aCtx->core11->glShadeModel (GL_SMOOTH); | |
198 | } | |
199 | #endif | |
c827ea3a | 200 | |
0b0320e7 | 201 | if (myBgGradientArray->IsDataChanged()) |
202 | { | |
203 | myBgGradientArray->Init (theWorkspace); | |
204 | } | |
2166f0fa | 205 | |
0b0320e7 | 206 | myBgGradientArray->Render (theWorkspace); |
2166f0fa | 207 | |
0b0320e7 | 208 | #if !defined(GL_ES_VERSION_2_0) |
209 | if (aCtx->core11 != NULL) | |
2166f0fa | 210 | { |
0b0320e7 | 211 | aCtx->core11->glShadeModel (aShadingModelOld); |
212 | } | |
213 | #endif | |
214 | } | |
2166f0fa | 215 | |
0b0320e7 | 216 | // Drawing background image if it is defined |
217 | // (texture is defined and fill type is not Aspect_FM_NONE) | |
218 | if (myBgTextureArray->IsDefined() | |
219 | && myTextureParams->DoTextureMap()) | |
220 | { | |
221 | aCtx->core11fwd->glDisable (GL_BLEND); | |
2166f0fa | 222 | |
0b0320e7 | 223 | const OpenGl_AspectFace* anOldAspectFace = theWorkspace->SetAspectFace (myTextureParams); |
2166f0fa | 224 | |
0b0320e7 | 225 | if (myBgTextureArray->IsDataChanged() |
226 | || myBgTextureArray->IsViewSizeChanged (theWorkspace)) | |
f8b2ed36 | 227 | { |
0b0320e7 | 228 | myBgTextureArray->Init (theWorkspace); |
f8b2ed36 | 229 | } |
2166f0fa | 230 | |
0b0320e7 | 231 | myBgTextureArray->Render (theWorkspace); |
c827ea3a | 232 | |
0b0320e7 | 233 | // restore aspects |
234 | theWorkspace->SetAspectFace (anOldAspectFace); | |
235 | } | |
2166f0fa | 236 | |
0b0320e7 | 237 | aCtx->WorldViewState.Pop(); |
238 | aCtx->ProjectionState.Pop(); | |
239 | aCtx->ApplyProjectionMatrix(); | |
699239e4 | 240 | aCtx->ApplyWorldViewMatrix(); |
2166f0fa | 241 | |
eae454e3 | 242 | if (wasUsedZBuffer) |
0b0320e7 | 243 | { |
eae454e3 | 244 | theWorkspace->SetUseZBuffer (Standard_True); |
0b0320e7 | 245 | aCtx->core11fwd->glEnable (GL_DEPTH_TEST); |
2166f0fa | 246 | } |
e276548b | 247 | } |
248 | ||
249 | /*----------------------------------------------------------------------*/ | |
250 | ||
251 | //call_func_redraw_all_structs_proc | |
252 | void OpenGl_View::Render (const Handle(OpenGl_PrinterContext)& thePrintContext, | |
a1954302 | 253 | const Handle(OpenGl_Workspace)& theWorkspace, |
38a0206f | 254 | OpenGl_FrameBuffer* theOutputFBO, |
255 | Graphic3d_Camera::Projection theProjection, | |
a1954302 | 256 | const Graphic3d_CView& theCView, |
257 | const Aspect_CLayer2d& theCUnderLayer, | |
258 | const Aspect_CLayer2d& theCOverLayer, | |
259 | const Standard_Boolean theToDrawImmediate) | |
e276548b | 260 | { |
b5ac8292 | 261 | // ================================== |
262 | // Step 1: Prepare for redraw | |
263 | // ================================== | |
264 | ||
265 | const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext(); | |
266 | ||
ca3c13d1 | 267 | #if !defined(GL_ES_VERSION_2_0) |
42f8ba56 | 268 | // Disable current clipping planes |
8625ef7e | 269 | if (aContext->core11 != NULL) |
e276548b | 270 | { |
42f8ba56 | 271 | const Standard_Integer aMaxPlanes = aContext->MaxClipPlanes(); |
272 | for (Standard_Integer aClipPlaneId = GL_CLIP_PLANE0; aClipPlaneId < GL_CLIP_PLANE0 + aMaxPlanes; ++aClipPlaneId) | |
e276548b | 273 | { |
42f8ba56 | 274 | aContext->core11fwd->glDisable (aClipPlaneId); |
e276548b | 275 | } |
276 | } | |
ca3c13d1 | 277 | #endif |
e276548b | 278 | |
c827ea3a | 279 | // Update states of OpenGl_BVHTreeSelector (frustum culling algorithm). |
b7cd4ba7 | 280 | Standard_Boolean isProjectionMatUpdateNeeded = Standard_False; |
281 | Standard_Boolean isOrientationMatUpdateNeeded = Standard_False; | |
282 | if (myBVHSelector.ProjectionState() != myCamera->ProjectionState()) | |
283 | { | |
284 | isProjectionMatUpdateNeeded = Standard_True; | |
285 | myBVHSelector.ChangeProjectionState() = myCamera->ProjectionState(); | |
286 | } | |
287 | if (myBVHSelector.ModelViewState() != myCamera->ModelViewState()) | |
288 | { | |
289 | isOrientationMatUpdateNeeded = Standard_True; | |
290 | myBVHSelector.ChangeModelViewState() = myCamera->ModelViewState(); | |
291 | } | |
292 | ||
c827ea3a | 293 | if (isProjectionMatUpdateNeeded |
294 | || isOrientationMatUpdateNeeded) | |
295 | { | |
296 | myBVHSelector.SetViewVolume (myCamera); | |
297 | } | |
298 | ||
b86bb3df | 299 | const Handle(OpenGl_ShaderManager)& aManager = aContext->ShaderManager(); |
300 | const Standard_Boolean isSameView = aManager->IsSameView (this); // force camera state update when needed | |
256f9ac0 | 301 | if (StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()) != myLastLightSourceState) |
392ac980 | 302 | { |
256f9ac0 | 303 | aManager->UpdateLightSourceStateTo (&myLights); |
304 | myLastLightSourceState = StateInfo (myCurrLightSourceState, aManager->LightSourceState().Index()); | |
305 | } | |
b5ac8292 | 306 | |
256f9ac0 | 307 | if (myProjectionState != myCamera->ProjectionState() |
308 | || !isSameView) | |
309 | { | |
310 | myProjectionState = myCamera->ProjectionState(); | |
c827ea3a | 311 | aContext->ProjectionState.SetCurrent (myCamera->ProjectionMatrixF()); |
312 | aContext->ApplyProjectionMatrix(); | |
256f9ac0 | 313 | } |
b5ac8292 | 314 | |
256f9ac0 | 315 | if (myModelViewState != myCamera->ModelViewState() |
316 | || !isSameView) | |
317 | { | |
318 | myModelViewState = myCamera->ModelViewState(); | |
c827ea3a | 319 | aContext->WorldViewState.SetCurrent (myCamera->OrientationMatrixF()); |
320 | aContext->ApplyWorldViewMatrix(); | |
256f9ac0 | 321 | } |
b5ac8292 | 322 | |
256f9ac0 | 323 | if (aManager->ModelWorldState().Index() == 0) |
324 | { | |
c827ea3a | 325 | aContext->ShaderManager()->UpdateModelWorldStateTo (OpenGl_Mat4()); |
b7cd4ba7 | 326 | } |
327 | ||
b5ac8292 | 328 | // ==================================== |
329 | // Step 2: Redraw background | |
330 | // ==================================== | |
e276548b | 331 | |
332 | // Render background | |
91c60b57 | 333 | if (!theToDrawImmediate) |
a89742cf | 334 | { |
0b0320e7 | 335 | DrawBackground (theWorkspace); |
a89742cf | 336 | } |
2166f0fa | 337 | |
ca3c13d1 | 338 | #if !defined(GL_ES_VERSION_2_0) |
2166f0fa | 339 | // Switch off lighting by default |
4e1523ef | 340 | if (aContext->core11 != NULL) |
341 | { | |
342 | glDisable(GL_LIGHTING); | |
343 | } | |
ca3c13d1 | 344 | #endif |
2166f0fa | 345 | |
b5ac8292 | 346 | // ================================= |
347 | // Step 3: Draw underlayer | |
348 | // ================================= | |
a1954302 | 349 | if (!theToDrawImmediate) |
350 | { | |
351 | RedrawLayer2d (thePrintContext, theWorkspace, theCView, theCUnderLayer); | |
352 | } | |
2166f0fa | 353 | |
b5ac8292 | 354 | // ================================= |
355 | // Step 4: Redraw main plane | |
356 | // ================================= | |
2166f0fa SK |
357 | |
358 | // Setup face culling | |
359 | GLboolean isCullFace = GL_FALSE; | |
360 | if ( myBackfacing ) | |
361 | { | |
362 | isCullFace = glIsEnabled( GL_CULL_FACE ); | |
363 | if ( myBackfacing < 0 ) | |
364 | { | |
365 | glEnable( GL_CULL_FACE ); | |
366 | glCullFace( GL_BACK ); | |
367 | } | |
b5ac8292 | 368 | else |
2166f0fa SK |
369 | glDisable( GL_CULL_FACE ); |
370 | } | |
371 | ||
ca3c13d1 | 372 | #if !defined(GL_ES_VERSION_2_0) |
b5ac8292 | 373 | // if the view is scaled normal vectors are scaled to unit |
374 | // length for correct displaying of shaded objects | |
375 | const gp_Pnt anAxialScale = myCamera->AxialScale(); | |
7d9e854b | 376 | if (anAxialScale.X() != 1.F || |
377 | anAxialScale.Y() != 1.F || | |
378 | anAxialScale.Z() != 1.F) | |
379 | { | |
380 | aContext->SetGlNormalizeEnabled (Standard_True); | |
381 | } | |
382 | else | |
383 | { | |
384 | aContext->SetGlNormalizeEnabled (Standard_False); | |
385 | } | |
2166f0fa | 386 | |
2166f0fa | 387 | // Apply Fog |
4e1523ef | 388 | if (myFog.IsOn |
389 | && aContext->core11 != NULL) | |
2166f0fa | 390 | { |
b5ac8292 | 391 | Standard_Real aFogFrontConverted = (Standard_Real )myFog.Front + myCamera->Distance(); |
392 | if (myCamera->ZFar() < aFogFrontConverted) | |
2166f0fa | 393 | { |
b5ac8292 | 394 | aFogFrontConverted = myCamera->ZFar(); |
395 | myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance()); | |
12381341 | 396 | } |
4fe9ad57 | 397 | |
b5ac8292 | 398 | Standard_Real aFogBackConverted = (Standard_Real )myFog.Back + myCamera->Distance(); |
399 | if (myCamera->ZFar() < aFogFrontConverted) | |
12381341 | 400 | { |
b5ac8292 | 401 | aFogBackConverted = myCamera->ZFar(); |
402 | myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance()); | |
12381341 | 403 | } |
b5ac8292 | 404 | |
405 | if (aFogFrontConverted > aFogBackConverted) | |
12381341 | 406 | { |
b5ac8292 | 407 | myFog.Front = (Standard_ShortReal )(aFogFrontConverted - myCamera->Distance()); |
408 | myFog.Back = (Standard_ShortReal )(aFogBackConverted - myCamera->Distance()); | |
ca3c13d1 | 409 | } |
b5ac8292 | 410 | |
411 | glFogi(GL_FOG_MODE, GL_LINEAR); | |
412 | glFogf(GL_FOG_START, (Standard_ShortReal )aFogFrontConverted); | |
413 | glFogf(GL_FOG_END, (Standard_ShortReal )aFogBackConverted); | |
414 | glFogfv(GL_FOG_COLOR, myFog.Color.rgb); | |
415 | glEnable(GL_FOG); | |
2166f0fa | 416 | } |
4e1523ef | 417 | else if (aContext->core11 != NULL) |
418 | { | |
419 | glDisable (GL_FOG); | |
420 | } | |
2166f0fa SK |
421 | |
422 | // Apply InteriorShadingMethod | |
4e1523ef | 423 | if (aContext->core11 != NULL) |
424 | { | |
425 | aContext->core11->glShadeModel (myShadingModel == Visual3d_TOM_FACET | |
426 | || myShadingModel == Visual3d_TOM_NONE ? GL_FLAT : GL_SMOOTH); | |
427 | } | |
ca3c13d1 | 428 | #endif |
2166f0fa | 429 | |
8625ef7e | 430 | aManager->SetShadingModel (myShadingModel); |
431 | ||
b5ac8292 | 432 | // Apply AntiAliasing |
433 | if (myAntiAliasing) | |
434 | theWorkspace->NamedStatus |= OPENGL_NS_ANTIALIASING; | |
435 | else | |
436 | theWorkspace->NamedStatus &= ~OPENGL_NS_ANTIALIASING; | |
437 | ||
438 | if (!aManager->IsEmpty()) | |
2166f0fa | 439 | { |
b5ac8292 | 440 | aManager->UpdateClippingState(); |
441 | } | |
2166f0fa | 442 | |
b5ac8292 | 443 | // Redraw 3d scene |
38a0206f | 444 | if (theProjection == Graphic3d_Camera::Projection_MonoLeftEye) |
b5ac8292 | 445 | { |
c827ea3a | 446 | aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoLeftF()); |
447 | aContext->ApplyProjectionMatrix(); | |
38a0206f | 448 | } |
449 | else if (theProjection == Graphic3d_Camera::Projection_MonoRightEye) | |
450 | { | |
c827ea3a | 451 | aContext->ProjectionState.SetCurrent (myCamera->ProjectionStereoRightF()); |
452 | aContext->ApplyProjectionMatrix(); | |
2166f0fa | 453 | } |
38a0206f | 454 | RedrawScene (thePrintContext, theWorkspace, theOutputFBO, theCView, theToDrawImmediate); |
2166f0fa | 455 | |
b5ac8292 | 456 | // =============================== |
457 | // Step 5: Trihedron | |
458 | // =============================== | |
2166f0fa | 459 | |
26395493 | 460 | // Resetting GL parameters according to the default aspects |
461 | // in order to synchronize GL state with the graphic driver state | |
462 | // before drawing auxiliary stuff (trihedrons, overlayer) | |
463 | // and invoking optional callbacks | |
b5ac8292 | 464 | theWorkspace->ResetAppliedAspect(); |
2166f0fa | 465 | |
b859a34d | 466 | aContext->ChangeClipping().RemoveAll(); |
2166f0fa | 467 | |
392ac980 | 468 | if (!aManager->IsEmpty()) |
30f0ad28 | 469 | { |
392ac980 | 470 | aManager->ResetMaterialStates(); |
471 | aManager->RevertClippingState(); | |
472 | ||
473 | // We need to disable (unbind) all shaders programs to ensure | |
474 | // that all objects without specified aspect will be drawn | |
475 | // correctly (such as background) | |
7d3e64ef | 476 | aContext->BindProgram (NULL); |
30f0ad28 | 477 | } |
478 | ||
a89742cf | 479 | // Render trihedron |
91c60b57 | 480 | if (!theToDrawImmediate) |
a174a3c5 | 481 | { |
a89742cf | 482 | RedrawTrihedron (theWorkspace); |
2166f0fa | 483 | |
a89742cf | 484 | // Restore face culling |
485 | if ( myBackfacing ) | |
2166f0fa | 486 | { |
a89742cf | 487 | if ( isCullFace ) |
488 | { | |
489 | glEnable ( GL_CULL_FACE ); | |
490 | glCullFace ( GL_BACK ); | |
491 | } | |
492 | else | |
493 | glDisable ( GL_CULL_FACE ); | |
2166f0fa | 494 | } |
2166f0fa SK |
495 | } |
496 | ||
b5ac8292 | 497 | // =============================== |
498 | // Step 6: Redraw overlay | |
499 | // =============================== | |
a1954302 | 500 | if (!theToDrawImmediate) |
501 | { | |
502 | const int aMode = 0; | |
503 | theWorkspace->DisplayCallback (theCView, (aMode | OCC_PRE_OVERLAY)); | |
b5ac8292 | 504 | |
a1954302 | 505 | RedrawLayer2d (thePrintContext, theWorkspace, theCView, theCOverLayer); |
2166f0fa | 506 | |
a1954302 | 507 | theWorkspace->DisplayCallback (theCView, aMode); |
508 | } | |
b5ac8292 | 509 | |
e6804ff7 | 510 | // ============================================================== |
42f8ba56 | 511 | // Step 7: Keep shader manager informed about last View |
e6804ff7 | 512 | // ============================================================== |
513 | ||
514 | if (!aManager.IsNull()) | |
515 | { | |
516 | aManager->SetLastView (this); | |
517 | } | |
2166f0fa SK |
518 | } |
519 | ||
b7cd4ba7 | 520 | // ======================================================================= |
521 | // function : InvalidateBVHData | |
522 | // purpose : | |
523 | // ======================================================================= | |
a1954302 | 524 | void OpenGl_View::InvalidateBVHData (const Graphic3d_ZLayerId theLayerId) |
b7cd4ba7 | 525 | { |
526 | myZLayers.InvalidateBVHData (theLayerId); | |
527 | } | |
528 | ||
2166f0fa SK |
529 | /*----------------------------------------------------------------------*/ |
530 | ||
531 | //ExecuteViewDisplay | |
b34efb62 | 532 | void OpenGl_View::RenderStructs (const Handle(OpenGl_Workspace)& theWorkspace, |
38a0206f | 533 | OpenGl_FrameBuffer* theReadDrawFbo, |
91c60b57 | 534 | const Graphic3d_CView& theCView, |
a1954302 | 535 | const Standard_Boolean theToDrawImmediate) |
2166f0fa | 536 | { |
59f45b7c | 537 | if ( myZLayers.NbStructures() <= 0 ) |
538 | return; | |
2166f0fa | 539 | |
b34efb62 | 540 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); |
2166f0fa | 541 | |
b34efb62 | 542 | if ( (theWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) == 0 ) |
2166f0fa | 543 | { |
ca3c13d1 | 544 | #if !defined(GL_ES_VERSION_2_0) |
b34efb62 | 545 | const int anAntiAliasingMode = theWorkspace->AntiAliasingMode(); |
ca3c13d1 | 546 | #endif |
2166f0fa SK |
547 | |
548 | if ( !myAntiAliasing ) | |
549 | { | |
ca3c13d1 | 550 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 551 | if (aCtx->core11 != NULL) |
552 | { | |
553 | glDisable (GL_POINT_SMOOTH); | |
554 | } | |
2166f0fa | 555 | glDisable(GL_LINE_SMOOTH); |
b34efb62 | 556 | if( anAntiAliasingMode & 2 ) glDisable(GL_POLYGON_SMOOTH); |
ca3c13d1 | 557 | #endif |
2166f0fa SK |
558 | glBlendFunc (GL_ONE, GL_ZERO); |
559 | glDisable (GL_BLEND); | |
560 | } | |
561 | else | |
562 | { | |
ca3c13d1 | 563 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 564 | if (aCtx->core11 != NULL) |
565 | { | |
566 | glEnable(GL_POINT_SMOOTH); | |
567 | } | |
2166f0fa | 568 | glEnable(GL_LINE_SMOOTH); |
b34efb62 | 569 | if( anAntiAliasingMode & 2 ) glEnable(GL_POLYGON_SMOOTH); |
ca3c13d1 | 570 | #endif |
3c3131a0 | 571 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
2166f0fa SK |
572 | glEnable (GL_BLEND); |
573 | } | |
574 | } | |
575 | ||
91c60b57 | 576 | Standard_Boolean toRenderGL = theToDrawImmediate || |
577 | theCView.RenderParams.Method != Graphic3d_RM_RAYTRACING || myRaytraceInitStatus == OpenGl_RT_FAIL; | |
578 | ||
579 | if (!toRenderGL) | |
580 | { | |
581 | toRenderGL = !initRaytraceResources (theCView, aCtx) || | |
582 | !updateRaytraceGeometry (OpenGl_GUM_CHECK, theWorkspace->ActiveViewId(), aCtx); | |
583 | ||
5682a029 | 584 | toRenderGL |= !myIsRaytraceDataValid; // if no ray-trace data use OpenGL |
91c60b57 | 585 | |
5682a029 | 586 | if (!toRenderGL) |
91c60b57 | 587 | { |
38a0206f | 588 | const Standard_Integer aSizeX = theReadDrawFbo != NULL ? |
589 | theReadDrawFbo->GetVPSizeX() : theWorkspace->Width(); | |
590 | const Standard_Integer aSizeY = theReadDrawFbo != NULL ? | |
591 | theReadDrawFbo->GetVPSizeY() : theWorkspace->Height(); | |
91c60b57 | 592 | |
593 | if (myOpenGlFBO.IsNull()) | |
594 | myOpenGlFBO = new OpenGl_FrameBuffer; | |
595 | ||
596 | if (myOpenGlFBO->GetVPSizeX() != aSizeX | |
597 | || myOpenGlFBO->GetVPSizeY() != aSizeY) | |
598 | { | |
599 | myOpenGlFBO->Init (aCtx, aSizeX, aSizeY); | |
600 | } | |
601 | ||
602 | if (myRaytraceFilter.IsNull()) | |
603 | myRaytraceFilter = new OpenGl_RaytraceFilter; | |
604 | ||
605 | myRaytraceFilter->SetPrevRenderFilter (theWorkspace->GetRenderFilter()); | |
606 | ||
38a0206f | 607 | if (theReadDrawFbo != NULL) |
608 | theReadDrawFbo->UnbindBuffer (aCtx); | |
91c60b57 | 609 | |
610 | // Prepare preliminary OpenGL output | |
611 | if (aCtx->arbFBOBlit != NULL) | |
612 | { | |
613 | // Render bottom OSD layer | |
614 | myZLayers.Render (theWorkspace, theToDrawImmediate, OpenGl_LF_Bottom); | |
615 | ||
616 | theWorkspace->SetRenderFilter (myRaytraceFilter); | |
617 | { | |
38a0206f | 618 | if (theReadDrawFbo != NULL) |
91c60b57 | 619 | { |
38a0206f | 620 | theReadDrawFbo->BindReadBuffer (aCtx); |
91c60b57 | 621 | } |
622 | else | |
623 | { | |
624 | aCtx->arbFBO->glBindFramebuffer (GL_READ_FRAMEBUFFER, 0); | |
625 | } | |
626 | ||
627 | myOpenGlFBO->BindDrawBuffer (aCtx); | |
628 | ||
629 | aCtx->arbFBOBlit->glBlitFramebuffer (0, 0, aSizeX, aSizeY, | |
630 | 0, 0, aSizeX, aSizeY, | |
631 | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, | |
632 | GL_NEAREST); | |
633 | ||
634 | // Render non-polygonal elements in default layer | |
635 | myZLayers.Render (theWorkspace, theToDrawImmediate, OpenGl_LF_Default); | |
636 | } | |
637 | theWorkspace->SetRenderFilter (myRaytraceFilter->PrevRenderFilter()); | |
638 | } | |
639 | ||
38a0206f | 640 | if (theReadDrawFbo != NULL) |
91c60b57 | 641 | { |
38a0206f | 642 | theReadDrawFbo->BindBuffer (aCtx); |
91c60b57 | 643 | } |
644 | else | |
645 | { | |
646 | aCtx->arbFBO->glBindFramebuffer (GL_FRAMEBUFFER, 0); | |
647 | } | |
648 | ||
649 | // Ray-tracing polygonal primitive arrays | |
38a0206f | 650 | raytrace (theCView, aSizeX, aSizeY, theReadDrawFbo, aCtx); |
91c60b57 | 651 | |
652 | // Render upper (top and topmost) OpenGL layers | |
653 | myZLayers.Render (theWorkspace, theToDrawImmediate, OpenGl_LF_Upper); | |
654 | } | |
655 | } | |
656 | ||
657 | // Redraw 3D scene using OpenGL in standard | |
658 | // mode or in case of ray-tracing failure | |
659 | if (toRenderGL) | |
660 | { | |
661 | myZLayers.Render (theWorkspace, theToDrawImmediate, OpenGl_LF_All); | |
662 | ||
663 | // Set flag that scene was redrawn by standard pipeline | |
664 | theCView.WasRedrawnGL = Standard_True; | |
665 | } | |
2166f0fa SK |
666 | } |
667 | ||
668 | /*----------------------------------------------------------------------*/ | |
669 | ||
670 | //call_togl_redraw_layer2d | |
a174a3c5 | 671 | void OpenGl_View::RedrawLayer2d (const Handle(OpenGl_PrinterContext)& thePrintContext, |
c827ea3a | 672 | const Handle(OpenGl_Workspace)& theWorkspace, |
93e572ca | 673 | const Graphic3d_CView& /*ACView*/, |
a174a3c5 | 674 | const Aspect_CLayer2d& ACLayer) |
2166f0fa | 675 | { |
ca3c13d1 | 676 | #if !defined(GL_ES_VERSION_2_0) |
2166f0fa SK |
677 | if (&ACLayer == NULL |
678 | || ACLayer.ptrLayer == NULL | |
679 | || ACLayer.ptrLayer->listIndex == 0) return; | |
680 | ||
c827ea3a | 681 | const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext(); |
4e1523ef | 682 | if (aContext->core11 == NULL) |
683 | { | |
684 | return; | |
685 | } | |
c827ea3a | 686 | |
529afc1a K |
687 | GLsizei dispWidth = (GLsizei )ACLayer.viewport[0]; |
688 | GLsizei dispHeight = (GLsizei )ACLayer.viewport[1]; | |
2166f0fa | 689 | |
c827ea3a | 690 | aContext->WorldViewState.Push(); |
691 | aContext->ProjectionState.Push(); | |
2166f0fa | 692 | |
c827ea3a | 693 | aContext->WorldViewState.SetIdentity(); |
694 | aContext->ProjectionState.SetIdentity(); | |
695 | ||
696 | aContext->ApplyWorldViewMatrix(); | |
697 | aContext->ApplyProjectionMatrix(); | |
2166f0fa SK |
698 | |
699 | if (!ACLayer.sizeDependent) | |
e3573bb9 | 700 | aContext->core11fwd->glViewport (0, 0, dispWidth, dispHeight); |
2166f0fa SK |
701 | |
702 | float left = ACLayer.ortho[0]; | |
703 | float right = ACLayer.ortho[1]; | |
704 | float bottom = ACLayer.ortho[2]; | |
705 | float top = ACLayer.ortho[3]; | |
706 | ||
707 | int attach = ACLayer.attach; | |
708 | ||
e3573bb9 | 709 | const float ratio = !ACLayer.sizeDependent |
710 | ? float(dispWidth) / float(dispHeight) | |
711 | : float(theWorkspace->Width()) / float(theWorkspace->Height()); | |
2166f0fa SK |
712 | |
713 | float delta; | |
c9d4eb9d | 714 | if (ratio >= 1.0) { |
2166f0fa SK |
715 | delta = (float )((top - bottom)/2.0); |
716 | switch (attach) { | |
717 | case 0: /* Aspect_TOC_BOTTOM_LEFT */ | |
718 | top = bottom + 2*delta/ratio; | |
719 | break; | |
720 | case 1: /* Aspect_TOC_BOTTOM_RIGHT */ | |
721 | top = bottom + 2*delta/ratio; | |
722 | break; | |
723 | case 2: /* Aspect_TOC_TOP_LEFT */ | |
724 | bottom = top - 2*delta/ratio; | |
725 | break; | |
726 | case 3: /* Aspect_TOC_TOP_RIGHT */ | |
727 | bottom = top - 2*delta/ratio; | |
728 | break; | |
729 | } | |
730 | } | |
c9d4eb9d | 731 | else { |
2166f0fa SK |
732 | delta = (float )((right - left)/2.0); |
733 | switch (attach) { | |
734 | case 0: /* Aspect_TOC_BOTTOM_LEFT */ | |
735 | right = left + 2*delta*ratio; | |
736 | break; | |
737 | case 1: /* Aspect_TOC_BOTTOM_RIGHT */ | |
738 | left = right - 2*delta*ratio; | |
739 | break; | |
740 | case 2: /* Aspect_TOC_TOP_LEFT */ | |
741 | right = left + 2*delta*ratio; | |
742 | break; | |
743 | case 3: /* Aspect_TOC_TOP_RIGHT */ | |
744 | left = right - 2*delta*ratio; | |
745 | break; | |
746 | } | |
747 | } | |
748 | ||
a174a3c5 | 749 | #ifdef _WIN32 |
2166f0fa | 750 | // Check printer context that exists only for print operation |
a174a3c5 | 751 | if (!thePrintContext.IsNull()) |
2166f0fa SK |
752 | { |
753 | // additional transformation matrix could be applied to | |
754 | // render only those parts of viewport that will be | |
755 | // passed to a printer as a current "frame" to provide | |
756 | // tiling; scaling of graphics by matrix helps render a | |
757 | // part of a view (frame) in same viewport, but with higher | |
758 | // resolution | |
c827ea3a | 759 | |
760 | // set printing scale/tiling transformation | |
761 | aContext->ProjectionState.SetCurrent (thePrintContext->ProjTransformation()); | |
762 | aContext->ApplyProjectionMatrix(); | |
2166f0fa SK |
763 | |
764 | // printing operation also assumes other viewport dimension | |
765 | // to comply with transformation matrix or graphics scaling | |
766 | // factors for tiling for layer redraw | |
767 | GLsizei anViewportX = 0; | |
768 | GLsizei anViewportY = 0; | |
a174a3c5 | 769 | thePrintContext->GetLayerViewport (anViewportX, anViewportY); |
2166f0fa | 770 | if (anViewportX != 0 && anViewportY != 0) |
e3573bb9 | 771 | aContext->core11fwd->glViewport (0, 0, anViewportX, anViewportY); |
2166f0fa | 772 | } |
3c3131a0 | 773 | #endif |
2166f0fa SK |
774 | |
775 | glOrtho (left, right, bottom, top, -1.0, 1.0); | |
776 | ||
2166f0fa SK |
777 | glPushAttrib ( |
778 | GL_LIGHTING_BIT | GL_LINE_BIT | GL_POLYGON_BIT | | |
779 | GL_DEPTH_BUFFER_BIT | GL_CURRENT_BIT | GL_TEXTURE_BIT ); | |
c9d4eb9d | 780 | |
2166f0fa | 781 | glDisable (GL_DEPTH_TEST); |
c9d4eb9d | 782 | glDisable (GL_TEXTURE_1D); |
783 | glDisable (GL_TEXTURE_2D); | |
784 | glDisable (GL_LIGHTING); | |
785 | ||
786 | // TODO: Obsolete code, the display list is always empty now, to be removed | |
2166f0fa SK |
787 | glCallList (ACLayer.ptrLayer->listIndex); |
788 | ||
789 | //calling dynamic render of LayerItems | |
790 | if ( ACLayer.ptrLayer->layerData ) | |
791 | { | |
30f0ad28 | 792 | InitLayerProp (ACLayer.ptrLayer->listIndex); |
2166f0fa | 793 | ((Visual3d_Layer*)ACLayer.ptrLayer->layerData)->RenderLayerItems(); |
30f0ad28 | 794 | InitLayerProp (0); |
2166f0fa SK |
795 | } |
796 | ||
797 | glPopAttrib (); | |
798 | ||
c827ea3a | 799 | aContext->WorldViewState.Pop(); |
800 | aContext->ProjectionState.Pop(); | |
2166f0fa | 801 | |
c827ea3a | 802 | aContext->ApplyProjectionMatrix(); |
803 | aContext->ApplyWorldViewMatrix(); | |
2166f0fa | 804 | |
2166f0fa | 805 | if (!ACLayer.sizeDependent) |
e3573bb9 | 806 | aContext->core11fwd->glViewport (0, 0, theWorkspace->Width(), theWorkspace->Height()); |
2166f0fa SK |
807 | |
808 | glFlush (); | |
ca3c13d1 | 809 | #endif |
2166f0fa SK |
810 | } |
811 | ||
812 | /*----------------------------------------------------------------------*/ | |
813 | ||
a89742cf | 814 | void OpenGl_View::RedrawTrihedron (const Handle(OpenGl_Workspace) &theWorkspace) |
815 | { | |
816 | // display global trihedron | |
536d98e2 | 817 | if (myToShowTrihedron) |
a89742cf | 818 | { |
536d98e2 | 819 | myTrihedron.Render (theWorkspace); |
a89742cf | 820 | } |
536d98e2 | 821 | if (myToShowGradTrihedron) |
a89742cf | 822 | { |
536d98e2 | 823 | myGraduatedTrihedron.Render (theWorkspace); |
a89742cf | 824 | } |
825 | } | |
826 | ||
827 | /*----------------------------------------------------------------------*/ | |
828 | ||
2166f0fa | 829 | //call_togl_create_bg_texture |
3c3131a0 | 830 | void OpenGl_View::CreateBackgroundTexture (const Standard_CString theFilePath, |
831 | const Aspect_FillMethod theFillStyle) | |
2166f0fa | 832 | { |
0b0320e7 | 833 | // Prepare aspect for texture storage |
834 | Handle(Graphic3d_AspectFillArea3d) anAspect = new Graphic3d_AspectFillArea3d(); | |
835 | Handle(Graphic3d_Texture2Dmanual) aTextureMap = new Graphic3d_Texture2Dmanual (TCollection_AsciiString (theFilePath)); | |
836 | aTextureMap->EnableRepeat(); | |
837 | aTextureMap->DisableModulate(); | |
838 | aTextureMap->GetParams()->SetGenMode (Graphic3d_TOTM_MANUAL, | |
839 | Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f), | |
840 | Graphic3d_Vec4 (0.0f, 0.0f, 0.0f, 0.0f)); | |
841 | anAspect->SetTextureMap (aTextureMap); | |
842 | anAspect->SetInteriorStyle (Aspect_IS_SOLID); | |
843 | // Enable texture mapping | |
844 | if (aTextureMap->IsDone()) | |
845 | { | |
846 | anAspect->SetTextureMapOn(); | |
3c3131a0 | 847 | } |
848 | else | |
849 | { | |
0b0320e7 | 850 | anAspect->SetTextureMapOff(); |
851 | return; | |
3c3131a0 | 852 | |
3c3131a0 | 853 | } |
2166f0fa | 854 | |
0b0320e7 | 855 | // Set texture parameters |
856 | myTextureParams->SetAspect (anAspect); | |
2166f0fa | 857 | |
0b0320e7 | 858 | myBgTextureArray->SetTextureParameters (theFillStyle); |
2166f0fa SK |
859 | } |
860 | ||
861 | /*----------------------------------------------------------------------*/ | |
862 | ||
863 | //call_togl_set_bg_texture_style | |
0b0320e7 | 864 | void OpenGl_View::SetBackgroundTextureStyle (const Aspect_FillMethod theFillStyle) |
2166f0fa | 865 | { |
0b0320e7 | 866 | myBgTextureArray->SetTextureFillMethod (theFillStyle); |
2166f0fa SK |
867 | } |
868 | ||
869 | /*----------------------------------------------------------------------*/ | |
870 | ||
871 | //call_togl_gradient_background | |
0b0320e7 | 872 | void OpenGl_View::SetBackgroundGradient (const Quantity_Color& theColor1, |
873 | const Quantity_Color& theColor2, | |
874 | const Aspect_GradientFillMethod theType) | |
2166f0fa | 875 | { |
0b0320e7 | 876 | myBgGradientArray->SetGradientParameters (theColor1, theColor2, theType); |
2166f0fa SK |
877 | } |
878 | ||
879 | /*----------------------------------------------------------------------*/ | |
880 | ||
881 | //call_togl_set_gradient_type | |
0b0320e7 | 882 | void OpenGl_View::SetBackgroundGradientType (const Aspect_GradientFillMethod theType) |
2166f0fa | 883 | { |
0b0320e7 | 884 | myBgGradientArray->SetGradientFillMethod (theType); |
2166f0fa SK |
885 | } |
886 | ||
59f45b7c | 887 | //======================================================================= |
888 | //function : AddZLayer | |
3c3131a0 | 889 | //purpose : |
59f45b7c | 890 | //======================================================================= |
891 | ||
a1954302 | 892 | void OpenGl_View::AddZLayer (const Graphic3d_ZLayerId theLayerId) |
59f45b7c | 893 | { |
894 | myZLayers.AddLayer (theLayerId); | |
895 | } | |
896 | ||
897 | //======================================================================= | |
898 | //function : RemoveZLayer | |
3c3131a0 | 899 | //purpose : |
59f45b7c | 900 | //======================================================================= |
901 | ||
a1954302 | 902 | void OpenGl_View::RemoveZLayer (const Graphic3d_ZLayerId theLayerId) |
59f45b7c | 903 | { |
904 | myZLayers.RemoveLayer (theLayerId); | |
905 | } | |
906 | ||
907 | //======================================================================= | |
908 | //function : DisplayStructure | |
3c3131a0 | 909 | //purpose : |
59f45b7c | 910 | //======================================================================= |
911 | ||
a1954302 | 912 | void OpenGl_View::DisplayStructure (const Handle(Graphic3d_Structure)& theStructure, |
913 | const Standard_Integer thePriority) | |
59f45b7c | 914 | { |
a1954302 | 915 | const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure->CStructure().operator->()); |
916 | const Graphic3d_ZLayerId aZLayer = aStruct->ZLayer(); | |
917 | myZLayers.AddStructure (aStruct, aZLayer, thePriority); | |
59f45b7c | 918 | } |
919 | ||
679ecdee | 920 | //======================================================================= |
921 | //function : DisplayImmediateStructure | |
922 | //purpose : | |
923 | //======================================================================= | |
924 | ||
a1954302 | 925 | void OpenGl_View::DisplayImmediateStructure (const Handle(Graphic3d_Structure)& theStructure) |
679ecdee | 926 | { |
a1954302 | 927 | const OpenGl_Structure* aStruct = reinterpret_cast<const OpenGl_Structure*> (theStructure->CStructure().operator->()); |
9f112210 | 928 | myImmediateList.Add (aStruct); |
679ecdee | 929 | } |
930 | ||
59f45b7c | 931 | //======================================================================= |
932 | //function : EraseStructure | |
3c3131a0 | 933 | //purpose : |
59f45b7c | 934 | //======================================================================= |
935 | ||
a1954302 | 936 | void OpenGl_View::EraseStructure (const Handle(Graphic3d_Structure)& theStructure) |
59f45b7c | 937 | { |
a1954302 | 938 | myZLayers.RemoveStructure (theStructure); |
59f45b7c | 939 | } |
940 | ||
941 | //======================================================================= | |
679ecdee | 942 | //function : EraseImmediateStructure |
943 | //purpose : | |
944 | //======================================================================= | |
945 | ||
946 | void OpenGl_View::EraseImmediateStructure (const OpenGl_Structure* theStructure) | |
947 | { | |
9f112210 | 948 | const Standard_Integer anIndex = myImmediateList.FindIndex (theStructure); |
949 | ||
950 | if (anIndex != 0) | |
679ecdee | 951 | { |
9f112210 | 952 | myImmediateList.Swap (myImmediateList.Size(), anIndex); |
953 | myImmediateList.RemoveLast(); | |
679ecdee | 954 | } |
955 | } | |
956 | ||
957 | //======================================================================= | |
59f45b7c | 958 | //function : ChangeZLayer |
959 | //purpose : | |
960 | //======================================================================= | |
961 | ||
a1954302 | 962 | void OpenGl_View::ChangeZLayer (const OpenGl_Structure* theStructure, |
963 | const Graphic3d_ZLayerId theNewLayerId) | |
59f45b7c | 964 | { |
a1954302 | 965 | const Graphic3d_ZLayerId anOldLayer = theStructure->ZLayer(); |
59f45b7c | 966 | myZLayers.ChangeLayer (theStructure, anOldLayer, theNewLayerId); |
967 | } | |
b5ac8292 | 968 | |
c5751993 | 969 | //======================================================================= |
970 | //function : SetZLayerSettings | |
971 | //purpose : | |
972 | //======================================================================= | |
a1954302 | 973 | void OpenGl_View::SetZLayerSettings (const Graphic3d_ZLayerId theLayerId, |
974 | const Graphic3d_ZLayerSettings& theSettings) | |
c5751993 | 975 | { |
a1954302 | 976 | myZLayers.SetLayerSettings (theLayerId, theSettings); |
c5751993 | 977 | } |
978 | ||
b7cd4ba7 | 979 | //======================================================================= |
980 | //function : ChangePriority | |
981 | //purpose : | |
982 | //======================================================================= | |
983 | void OpenGl_View::ChangePriority (const OpenGl_Structure *theStructure, | |
984 | const Standard_Integer theNewPriority) | |
985 | { | |
a1954302 | 986 | const Graphic3d_ZLayerId aLayerId = theStructure->ZLayer(); |
b7cd4ba7 | 987 | myZLayers.ChangePriority (theStructure, aLayerId, theNewPriority); |
988 | } | |
c5751993 | 989 | |
b5ac8292 | 990 | //======================================================================= |
991 | //function : RedrawScene | |
992 | //purpose : | |
993 | //======================================================================= | |
994 | ||
995 | void OpenGl_View::RedrawScene (const Handle(OpenGl_PrinterContext)& thePrintContext, | |
a1954302 | 996 | const Handle(OpenGl_Workspace)& theWorkspace, |
38a0206f | 997 | OpenGl_FrameBuffer* theReadDrawFbo, |
91c60b57 | 998 | const Graphic3d_CView& theCView, |
a1954302 | 999 | const Standard_Boolean theToDrawImmediate) |
b5ac8292 | 1000 | { |
1001 | const Handle(OpenGl_Context)& aContext = theWorkspace->GetGlContext(); | |
1002 | ||
1003 | if (myZClip.Back.IsOn || myZClip.Front.IsOn) | |
1004 | { | |
1005 | Handle(Graphic3d_ClipPlane) aPlaneBack; | |
1006 | Handle(Graphic3d_ClipPlane) aPlaneFront; | |
1007 | ||
1008 | if (myZClip.Back.IsOn) | |
1009 | { | |
1010 | Standard_Real aClipBackConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance(); | |
1011 | if (myCamera->ZFar() < aClipBackConverted) | |
1012 | { | |
1013 | aClipBackConverted = myCamera->ZFar(); | |
1014 | myZClip.Back.Limit = (Standard_ShortReal )(aClipBackConverted - myCamera->Distance()); | |
1015 | } | |
1016 | const Graphic3d_ClipPlane::Equation aBackEquation (0.0, 0.0, 1.0, (Standard_ShortReal )aClipBackConverted); | |
1017 | aPlaneBack = new Graphic3d_ClipPlane (aBackEquation); | |
1018 | } | |
1019 | ||
1020 | if (myZClip.Front.IsOn) | |
1021 | { | |
1022 | Standard_Real aClipFrontConverted = (Standard_Real )myZClip.Front.Limit + myCamera->Distance(); | |
1023 | if (myCamera->ZNear() > aClipFrontConverted) | |
1024 | { | |
1025 | aClipFrontConverted = myCamera->ZNear(); | |
1026 | myZClip.Front.Limit = (Standard_ShortReal )(aClipFrontConverted - myCamera->Distance()); | |
1027 | } | |
1028 | const Graphic3d_ClipPlane::Equation aFrontEquation (0.0, 0.0, -1.0, (Standard_ShortReal )-aClipFrontConverted); | |
1029 | aPlaneFront = new Graphic3d_ClipPlane (aFrontEquation); | |
1030 | } | |
1031 | ||
cddbf6a9 | 1032 | // Specify slicing planes with identity transformation |
b5ac8292 | 1033 | if (!aPlaneBack.IsNull() || !aPlaneFront.IsNull()) |
1034 | { | |
1035 | Graphic3d_SequenceOfHClipPlane aSlicingPlanes; | |
1036 | if (!aPlaneBack.IsNull()) | |
1037 | { | |
1038 | aSlicingPlanes.Append (aPlaneBack); | |
1039 | } | |
1040 | ||
1041 | if (!aPlaneFront.IsNull()) | |
1042 | { | |
1043 | aSlicingPlanes.Append (aPlaneFront); | |
1044 | } | |
1045 | ||
1046 | // add planes at loaded view matrix state | |
1047 | aContext->ChangeClipping().AddView (aSlicingPlanes, theWorkspace); | |
1048 | } | |
1049 | } | |
1050 | ||
b5ac8292 | 1051 | #ifdef _WIN32 |
c827ea3a | 1052 | // set printing scale/tiling transformation |
b5ac8292 | 1053 | if (!thePrintContext.IsNull()) |
1054 | { | |
c827ea3a | 1055 | aContext->ProjectionState.Push(); |
1056 | aContext->ProjectionState.SetCurrent (thePrintContext->ProjTransformation() * aContext->ProjectionState.Current()); | |
1057 | aContext->ApplyProjectionMatrix(); | |
b5ac8292 | 1058 | } |
ca3c13d1 | 1059 | #endif |
b5ac8292 | 1060 | |
cddbf6a9 | 1061 | // Specify clipping planes in view transformation space |
1062 | if (!myClipPlanes.IsEmpty()) | |
1063 | { | |
1064 | Graphic3d_SequenceOfHClipPlane aUserPlanes; | |
1065 | Graphic3d_SequenceOfHClipPlane::Iterator aClippingIt (myClipPlanes); | |
1066 | for (; aClippingIt.More(); aClippingIt.Next()) | |
1067 | { | |
1068 | const Handle(Graphic3d_ClipPlane)& aClipPlane = aClippingIt.Value(); | |
1069 | if (aClipPlane->IsOn()) | |
1070 | { | |
1071 | aUserPlanes.Append (aClipPlane); | |
1072 | } | |
1073 | } | |
1074 | ||
1075 | if (!aUserPlanes.IsEmpty()) | |
1076 | { | |
1077 | aContext->ChangeClipping().AddWorld (aUserPlanes); | |
1078 | } | |
1079 | ||
1080 | if (!aContext->ShaderManager()->IsEmpty()) | |
1081 | { | |
1082 | aContext->ShaderManager()->UpdateClippingState(); | |
1083 | } | |
1084 | } | |
1085 | ||
ca3c13d1 | 1086 | #if !defined(GL_ES_VERSION_2_0) |
b5ac8292 | 1087 | // Apply Lights |
4e1523ef | 1088 | if (aContext->core11 != NULL) |
b5ac8292 | 1089 | { |
1090 | // setup lights | |
1091 | Graphic3d_Vec4 anAmbientColor (THE_DEFAULT_AMBIENT[0], | |
1092 | THE_DEFAULT_AMBIENT[1], | |
1093 | THE_DEFAULT_AMBIENT[2], | |
1094 | THE_DEFAULT_AMBIENT[3]); | |
1095 | GLenum aLightGlId = GL_LIGHT0; | |
1096 | for (OpenGl_ListOfLight::Iterator aLightIt (myLights); | |
1097 | aLightIt.More(); aLightIt.Next()) | |
1098 | { | |
c827ea3a | 1099 | bindLight (aLightIt.Value(), aLightGlId, anAmbientColor, theWorkspace); |
b5ac8292 | 1100 | } |
1101 | ||
1102 | // apply accumulated ambient color | |
1103 | anAmbientColor.a() = 1.0f; | |
1104 | glLightModelfv (GL_LIGHT_MODEL_AMBIENT, anAmbientColor.GetData()); | |
1105 | ||
1106 | if (aLightGlId != GL_LIGHT0) | |
1107 | { | |
1108 | glEnable (GL_LIGHTING); | |
1109 | } | |
1110 | // switch off unused lights | |
1111 | for (; aLightGlId <= GL_LIGHT7; ++aLightGlId) | |
1112 | { | |
1113 | glDisable (aLightGlId); | |
1114 | } | |
1115 | } | |
ca3c13d1 | 1116 | #endif |
b5ac8292 | 1117 | |
1118 | // Clear status bitfields | |
1119 | theWorkspace->NamedStatus &= ~(OPENGL_NS_2NDPASSNEED | OPENGL_NS_2NDPASSDO); | |
1120 | ||
e135a155 | 1121 | // Update state of surface detail level |
1122 | theWorkspace->GetGlContext()->ShaderManager()->UpdateSurfaceDetailStateTo (mySurfaceDetail); | |
1123 | ||
b5ac8292 | 1124 | // Added PCT for handling of textures |
1125 | switch (mySurfaceDetail) | |
1126 | { | |
1127 | case Visual3d_TOD_NONE: | |
1128 | theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX; | |
1129 | theWorkspace->DisableTexture(); | |
1130 | // Render the view | |
38a0206f | 1131 | RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate); |
b5ac8292 | 1132 | break; |
1133 | ||
1134 | case Visual3d_TOD_ENVIRONMENT: | |
1135 | theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX; | |
1136 | theWorkspace->EnableTexture (myTextureEnv); | |
1137 | // Render the view | |
38a0206f | 1138 | RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate); |
b5ac8292 | 1139 | theWorkspace->DisableTexture(); |
1140 | break; | |
1141 | ||
1142 | case Visual3d_TOD_ALL: | |
1143 | // First pass | |
1144 | theWorkspace->NamedStatus &= ~OPENGL_NS_FORBIDSETTEX; | |
1145 | // Render the view | |
38a0206f | 1146 | RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate); |
b5ac8292 | 1147 | theWorkspace->DisableTexture(); |
1148 | ||
1149 | // Second pass | |
1150 | if (theWorkspace->NamedStatus & OPENGL_NS_2NDPASSNEED) | |
1151 | { | |
1152 | theWorkspace->NamedStatus |= OPENGL_NS_2NDPASSDO; | |
1153 | theWorkspace->EnableTexture (myTextureEnv); | |
1154 | ||
1155 | // Remember OpenGl properties | |
ca3c13d1 | 1156 | GLint aSaveBlendDst = GL_ONE_MINUS_SRC_ALPHA, aSaveBlendSrc = GL_SRC_ALPHA; |
b5ac8292 | 1157 | GLint aSaveZbuffFunc; |
1158 | GLboolean aSaveZbuffWrite; | |
1159 | glGetBooleanv (GL_DEPTH_WRITEMASK, &aSaveZbuffWrite); | |
1160 | glGetIntegerv (GL_DEPTH_FUNC, &aSaveZbuffFunc); | |
ca3c13d1 | 1161 | #if !defined(GL_ES_VERSION_2_0) |
b5ac8292 | 1162 | glGetIntegerv (GL_BLEND_DST, &aSaveBlendDst); |
1163 | glGetIntegerv (GL_BLEND_SRC, &aSaveBlendSrc); | |
ca3c13d1 | 1164 | #endif |
b5ac8292 | 1165 | GLboolean wasZbuffEnabled = glIsEnabled (GL_DEPTH_TEST); |
1166 | GLboolean wasBlendEnabled = glIsEnabled (GL_BLEND); | |
1167 | ||
1168 | // Change the properties for second rendering pass | |
1169 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
1170 | glEnable (GL_BLEND); | |
1171 | ||
1172 | glDepthFunc (GL_EQUAL); | |
1173 | glDepthMask (GL_FALSE); | |
1174 | glEnable (GL_DEPTH_TEST); | |
1175 | ||
1176 | theWorkspace->NamedStatus |= OPENGL_NS_FORBIDSETTEX; | |
1177 | ||
1178 | // Render the view | |
38a0206f | 1179 | RenderStructs (theWorkspace, theReadDrawFbo, theCView, theToDrawImmediate); |
b5ac8292 | 1180 | theWorkspace->DisableTexture(); |
1181 | ||
1182 | // Restore properties back | |
1183 | glBlendFunc (aSaveBlendSrc, aSaveBlendDst); | |
1184 | if (!wasBlendEnabled) | |
1185 | glDisable (GL_BLEND); | |
1186 | ||
1187 | glDepthFunc (aSaveZbuffFunc); | |
1188 | glDepthMask (aSaveZbuffWrite); | |
1189 | if (!wasZbuffEnabled) | |
1190 | glDisable (GL_DEPTH_FUNC); | |
1191 | } | |
1192 | break; | |
1193 | } | |
c827ea3a | 1194 | |
1195 | // Apply restored view matrix. | |
1196 | aContext->ApplyWorldViewMatrix(); | |
1197 | ||
1198 | #ifdef _WIN32 | |
1199 | // set printing scale/tiling transformation | |
1200 | if (!thePrintContext.IsNull()) | |
1201 | { | |
1202 | aContext->ProjectionState.Pop(); | |
1203 | aContext->ApplyProjectionMatrix(); | |
1204 | } | |
1205 | #endif | |
1206 | ||
b5ac8292 | 1207 | } |