0024234: occt master is not compiled by VC++ 2005 (vc8 32/64 bit TKBO)
[occt.git] / src / OpenGl / OpenGl_Workspace_3.cxx
1 // Created on: 2011-09-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 #include <stdio.h>
21
22 #include <OpenGl_GlCore11.hxx>
23 #include <OpenGl_Context.hxx>
24 #include <OpenGl_Structure.hxx>
25 #include <OpenGl_Workspace.hxx>
26
27 /*----------------------------------------------------------------------*/
28 /*  Mode Ajout              */
29 /*----------------------------------------------------------------------*/
30
31 //call_togl_begin_ajout_mode
32 Standard_Boolean OpenGl_Workspace::BeginAddMode()
33 {
34   if (!Activate())
35   {
36     return Standard_False;
37   }
38
39   NamedStatus |= OPENGL_NS_ADD;
40
41   MakeFrontAndBackBufCurrent();
42
43   //TsmPushAttri();
44
45   return Standard_True;
46 }
47
48 /*----------------------------------------------------------------------*/
49
50 //call_togl_end_ajout_mode
51 void OpenGl_Workspace::EndAddMode ()
52 {
53   if (NamedStatus & OPENGL_NS_ADD)
54   {
55     OpenGl_Workspace::MakeBackBufCurrent();
56
57     // Clear add mode flag
58     NamedStatus &= ~OPENGL_NS_ADD;
59   }
60
61   // FMN necessaire pour l'affichage sur WNT
62   glFlush();
63
64   //TsmPopAttri();
65 }
66
67 /*----------------------------------------------------------------------*/
68 /*  Mode Transient              */
69 /*----------------------------------------------------------------------*/
70
71 //call_togl_clear_immediat_mode
72 void OpenGl_Workspace::ClearImmediatMode (const Graphic3d_CView& theCView,
73                                           const Standard_Boolean theToFlush)
74 {
75   if (myIsTransientOpen)
76   {
77     EndImmediatMode();
78   }
79
80   if (!Activate())
81   {
82     myTransientList.Clear(); // Clear current list contents
83     return;
84   }
85
86   GLboolean isDoubleBuffer = GL_FALSE;
87   glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
88   if (!myBackBufferRestored || !myTransientDrawToFront || !isDoubleBuffer)
89   {
90     Redraw1 (theCView, *((CALL_DEF_LAYER* )theCView.ptrUnderLayer), *((CALL_DEF_LAYER* )theCView.ptrOverLayer), theToFlush);
91
92     // After a redraw,
93     // Made the back identical to the front buffer.
94     // Always perform full copy (partial update optimization is useless on mordern hardware)!
95     if (myRetainMode && myTransientDrawToFront && isDoubleBuffer)
96     {
97       const Standard_Boolean toCopyFrontToBack = Standard_True;
98       CopyBuffers (toCopyFrontToBack);
99     }
100
101     myBackBufferRestored = Standard_True;
102   }
103   else if (!myTransientList.IsEmpty() && isDoubleBuffer)
104   {
105     // restore pixels from the back buffer
106     const Standard_Boolean toCopyFrontToBack = Standard_False;
107     CopyBuffers (toCopyFrontToBack);
108   }
109
110   myTransientList.Clear(); // clear current list contents
111 }
112
113 /*----------------------------------------------------------------------*/
114
115 //call_togl_redraw_immediat_mode
116 void OpenGl_Workspace::RedrawImmediatMode()
117 {
118   if (!myRetainMode || myTransientList.IsEmpty())
119   {
120     return;
121   }
122
123   GLboolean isDoubleBuffer = GL_FALSE;
124   glGetBooleanv (GL_DOUBLEBUFFER, &isDoubleBuffer);
125   if (isDoubleBuffer && myTransientDrawToFront)
126   {
127     MakeFrontBufCurrent();
128   }
129   else
130   {
131     myBackBufferRestored = Standard_False;
132   }
133   glDisable (GL_LIGHTING);
134
135   Handle(OpenGl_Workspace) aWS (this);
136   for (Standard_Integer anIter = 1; anIter <= myTransientList.Size(); ++anIter)
137   {
138     const OpenGl_Structure* aStructure = myTransientList.Value (anIter);
139     aStructure->Render (aWS);
140   }
141
142   if (isDoubleBuffer && myTransientDrawToFront)
143   {
144     glFlush(); // FMN necessaire pour l'affichage sur WNT
145     MakeBackBufCurrent();
146   }
147 }
148
149 /*----------------------------------------------------------------------*/
150
151 //call_togl_begin_immediat_mode
152 Standard_Boolean OpenGl_Workspace::BeginImmediatMode (const Graphic3d_CView& theCView,
153                                                       const Standard_Boolean theToUseDepthTest,
154                                                       const Standard_Boolean theRetainMode)
155 {
156   if (!Activate())
157   {
158     return Standard_False;
159   }
160
161   OpenGl_Workspace::ClearImmediatMode (theCView, Standard_True);
162
163   NamedStatus |= OPENGL_NS_IMMEDIATE;
164   myRetainMode = theRetainMode;
165
166   if (myTransientDrawToFront)
167   {
168     MakeFrontBufCurrent();
169   }
170
171   //TsmPushAttri();
172
173   if (myRetainMode)
174   {
175     myIsTransientOpen = Standard_True;
176   }
177
178   if (theToUseDepthTest)
179   {
180     glEnable (GL_DEPTH_TEST);
181   }
182   else
183   {
184     glDisable (GL_DEPTH_TEST);
185   }
186
187   return Standard_True;
188 }
189
190 /*----------------------------------------------------------------------*/
191
192 //call_togl_end_immediat_mode
193 void OpenGl_Workspace::EndImmediatMode()
194 {
195   if (NamedStatus & OPENGL_NS_IMMEDIATE)
196   {
197     if (myIsTransientOpen)
198     {
199       myIsTransientOpen = Standard_False;
200     }
201     if (myTransientDrawToFront)
202     {
203       MakeBackBufCurrent();
204     }
205
206     // Clear immediate mode flag
207     NamedStatus &= ~OPENGL_NS_IMMEDIATE;
208   }
209
210   if (myTransientDrawToFront)
211   {
212     // Ajout CAL : pour voir quelque chose avant le prochain begin_immediat_mode
213     glFinish();
214   }
215   else
216   {
217     GetGlContext()->SwapBuffers();
218   }
219
220   //TsmPopAttri();
221 }
222
223 //call_togl_draw_structure
224 void OpenGl_Workspace::DrawStructure (const OpenGl_Structure* theStructure)
225 {
226   if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
227   {
228     Handle(OpenGl_Workspace) aWS (this);
229     theStructure->Render (aWS);
230
231     if (myIsTransientOpen && myRetainMode)
232     {
233       myTransientList.Append (theStructure);
234     }
235   }
236 }