1 // Created on: 2011-09-20
2 // Created by: Sergey ZERCHANINOV
3 // Copyright (c) 2011-2012 OPEN CASCADE SAS
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.
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.
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.
24 #include <OpenGl_GlCore11.hxx>
25 #include <OpenGl_Context.hxx>
26 #include <OpenGl_telem_util.hxx>
27 #include <OpenGl_AspectLine.hxx>
28 #include <OpenGl_Structure.hxx>
34 /*----------------------------------------------------------------------*/
36 * Prototypes Private functions
39 static void call_util_transform_pt (float *x, float *y, float *z);
40 static void call_util_transpose_mat (float tmat[16], float mat[4][4]);
42 /*----------------------------------------------------------------------*/
47 static int openglNumberOfPoints = 0;
49 static int myImmediateMatIsIdentity = 1;
51 static int partial = -1; /* -1 init, 0 complete, 1 partielle */
53 static float xm, ym, zm, XM, YM, ZM;
55 static float myImmediateMat[4][4] = {
62 /*----------------------------------------------------------------------*/
64 /*----------------------------------------------------------------------*/
66 //call_togl_begin_ajout_mode
67 Standard_Boolean OpenGl_Workspace::BeginAddMode ()
70 return Standard_False;
72 NamedStatus |= OPENGL_NS_ADD;
74 MakeFrontAndBackBufCurrent();
81 /*----------------------------------------------------------------------*/
83 //call_togl_end_ajout_mode
84 void OpenGl_Workspace::EndAddMode ()
86 if (NamedStatus & OPENGL_NS_ADD)
88 OpenGl_Workspace::MakeBackBufCurrent();
90 // Clear add mode flag
91 NamedStatus &= ~OPENGL_NS_ADD;
94 myImmediateMatIsIdentity = 1;
96 /* FMN necessaire pour l'affichage sur WNT */
102 /*----------------------------------------------------------------------*/
104 /*----------------------------------------------------------------------*/
106 //call_togl_clear_immediat_mode
107 void OpenGl_Workspace::ClearImmediatMode (const Graphic3d_CView& ACView, const Standard_Boolean AFlush)
109 if ( myIsTransientOpen )
112 if (!Activate()) return;
114 if ( !myBackBufferRestored )
118 Redraw1(ACView,*((CALL_DEF_LAYER *)ACView.ptrUnderLayer),*((CALL_DEF_LAYER *)ACView.ptrOverLayer),AFlush);
121 // Made the back identical to the front buffer.
122 // Always perform full copy (partial update optimization is useless on mordern hardware)!
124 CopyBuffers (ACView.ViewId, 1 /* GL_FRONT -> GL_BACK */, xm, ym, zm, XM, YM, ZM, 0);
126 myBackBufferRestored = Standard_True;
128 else if ( partial >= 0 )
130 // Restore pixels from the back buffer.
131 // Always perform full copy (partial update optimization is useless on mordern hardware)!
132 CopyBuffers (ACView.ViewId, 0 /* GL_BACK -> GL_FRONT */, xm, ym, zm, XM, YM, ZM, 0);
137 /* Clear current list contents */
138 glNewList( (GLuint) myTransientList, GL_COMPILE_AND_EXECUTE);
142 XM = YM = ZM = (float ) shortrealfirst ();
143 xm = ym = zm = (float ) shortreallast ();
146 /*----------------------------------------------------------------------*/
148 //call_togl_redraw_immediat_mode
149 void OpenGl_Workspace::RedrawImmediatMode ()
155 MakeFrontBufCurrent();
156 glDisable(GL_LIGHTING);
157 glCallList((GLuint) myTransientList);
158 /* FMN necessaire pour l'affichage sur WNT */
160 MakeBackBufCurrent();
165 /*----------------------------------------------------------------------*/
167 //call_togl_begin_immediat_mode
168 Standard_Boolean OpenGl_Workspace::BeginImmediatMode (const Graphic3d_CView& ACView, const Standard_Boolean UseDepthTest, const Standard_Boolean RetainMode)
171 return Standard_False;
173 OpenGl_Workspace::ClearImmediatMode(ACView,1);
175 NamedStatus |= OPENGL_NS_IMMEDIATE;
176 myRetainMode = RetainMode;
178 MakeFrontBufCurrent();
184 GLuint listid = (GLuint) myTransientList;
186 listid = glGenLists(1);
187 if (!listid) return Standard_False;
189 glNewList(listid, GL_COMPILE_AND_EXECUTE);
190 myTransientList = listid;
191 myIsTransientOpen = Standard_True;
195 glEnable(GL_DEPTH_TEST);
197 glDisable(GL_DEPTH_TEST);
199 return Standard_True;
202 /*----------------------------------------------------------------------*/
204 //call_togl_end_immediat_mode
205 void OpenGl_Workspace::EndImmediatMode ()
207 if (NamedStatus & OPENGL_NS_IMMEDIATE)
209 if (myIsTransientOpen)
212 myIsTransientOpen = Standard_False;
214 MakeBackBufCurrent();
216 // Clear immediate mode flag
217 NamedStatus &= ~OPENGL_NS_IMMEDIATE;
220 // Ajout CAL : pour voir quelque chose avant le prochain begin_immediat_mode
223 myImmediateMatIsIdentity = 1;
228 /*----------------------------------------------------------------------*/
230 //call_togl_transform
231 void OpenGl_Workspace::Transform (const TColStd_Array2OfReal& AMatrix, const Graphic3d_TypeOfComposition AType)
233 //call_togl_transform in OpenGl_togl_begin_immediat_mode.cxx
234 const Standard_Integer lr = AMatrix.LowerRow ();
235 const Standard_Integer lc = AMatrix.LowerCol ();
237 Standard_Integer i, j;
238 if ((AType == Graphic3d_TOC_REPLACE) || myImmediateMatIsIdentity)
242 myImmediateMat[i][j] = float (AMatrix (i+lr, j+lc));
246 float theMatrix[4][4];
249 theMatrix[i][j] = float (AMatrix (i+lr, j+lc));
251 TelMultiplymat3 (myImmediateMat, myImmediateMat, theMatrix);
254 myImmediateMatIsIdentity = 1;
255 for (i = 0; i < 4; i++)
256 for (j = 0; j < 4; j++)
257 if (myImmediateMat[i][j] != (i == j? 1. : 0.))
259 myImmediateMatIsIdentity = 0;
264 /*----------------------------------------------------------------------*/
266 //call_togl_begin_polyline
267 void OpenGl_Workspace::BeginPolyline ()
269 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
271 openglNumberOfPoints = 0;
272 glDisable(GL_LIGHTING);
273 glBegin(GL_LINE_STRIP);
277 /*----------------------------------------------------------------------*/
279 //call_togl_end_polyline
280 void OpenGl_Workspace::EndPolyline ()
282 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
286 /*----------------------------------------------------------------------*/
289 void OpenGl_Workspace::Draw (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Z)
291 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
293 openglNumberOfPoints++;
294 float x = X, y = Y, z = Z;
295 if (!myImmediateMatIsIdentity)
296 call_util_transform_pt (&x, &y, &z);
303 glVertex3f (x, y, z);
308 /*----------------------------------------------------------------------*/
311 void OpenGl_Workspace::Move (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Z)
313 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
315 if (openglNumberOfPoints != 0)
317 OpenGl_Workspace::EndPolyline();
318 OpenGl_Workspace::BeginPolyline();
320 OpenGl_Workspace::Draw(X,Y,Z);
324 /*----------------------------------------------------------------------*/
326 //call_togl_set_linecolor
327 void OpenGl_Workspace::SetLineColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
329 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
339 /*----------------------------------------------------------------------*/
341 //call_togl_set_linetype
342 void OpenGl_Workspace::SetLineType (const Standard_Integer Type)
344 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
346 myDisplay->SetTypeOfLine((Aspect_TypeOfLine)Type);
350 /*----------------------------------------------------------------------*/
352 //call_togl_set_linewidth
353 void OpenGl_Workspace::SetLineWidth (const Standard_ShortReal Width)
355 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
357 glLineWidth( (GLfloat)Width );
359 gl2psLineWidth( (GLfloat)Width );
364 /*----------------------------------------------------------------------*/
366 //call_togl_draw_structure
367 void OpenGl_Workspace::DrawStructure (const OpenGl_Structure *AStructure)
369 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
376 /* mise en place de la matrice de transformation du trace transient */
377 if (!myImmediateMatIsIdentity)
379 call_util_transpose_mat (mat16, myImmediateMat);
380 glGetIntegerv (GL_MATRIX_MODE, &mode1);
381 glMatrixMode (GL_MODELVIEW);
383 glScalef (1.F, 1.F, 1.F);
384 glMultMatrixf (mat16);
388 Handle(OpenGl_Workspace) aWS(this);
389 AStructure->Render(aWS);
393 if (!myImmediateMatIsIdentity)
396 glMatrixMode (mode1);
401 /*----------------------------------------------------------------------*/
403 //call_togl_set_minmax
404 void OpenGl_Workspace::SetMinMax (const Standard_ShortReal X1, const Standard_ShortReal Y1, const Standard_ShortReal Z1, const Standard_ShortReal X2, const Standard_ShortReal Y2, const Standard_ShortReal Z2)
406 if ((X1 > shortreallast ()) || (Y1 > shortreallast ()) || (Z1 > shortreallast ()) ||
407 (X2 > shortreallast ()) || (Y2 > shortreallast ()) || (Z2 > shortreallast ()) ||
408 (X1 < shortrealfirst ()) || (Y1 < shortrealfirst ()) || (Z1 < shortrealfirst ()) ||
409 (X2 < shortrealfirst ()) || (Y2 < shortrealfirst ()) || (Z2 < shortrealfirst ()))
411 XM = YM = ZM = (float ) shortreallast ();
412 xm = ym = zm = (float ) shortrealfirst ();
417 float x1=X1,y1=Y1,z1=Z1,x2=X2,y2=Y2,z2=Z2;
418 if (!myImmediateMatIsIdentity)
420 call_util_transform_pt (&x1, &y1, &z1);
421 call_util_transform_pt (&x2, &y2, &z2);
423 if (x1 > XM) XM = x1;
424 if (x1 < xm) xm = x1;
425 if (y1 > YM) YM = y1;
426 if (y1 < ym) ym = y1;
427 if (z1 > ZM) ZM = z1;
428 if (z1 < zm) zm = z1;
430 if (x2 > XM) XM = x2;
431 if (x2 < xm) xm = x2;
432 if (y2 > YM) YM = y2;
433 if (y2 < ym) ym = y2;
434 if (z2 > ZM) ZM = z2;
435 if (z2 < zm) zm = z2;
436 if (partial != 0) partial = 1;
440 /*----------------------------------------------------------------------*/
445 /*----------------------------------------------------------------------*/
447 Transform the point pt
449 static void call_util_transform_pt ( float *x, float *y, float *z )
452 pt[0] = *x, pt[1] = *y, pt[2] = *z, pt[3] = 1.0;
455 for (i = 0; i < 4; i++)
458 for (j = 0; j < 4; j++)
459 sum += myImmediateMat[i][j] * pt[j];
463 *x = tpt[0], *y = tpt[1], *z = tpt[2];
466 /*----------------------------------------------------------------------*/
468 void call_util_transpose_mat (tmat, mat)
472 Transpose mat and returns tmat.
475 static void call_util_transpose_mat (float tmat[16], float mat[4][4])
481 tmat[j*4+i] = mat[i][j];