0023500: MFC texture example is crashed when view closed
[occt.git] / src / OpenGl / OpenGl_GraphicDriver_4.cxx
1 // Created on: 2011-10-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20
21 #include <OpenGl_GraphicDriver.hxx>
22
23 #include <NCollection_DataMap.hxx>
24 #include <OpenGl_Structure.hxx>
25 #include <OpenGl_CView.hxx>
26
27 void OpenGl_GraphicDriver::ClearStructure (const Graphic3d_CStructure& theCStructure)
28 {
29   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
30   if (aStructure == NULL)
31     return;
32
33   aStructure->Clear (GetSharedContext());
34   InvalidateAllWorkspaces();
35 }
36
37 void OpenGl_GraphicDriver::ContextStructure (const Graphic3d_CStructure& theCStructure)
38 {
39   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
40   if (aStructure == NULL)
41     return;
42
43   aStructure->SetTransformPersistence (theCStructure.TransformPersistence);
44
45   if (theCStructure.ContextLine.IsDef)
46     aStructure->SetAspectLine (theCStructure.ContextLine);
47
48   if (theCStructure.ContextFillArea.IsDef)
49     aStructure->SetAspectFace (theCStructure.ContextFillArea);
50
51   if (theCStructure.ContextMarker.IsDef)
52     aStructure->SetAspectMarker (theCStructure.ContextMarker);
53
54   if (theCStructure.ContextText.IsDef)
55     aStructure->SetAspectText (theCStructure.ContextText);
56
57   InvalidateAllWorkspaces();
58 }
59
60 void OpenGl_GraphicDriver::Connect (const Graphic3d_CStructure& theFather,
61                                     const Graphic3d_CStructure& theSon)
62 {
63   OpenGl_Structure* aFather = (OpenGl_Structure* )theFather.ptrStructure;
64   OpenGl_Structure* aSon = (OpenGl_Structure* )theSon.ptrStructure;
65   if (aFather && aSon)
66     aFather->Connect(aSon);
67 }
68
69 void OpenGl_GraphicDriver::Disconnect (const Graphic3d_CStructure& theFather,
70                                        const Graphic3d_CStructure& theSon)
71 {
72   OpenGl_Structure* aFather = (OpenGl_Structure* )theFather.ptrStructure;
73   OpenGl_Structure* aSon = (OpenGl_Structure* )theSon.ptrStructure;
74   if (aFather && aSon)
75     aFather->Disconnect(aSon);
76 }
77
78 void OpenGl_GraphicDriver::DisplayStructure (const Graphic3d_CView&      theCView,
79                                              const Graphic3d_CStructure& theCStructure,
80                                              const Standard_Integer      thePriority)
81 {
82   const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
83   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
84   if (aCView == NULL || aStructure == NULL)
85     return;
86
87   aCView->View->DisplayStructure (aStructure, thePriority);
88 }
89
90 void OpenGl_GraphicDriver::EraseStructure (const Graphic3d_CView&      theCView,
91                                            const Graphic3d_CStructure& theCStructure)
92 {
93   const OpenGl_CView* aCView = (const OpenGl_CView* )theCView.ptrView;
94   OpenGl_Structure* aStructure = (OpenGl_Structure* )theCStructure.ptrStructure;
95   if (aCView == NULL || aStructure == NULL)
96     return;
97
98   aCView->View->EraseStructure (aStructure);
99 }
100
101 void OpenGl_GraphicDriver::RemoveStructure (const Graphic3d_CStructure& theCStructure)
102 {
103   if (!myMapOfStructure.IsBound (theCStructure.Id))
104     return;
105
106   OpenGl_Structure* aStructure = myMapOfStructure.Find (theCStructure.Id);
107   myMapOfStructure.UnBind (theCStructure.Id);
108   OpenGl_Element::Destroy (GetSharedContext(), aStructure);
109   InvalidateAllWorkspaces();
110 }
111
112 void OpenGl_GraphicDriver::Structure (Graphic3d_CStructure& theCStructure)
113 {
114   RemoveStructure (theCStructure);
115
116   OpenGl_Structure* aStructure = new OpenGl_Structure();
117
118   aStructure->SetDegenerateModel (theCStructure.ContextFillArea.DegenerationMode,
119                                   theCStructure.ContextFillArea.SkipRatio);
120
121   Standard_Integer aStatus = 0;
122   if (theCStructure.highlight) aStatus |= OPENGL_NS_HIGHLIGHT;
123   if (!theCStructure.visible)  aStatus |= OPENGL_NS_HIDE;
124   aStructure->SetNamedStatus (aStatus);
125
126   theCStructure.ptrStructure = aStructure;
127   myMapOfStructure.Bind (theCStructure.Id, aStructure);
128   InvalidateAllWorkspaces();
129 }
130
131 //=======================================================================
132 //function : ChangeZLayer
133 //purpose  :
134 //=======================================================================
135
136 void OpenGl_GraphicDriver::ChangeZLayer (const Graphic3d_CStructure& theCStructure,
137                                          const Standard_Integer theLayer)
138 {
139   if (!myMapOfStructure.IsBound (theCStructure.Id))
140     return;
141
142   OpenGl_Structure* aStructure = myMapOfStructure.Find (theCStructure.Id);
143
144   aStructure->SetZLayer (theLayer);
145 }
146
147 //=======================================================================
148 //function : ChangeZLayer
149 //purpose  :
150 //=======================================================================
151
152 void OpenGl_GraphicDriver::ChangeZLayer (const Graphic3d_CStructure& theCStructure,
153                                          const Graphic3d_CView& theCView,
154                                          const Standard_Integer theNewLayerId)
155 {
156   const OpenGl_CView *aCView = (const OpenGl_CView *)theCView.ptrView;
157
158   if (!myMapOfStructure.IsBound (theCStructure.Id) || !aCView)
159     return;
160
161   OpenGl_Structure* aStructure = myMapOfStructure.Find (theCStructure.Id);
162
163   aCView->View->ChangeZLayer (aStructure, theNewLayerId);
164 }
165
166 //=======================================================================
167 //function : GetZLayer
168 //purpose  :
169 //=======================================================================
170
171 Standard_Integer OpenGl_GraphicDriver::GetZLayer (const Graphic3d_CStructure& theCStructure) const
172 {
173   if (!myMapOfStructure.IsBound (theCStructure.Id))
174     return -1;
175
176   OpenGl_Structure* aStructure = myMapOfStructure.Find (theCStructure.Id);
177
178   return aStructure->GetZLayer();
179 }
180
181 //=======================================================================
182 //function : UnsetZLayer
183 //purpose  :
184 //=======================================================================
185
186 void OpenGl_GraphicDriver::UnsetZLayer (const Standard_Integer theLayerId)
187 {
188   NCollection_DataMap<Standard_Integer, OpenGl_Structure*>::Iterator aStructIt (myMapOfStructure);
189   for( ; aStructIt.More (); aStructIt.Next ())
190   {
191     OpenGl_Structure* aStruct = aStructIt.ChangeValue ();
192     if (aStruct->GetZLayer () == theLayerId)
193       aStruct->SetZLayer (0);
194   }
195 }