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 | |
c357e426 | 16 | #include <OpenGl_Workspace.hxx> |
5f8b738e | 17 | |
2166f0fa SK |
18 | #include <InterfaceGraphic.hxx> |
19 | ||
c357e426 | 20 | #include <OpenGl_ArbFBO.hxx> |
2166f0fa SK |
21 | #include <OpenGl_AspectLine.hxx> |
22 | #include <OpenGl_AspectFace.hxx> | |
23 | #include <OpenGl_AspectMarker.hxx> | |
24 | #include <OpenGl_AspectText.hxx> | |
bf75be98 | 25 | #include <OpenGl_Context.hxx> |
679ecdee | 26 | #include <OpenGl_Element.hxx> |
a174a3c5 | 27 | #include <OpenGl_FrameBuffer.hxx> |
c357e426 | 28 | #include <OpenGl_GlCore15.hxx> |
29 | #include <OpenGl_SceneGeometry.hxx> | |
679ecdee | 30 | #include <OpenGl_Structure.hxx> |
25ef750e | 31 | #include <OpenGl_Sampler.hxx> |
bf75be98 | 32 | #include <OpenGl_Texture.hxx> |
e276548b | 33 | #include <OpenGl_View.hxx> |
c357e426 | 34 | #include <OpenGl_Window.hxx> |
2166f0fa | 35 | |
bf75be98 | 36 | #include <Graphic3d_TextureParams.hxx> |
825aa485 | 37 | #include <Graphic3d_TransformUtils.hxx> |
2166f0fa | 38 | |
92efcf78 | 39 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Workspace,Standard_Transient) |
40 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_RaytraceFilter,OpenGl_RenderFilter) | |
41 | ||
c357e426 | 42 | #ifdef HAVE_GL2PS |
43 | #include <gl2ps.h> | |
44 | /* OCC22216 NOTE: linker dependency can be switched off by undefining macro. | |
45 | Pragma comment for gl2ps.lib is defined only here. */ | |
46 | #ifdef _MSC_VER | |
47 | #pragma comment( lib, "gl2ps.lib" ) | |
48 | #endif | |
a174a3c5 | 49 | #endif |
50 | ||
2166f0fa SK |
51 | namespace |
52 | { | |
0adbd30f | 53 | static const TEL_COLOUR THE_WHITE_COLOR = { { 1.0f, 1.0f, 1.0f, 1.0f } }; |
54 | static const OpenGl_Vec4 THE_BLACK_COLOR (0.0f, 0.0f, 0.0f, 1.0f); | |
2166f0fa SK |
55 | |
56 | static const OpenGl_AspectLine myDefaultAspectLine; | |
57 | static const OpenGl_AspectFace myDefaultAspectFace; | |
58 | static const OpenGl_AspectMarker myDefaultAspectMarker; | |
2166f0fa | 59 | |
2166f0fa SK |
60 | static const OpenGl_Matrix myDefaultMatrix = |
61 | { | |
62 | {{ 1.0F, 0.0F, 0.0F, 0.0F }, | |
63 | { 0.0F, 1.0F, 0.0F, 0.0F }, | |
64 | { 0.0F, 0.0F, 1.0F, 0.0F }, | |
65 | { 0.0F, 0.0F, 0.0F, 1.0F }} | |
66 | }; | |
bf75be98 | 67 | |
a3f6f591 | 68 | } |
2166f0fa | 69 | |
0adbd30f | 70 | // ======================================================================= |
71 | // function : Init | |
72 | // purpose : | |
73 | // ======================================================================= | |
74 | void OpenGl_Material::Init (const OPENGL_SURF_PROP& theProp) | |
75 | { | |
76 | // ambient component | |
77 | if (theProp.color_mask & OPENGL_AMBIENT_MASK) | |
78 | { | |
79 | const float* aSrcAmb = theProp.isphysic ? theProp.ambcol.rgb : theProp.matcol.rgb; | |
80 | Ambient = OpenGl_Vec4 (aSrcAmb[0] * theProp.amb, | |
81 | aSrcAmb[1] * theProp.amb, | |
82 | aSrcAmb[2] * theProp.amb, | |
83 | 1.0f); | |
84 | } | |
85 | else | |
86 | { | |
87 | Ambient = THE_BLACK_COLOR; | |
88 | } | |
89 | ||
90 | // diffusion component | |
91 | if (theProp.color_mask & OPENGL_DIFFUSE_MASK) | |
92 | { | |
93 | const float* aSrcDif = theProp.isphysic ? theProp.difcol.rgb : theProp.matcol.rgb; | |
94 | Diffuse = OpenGl_Vec4 (aSrcDif[0] * theProp.diff, | |
95 | aSrcDif[1] * theProp.diff, | |
96 | aSrcDif[2] * theProp.diff, | |
97 | 1.0f); | |
98 | } | |
99 | else | |
100 | { | |
101 | Diffuse = THE_BLACK_COLOR; | |
102 | } | |
103 | ||
104 | // specular component | |
105 | if (theProp.color_mask & OPENGL_SPECULAR_MASK) | |
106 | { | |
107 | const float* aSrcSpe = theProp.isphysic ? theProp.speccol.rgb : THE_WHITE_COLOR.rgb; | |
108 | Specular = OpenGl_Vec4 (aSrcSpe[0] * theProp.spec, | |
109 | aSrcSpe[1] * theProp.spec, | |
110 | aSrcSpe[2] * theProp.spec, | |
111 | 1.0f); | |
112 | } | |
113 | else | |
114 | { | |
115 | Specular = THE_BLACK_COLOR; | |
116 | } | |
117 | ||
118 | // emission component | |
119 | if (theProp.color_mask & OPENGL_EMISSIVE_MASK) | |
120 | { | |
121 | const float* aSrcEms = theProp.isphysic ? theProp.emscol.rgb : theProp.matcol.rgb; | |
122 | Emission = OpenGl_Vec4 (aSrcEms[0] * theProp.emsv, | |
123 | aSrcEms[1] * theProp.emsv, | |
124 | aSrcEms[2] * theProp.emsv, | |
125 | 1.0f); | |
126 | } | |
127 | else | |
128 | { | |
129 | Emission = THE_BLACK_COLOR; | |
130 | } | |
131 | ||
132 | ChangeShine() = theProp.shine; | |
133 | ChangeTransparency() = theProp.trans; | |
134 | } | |
135 | ||
2166f0fa SK |
136 | // ======================================================================= |
137 | // function : OpenGl_Workspace | |
138 | // purpose : | |
139 | // ======================================================================= | |
c357e426 | 140 | OpenGl_Workspace::OpenGl_Workspace (OpenGl_View* theView, const Handle(OpenGl_Window)& theWindow) |
141 | : NamedStatus (0), | |
0adbd30f | 142 | HighlightColor (&THE_WHITE_COLOR), |
c357e426 | 143 | myView (theView), |
144 | myWindow (theWindow), | |
145 | myGlContext (!theWindow.IsNull() ? theWindow->GetGlContext() : NULL), | |
eae454e3 | 146 | myUseZBuffer (Standard_True), |
147 | myUseDepthWrite (Standard_True), | |
2166f0fa | 148 | myUseGLLight (Standard_True), |
2166f0fa | 149 | // |
2166f0fa SK |
150 | AspectLine_set (&myDefaultAspectLine), |
151 | AspectLine_applied (NULL), | |
152 | AspectFace_set (&myDefaultAspectFace), | |
153 | AspectFace_applied (NULL), | |
154 | AspectMarker_set (&myDefaultAspectMarker), | |
155 | AspectMarker_applied (NULL), | |
2166f0fa SK |
156 | ViewMatrix_applied (&myDefaultMatrix), |
157 | StructureMatrix_applied (&myDefaultMatrix), | |
3b1817a9 | 158 | myCullingMode (TelCullUndefined), |
0f8c0fb8 | 159 | myModelViewMatrix (myDefaultMatrix), |
550f3b8b | 160 | PolygonOffset_applied (THE_DEFAULT_POFFSET) |
2166f0fa | 161 | { |
c357e426 | 162 | if (!myGlContext.IsNull() && myGlContext->MakeCurrent()) |
73192b37 | 163 | { |
c357e426 | 164 | myGlContext->core11fwd->glPixelStorei (GL_UNPACK_ALIGNMENT, 1); |
2166f0fa | 165 | |
c357e426 | 166 | if (!myGlContext->GetResource ("OpenGl_LineAttributes", myLineAttribs)) |
167 | { | |
168 | // share and register for release once the resource is no longer used | |
169 | myLineAttribs = new OpenGl_LineAttributes(); | |
170 | myGlContext->ShareResource ("OpenGl_LineAttributes", myLineAttribs); | |
171 | myLineAttribs->Init (myGlContext); | |
172 | } | |
2166f0fa | 173 | |
c357e426 | 174 | // General initialization of the context |
4e1523ef | 175 | |
c357e426 | 176 | #if !defined(GL_ES_VERSION_2_0) |
177 | if (myGlContext->core11 != NULL) | |
178 | { | |
179 | // Eviter d'avoir les faces mal orientees en noir. | |
180 | // Pourrait etre utiliser pour detecter les problemes d'orientation | |
181 | glLightModeli ((GLenum )GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); | |
2166f0fa | 182 | |
c357e426 | 183 | // Optimisation pour le Fog et l'antialiasing |
184 | glHint (GL_FOG_HINT, GL_FASTEST); | |
185 | glHint (GL_POINT_SMOOTH_HINT, GL_FASTEST); | |
186 | } | |
2166f0fa | 187 | |
c357e426 | 188 | glHint (GL_LINE_SMOOTH_HINT, GL_FASTEST); |
189 | glHint (GL_POLYGON_SMOOTH_HINT, GL_FASTEST); | |
190 | #endif | |
73192b37 | 191 | } |
f8ae3605 | 192 | |
193 | myDefaultCappingAlgoFilter = new OpenGl_CappingAlgoFilter(); | |
194 | myNoneCulling.ChangeCullingMode() = TelCullNone; | |
195 | myNoneCulling.ChangeEdge() = 0; | |
196 | myFrontCulling.ChangeCullingMode() = TelCullBack; | |
197 | myFrontCulling.ChangeEdge() = 0; | |
2166f0fa SK |
198 | } |
199 | ||
200 | // ======================================================================= | |
201 | // function : ~OpenGl_Workspace | |
202 | // purpose : | |
203 | // ======================================================================= | |
204 | OpenGl_Workspace::~OpenGl_Workspace() | |
205 | { | |
73192b37 | 206 | if (!myLineAttribs.IsNull()) |
207 | { | |
208 | myLineAttribs.Nullify(); | |
209 | myGlContext->ReleaseResource ("OpenGl_LineAttributes", Standard_True); | |
210 | } | |
2166f0fa SK |
211 | } |
212 | ||
213 | // ======================================================================= | |
214 | // function : Activate | |
215 | // purpose : | |
216 | // ======================================================================= | |
217 | Standard_Boolean OpenGl_Workspace::Activate() | |
218 | { | |
c357e426 | 219 | if (myWindow.IsNull() || !myWindow->Activate()) |
220 | { | |
2166f0fa | 221 | return Standard_False; |
c357e426 | 222 | } |
2166f0fa | 223 | |
2166f0fa SK |
224 | ViewMatrix_applied = &myDefaultMatrix; |
225 | StructureMatrix_applied = &myDefaultMatrix; | |
26395493 | 226 | |
227 | ResetAppliedAspect(); | |
228 | ||
229 | return Standard_True; | |
2166f0fa SK |
230 | } |
231 | ||
26395493 | 232 | //======================================================================= |
233 | //function : ResetAppliedAspect | |
234 | //purpose : Sets default values of GL parameters in accordance with default aspects | |
235 | //======================================================================= | |
236 | void OpenGl_Workspace::ResetAppliedAspect() | |
237 | { | |
4e1523ef | 238 | myGlContext->BindDefaultVao(); |
239 | ||
bf75be98 | 240 | NamedStatus = !myTextureBound.IsNull() ? OPENGL_NS_TEXTURE : 0; |
0adbd30f | 241 | HighlightColor = &THE_WHITE_COLOR; |
26395493 | 242 | AspectLine_set = &myDefaultAspectLine; |
243 | AspectLine_applied = NULL; | |
244 | AspectFace_set = &myDefaultAspectFace; | |
245 | AspectFace_applied = NULL; | |
246 | AspectMarker_set = &myDefaultAspectMarker; | |
247 | AspectMarker_applied = NULL; | |
550f3b8b | 248 | PolygonOffset_applied = THE_DEFAULT_POFFSET; |
3b1817a9 | 249 | myCullingMode = TelCullUndefined; |
26395493 | 250 | |
251 | AspectLine(Standard_True); | |
252 | AspectFace(Standard_True); | |
253 | AspectMarker(Standard_True); | |
254 | AspectText(Standard_True); | |
ac116c22 | 255 | |
256 | myGlContext->SetTypeOfLine (myDefaultAspectLine.Type()); | |
257 | myGlContext->SetLineWidth (myDefaultAspectLine.Width()); | |
26395493 | 258 | } |
bf75be98 | 259 | |
260 | // ======================================================================= | |
261 | // function : DisableTexture | |
262 | // purpose : | |
263 | // ======================================================================= | |
264 | Handle(OpenGl_Texture) OpenGl_Workspace::DisableTexture() | |
265 | { | |
266 | if (myTextureBound.IsNull()) | |
267 | { | |
268 | return myTextureBound; | |
269 | } | |
270 | ||
e3414ada | 271 | const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler(); |
272 | if (!aSampler.IsNull()) | |
273 | { | |
274 | aSampler->Unbind (*myGlContext); | |
275 | } | |
276 | ||
ca3c13d1 | 277 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 278 | // reset texture matrix because some code may expect it is identity |
4e1523ef | 279 | if (myGlContext->core11 != NULL) |
280 | { | |
281 | GLint aMatrixMode = GL_TEXTURE; | |
282 | glGetIntegerv (GL_MATRIX_MODE, &aMatrixMode); | |
283 | glMatrixMode (GL_TEXTURE); | |
284 | glLoadIdentity(); | |
285 | glMatrixMode (aMatrixMode); | |
286 | } | |
ca3c13d1 | 287 | #endif |
bf75be98 | 288 | |
289 | myTextureBound->Unbind (myGlContext); | |
290 | switch (myTextureBound->GetTarget()) | |
291 | { | |
ca3c13d1 | 292 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 293 | case GL_TEXTURE_1D: |
294 | { | |
4e1523ef | 295 | if (myGlContext->core11 != NULL) |
bf75be98 | 296 | { |
4e1523ef | 297 | if (myTextureBound->GetParams()->GenMode() != GL_NONE) |
298 | { | |
299 | glDisable (GL_TEXTURE_GEN_S); | |
300 | } | |
301 | glDisable (GL_TEXTURE_1D); | |
bf75be98 | 302 | } |
bf75be98 | 303 | break; |
304 | } | |
ca3c13d1 | 305 | #endif |
bf75be98 | 306 | case GL_TEXTURE_2D: |
307 | { | |
ca3c13d1 | 308 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 309 | if (myGlContext->core11 != NULL) |
bf75be98 | 310 | { |
4e1523ef | 311 | if (myTextureBound->GetParams()->GenMode() != GL_NONE) |
a577aaab | 312 | { |
4e1523ef | 313 | glDisable (GL_TEXTURE_GEN_S); |
314 | glDisable (GL_TEXTURE_GEN_T); | |
315 | if (myTextureBound->GetParams()->GenMode() == Graphic3d_TOTM_SPRITE) | |
316 | { | |
317 | glDisable (GL_POINT_SPRITE); | |
318 | } | |
a577aaab | 319 | } |
4e1523ef | 320 | glDisable (GL_TEXTURE_2D); |
bf75be98 | 321 | } |
05e2200b | 322 | #endif |
bf75be98 | 323 | break; |
324 | } | |
325 | default: break; | |
326 | } | |
327 | ||
328 | Handle(OpenGl_Texture) aPrevTexture = myTextureBound; | |
329 | myTextureBound.Nullify(); | |
330 | return aPrevTexture; | |
331 | } | |
332 | ||
333 | // ======================================================================= | |
334 | // function : setTextureParams | |
335 | // purpose : | |
336 | // ======================================================================= | |
337 | void OpenGl_Workspace::setTextureParams (Handle(OpenGl_Texture)& theTexture, | |
338 | const Handle(Graphic3d_TextureParams)& theParams) | |
339 | { | |
340 | const Handle(Graphic3d_TextureParams)& aParams = theParams.IsNull() ? theTexture->GetParams() : theParams; | |
341 | if (aParams.IsNull()) | |
342 | { | |
343 | return; | |
344 | } | |
345 | ||
ca3c13d1 | 346 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 347 | if (myGlContext->core11 != NULL) |
348 | { | |
4e1523ef | 349 | GLint anEnvMode = GL_MODULATE; // lighting mode |
350 | if (!aParams->IsModulate()) | |
076ca35c | 351 | { |
4e1523ef | 352 | anEnvMode = GL_DECAL; |
353 | if (theTexture->GetFormat() == GL_ALPHA | |
354 | || theTexture->GetFormat() == GL_LUMINANCE) | |
355 | { | |
356 | anEnvMode = GL_REPLACE; | |
357 | } | |
076ca35c | 358 | } |
076ca35c | 359 | |
4e1523ef | 360 | // setup generation of texture coordinates |
361 | switch (aParams->GenMode()) | |
bf75be98 | 362 | { |
4e1523ef | 363 | case Graphic3d_TOTM_OBJECT: |
bf75be98 | 364 | { |
4e1523ef | 365 | glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
366 | glTexGenfv (GL_S, GL_OBJECT_PLANE, aParams->GenPlaneS().GetData()); | |
367 | if (theTexture->GetTarget() != GL_TEXTURE_1D) | |
368 | { | |
369 | glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); | |
370 | glTexGenfv (GL_T, GL_OBJECT_PLANE, aParams->GenPlaneT().GetData()); | |
371 | } | |
372 | break; | |
bf75be98 | 373 | } |
4e1523ef | 374 | case Graphic3d_TOTM_SPHERE: |
bf75be98 | 375 | { |
4e1523ef | 376 | glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); |
377 | if (theTexture->GetTarget() != GL_TEXTURE_1D) | |
378 | { | |
379 | glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); | |
380 | } | |
381 | break; | |
bf75be98 | 382 | } |
4e1523ef | 383 | case Graphic3d_TOTM_EYE: |
384 | { | |
385 | myGlContext->WorldViewState.Push(); | |
c827ea3a | 386 | |
4e1523ef | 387 | myGlContext->WorldViewState.SetIdentity(); |
388 | myGlContext->ApplyWorldViewMatrix(); | |
bf75be98 | 389 | |
4e1523ef | 390 | glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
391 | glTexGenfv (GL_S, GL_EYE_PLANE, aParams->GenPlaneS().GetData()); | |
bf75be98 | 392 | |
4e1523ef | 393 | if (theTexture->GetTarget() != GL_TEXTURE_1D) |
394 | { | |
395 | glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); | |
396 | glTexGenfv (GL_T, GL_EYE_PLANE, aParams->GenPlaneT().GetData()); | |
397 | } | |
c827ea3a | 398 | |
4e1523ef | 399 | myGlContext->WorldViewState.Pop(); |
c827ea3a | 400 | |
4e1523ef | 401 | break; |
402 | } | |
403 | case Graphic3d_TOTM_SPRITE: | |
a577aaab | 404 | { |
c357e426 | 405 | if (myGlContext->core20fwd != NULL) |
4e1523ef | 406 | { |
407 | glEnable (GL_POINT_SPRITE); | |
408 | glTexEnvi (GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE); | |
409 | anEnvMode = GL_REPLACE; | |
c357e426 | 410 | myGlContext->core15->glPointParameteri (GL_POINT_SPRITE_COORD_ORIGIN, GL_LOWER_LEFT); |
4e1523ef | 411 | } |
412 | break; | |
a577aaab | 413 | } |
4e1523ef | 414 | case Graphic3d_TOTM_MANUAL: |
415 | default: break; | |
a577aaab | 416 | } |
bf75be98 | 417 | |
4e1523ef | 418 | // setup lighting |
419 | glTexEnvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, anEnvMode); | |
420 | } | |
ca3c13d1 | 421 | #endif |
bf75be98 | 422 | |
25ef750e | 423 | // get active sampler object to override default texture parameters |
424 | const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler(); | |
425 | ||
bf75be98 | 426 | // setup texture filtering and wrapping |
427 | //if (theTexture->GetParams() != theParams) | |
428 | const GLenum aFilter = (aParams->Filter() == Graphic3d_TOTF_NEAREST) ? GL_NEAREST : GL_LINEAR; | |
ca3c13d1 | 429 | const GLenum aWrapMode = aParams->IsRepeat() ? GL_REPEAT : myGlContext->TextureWrapClamp(); |
bf75be98 | 430 | switch (theTexture->GetTarget()) |
431 | { | |
ca3c13d1 | 432 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 433 | case GL_TEXTURE_1D: |
434 | { | |
25ef750e | 435 | if (aSampler.IsNull() || !aSampler->IsValid()) |
436 | { | |
437 | glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, aFilter); | |
438 | glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, aFilter); | |
439 | glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, aWrapMode); | |
440 | } | |
441 | else | |
442 | { | |
443 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter); | |
444 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilter); | |
445 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode); | |
446 | } | |
447 | ||
bf75be98 | 448 | break; |
449 | } | |
ca3c13d1 | 450 | #endif |
bf75be98 | 451 | case GL_TEXTURE_2D: |
452 | { | |
453 | GLenum aFilterMin = aFilter; | |
454 | if (theTexture->HasMipmaps()) | |
455 | { | |
456 | aFilterMin = GL_NEAREST_MIPMAP_NEAREST; | |
457 | if (aParams->Filter() == Graphic3d_TOTF_BILINEAR) | |
458 | { | |
459 | aFilterMin = GL_LINEAR_MIPMAP_NEAREST; | |
460 | } | |
461 | else if (aParams->Filter() == Graphic3d_TOTF_TRILINEAR) | |
462 | { | |
463 | aFilterMin = GL_LINEAR_MIPMAP_LINEAR; | |
464 | } | |
465 | ||
466 | if (myGlContext->extAnis) | |
467 | { | |
468 | // setup degree of anisotropy filter | |
469 | const GLint aMaxDegree = myGlContext->MaxDegreeOfAnisotropy(); | |
25ef750e | 470 | GLint aDegree; |
bf75be98 | 471 | switch (aParams->AnisoFilter()) |
472 | { | |
473 | case Graphic3d_LOTA_QUALITY: | |
474 | { | |
25ef750e | 475 | aDegree = aMaxDegree; |
bf75be98 | 476 | break; |
477 | } | |
478 | case Graphic3d_LOTA_MIDDLE: | |
479 | { | |
25ef750e | 480 | aDegree = (aMaxDegree <= 4) ? 2 : (aMaxDegree / 2); |
bf75be98 | 481 | break; |
482 | } | |
483 | case Graphic3d_LOTA_FAST: | |
484 | { | |
25ef750e | 485 | aDegree = 2; |
bf75be98 | 486 | break; |
487 | } | |
488 | case Graphic3d_LOTA_OFF: | |
489 | default: | |
490 | { | |
25ef750e | 491 | aDegree = 1; |
bf75be98 | 492 | break; |
493 | } | |
494 | } | |
25ef750e | 495 | |
496 | if (aSampler.IsNull() || !aSampler->IsValid()) | |
497 | { | |
498 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree); | |
499 | } | |
500 | else | |
501 | { | |
502 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAX_ANISOTROPY_EXT, aDegree); | |
503 | } | |
bf75be98 | 504 | } |
505 | } | |
25ef750e | 506 | |
507 | if (aSampler.IsNull() || !aSampler->IsValid()) | |
508 | { | |
509 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, aFilterMin); | |
510 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, aFilter); | |
511 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, aWrapMode); | |
512 | glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, aWrapMode); | |
513 | } | |
514 | else | |
515 | { | |
516 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MIN_FILTER, aFilterMin); | |
517 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_MAG_FILTER, aFilter); | |
518 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_S, aWrapMode); | |
519 | aSampler->SetParameter (*myGlContext, GL_TEXTURE_WRAP_T, aWrapMode); | |
520 | } | |
521 | ||
bf75be98 | 522 | break; |
523 | } | |
524 | default: break; | |
525 | } | |
526 | ||
527 | switch (theTexture->GetTarget()) | |
528 | { | |
ca3c13d1 | 529 | #if !defined(GL_ES_VERSION_2_0) |
bf75be98 | 530 | case GL_TEXTURE_1D: |
531 | { | |
4e1523ef | 532 | if (myGlContext->core11 != NULL) |
bf75be98 | 533 | { |
4e1523ef | 534 | if (aParams->GenMode() != Graphic3d_TOTM_MANUAL) |
535 | { | |
536 | glEnable (GL_TEXTURE_GEN_S); | |
537 | } | |
538 | glEnable (GL_TEXTURE_1D); | |
bf75be98 | 539 | } |
bf75be98 | 540 | break; |
541 | } | |
ca3c13d1 | 542 | #endif |
bf75be98 | 543 | case GL_TEXTURE_2D: |
544 | { | |
ca3c13d1 | 545 | #if !defined(GL_ES_VERSION_2_0) |
4e1523ef | 546 | if (myGlContext->core11 != NULL) |
bf75be98 | 547 | { |
4e1523ef | 548 | if (aParams->GenMode() != Graphic3d_TOTM_MANUAL) |
549 | { | |
550 | glEnable (GL_TEXTURE_GEN_S); | |
551 | glEnable (GL_TEXTURE_GEN_T); | |
552 | } | |
553 | glEnable (GL_TEXTURE_2D); | |
bf75be98 | 554 | } |
05e2200b | 555 | #endif |
bf75be98 | 556 | break; |
557 | } | |
558 | default: break; | |
559 | } | |
560 | ||
bf75be98 | 561 | theTexture->SetParams (aParams); |
562 | } | |
563 | ||
564 | // ======================================================================= | |
565 | // function : EnableTexture | |
566 | // purpose : | |
567 | // ======================================================================= | |
568 | Handle(OpenGl_Texture) OpenGl_Workspace::EnableTexture (const Handle(OpenGl_Texture)& theTexture, | |
569 | const Handle(Graphic3d_TextureParams)& theParams) | |
570 | { | |
571 | if (theTexture.IsNull() || !theTexture->IsValid()) | |
572 | { | |
573 | return DisableTexture(); | |
574 | } | |
575 | ||
bca1d6e2 | 576 | if (myTextureBound == theTexture |
577 | && (theParams.IsNull() || theParams == theTexture->GetParams())) | |
bf75be98 | 578 | { |
bca1d6e2 | 579 | // already bound |
580 | return myTextureBound; | |
bf75be98 | 581 | } |
582 | ||
583 | Handle(OpenGl_Texture) aPrevTexture = DisableTexture(); | |
584 | myTextureBound = theTexture; | |
585 | myTextureBound->Bind (myGlContext); | |
586 | setTextureParams (myTextureBound, theParams); | |
587 | ||
25ef750e | 588 | // If custom sampler object is available it will be |
589 | // used for overriding default texture parameters | |
590 | const Handle(OpenGl_Sampler)& aSampler = myGlContext->TextureSampler(); | |
591 | ||
592 | if (!aSampler.IsNull() && aSampler->IsValid()) | |
593 | { | |
594 | aSampler->Bind (*myGlContext); | |
595 | } | |
596 | ||
bf75be98 | 597 | return aPrevTexture; |
598 | } | |
a174a3c5 | 599 | |
38a0206f | 600 | // ======================================================================= |
c357e426 | 601 | // function : TelUpdatePolygonOffsets |
38a0206f | 602 | // purpose : |
603 | // ======================================================================= | |
c357e426 | 604 | static void TelUpdatePolygonOffsets (const TEL_POFFSET_PARAM& theOffsetData) |
38a0206f | 605 | { |
c357e426 | 606 | if ((theOffsetData.mode & Aspect_POM_Fill) == Aspect_POM_Fill) |
38a0206f | 607 | { |
c357e426 | 608 | glEnable (GL_POLYGON_OFFSET_FILL); |
38a0206f | 609 | } |
610 | else | |
611 | { | |
c357e426 | 612 | glDisable (GL_POLYGON_OFFSET_FILL); |
38a0206f | 613 | } |
38a0206f | 614 | |
c357e426 | 615 | #if !defined(GL_ES_VERSION_2_0) |
616 | if ((theOffsetData.mode & Aspect_POM_Line) == Aspect_POM_Line) | |
62e1beed | 617 | { |
c357e426 | 618 | glEnable (GL_POLYGON_OFFSET_LINE); |
62e1beed | 619 | } |
620 | else | |
621 | { | |
c357e426 | 622 | glDisable (GL_POLYGON_OFFSET_LINE); |
62e1beed | 623 | } |
c357e426 | 624 | |
625 | if ((theOffsetData.mode & Aspect_POM_Point) == Aspect_POM_Point) | |
626 | { | |
627 | glEnable (GL_POLYGON_OFFSET_POINT); | |
628 | } | |
629 | else | |
630 | { | |
631 | glDisable (GL_POLYGON_OFFSET_POINT); | |
632 | } | |
633 | #endif | |
634 | ||
635 | glPolygonOffset (theOffsetData.factor, theOffsetData.units); | |
62e1beed | 636 | } |
637 | ||
38a0206f | 638 | // ======================================================================= |
c357e426 | 639 | // function : updateMaterial |
38a0206f | 640 | // purpose : |
641 | // ======================================================================= | |
c357e426 | 642 | void OpenGl_Workspace::updateMaterial (const int theFlag) |
38a0206f | 643 | { |
c357e426 | 644 | // Case of hidden line |
645 | if (AspectFace_set->InteriorStyle() == Aspect_IS_HIDDENLINE) | |
f978241f | 646 | { |
c357e426 | 647 | myAspectFaceHl = *AspectFace_set; // copy all values including line edge aspect |
648 | myAspectFaceHl.ChangeIntFront().matcol = myView->BackgroundColor(); | |
649 | myAspectFaceHl.ChangeIntFront().color_mask = 0; | |
650 | myAspectFaceHl.ChangeIntFront().color_mask = 0; | |
651 | ||
652 | AspectFace_set = &myAspectFaceHl; | |
653 | return; | |
f978241f | 654 | } |
38a0206f | 655 | |
c357e426 | 656 | const OPENGL_SURF_PROP* aProps = &AspectFace_set->IntFront(); |
657 | GLenum aFace = GL_FRONT_AND_BACK; | |
658 | if (theFlag == TEL_BACK_MATERIAL) | |
38a0206f | 659 | { |
c357e426 | 660 | aFace = GL_BACK; |
661 | aProps = &AspectFace_set->IntBack(); | |
38a0206f | 662 | } |
c357e426 | 663 | else if (AspectFace_set->DistinguishingMode() == TOn |
664 | && !(NamedStatus & OPENGL_NS_RESMAT)) | |
38a0206f | 665 | { |
c357e426 | 666 | aFace = GL_FRONT; |
38a0206f | 667 | } |
38a0206f | 668 | |
c357e426 | 669 | myMatTmp.Init (*aProps); |
670 | ||
671 | // handling transparency | |
672 | if (NamedStatus & OPENGL_NS_2NDPASSDO) | |
38a0206f | 673 | { |
c357e426 | 674 | // second pass |
675 | myMatTmp.Diffuse.a() = aProps->env_reflexion; | |
676 | } | |
677 | else | |
678 | { | |
679 | if (aProps->env_reflexion != 0.0f) | |
38a0206f | 680 | { |
c357e426 | 681 | // if the material reflects the environment scene, the second pass is needed |
682 | NamedStatus |= OPENGL_NS_2NDPASSNEED; | |
38a0206f | 683 | } |
38a0206f | 684 | |
c357e426 | 685 | if (aProps->trans != 1.0f) |
38a0206f | 686 | { |
c357e426 | 687 | // render transparent |
688 | myMatTmp.Diffuse.a() = aProps->trans; | |
689 | glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
690 | glEnable (GL_BLEND); | |
691 | if (myUseDepthWrite) | |
692 | { | |
693 | glDepthMask (GL_FALSE); | |
694 | } | |
38a0206f | 695 | } |
696 | else | |
697 | { | |
c357e426 | 698 | // render opaque |
699 | if ((NamedStatus & OPENGL_NS_ANTIALIASING) == 0) | |
700 | { | |
701 | glBlendFunc (GL_ONE, GL_ZERO); | |
702 | glDisable (GL_BLEND); | |
703 | } | |
704 | if (myUseDepthWrite) | |
705 | { | |
706 | glDepthMask (GL_TRUE); | |
707 | } | |
38a0206f | 708 | } |
709 | } | |
c357e426 | 710 | |
711 | // do not update material properties in case of zero reflection mode, | |
712 | // because GL lighting will be disabled by OpenGl_PrimitiveArray::DrawArray() anyway. | |
713 | if (aProps->color_mask == 0) | |
38a0206f | 714 | { |
c357e426 | 715 | return; |
716 | } | |
38a0206f | 717 | |
c357e426 | 718 | // reset material |
719 | if (NamedStatus & OPENGL_NS_RESMAT) | |
720 | { | |
721 | #if !defined(GL_ES_VERSION_2_0) | |
722 | if (myGlContext->core11 != NULL) | |
723 | { | |
724 | myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData()); | |
725 | myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData()); | |
726 | myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData()); | |
727 | myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData()); | |
728 | myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine()); | |
729 | } | |
730 | #endif | |
38a0206f | 731 | |
c357e426 | 732 | if (theFlag == TEL_FRONT_MATERIAL) |
733 | { | |
734 | myMatFront = myMatTmp; | |
735 | myMatBack = myMatTmp; | |
736 | } | |
737 | else | |
38a0206f | 738 | { |
c357e426 | 739 | myMatBack = myMatTmp; |
740 | } | |
38a0206f | 741 | |
c357e426 | 742 | NamedStatus &= ~OPENGL_NS_RESMAT; |
743 | return; | |
744 | } | |
38a0206f | 745 | |
c357e426 | 746 | // reduce updates |
747 | OpenGl_Material& anOld = (theFlag == TEL_FRONT_MATERIAL) | |
748 | ? myMatFront | |
749 | : myMatBack; | |
750 | #if !defined(GL_ES_VERSION_2_0) | |
751 | if (myGlContext->core11 != NULL) | |
752 | { | |
753 | if (myMatTmp.Ambient.r() != anOld.Ambient.r() | |
754 | || myMatTmp.Ambient.g() != anOld.Ambient.g() | |
755 | || myMatTmp.Ambient.b() != anOld.Ambient.b()) | |
756 | { | |
757 | myGlContext->core11->glMaterialfv (aFace, GL_AMBIENT, myMatTmp.Ambient.GetData()); | |
38a0206f | 758 | } |
c357e426 | 759 | if (myMatTmp.Diffuse.r() != anOld.Diffuse.r() |
760 | || myMatTmp.Diffuse.g() != anOld.Diffuse.g() | |
761 | || myMatTmp.Diffuse.b() != anOld.Diffuse.b() | |
762 | || fabs (myMatTmp.Diffuse.a() - anOld.Diffuse.a()) > 0.01f) | |
38a0206f | 763 | { |
c357e426 | 764 | myGlContext->core11->glMaterialfv (aFace, GL_DIFFUSE, myMatTmp.Diffuse.GetData()); |
765 | } | |
766 | if (myMatTmp.Specular.r() != anOld.Specular.r() | |
767 | || myMatTmp.Specular.g() != anOld.Specular.g() | |
768 | || myMatTmp.Specular.b() != anOld.Specular.b()) | |
769 | { | |
770 | myGlContext->core11->glMaterialfv (aFace, GL_SPECULAR, myMatTmp.Specular.GetData()); | |
771 | } | |
772 | if (myMatTmp.Emission.r() != anOld.Emission.r() | |
773 | || myMatTmp.Emission.g() != anOld.Emission.g() | |
774 | || myMatTmp.Emission.b() != anOld.Emission.b()) | |
775 | { | |
776 | myGlContext->core11->glMaterialfv (aFace, GL_EMISSION, myMatTmp.Emission.GetData()); | |
777 | } | |
778 | if (myMatTmp.Shine() != anOld.Shine()) | |
779 | { | |
780 | myGlContext->core11->glMaterialf (aFace, GL_SHININESS, myMatTmp.Shine()); | |
38a0206f | 781 | } |
782 | } | |
c357e426 | 783 | #endif |
784 | anOld = myMatTmp; | |
785 | if (aFace == GL_FRONT_AND_BACK) | |
786 | { | |
787 | myMatBack = myMatTmp; | |
788 | } | |
38a0206f | 789 | } |
790 | ||
791 | // ======================================================================= | |
c357e426 | 792 | // function : SetAspectLine |
38a0206f | 793 | // purpose : |
794 | // ======================================================================= | |
c357e426 | 795 | const OpenGl_AspectLine * OpenGl_Workspace::SetAspectLine(const OpenGl_AspectLine *AnAspect) |
38a0206f | 796 | { |
c357e426 | 797 | const OpenGl_AspectLine *AspectLine_old = AspectLine_set; |
798 | AspectLine_set = AnAspect; | |
799 | return AspectLine_old; | |
800 | } | |
38a0206f | 801 | |
c357e426 | 802 | // ======================================================================= |
803 | // function : SetAspectFace | |
804 | // purpose : | |
805 | // ======================================================================= | |
806 | const OpenGl_AspectFace * OpenGl_Workspace::SetAspectFace(const OpenGl_AspectFace *AnAspect) | |
807 | { | |
808 | const OpenGl_AspectFace *AspectFace_old = AspectFace_set; | |
809 | AspectFace_set = AnAspect; | |
810 | return AspectFace_old; | |
811 | } | |
38a0206f | 812 | |
c357e426 | 813 | // ======================================================================= |
814 | // function : SetAspectMarker | |
815 | // purpose : | |
816 | // ======================================================================= | |
817 | const OpenGl_AspectMarker * OpenGl_Workspace::SetAspectMarker(const OpenGl_AspectMarker *AnAspect) | |
818 | { | |
819 | const OpenGl_AspectMarker *AspectMarker_old = AspectMarker_set; | |
820 | AspectMarker_set = AnAspect; | |
821 | return AspectMarker_old; | |
822 | } | |
f978241f | 823 | |
c357e426 | 824 | // ======================================================================= |
825 | // function : SetAspectText | |
826 | // purpose : | |
827 | // ======================================================================= | |
828 | const OpenGl_AspectText * OpenGl_Workspace::SetAspectText(const OpenGl_AspectText *AnAspect) | |
829 | { | |
830 | const OpenGl_AspectText *AspectText_old = AspectText_set; | |
831 | AspectText_set = AnAspect; | |
832 | return AspectText_old; | |
833 | } | |
834 | ||
835 | // ======================================================================= | |
836 | // function : AspectLine | |
837 | // purpose : | |
838 | // ======================================================================= | |
839 | const OpenGl_AspectLine * OpenGl_Workspace::AspectLine(const Standard_Boolean theWithApply) | |
840 | { | |
841 | if (theWithApply) | |
f978241f | 842 | { |
c357e426 | 843 | AspectLine_applied = AspectLine_set; |
f978241f | 844 | } |
845 | ||
c357e426 | 846 | return AspectLine_set; |
847 | } | |
38a0206f | 848 | |
c357e426 | 849 | // ======================================================================= |
850 | // function : AspectFace | |
851 | // purpose : | |
852 | // ======================================================================= | |
853 | const OpenGl_AspectFace* OpenGl_Workspace::AspectFace (const Standard_Boolean theToApply) | |
854 | { | |
855 | if (!theToApply) | |
856 | { | |
857 | return AspectFace_set; | |
858 | } | |
38a0206f | 859 | |
c357e426 | 860 | if (myView->BackfacingModel() == Graphic3d_TOBM_AUTOMATIC) |
38a0206f | 861 | { |
c357e426 | 862 | // manage back face culling mode, disable culling when clipping is enabled |
863 | TelCullMode aCullingMode = (myGlContext->Clipping().IsClippingOrCappingOn() | |
864 | || AspectFace_set->InteriorStyle() == Aspect_IS_HATCH) | |
865 | ? TelCullNone | |
866 | : (TelCullMode )AspectFace_set->CullingMode(); | |
867 | if (aCullingMode != TelCullNone | |
868 | && !(NamedStatus & OPENGL_NS_2NDPASSDO)) | |
f978241f | 869 | { |
c357e426 | 870 | // disable culling in case of translucent shading aspect |
871 | if (AspectFace_set->IntFront().trans != 1.0f) | |
f978241f | 872 | { |
c357e426 | 873 | aCullingMode = TelCullNone; |
874 | } | |
875 | } | |
876 | if (myCullingMode != aCullingMode) | |
877 | { | |
878 | myCullingMode = aCullingMode; | |
879 | switch (myCullingMode) | |
880 | { | |
881 | case TelCullNone: | |
882 | case TelCullUndefined: | |
f978241f | 883 | { |
c357e426 | 884 | glDisable (GL_CULL_FACE); |
f978241f | 885 | break; |
886 | } | |
c357e426 | 887 | case TelCullFront: |
f978241f | 888 | { |
c357e426 | 889 | glCullFace (GL_FRONT); |
890 | glEnable (GL_CULL_FACE); | |
f978241f | 891 | break; |
892 | } | |
c357e426 | 893 | case TelCullBack: |
f978241f | 894 | { |
c357e426 | 895 | glCullFace (GL_BACK); |
896 | glEnable (GL_CULL_FACE); | |
f978241f | 897 | break; |
898 | } | |
899 | } | |
f978241f | 900 | } |
f978241f | 901 | } |
902 | ||
c357e426 | 903 | if (AspectFace_set == AspectFace_applied) |
a2e4f780 | 904 | { |
c357e426 | 905 | return AspectFace_set; |
a2e4f780 | 906 | } |
907 | ||
c357e426 | 908 | #if !defined(GL_ES_VERSION_2_0) |
909 | const Aspect_InteriorStyle anIntstyle = AspectFace_set->InteriorStyle(); | |
910 | if (AspectFace_applied == NULL || AspectFace_applied->InteriorStyle() != anIntstyle) | |
b86bb3df | 911 | { |
c357e426 | 912 | switch (anIntstyle) |
760c21c2 | 913 | { |
c357e426 | 914 | case Aspect_IS_EMPTY: |
915 | case Aspect_IS_HOLLOW: | |
ee51a9fe | 916 | { |
c357e426 | 917 | glPolygonMode (GL_FRONT_AND_BACK, GL_LINE); |
918 | break; | |
ee51a9fe | 919 | } |
c357e426 | 920 | case Aspect_IS_HATCH: |
62e1beed | 921 | { |
c357e426 | 922 | glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); |
923 | myLineAttribs->SetTypeOfHatch (AspectFace_applied != NULL ? AspectFace_applied->Hatch() : TEL_HS_SOLID); | |
924 | break; | |
62e1beed | 925 | } |
c357e426 | 926 | case Aspect_IS_SOLID: |
927 | case Aspect_IS_HIDDENLINE: | |
928 | { | |
929 | glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); | |
930 | if (myGlContext->core11 != NULL) | |
931 | { | |
932 | glDisable (GL_POLYGON_STIPPLE); | |
933 | } | |
934 | break; | |
935 | } | |
936 | case Aspect_IS_POINT: | |
38a0206f | 937 | { |
c357e426 | 938 | glPolygonMode (GL_FRONT_AND_BACK, GL_POINT); |
939 | break; | |
38a0206f | 940 | } |
38a0206f | 941 | } |
91c60b57 | 942 | } |
e276548b | 943 | |
c357e426 | 944 | if (anIntstyle == Aspect_IS_HATCH) |
91c60b57 | 945 | { |
c357e426 | 946 | const Tint hatchstyle = AspectFace_set->Hatch(); |
947 | if (AspectFace_applied == NULL || AspectFace_applied->Hatch() != hatchstyle) | |
f978241f | 948 | { |
c357e426 | 949 | myLineAttribs->SetTypeOfHatch (hatchstyle); |
38a0206f | 950 | } |
c357e426 | 951 | } |
952 | #endif | |
38a0206f | 953 | |
c357e426 | 954 | // Aspect_POM_None means: do not change current settings |
955 | if ((AspectFace_set->PolygonOffset().mode & Aspect_POM_None) != Aspect_POM_None) | |
956 | { | |
957 | if (PolygonOffset_applied.mode != AspectFace_set->PolygonOffset().mode | |
958 | || PolygonOffset_applied.factor != AspectFace_set->PolygonOffset().factor | |
959 | || PolygonOffset_applied.units != AspectFace_set->PolygonOffset().units) | |
38a0206f | 960 | { |
c357e426 | 961 | SetPolygonOffset (AspectFace_set->PolygonOffset().mode, |
962 | AspectFace_set->PolygonOffset().factor, | |
963 | AspectFace_set->PolygonOffset().units); | |
38a0206f | 964 | } |
965 | } | |
c357e426 | 966 | |
967 | updateMaterial (TEL_FRONT_MATERIAL); | |
968 | if (AspectFace_set->DistinguishingMode() == TOn) | |
a174a3c5 | 969 | { |
c357e426 | 970 | updateMaterial (TEL_BACK_MATERIAL); |
971 | } | |
62e1beed | 972 | |
83da37b1 | 973 | if (AspectFace_set->DoTextureMap()) |
c357e426 | 974 | { |
83da37b1 | 975 | EnableTexture (AspectFace_set->TextureRes (myGlContext), |
976 | AspectFace_set->TextureParams()); | |
977 | } | |
978 | else | |
979 | { | |
980 | if (!myEnvironmentTexture.IsNull()) | |
38a0206f | 981 | { |
83da37b1 | 982 | EnableTexture (myEnvironmentTexture, |
983 | myEnvironmentTexture->GetParams()); | |
38a0206f | 984 | } |
c357e426 | 985 | else |
62e1beed | 986 | { |
c357e426 | 987 | DisableTexture(); |
62e1beed | 988 | } |
a174a3c5 | 989 | } |
990 | ||
c357e426 | 991 | AspectFace_applied = AspectFace_set; |
992 | return AspectFace_set; | |
993 | } | |
a174a3c5 | 994 | |
c357e426 | 995 | //======================================================================= |
996 | //function : SetPolygonOffset | |
997 | //purpose : | |
998 | //======================================================================= | |
999 | void OpenGl_Workspace::SetPolygonOffset (int theMode, | |
1000 | Standard_ShortReal theFactor, | |
1001 | Standard_ShortReal theUnits) | |
1002 | { | |
1003 | PolygonOffset_applied.mode = theMode; | |
1004 | PolygonOffset_applied.factor = theFactor; | |
1005 | PolygonOffset_applied.units = theUnits; | |
a174a3c5 | 1006 | |
c357e426 | 1007 | TelUpdatePolygonOffsets (PolygonOffset_applied); |
1008 | } | |
38a0206f | 1009 | |
c357e426 | 1010 | // ======================================================================= |
1011 | // function : AspectMarker | |
1012 | // purpose : | |
1013 | // ======================================================================= | |
1014 | const OpenGl_AspectMarker* OpenGl_Workspace::AspectMarker (const Standard_Boolean theToApply) | |
1015 | { | |
1016 | if (theToApply && (AspectMarker_set != AspectMarker_applied)) | |
ee51a9fe | 1017 | { |
c357e426 | 1018 | if (!AspectMarker_applied || (AspectMarker_set->Scale() != AspectMarker_applied->Scale())) |
ee51a9fe | 1019 | { |
c357e426 | 1020 | #if !defined(GL_ES_VERSION_2_0) |
1021 | glPointSize (AspectMarker_set->Scale()); | |
1022 | #ifdef HAVE_GL2PS | |
1023 | gl2psPointSize (AspectMarker_set->Scale()); | |
1024 | #endif | |
1025 | #endif | |
ee51a9fe | 1026 | } |
c357e426 | 1027 | AspectMarker_applied = AspectMarker_set; |
ee51a9fe | 1028 | } |
c357e426 | 1029 | return AspectMarker_set; |
a174a3c5 | 1030 | } |
679ecdee | 1031 | |
1032 | // ======================================================================= | |
c357e426 | 1033 | // function : AspectText |
679ecdee | 1034 | // purpose : |
1035 | // ======================================================================= | |
c357e426 | 1036 | const OpenGl_AspectText* OpenGl_Workspace::AspectText (const Standard_Boolean theWithApply) |
679ecdee | 1037 | { |
c357e426 | 1038 | if (theWithApply) |
679ecdee | 1039 | { |
c357e426 | 1040 | AspectText_applied = AspectText_set; |
38a0206f | 1041 | } |
1042 | ||
c357e426 | 1043 | return AspectText_set; |
679ecdee | 1044 | } |
1045 | ||
1046 | // ======================================================================= | |
c357e426 | 1047 | // function : Width |
679ecdee | 1048 | // purpose : |
1049 | // ======================================================================= | |
c357e426 | 1050 | Standard_Integer OpenGl_Workspace::Width() const |
679ecdee | 1051 | { |
c357e426 | 1052 | return !myView->GlWindow().IsNull() ? myView->GlWindow()->Width() : 0; |
1053 | } | |
679ecdee | 1054 | |
c357e426 | 1055 | // ======================================================================= |
1056 | // function : Height | |
1057 | // purpose : | |
1058 | // ======================================================================= | |
1059 | Standard_Integer OpenGl_Workspace::Height() const | |
1060 | { | |
1061 | return !myView->GlWindow().IsNull() ? myView->GlWindow()->Height() : 0; | |
1062 | } | |
679ecdee | 1063 | |
c357e426 | 1064 | // ======================================================================= |
1065 | // function : UseGLLight | |
1066 | // purpose : | |
1067 | // ======================================================================= | |
1068 | Standard_Boolean OpenGl_Workspace::UseGLLight() const | |
1069 | { | |
1070 | return myView->IsGLLightEnabled(); | |
1071 | } | |
679ecdee | 1072 | |
c357e426 | 1073 | // ======================================================================= |
1074 | // function : AntiAliasingMode | |
1075 | // purpose : | |
1076 | // ======================================================================= | |
1077 | Standard_Integer OpenGl_Workspace::AntiAliasingMode() const | |
1078 | { | |
1079 | return myView->IsAntialiasingEnabled(); | |
1080 | } | |
679ecdee | 1081 | |
c357e426 | 1082 | // ======================================================================= |
1083 | // function : IsCullingEnabled | |
1084 | // purpose : | |
1085 | // ======================================================================= | |
1086 | Standard_Boolean OpenGl_Workspace::IsCullingEnabled() const | |
1087 | { | |
1088 | return myView->IsCullingEnabled(); | |
679ecdee | 1089 | } |
1090 | ||
1091 | // ======================================================================= | |
c357e426 | 1092 | // function : FBOCreate |
679ecdee | 1093 | // purpose : |
1094 | // ======================================================================= | |
b128c892 | 1095 | Handle(OpenGl_FrameBuffer) OpenGl_Workspace::FBOCreate (const Standard_Integer theWidth, |
1096 | const Standard_Integer theHeight) | |
679ecdee | 1097 | { |
c357e426 | 1098 | // activate OpenGL context |
1099 | if (!Activate()) | |
b128c892 | 1100 | return Handle(OpenGl_FrameBuffer)(); |
c357e426 | 1101 | |
83da37b1 | 1102 | DisableTexture(); |
1103 | ||
c357e426 | 1104 | // create the FBO |
1105 | const Handle(OpenGl_Context)& aCtx = GetGlContext(); | |
b128c892 | 1106 | Handle(OpenGl_FrameBuffer) aFrameBuffer = new OpenGl_FrameBuffer(); |
3c4b62a4 | 1107 | if (!aFrameBuffer->Init (aCtx, theWidth, theHeight, GL_RGBA8, GL_DEPTH24_STENCIL8, 0)) |
679ecdee | 1108 | { |
c357e426 | 1109 | aFrameBuffer->Release (aCtx.operator->()); |
b128c892 | 1110 | return Handle(OpenGl_FrameBuffer)(); |
679ecdee | 1111 | } |
b128c892 | 1112 | return aFrameBuffer; |
679ecdee | 1113 | } |
1114 | ||
1115 | // ======================================================================= | |
c357e426 | 1116 | // function : FBORelease |
679ecdee | 1117 | // purpose : |
1118 | // ======================================================================= | |
b128c892 | 1119 | void OpenGl_Workspace::FBORelease (Handle(OpenGl_FrameBuffer)& theFbo) |
679ecdee | 1120 | { |
c357e426 | 1121 | // activate OpenGL context |
1122 | if (!Activate() | |
b128c892 | 1123 | || theFbo.IsNull()) |
62e1beed | 1124 | { |
62e1beed | 1125 | return; |
1126 | } | |
c357e426 | 1127 | |
b128c892 | 1128 | theFbo->Release (GetGlContext().operator->()); |
1129 | theFbo.Nullify(); | |
c357e426 | 1130 | } |
62e1beed | 1131 | |
c357e426 | 1132 | inline bool getDataFormat (const Image_PixMap& theData, |
1133 | GLenum& thePixelFormat, | |
1134 | GLenum& theDataType) | |
1135 | { | |
1136 | thePixelFormat = GL_RGB; | |
1137 | theDataType = GL_UNSIGNED_BYTE; | |
1138 | switch (theData.Format()) | |
38a0206f | 1139 | { |
c357e426 | 1140 | #if !defined(GL_ES_VERSION_2_0) |
1141 | case Image_PixMap::ImgGray: | |
1142 | thePixelFormat = GL_DEPTH_COMPONENT; | |
1143 | theDataType = GL_UNSIGNED_BYTE; | |
1144 | return true; | |
1145 | case Image_PixMap::ImgGrayF: | |
1146 | thePixelFormat = GL_DEPTH_COMPONENT; | |
1147 | theDataType = GL_FLOAT; | |
1148 | return true; | |
1149 | case Image_PixMap::ImgBGR: | |
1150 | thePixelFormat = GL_BGR; | |
1151 | theDataType = GL_UNSIGNED_BYTE; | |
1152 | return true; | |
1153 | case Image_PixMap::ImgBGRA: | |
1154 | case Image_PixMap::ImgBGR32: | |
1155 | thePixelFormat = GL_BGRA; | |
1156 | theDataType = GL_UNSIGNED_BYTE; | |
1157 | return true; | |
1158 | case Image_PixMap::ImgBGRF: | |
1159 | thePixelFormat = GL_BGR; | |
1160 | theDataType = GL_FLOAT; | |
1161 | return true; | |
1162 | case Image_PixMap::ImgBGRAF: | |
1163 | thePixelFormat = GL_BGRA; | |
1164 | theDataType = GL_FLOAT; | |
1165 | return true; | |
1166 | #else | |
1167 | case Image_PixMap::ImgGray: | |
1168 | case Image_PixMap::ImgGrayF: | |
1169 | case Image_PixMap::ImgBGR: | |
1170 | case Image_PixMap::ImgBGRA: | |
1171 | case Image_PixMap::ImgBGR32: | |
1172 | case Image_PixMap::ImgBGRF: | |
1173 | case Image_PixMap::ImgBGRAF: | |
1174 | return false; | |
1175 | #endif | |
1176 | case Image_PixMap::ImgRGB: | |
1177 | thePixelFormat = GL_RGB; | |
1178 | theDataType = GL_UNSIGNED_BYTE; | |
1179 | return true; | |
1180 | case Image_PixMap::ImgRGBA: | |
1181 | case Image_PixMap::ImgRGB32: | |
1182 | thePixelFormat = GL_RGBA; | |
1183 | theDataType = GL_UNSIGNED_BYTE; | |
1184 | return true; | |
1185 | case Image_PixMap::ImgRGBF: | |
1186 | thePixelFormat = GL_RGB; | |
1187 | theDataType = GL_FLOAT; | |
1188 | return true; | |
1189 | case Image_PixMap::ImgRGBAF: | |
1190 | thePixelFormat = GL_RGBA; | |
1191 | theDataType = GL_FLOAT; | |
1192 | return true; | |
1193 | case Image_PixMap::ImgAlpha: | |
1194 | case Image_PixMap::ImgAlphaF: | |
1195 | return false; // GL_ALPHA is no more supported in core context | |
1196 | case Image_PixMap::ImgUNKNOWN: | |
1197 | return false; | |
38a0206f | 1198 | } |
c357e426 | 1199 | return false; |
1200 | } | |
38a0206f | 1201 | |
c357e426 | 1202 | // ======================================================================= |
1203 | // function : getAligned | |
1204 | // purpose : | |
1205 | // ======================================================================= | |
1206 | inline Standard_Size getAligned (const Standard_Size theNumber, | |
1207 | const Standard_Size theAlignment) | |
1208 | { | |
1209 | return theNumber + theAlignment - 1 - (theNumber - 1) % theAlignment; | |
1210 | } | |
1211 | ||
1212 | // ======================================================================= | |
1213 | // function : BufferDump | |
1214 | // purpose : | |
1215 | // ======================================================================= | |
b128c892 | 1216 | Standard_Boolean OpenGl_Workspace::BufferDump (const Handle(OpenGl_FrameBuffer)& theFbo, |
1217 | Image_PixMap& theImage, | |
1218 | const Graphic3d_BufferType& theBufferType) | |
c357e426 | 1219 | { |
1220 | GLenum aFormat, aType; | |
1221 | if (theImage.IsEmpty() | |
1222 | || !getDataFormat (theImage, aFormat, aType) | |
1223 | || !Activate()) | |
38a0206f | 1224 | { |
c357e426 | 1225 | return Standard_False; |
38a0206f | 1226 | } |
c357e426 | 1227 | #if !defined(GL_ES_VERSION_2_0) |
1228 | GLint aReadBufferPrev = GL_BACK; | |
1229 | if (theBufferType == Graphic3d_BT_Depth | |
1230 | && aFormat != GL_DEPTH_COMPONENT) | |
38a0206f | 1231 | { |
c357e426 | 1232 | return Standard_False; |
1233 | } | |
20aeeb7b | 1234 | #else |
1235 | (void )theBufferType; | |
c357e426 | 1236 | #endif |
38a0206f | 1237 | |
c357e426 | 1238 | // bind FBO if used |
b128c892 | 1239 | if (!theFbo.IsNull() && theFbo->IsValid()) |
c357e426 | 1240 | { |
b128c892 | 1241 | theFbo->BindBuffer (GetGlContext()); |
38a0206f | 1242 | } |
1243 | else | |
a2e4f780 | 1244 | { |
38a0206f | 1245 | #if !defined(GL_ES_VERSION_2_0) |
c357e426 | 1246 | glGetIntegerv (GL_READ_BUFFER, &aReadBufferPrev); |
1247 | GLint aDrawBufferPrev = GL_BACK; | |
1248 | glGetIntegerv (GL_DRAW_BUFFER, &aDrawBufferPrev); | |
1249 | glReadBuffer (aDrawBufferPrev); | |
38a0206f | 1250 | #endif |
a2e4f780 | 1251 | } |
1252 | ||
c357e426 | 1253 | // setup alignment |
1254 | const GLint anAligment = Min (GLint(theImage.MaxRowAligmentBytes()), 8); // limit to 8 bytes for OpenGL | |
1255 | glPixelStorei (GL_PACK_ALIGNMENT, anAligment); | |
1256 | bool isBatchCopy = !theImage.IsTopDown(); | |
38a0206f | 1257 | |
c357e426 | 1258 | const GLint anExtraBytes = GLint(theImage.RowExtraBytes()); |
1259 | GLint aPixelsWidth = GLint(theImage.SizeRowBytes() / theImage.SizePixelBytes()); | |
1260 | Standard_Size aSizeRowBytesEstim = getAligned (theImage.SizePixelBytes() * aPixelsWidth, anAligment); | |
1261 | if (anExtraBytes < anAligment) | |
b86bb3df | 1262 | { |
c357e426 | 1263 | aPixelsWidth = 0; |
ee51a9fe | 1264 | } |
c357e426 | 1265 | else if (aSizeRowBytesEstim != theImage.SizeRowBytes()) |
ee51a9fe | 1266 | { |
c357e426 | 1267 | aPixelsWidth = 0; |
1268 | isBatchCopy = false; | |
ee51a9fe | 1269 | } |
c357e426 | 1270 | #if !defined(GL_ES_VERSION_2_0) |
1271 | glPixelStorei (GL_PACK_ROW_LENGTH, aPixelsWidth); | |
1272 | #else | |
1273 | if (aPixelsWidth != 0) | |
ee51a9fe | 1274 | { |
c357e426 | 1275 | isBatchCopy = false; |
b86bb3df | 1276 | } |
c357e426 | 1277 | #endif |
1278 | ||
1279 | if (!isBatchCopy) | |
679ecdee | 1280 | { |
c357e426 | 1281 | // copy row by row |
1282 | for (Standard_Size aRow = 0; aRow < theImage.SizeY(); ++aRow) | |
679ecdee | 1283 | { |
c357e426 | 1284 | // Image_PixMap rows indexation always starts from the upper corner |
1285 | // while order in memory depends on the flag and processed by ChangeRow() method | |
1286 | glReadPixels (0, GLint(theImage.SizeY() - aRow - 1), GLsizei (theImage.SizeX()), 1, aFormat, aType, theImage.ChangeRow (aRow)); | |
679ecdee | 1287 | } |
679ecdee | 1288 | } |
1289 | else | |
1290 | { | |
c357e426 | 1291 | glReadPixels (0, 0, GLsizei (theImage.SizeX()), GLsizei (theImage.SizeY()), aFormat, aType, theImage.ChangeData()); |
679ecdee | 1292 | } |
a1954302 | 1293 | |
c357e426 | 1294 | glPixelStorei (GL_PACK_ALIGNMENT, 1); |
eae454e3 | 1295 | #if !defined(GL_ES_VERSION_2_0) |
c357e426 | 1296 | glPixelStorei (GL_PACK_ROW_LENGTH, 0); |
eae454e3 | 1297 | #endif |
a1954302 | 1298 | |
b128c892 | 1299 | if (!theFbo.IsNull() && theFbo->IsValid()) |
c357e426 | 1300 | { |
b128c892 | 1301 | theFbo->UnbindBuffer (GetGlContext()); |
c357e426 | 1302 | } |
1303 | else | |
1304 | { | |
1305 | #if !defined(GL_ES_VERSION_2_0) | |
1306 | glReadBuffer (aReadBufferPrev); | |
1307 | #endif | |
1308 | } | |
1309 | return Standard_True; | |
679ecdee | 1310 | } |
91c60b57 | 1311 | |
91c60b57 | 1312 | // ======================================================================= |
1313 | // function : CanRender | |
1314 | // purpose : | |
1315 | // ======================================================================= | |
1316 | Standard_Boolean OpenGl_RaytraceFilter::CanRender (const OpenGl_Element* theElement) | |
1317 | { | |
1318 | Standard_Boolean aPrevFilterResult = Standard_True; | |
1319 | if (!myPrevRenderFilter.IsNull()) | |
1320 | { | |
1321 | aPrevFilterResult = myPrevRenderFilter->CanRender (theElement); | |
1322 | } | |
1323 | return aPrevFilterResult && | |
1324 | !OpenGl_Raytrace::IsRaytracedElement (theElement); | |
1325 | } |