Commit | Line | Data |
---|---|---|
b311480e | 1 | // Created on: 2011-08-01 |
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 | |
4269bd1b | 16 | #include <OpenGl_CappingAlgo.hxx> |
30f0ad28 | 17 | #include <OpenGl_GlCore11.hxx> |
25c35042 | 18 | #include <OpenGl_ClippingIterator.hxx> |
30f0ad28 | 19 | #include <OpenGl_ShaderManager.hxx> |
20 | #include <OpenGl_ShaderProgram.hxx> | |
2bd4bfac | 21 | #include <OpenGl_StructureShadow.hxx> |
30f0ad28 | 22 | #include <OpenGl_Vec.hxx> |
23 | #include <OpenGl_View.hxx> | |
24 | #include <OpenGl_Workspace.hxx> | |
2166f0fa | 25 | |
92efcf78 | 26 | IMPLEMENT_STANDARD_RTTIEXT(OpenGl_Structure,Graphic3d_CStructure) |
27 | ||
98b15dbf | 28 | // ======================================================================= |
29 | // function : renderBoundingBox | |
30 | // purpose : | |
31 | // ======================================================================= | |
32 | void OpenGl_Structure::renderBoundingBox (const Handle(OpenGl_Workspace)& theWorkspace) const | |
27eed937 | 33 | { |
98b15dbf | 34 | if (!myBndBox.IsValid()) |
27eed937 | 35 | { |
98b15dbf | 36 | return; |
27eed937 | 37 | } |
38 | ||
98b15dbf | 39 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); |
72f6dc61 | 40 | const Handle(OpenGl_TextureSet) aPrevTexture = aCtx->BindTextures (Handle(OpenGl_TextureSet)(), Handle(OpenGl_ShaderProgram)()); |
98b15dbf | 41 | const Graphic3d_ZLayerSettings& aLayer = myGraphicDriver->ZLayerSettings (myZLayer); |
42 | const Graphic3d_Vec3d aMoveVec = myTrsfPers.IsNull() | |
43 | && !aLayer.OriginTransformation().IsNull() | |
44 | ? -Graphic3d_Vec3d (aLayer.Origin().X(), aLayer.Origin().Y(), aLayer.Origin().Z()) | |
45 | : Graphic3d_Vec3d (0.0, 0.0, 0.0); | |
46 | if (aCtx->core20fwd != NULL | |
47 | && aCtx->ShaderManager()->BindBoundBoxProgram()) | |
48 | { | |
49 | const Graphic3d_Vec3d aCenter = myBndBox.Center() + aMoveVec; | |
50 | const Graphic3d_Vec3d aSize = myBndBox.Size(); | |
51 | aCtx->ActiveProgram()->SetUniform (aCtx, "occBBoxCenter", Graphic3d_Vec3 ((float )aCenter.x(), (float )aCenter.y(), (float )aCenter.z())); | |
52 | aCtx->ActiveProgram()->SetUniform (aCtx, "occBBoxSize", Graphic3d_Vec3 ((float )aSize.x(), (float )aSize.y(), (float )aSize.z())); | |
bf5f0ca2 | 53 | aCtx->SetColor4fv (theWorkspace->InteriorColor()); |
98b15dbf | 54 | |
55 | const Handle(OpenGl_VertexBuffer)& aBoundBoxVertBuffer = aCtx->ShaderManager()->BoundBoxVertBuffer(); | |
56 | aBoundBoxVertBuffer->BindAttribute (aCtx, Graphic3d_TOA_POS); | |
57 | aCtx->core20fwd->glDrawArrays (GL_LINES, 0, aBoundBoxVertBuffer->GetElemsNb()); | |
58 | aBoundBoxVertBuffer->UnbindAttribute(aCtx, Graphic3d_TOA_POS); | |
59 | } | |
43eddb47 | 60 | else if (aCtx->core11ffp != NULL) |
27eed937 | 61 | { |
98b15dbf | 62 | const Graphic3d_Vec3d aMind = myBndBox.CornerMin() + aMoveVec; |
63 | const Graphic3d_Vec3d aMaxd = myBndBox.CornerMax() + aMoveVec; | |
64 | const Graphic3d_Vec3 aMin ((float )aMind.x(), (float )aMind.y(), (float )aMind.z()); | |
65 | const Graphic3d_Vec3 aMax ((float )aMaxd.x(), (float )aMaxd.y(), (float )aMaxd.z()); | |
66 | const OpenGl_Vec3 aVerts[16] = | |
67 | { | |
68 | OpenGl_Vec3 (aMin.x(), aMin.y(), aMin.z()), | |
69 | OpenGl_Vec3 (aMin.x(), aMin.y(), aMax.z()), | |
70 | OpenGl_Vec3 (aMin.x(), aMax.y(), aMax.z()), | |
71 | OpenGl_Vec3 (aMin.x(), aMax.y(), aMin.z()), | |
72 | OpenGl_Vec3 (aMin.x(), aMin.y(), aMin.z()), | |
73 | OpenGl_Vec3 (aMax.x(), aMin.y(), aMin.z()), | |
74 | OpenGl_Vec3 (aMax.x(), aMin.y(), aMax.z()), | |
75 | OpenGl_Vec3 (aMax.x(), aMax.y(), aMax.z()), | |
76 | OpenGl_Vec3 (aMax.x(), aMax.y(), aMin.z()), | |
77 | OpenGl_Vec3 (aMax.x(), aMin.y(), aMin.z()), | |
78 | OpenGl_Vec3 (aMax.x(), aMax.y(), aMin.z()), | |
79 | OpenGl_Vec3 (aMin.x(), aMax.y(), aMin.z()), | |
80 | OpenGl_Vec3 (aMin.x(), aMax.y(), aMax.z()), | |
81 | OpenGl_Vec3 (aMax.x(), aMax.y(), aMax.z()), | |
82 | OpenGl_Vec3 (aMax.x(), aMin.y(), aMax.z()), | |
83 | OpenGl_Vec3 (aMin.x(), aMin.y(), aMax.z()) | |
84 | }; | |
85 | ||
13b36bb1 | 86 | aCtx->ShaderManager()->BindLineProgram (Handle(OpenGl_TextureSet)(), Aspect_TOL_SOLID, Graphic3d_TypeOfShadingModel_Unlit, Graphic3d_AlphaMode_Opaque, false, Handle(OpenGl_ShaderProgram)()); |
bf5f0ca2 | 87 | aCtx->SetColor4fv (theWorkspace->InteriorColor()); |
98b15dbf | 88 | aCtx->core11fwd->glDisable (GL_LIGHTING); |
43eddb47 | 89 | aCtx->core11ffp->glEnableClientState (GL_VERTEX_ARRAY); |
90 | aCtx->core11ffp->glVertexPointer (3, GL_FLOAT, 0, aVerts[0].GetData()); | |
98b15dbf | 91 | aCtx->core11fwd->glDrawArrays (GL_LINE_STRIP, 0, 16); |
43eddb47 | 92 | aCtx->core11ffp->glDisableClientState (GL_VERTEX_ARRAY); |
27eed937 | 93 | } |
72f6dc61 | 94 | aCtx->BindTextures (aPrevTexture, Handle(OpenGl_ShaderProgram)()); |
98b15dbf | 95 | } |
2166f0fa | 96 | |
4269bd1b | 97 | // ======================================================================= |
98 | // function : OpenGl_Structure | |
99 | // purpose : | |
100 | // ======================================================================= | |
63bcc448 | 101 | OpenGl_Structure::OpenGl_Structure (const Handle(Graphic3d_StructureManager)& theManager) |
102 | : Graphic3d_CStructure (theManager), | |
d4aaad5b | 103 | myInstancedStructure (NULL), |
104 | myIsRaytracable (Standard_False), | |
105 | myModificationState (0), | |
d4aaad5b | 106 | myIsMirrored (Standard_False) |
2166f0fa | 107 | { |
7c3ef2f7 | 108 | updateLayerTransformation(); |
2166f0fa SK |
109 | } |
110 | ||
4269bd1b | 111 | // ======================================================================= |
112 | // function : ~OpenGl_Structure | |
113 | // purpose : | |
114 | // ======================================================================= | |
5e27df78 | 115 | OpenGl_Structure::~OpenGl_Structure() |
2166f0fa | 116 | { |
5e27df78 | 117 | Release (Handle(OpenGl_Context)()); |
2166f0fa SK |
118 | } |
119 | ||
7c3ef2f7 | 120 | // ======================================================================= |
121 | // function : SetZLayer | |
122 | // purpose : | |
123 | // ======================================================================= | |
124 | void OpenGl_Structure::SetZLayer (const Graphic3d_ZLayerId theLayerIndex) | |
125 | { | |
126 | Graphic3d_CStructure::SetZLayer (theLayerIndex); | |
127 | updateLayerTransformation(); | |
128 | } | |
129 | ||
63bcc448 | 130 | // ======================================================================= |
1f7f5a90 | 131 | // function : SetTransformation |
63bcc448 | 132 | // purpose : |
133 | // ======================================================================= | |
7f24b768 | 134 | void OpenGl_Structure::SetTransformation (const Handle(TopLoc_Datum3D)& theTrsf) |
63bcc448 | 135 | { |
1f7f5a90 | 136 | myTrsf = theTrsf; |
137 | myIsMirrored = Standard_False; | |
138 | if (!myTrsf.IsNull()) | |
139 | { | |
140 | // Determinant of transform matrix less then 0 means that mirror transform applied. | |
7f24b768 | 141 | const gp_Trsf& aTrsf = myTrsf->Transformation(); |
142 | const Standard_Real aDet = aTrsf.Value(1, 1) * (aTrsf.Value (2, 2) * aTrsf.Value (3, 3) - aTrsf.Value (3, 2) * aTrsf.Value (2, 3)) | |
143 | - aTrsf.Value(1, 2) * (aTrsf.Value (2, 1) * aTrsf.Value (3, 3) - aTrsf.Value (3, 1) * aTrsf.Value (2, 3)) | |
144 | + aTrsf.Value(1, 3) * (aTrsf.Value (2, 1) * aTrsf.Value (3, 2) - aTrsf.Value (3, 1) * aTrsf.Value (2, 2)); | |
1f7f5a90 | 145 | myIsMirrored = aDet < 0.0; |
146 | } | |
7d9e854b | 147 | |
7c3ef2f7 | 148 | updateLayerTransformation(); |
d4aaad5b | 149 | if (IsRaytracable()) |
e276548b | 150 | { |
d4aaad5b | 151 | ++myModificationState; |
e276548b | 152 | } |
2166f0fa SK |
153 | } |
154 | ||
7c3ef2f7 | 155 | // ======================================================================= |
156 | // function : SetTransformPersistence | |
157 | // purpose : | |
158 | // ======================================================================= | |
159 | void OpenGl_Structure::SetTransformPersistence (const Handle(Graphic3d_TransformPers)& theTrsfPers) | |
160 | { | |
4552cb85 | 161 | if ((myTrsfPers.IsNull() || theTrsfPers.IsNull()) && myTrsfPers != theTrsfPers) |
162 | { | |
163 | ++myModificationState; | |
164 | } | |
7c3ef2f7 | 165 | myTrsfPers = theTrsfPers; |
166 | updateLayerTransformation(); | |
167 | } | |
168 | ||
169 | // ======================================================================= | |
170 | // function : updateLayerTransformation | |
171 | // purpose : | |
172 | // ======================================================================= | |
173 | void OpenGl_Structure::updateLayerTransformation() | |
174 | { | |
175 | gp_Trsf aRenderTrsf; | |
176 | if (!myTrsf.IsNull()) | |
177 | { | |
178 | aRenderTrsf = myTrsf->Trsf(); | |
179 | } | |
180 | ||
181 | const Graphic3d_ZLayerSettings& aLayer = myGraphicDriver->ZLayerSettings (myZLayer); | |
182 | if (!aLayer.OriginTransformation().IsNull() | |
183 | && myTrsfPers.IsNull()) | |
184 | { | |
185 | aRenderTrsf.SetTranslationPart (aRenderTrsf.TranslationPart() - aLayer.Origin()); | |
186 | } | |
187 | aRenderTrsf.GetMat4 (myRenderTrsf); | |
188 | } | |
189 | ||
4269bd1b | 190 | // ======================================================================= |
8e5fb5ea | 191 | // function : GraphicHighlight |
4269bd1b | 192 | // purpose : |
193 | // ======================================================================= | |
98b15dbf | 194 | void OpenGl_Structure::GraphicHighlight (const Handle(Graphic3d_PresentationAttributes)& theStyle) |
2166f0fa | 195 | { |
8e5fb5ea | 196 | myHighlightStyle = theStyle; |
8e5fb5ea | 197 | highlight = 1; |
2166f0fa SK |
198 | } |
199 | ||
4269bd1b | 200 | // ======================================================================= |
8e5fb5ea | 201 | // function : GraphicUnhighlight |
4269bd1b | 202 | // purpose : |
203 | // ======================================================================= | |
8e5fb5ea | 204 | void OpenGl_Structure::GraphicUnhighlight() |
2166f0fa | 205 | { |
8e5fb5ea | 206 | highlight = 0; |
8e5fb5ea | 207 | myHighlightStyle.Nullify(); |
2166f0fa SK |
208 | } |
209 | ||
e276548b | 210 | // ======================================================================= |
a1954302 | 211 | // function : OnVisibilityChanged |
e276548b | 212 | // purpose : |
213 | // ======================================================================= | |
a1954302 | 214 | void OpenGl_Structure::OnVisibilityChanged() |
e276548b | 215 | { |
d4aaad5b | 216 | if (IsRaytracable()) |
e276548b | 217 | { |
d4aaad5b | 218 | ++myModificationState; |
e276548b | 219 | } |
e276548b | 220 | } |
221 | ||
e276548b | 222 | // ======================================================================= |
d4aaad5b | 223 | // function : IsRaytracable |
e276548b | 224 | // purpose : |
225 | // ======================================================================= | |
d4aaad5b | 226 | Standard_Boolean OpenGl_Structure::IsRaytracable() const |
e276548b | 227 | { |
3e05329c | 228 | if (!myGroups.IsEmpty() |
4552cb85 | 229 | && myIsRaytracable |
230 | && myTrsfPers.IsNull()) | |
e276548b | 231 | { |
3e05329c | 232 | return Standard_True; |
e276548b | 233 | } |
d5af8626 | 234 | |
3e05329c | 235 | return myInstancedStructure != NULL |
236 | && myInstancedStructure->IsRaytracable(); | |
d5af8626 | 237 | } |
238 | ||
e276548b | 239 | // ======================================================================= |
d4aaad5b | 240 | // function : UpdateRaytracableState |
e276548b | 241 | // purpose : |
242 | // ======================================================================= | |
d4aaad5b | 243 | void OpenGl_Structure::UpdateStateIfRaytracable (const Standard_Boolean toCheck) const |
e276548b | 244 | { |
3e05329c | 245 | myIsRaytracable = !toCheck; |
246 | if (!myIsRaytracable) | |
247 | { | |
248 | for (OpenGl_Structure::GroupIterator anIter (myGroups); anIter.More(); anIter.Next()) | |
249 | { | |
250 | if (anIter.Value()->IsRaytracable()) | |
251 | { | |
252 | myIsRaytracable = Standard_True; | |
253 | break; | |
254 | } | |
255 | } | |
256 | } | |
e276548b | 257 | |
d4aaad5b | 258 | if (IsRaytracable()) |
e276548b | 259 | { |
d4aaad5b | 260 | ++myModificationState; |
e276548b | 261 | } |
262 | } | |
263 | ||
4269bd1b | 264 | // ======================================================================= |
265 | // function : Connect | |
266 | // purpose : | |
267 | // ======================================================================= | |
63bcc448 | 268 | void OpenGl_Structure::Connect (Graphic3d_CStructure& theStructure) |
2166f0fa | 269 | { |
d4aaad5b | 270 | OpenGl_Structure* aStruct = static_cast<OpenGl_Structure*> (&theStructure); |
271 | ||
272 | Standard_ASSERT_RAISE (myInstancedStructure == NULL || myInstancedStructure == aStruct, | |
273 | "Error! Instanced structure is already defined"); | |
274 | ||
275 | myInstancedStructure = aStruct; | |
e276548b | 276 | |
63bcc448 | 277 | if (aStruct->IsRaytracable()) |
e276548b | 278 | { |
d4aaad5b | 279 | UpdateStateIfRaytracable (Standard_False); |
e276548b | 280 | } |
2166f0fa SK |
281 | } |
282 | ||
4269bd1b | 283 | // ======================================================================= |
284 | // function : Disconnect | |
285 | // purpose : | |
286 | // ======================================================================= | |
63bcc448 | 287 | void OpenGl_Structure::Disconnect (Graphic3d_CStructure& theStructure) |
2166f0fa | 288 | { |
d4aaad5b | 289 | OpenGl_Structure* aStruct = static_cast<OpenGl_Structure*> (&theStructure); |
e276548b | 290 | |
d4aaad5b | 291 | if (myInstancedStructure == aStruct) |
292 | { | |
293 | myInstancedStructure = NULL; | |
e276548b | 294 | |
d4aaad5b | 295 | if (aStruct->IsRaytracable()) |
296 | { | |
297 | UpdateStateIfRaytracable(); | |
2166f0fa | 298 | } |
2166f0fa SK |
299 | } |
300 | } | |
301 | ||
4269bd1b | 302 | // ======================================================================= |
b64d84be | 303 | // function : NewGroup |
4269bd1b | 304 | // purpose : |
305 | // ======================================================================= | |
b64d84be | 306 | Handle(Graphic3d_Group) OpenGl_Structure::NewGroup (const Handle(Graphic3d_Structure)& theStruct) |
2166f0fa | 307 | { |
b64d84be | 308 | Handle(OpenGl_Group) aGroup = new OpenGl_Group (theStruct); |
309 | myGroups.Append (aGroup); | |
310 | return aGroup; | |
2166f0fa SK |
311 | } |
312 | ||
4269bd1b | 313 | // ======================================================================= |
314 | // function : RemoveGroup | |
315 | // purpose : | |
316 | // ======================================================================= | |
b64d84be | 317 | void OpenGl_Structure::RemoveGroup (const Handle(Graphic3d_Group)& theGroup) |
2166f0fa | 318 | { |
b64d84be | 319 | if (theGroup.IsNull()) |
320 | { | |
321 | return; | |
322 | } | |
323 | ||
324 | for (Graphic3d_SequenceOfGroup::Iterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) | |
2166f0fa SK |
325 | { |
326 | // Check for the given group | |
b64d84be | 327 | if (aGroupIter.Value() == theGroup) |
2166f0fa | 328 | { |
d4aaad5b | 329 | const Standard_Boolean wasRaytracable = |
330 | static_cast<const OpenGl_Group&> (*theGroup).IsRaytracable(); | |
331 | ||
b64d84be | 332 | theGroup->Clear (Standard_False); |
e276548b | 333 | |
d4aaad5b | 334 | if (wasRaytracable) |
e276548b | 335 | { |
d4aaad5b | 336 | UpdateStateIfRaytracable(); |
e276548b | 337 | } |
e276548b | 338 | |
b64d84be | 339 | myGroups.Remove (aGroupIter); |
2166f0fa SK |
340 | return; |
341 | } | |
2166f0fa SK |
342 | } |
343 | } | |
344 | ||
63bcc448 | 345 | // ======================================================================= |
346 | // function : Clear | |
347 | // purpose : | |
348 | // ======================================================================= | |
349 | void OpenGl_Structure::Clear() | |
350 | { | |
351 | Clear (GlDriver()->GetSharedContext()); | |
352 | } | |
353 | ||
4269bd1b | 354 | // ======================================================================= |
355 | // function : Clear | |
356 | // purpose : | |
357 | // ======================================================================= | |
5e27df78 | 358 | void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx) |
2166f0fa | 359 | { |
e276548b | 360 | Standard_Boolean aRaytracableGroupDeleted (Standard_False); |
e276548b | 361 | |
5e27df78 | 362 | // Release groups |
b64d84be | 363 | for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
2166f0fa | 364 | { |
b64d84be | 365 | aRaytracableGroupDeleted |= aGroupIter.Value()->IsRaytracable(); |
5322131b | 366 | |
2166f0fa | 367 | // Delete objects |
b64d84be | 368 | aGroupIter.ChangeValue()->Release (theGlCtx); |
2166f0fa SK |
369 | } |
370 | myGroups.Clear(); | |
e276548b | 371 | |
e276548b | 372 | if (aRaytracableGroupDeleted) |
373 | { | |
d4aaad5b | 374 | myIsRaytracable = Standard_False; |
e276548b | 375 | } |
b7cd4ba7 | 376 | |
377 | Is2dText = Standard_False; | |
378 | IsForHighlight = Standard_False; | |
2166f0fa SK |
379 | } |
380 | ||
0717ddc1 | 381 | // ======================================================================= |
cc6852f3 | 382 | // function : renderGeometry |
0717ddc1 | 383 | // purpose : |
384 | // ======================================================================= | |
cc6852f3 | 385 | void OpenGl_Structure::renderGeometry (const Handle(OpenGl_Workspace)& theWorkspace, |
386 | bool& theHasClosed) const | |
0717ddc1 | 387 | { |
cc6852f3 | 388 | if (myInstancedStructure != NULL) |
0717ddc1 | 389 | { |
cc6852f3 | 390 | myInstancedStructure->renderGeometry (theWorkspace, theHasClosed); |
391 | } | |
392 | ||
4e993e4d | 393 | bool anOldCastShadows = false; |
394 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); | |
cc6852f3 | 395 | for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
396 | { | |
4e993e4d | 397 | const OpenGl_Group* aGroup = aGroupIter.Value(); |
398 | ||
399 | const Handle(Graphic3d_TransformPers)& aTrsfPers = aGroup->TransformPersistence(); | |
400 | if (!aTrsfPers.IsNull()) | |
401 | { | |
402 | applyPersistence (aCtx, aTrsfPers, true, anOldCastShadows); | |
403 | aCtx->ApplyModelViewMatrix(); | |
404 | } | |
405 | ||
406 | theHasClosed = theHasClosed || aGroup->IsClosed(); | |
407 | aGroup->Render (theWorkspace); | |
408 | ||
409 | if (!aTrsfPers.IsNull()) | |
410 | { | |
411 | revertPersistence (aCtx, aTrsfPers, true, anOldCastShadows); | |
412 | aCtx->ApplyModelViewMatrix(); | |
413 | } | |
0717ddc1 | 414 | } |
415 | } | |
cc6852f3 | 416 | |
4269bd1b | 417 | // ======================================================================= |
418 | // function : Render | |
419 | // purpose : | |
420 | // ======================================================================= | |
7d9e854b | 421 | void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &theWorkspace) const |
2166f0fa SK |
422 | { |
423 | // Process the structure only if visible | |
a1954302 | 424 | if (!visible) |
7d9e854b | 425 | { |
2166f0fa | 426 | return; |
7d9e854b | 427 | } |
2166f0fa | 428 | |
c827ea3a | 429 | const Handle(OpenGl_Context)& aCtx = theWorkspace->GetGlContext(); |
b7cd4ba7 | 430 | |
2166f0fa | 431 | // Render named status |
98b15dbf | 432 | if (highlight && !myHighlightStyle.IsNull() && myHighlightStyle->Method() != Aspect_TOHM_BOUNDBOX) |
a1954302 | 433 | { |
f838dac4 | 434 | theWorkspace->SetHighlightStyle (myHighlightStyle); |
a1954302 | 435 | } |
2166f0fa | 436 | |
2166f0fa | 437 | // Apply local transformation |
6bd94e0d | 438 | aCtx->ModelWorldState.Push(); |
1f7f5a90 | 439 | OpenGl_Mat4& aModelWorld = aCtx->ModelWorldState.ChangeCurrent(); |
7c3ef2f7 | 440 | aModelWorld = myRenderTrsf; |
6bd94e0d | 441 | |
1d92133e | 442 | const Standard_Boolean anOldGlNormalize = aCtx->IsGlNormalizeEnabled(); |
8f7159cb | 443 | |
1d92133e | 444 | // detect scale transform |
43eddb47 | 445 | if (aCtx->core11ffp != NULL |
1f7f5a90 | 446 | && !myTrsf.IsNull()) |
1d92133e | 447 | { |
7f24b768 | 448 | const Standard_Real aScale = myTrsf->Trsf().ScaleFactor(); |
1f7f5a90 | 449 | if (Abs (aScale - 1.0) > Precision::Confusion()) |
1d92133e | 450 | { |
451 | aCtx->SetGlNormalizeEnabled (Standard_True); | |
452 | } | |
453 | } | |
1d92133e | 454 | |
d84e8669 | 455 | bool anOldCastShadows = false; |
2daa5d95 | 456 | #ifdef GL_DEPTH_CLAMP |
457 | bool toRestoreDepthClamp = false; | |
458 | #endif | |
778cd667 | 459 | if (!myTrsfPers.IsNull()) |
825aa485 | 460 | { |
4e993e4d | 461 | applyPersistence (aCtx, myTrsfPers, false, anOldCastShadows); |
2daa5d95 | 462 | |
463 | #ifdef GL_DEPTH_CLAMP | |
464 | if (myTrsfPers->Mode() == Graphic3d_TMF_CameraPers | |
465 | && aCtx->arbDepthClamp) | |
466 | { | |
467 | toRestoreDepthClamp = true; | |
468 | aCtx->core11fwd->glEnable (GL_DEPTH_CLAMP); | |
469 | } | |
470 | #endif | |
04be5003 | 471 | } |
472 | ||
d437b80d | 473 | // Take into account transform persistence |
474 | aCtx->ApplyModelViewMatrix(); | |
c827ea3a | 475 | |
f9ba5c4d | 476 | // remember aspects |
bf5f0ca2 | 477 | const OpenGl_Aspects* aPrevAspectFace = theWorkspace->Aspects(); |
7d9e854b | 478 | |
479 | // Apply correction for mirror transform | |
480 | if (myIsMirrored) | |
481 | { | |
c827ea3a | 482 | aCtx->core11fwd->glFrontFace (GL_CW); |
7d9e854b | 483 | } |
2166f0fa | 484 | |
b859a34d | 485 | // Collect clipping planes of structure scope |
25c35042 | 486 | aCtx->ChangeClipping().SetLocalPlanes (myClipPlanes); |
4269bd1b | 487 | |
1a7ece8f | 488 | // True if structure is fully clipped |
489 | bool isClipped = false; | |
3202bf1e | 490 | bool hasDisabled = false; |
491 | if (aCtx->Clipping().IsClippingOrCappingOn()) | |
1a7ece8f | 492 | { |
7c3ef2f7 | 493 | const Graphic3d_BndBox3d& aBBox = BoundingBox(); |
8b1441e3 | 494 | if (!myClipPlanes.IsNull() |
495 | && myClipPlanes->ToOverrideGlobal()) | |
1a7ece8f | 496 | { |
25c35042 | 497 | aCtx->ChangeClipping().DisableGlobal(); |
3202bf1e | 498 | hasDisabled = aCtx->Clipping().HasDisabled(); |
499 | } | |
8b1441e3 | 500 | else if (!myTrsfPers.IsNull()) |
501 | { | |
502 | if (myTrsfPers->IsZoomOrRotate()) | |
503 | { | |
504 | // Zoom/rotate persistence object lives in two worlds at the same time. | |
505 | // Global clipping planes can not be trivially applied without being converted | |
506 | // into local space of transformation persistence object. | |
507 | // As more simple alternative - just clip entire object by its anchor point defined in the world space. | |
508 | const gp_Pnt anAnchor = myTrsfPers->AnchorPoint(); | |
509 | for (OpenGl_ClippingIterator aPlaneIt (aCtx->Clipping()); aPlaneIt.More() && aPlaneIt.IsGlobal(); aPlaneIt.Next()) | |
510 | { | |
511 | const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value(); | |
512 | if (!aPlane->IsOn()) | |
513 | { | |
514 | continue; | |
515 | } | |
516 | ||
517 | // check for clipping | |
8b1441e3 | 518 | const Graphic3d_Vec4d aCheckPnt (anAnchor.X(), anAnchor.Y(), anAnchor.Z(), 1.0); |
25c35042 | 519 | if (aPlane->ProbePoint (aCheckPnt) == Graphic3d_ClipState_Out) |
8b1441e3 | 520 | { |
521 | isClipped = true; | |
522 | break; | |
523 | } | |
524 | } | |
525 | } | |
526 | ||
25c35042 | 527 | aCtx->ChangeClipping().DisableGlobal(); |
8b1441e3 | 528 | hasDisabled = aCtx->Clipping().HasDisabled(); |
529 | } | |
1a7ece8f | 530 | |
3202bf1e | 531 | // Set of clipping planes that do not intersect the structure, |
532 | // and thus can be disabled to improve rendering performance | |
533 | if (aBBox.IsValid() | |
778cd667 | 534 | && myTrsfPers.IsNull()) |
3202bf1e | 535 | { |
536 | for (OpenGl_ClippingIterator aPlaneIt (aCtx->Clipping()); aPlaneIt.More(); aPlaneIt.Next()) | |
1a7ece8f | 537 | { |
3202bf1e | 538 | const Handle(Graphic3d_ClipPlane)& aPlane = aPlaneIt.Value(); |
32ca7711 | 539 | if (aPlaneIt.IsDisabled()) |
3202bf1e | 540 | { |
541 | continue; | |
542 | } | |
1a7ece8f | 543 | |
25c35042 | 544 | const Graphic3d_ClipState aBoxState = aPlane->ProbeBox (aBBox); |
8e6ce38c | 545 | if (aBoxState == Graphic3d_ClipState_In) |
1a7ece8f | 546 | { |
25c35042 | 547 | aCtx->ChangeClipping().SetEnabled (aPlaneIt, false); |
3202bf1e | 548 | hasDisabled = true; |
1a7ece8f | 549 | } |
8e6ce38c | 550 | else if (aBoxState == Graphic3d_ClipState_Out && myBndBoxClipCheck) |
551 | { | |
552 | isClipped = true; | |
553 | break; | |
554 | } | |
1a7ece8f | 555 | } |
556 | } | |
3202bf1e | 557 | |
558 | if ((!myClipPlanes.IsNull() && !myClipPlanes->IsEmpty()) | |
559 | || hasDisabled) | |
560 | { | |
561 | // Set OCCT state uniform variables | |
562 | aCtx->ShaderManager()->UpdateClippingState(); | |
563 | } | |
1a7ece8f | 564 | } |
565 | ||
2166f0fa | 566 | // Render groups |
cc6852f3 | 567 | bool hasClosedPrims = false; |
1a7ece8f | 568 | if (!isClipped) |
569 | { | |
570 | renderGeometry (theWorkspace, hasClosedPrims); | |
571 | } | |
2166f0fa | 572 | |
7d9e854b | 573 | // Reset correction for mirror transform |
574 | if (myIsMirrored) | |
c827ea3a | 575 | { |
576 | aCtx->core11fwd->glFrontFace (GL_CCW); | |
577 | } | |
7d9e854b | 578 | |
b859a34d | 579 | // Render capping for structure groups |
cc6852f3 | 580 | if (hasClosedPrims |
1a7ece8f | 581 | && aCtx->Clipping().IsCappingOn()) |
b859a34d | 582 | { |
cc6852f3 | 583 | OpenGl_CappingAlgo::RenderCapping (theWorkspace, *this); |
b859a34d | 584 | } |
4269bd1b | 585 | |
b859a34d | 586 | // Revert structure clippings |
3202bf1e | 587 | if (hasDisabled) |
1a7ece8f | 588 | { |
589 | // enable planes that were previously disabled | |
25c35042 | 590 | aCtx->ChangeClipping().RestoreDisabled(); |
1a7ece8f | 591 | } |
25c35042 | 592 | aCtx->ChangeClipping().SetLocalPlanes (Handle(Graphic3d_SequenceOfHClipPlane)()); |
3202bf1e | 593 | if ((!myClipPlanes.IsNull() && !myClipPlanes->IsEmpty()) |
594 | || hasDisabled) | |
4269bd1b | 595 | { |
30f0ad28 | 596 | // Set OCCT state uniform variables |
deb02f86 | 597 | aCtx->ShaderManager()->RevertClippingState(); |
4269bd1b | 598 | } |
599 | ||
825aa485 | 600 | // Restore local transformation |
6bd94e0d | 601 | aCtx->ModelWorldState.Pop(); |
602 | aCtx->SetGlNormalizeEnabled (anOldGlNormalize); | |
c827ea3a | 603 | |
2166f0fa | 604 | // Restore aspects |
bf5f0ca2 | 605 | theWorkspace->SetAspects (aPrevAspectFace); |
2166f0fa | 606 | |
b7cd4ba7 | 607 | // Apply highlight box |
98b15dbf | 608 | if (!isClipped |
609 | && !myHighlightStyle.IsNull() | |
610 | && myHighlightStyle->Method() == Aspect_TOHM_BOUNDBOX) | |
b7cd4ba7 | 611 | { |
98b15dbf | 612 | aCtx->ApplyModelViewMatrix(); |
f838dac4 | 613 | theWorkspace->SetHighlightStyle (myHighlightStyle); |
98b15dbf | 614 | renderBoundingBox (theWorkspace); |
615 | } | |
616 | ||
617 | if (!myTrsfPers.IsNull()) | |
618 | { | |
4e993e4d | 619 | revertPersistence (aCtx, myTrsfPers, false, anOldCastShadows); |
2daa5d95 | 620 | #ifdef GL_DEPTH_CLAMP |
621 | if (toRestoreDepthClamp) { aCtx->core11fwd->glDisable (GL_DEPTH_CLAMP); } | |
622 | #endif | |
b7cd4ba7 | 623 | } |
624 | ||
2166f0fa | 625 | // Restore named status |
f838dac4 | 626 | theWorkspace->SetHighlightStyle (Handle(Graphic3d_PresentationAttributes)()); |
2166f0fa SK |
627 | } |
628 | ||
5e27df78 | 629 | // ======================================================================= |
630 | // function : Release | |
631 | // purpose : | |
632 | // ======================================================================= | |
633 | void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx) | |
634 | { | |
635 | // Release groups | |
636 | Clear (theGlCtx); | |
8e5fb5ea | 637 | myHighlightStyle.Nullify(); |
5e27df78 | 638 | } |
639 | ||
dd8a4ce9 | 640 | // ======================================================================= |
641 | // function : ReleaseGlResources | |
642 | // purpose : | |
643 | // ======================================================================= | |
644 | void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx) | |
645 | { | |
b64d84be | 646 | for (OpenGl_Structure::GroupIterator aGroupIter (myGroups); aGroupIter.More(); aGroupIter.Next()) |
dd8a4ce9 | 647 | { |
b64d84be | 648 | aGroupIter.ChangeValue()->Release (theGlCtx); |
dd8a4ce9 | 649 | } |
dd8a4ce9 | 650 | } |
651 | ||
679ecdee | 652 | //======================================================================= |
653 | //function : ShadowLink | |
654 | //purpose : | |
655 | //======================================================================= | |
656 | Handle(Graphic3d_CStructure) OpenGl_Structure::ShadowLink (const Handle(Graphic3d_StructureManager)& theManager) const | |
657 | { | |
658 | return new OpenGl_StructureShadow (theManager, this); | |
659 | } | |
bc73b006 | 660 | |
4e993e4d | 661 | // ======================================================================= |
662 | // function : applyPersistence | |
663 | // purpose : | |
664 | // ======================================================================= | |
665 | void OpenGl_Structure::applyPersistence (const Handle(OpenGl_Context)& theCtx, | |
666 | const Handle(Graphic3d_TransformPers)& theTrsfPers, | |
667 | const Standard_Boolean theIsLocal, | |
668 | Standard_Boolean& theOldCastShadows) const | |
669 | { | |
670 | // temporarily disable shadows on non-3d objects | |
671 | theOldCastShadows = theCtx->ShaderManager()->SetCastShadows (false); | |
672 | ||
673 | theCtx->WorldViewState.Push(); | |
674 | OpenGl_Mat4& aWorldView = theCtx->WorldViewState.ChangeCurrent(); | |
675 | ||
676 | if (theIsLocal | |
677 | && theTrsfPers->IsZoomOrRotate()) | |
678 | { | |
679 | // move anchor point to presentation location | |
680 | theCtx->ModelWorldState.Push(); | |
681 | OpenGl_Mat4& aModelWorld = theCtx->ModelWorldState.ChangeCurrent(); | |
682 | gp_Pnt aStartPnt = theTrsfPers->AnchorPoint(); | |
683 | Graphic3d_Vec4 anAnchorPoint = aModelWorld * Graphic3d_Vec4 ((Standard_ShortReal)aStartPnt.X(), | |
684 | (Standard_ShortReal)aStartPnt.Y(), | |
685 | (Standard_ShortReal)aStartPnt.Z(), 1.0f); | |
686 | aModelWorld.SetColumn (3, Graphic3d_Vec4 (Graphic3d_Vec3 (0.0), 1.0)); // reset translation part | |
687 | aStartPnt.SetCoord (anAnchorPoint.x(), anAnchorPoint.y(), anAnchorPoint.z()); | |
688 | ||
689 | theTrsfPers->Apply (theCtx->Camera(), | |
690 | theCtx->ProjectionState.Current(), aWorldView, | |
691 | theCtx->VirtualViewport()[2], theCtx->VirtualViewport()[3], | |
692 | &aStartPnt); | |
693 | } | |
694 | else | |
695 | { | |
696 | theTrsfPers->Apply (theCtx->Camera(), | |
697 | theCtx->ProjectionState.Current(), aWorldView, | |
698 | theCtx->VirtualViewport()[2], theCtx->VirtualViewport()[3]); | |
699 | } | |
700 | ||
4e993e4d | 701 | if (!theCtx->IsGlNormalizeEnabled() |
702 | && theCtx->core11ffp != NULL) | |
703 | { | |
704 | const Standard_Real aScale = Graphic3d_TransformUtils::ScaleFactor (aWorldView); | |
705 | if (Abs (aScale - 1.0) > Precision::Confusion()) | |
706 | { | |
707 | theCtx->SetGlNormalizeEnabled (true); | |
708 | } | |
709 | } | |
4e993e4d | 710 | } |
711 | ||
712 | // ======================================================================= | |
713 | // function : revertPersistence | |
714 | // purpose : | |
715 | // ======================================================================= | |
716 | void OpenGl_Structure::revertPersistence (const Handle(OpenGl_Context)& theCtx, | |
717 | const Handle(Graphic3d_TransformPers)& theTrsfPers, | |
718 | const Standard_Boolean theIsLocal, | |
719 | const Standard_Boolean theOldCastShadows) const | |
720 | { | |
721 | if (theIsLocal | |
722 | && theTrsfPers->IsZoomOrRotate()) | |
723 | { | |
724 | theCtx->ModelWorldState.Pop(); | |
725 | } | |
726 | theCtx->WorldViewState.Pop(); | |
727 | theCtx->ShaderManager()->SetCastShadows (theOldCastShadows); | |
728 | } | |
729 | ||
bc73b006 | 730 | //======================================================================= |
731 | //function : DumpJson | |
732 | //purpose : | |
733 | //======================================================================= | |
734 | void OpenGl_Structure::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const | |
735 | { | |
736 | OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream) | |
737 | ||
738 | OCCT_DUMP_BASE_CLASS (theOStream, theDepth, Graphic3d_CStructure) | |
739 | ||
740 | OCCT_DUMP_FIELD_VALUE_POINTER (theOStream, myInstancedStructure) | |
741 | ||
742 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsRaytracable) | |
743 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myModificationState) | |
744 | OCCT_DUMP_FIELD_VALUE_NUMERICAL (theOStream, myIsMirrored) | |
745 | } |