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>
38 /*----------------------------------------------------------------------*/
40 * Prototypes Private functions
43 static void call_util_transform_pt (float *x, float *y, float *z);
44 static void call_util_transpose_mat (float tmat[16], float mat[4][4]);
46 /*----------------------------------------------------------------------*/
51 static int openglNumberOfPoints = 0;
53 static int myImmediateMatIsIdentity = 1;
55 static int partial = -1; /* -1 init, 0 complete, 1 partielle */
57 static float xm, ym, zm, XM, YM, ZM;
59 static float myImmediateMat[4][4] = {
66 /*----------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------*/
70 //call_togl_begin_ajout_mode
71 Standard_Boolean OpenGl_Workspace::BeginAddMode ()
74 return Standard_False;
76 NamedStatus |= OPENGL_NS_ADD;
78 MakeFrontAndBackBufCurrent();
85 /*----------------------------------------------------------------------*/
87 //call_togl_end_ajout_mode
88 void OpenGl_Workspace::EndAddMode ()
90 if (NamedStatus & OPENGL_NS_ADD)
92 OpenGl_Workspace::MakeBackBufCurrent();
94 // Clear add mode flag
95 NamedStatus &= ~OPENGL_NS_ADD;
98 myImmediateMatIsIdentity = 1;
100 /* FMN necessaire pour l'affichage sur WNT */
106 /*----------------------------------------------------------------------*/
108 /*----------------------------------------------------------------------*/
110 //call_togl_clear_immediat_mode
111 void OpenGl_Workspace::ClearImmediatMode (const Graphic3d_CView& ACView, const Standard_Boolean AFlush)
113 if ( myIsTransientOpen )
116 if (!Activate()) return;
118 if ( !myBackBufferRestored )
122 Redraw1(ACView,*((CALL_DEF_LAYER *)ACView.ptrUnderLayer),*((CALL_DEF_LAYER *)ACView.ptrOverLayer),AFlush);
125 // Made the back identical to the front buffer.
126 // Always perform full copy (partial update optimization is useless on mordern hardware)!
128 CopyBuffers (ACView.ViewId, 1 /* GL_FRONT -> GL_BACK */, xm, ym, zm, XM, YM, ZM, 0);
130 myBackBufferRestored = Standard_True;
132 else if ( partial >= 0 )
134 // Restore pixels from the back buffer.
135 // Always perform full copy (partial update optimization is useless on mordern hardware)!
136 CopyBuffers (ACView.ViewId, 0 /* GL_BACK -> GL_FRONT */, xm, ym, zm, XM, YM, ZM, 0);
141 /* Clear current list contents */
142 glNewList( (GLuint) myTransientList, GL_COMPILE_AND_EXECUTE);
146 XM = YM = ZM = (float ) shortrealfirst ();
147 xm = ym = zm = (float ) shortreallast ();
150 /*----------------------------------------------------------------------*/
152 //call_togl_redraw_immediat_mode
153 void OpenGl_Workspace::RedrawImmediatMode ()
159 MakeFrontBufCurrent();
160 glDisable(GL_LIGHTING);
161 glCallList((GLuint) myTransientList);
162 /* FMN necessaire pour l'affichage sur WNT */
164 MakeBackBufCurrent();
169 /*----------------------------------------------------------------------*/
171 //call_togl_begin_immediat_mode
172 Standard_Boolean OpenGl_Workspace::BeginImmediatMode (const Graphic3d_CView& ACView, const Standard_Boolean UseDepthTest, const Standard_Boolean RetainMode)
175 return Standard_False;
177 OpenGl_Workspace::ClearImmediatMode(ACView,1);
179 NamedStatus |= OPENGL_NS_IMMEDIATE;
180 myRetainMode = RetainMode;
182 MakeFrontBufCurrent();
188 GLuint listid = (GLuint) myTransientList;
190 listid = glGenLists(1);
191 if (!listid) return Standard_False;
193 glNewList(listid, GL_COMPILE_AND_EXECUTE);
194 myTransientList = listid;
195 myIsTransientOpen = Standard_True;
199 glEnable(GL_DEPTH_TEST);
201 glDisable(GL_DEPTH_TEST);
203 return Standard_True;
206 /*----------------------------------------------------------------------*/
208 //call_togl_end_immediat_mode
209 void OpenGl_Workspace::EndImmediatMode ()
211 if (NamedStatus & OPENGL_NS_IMMEDIATE)
213 if (myIsTransientOpen)
216 myIsTransientOpen = Standard_False;
218 MakeBackBufCurrent();
220 // Clear immediate mode flag
221 NamedStatus &= ~OPENGL_NS_IMMEDIATE;
224 // Ajout CAL : pour voir quelque chose avant le prochain begin_immediat_mode
227 myImmediateMatIsIdentity = 1;
232 /*----------------------------------------------------------------------*/
234 //call_togl_transform
235 void OpenGl_Workspace::Transform (const TColStd_Array2OfReal& AMatrix, const Graphic3d_TypeOfComposition AType)
237 //call_togl_transform in OpenGl_togl_begin_immediat_mode.cxx
238 const Standard_Integer lr = AMatrix.LowerRow ();
239 const Standard_Integer lc = AMatrix.LowerCol ();
241 Standard_Integer i, j;
242 if ((AType == Graphic3d_TOC_REPLACE) || myImmediateMatIsIdentity)
246 myImmediateMat[i][j] = float (AMatrix (i+lr, j+lc));
250 float theMatrix[4][4];
253 theMatrix[i][j] = float (AMatrix (i+lr, j+lc));
255 TelMultiplymat3 (myImmediateMat, myImmediateMat, theMatrix);
258 myImmediateMatIsIdentity = 1;
259 for (i = 0; i < 4; i++)
260 for (j = 0; j < 4; j++)
261 if (myImmediateMat[i][j] != (i == j? 1. : 0.))
263 myImmediateMatIsIdentity = 0;
268 /*----------------------------------------------------------------------*/
270 //call_togl_begin_polyline
271 void OpenGl_Workspace::BeginPolyline ()
273 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
275 openglNumberOfPoints = 0;
276 glDisable(GL_LIGHTING);
277 glBegin(GL_LINE_STRIP);
281 /*----------------------------------------------------------------------*/
283 //call_togl_end_polyline
284 void OpenGl_Workspace::EndPolyline ()
286 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
290 /*----------------------------------------------------------------------*/
293 void OpenGl_Workspace::Draw (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Z)
295 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
297 openglNumberOfPoints++;
298 float x = X, y = Y, z = Z;
299 if (!myImmediateMatIsIdentity)
300 call_util_transform_pt (&x, &y, &z);
307 glVertex3f (x, y, z);
312 /*----------------------------------------------------------------------*/
315 void OpenGl_Workspace::Move (const Standard_ShortReal X, const Standard_ShortReal Y, const Standard_ShortReal Z)
317 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
319 if (openglNumberOfPoints != 0)
321 OpenGl_Workspace::EndPolyline();
322 OpenGl_Workspace::BeginPolyline();
324 OpenGl_Workspace::Draw(X,Y,Z);
328 /*----------------------------------------------------------------------*/
330 //call_togl_set_linecolor
331 void OpenGl_Workspace::SetLineColor (const Standard_ShortReal R, const Standard_ShortReal G, const Standard_ShortReal B)
333 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
343 /*----------------------------------------------------------------------*/
345 //call_togl_set_linetype
346 void OpenGl_Workspace::SetLineType (const Standard_Integer Type)
348 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
350 myDisplay->SetTypeOfLine((Aspect_TypeOfLine)Type);
354 /*----------------------------------------------------------------------*/
356 //call_togl_set_linewidth
357 void OpenGl_Workspace::SetLineWidth (const Standard_ShortReal Width)
359 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
361 glLineWidth( (GLfloat)Width );
363 gl2psLineWidth( (GLfloat)Width );
368 /*----------------------------------------------------------------------*/
370 //call_togl_draw_structure
371 void OpenGl_Workspace::DrawStructure (const OpenGl_Structure *AStructure)
373 if (NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE))
380 /* mise en place de la matrice de transformation du trace transient */
381 if (!myImmediateMatIsIdentity)
383 call_util_transpose_mat (mat16, myImmediateMat);
384 glGetIntegerv (GL_MATRIX_MODE, &mode1);
385 glMatrixMode (GL_MODELVIEW);
387 glScalef (1.F, 1.F, 1.F);
388 glMultMatrixf (mat16);
392 Handle(OpenGl_Workspace) aWS(this);
393 AStructure->Render(aWS);
397 if (!myImmediateMatIsIdentity)
400 glMatrixMode (mode1);
405 /*----------------------------------------------------------------------*/
407 //call_togl_set_minmax
408 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)
410 if ((X1 > shortreallast ()) || (Y1 > shortreallast ()) || (Z1 > shortreallast ()) ||
411 (X2 > shortreallast ()) || (Y2 > shortreallast ()) || (Z2 > shortreallast ()) ||
412 (X1 < shortrealfirst ()) || (Y1 < shortrealfirst ()) || (Z1 < shortrealfirst ()) ||
413 (X2 < shortrealfirst ()) || (Y2 < shortrealfirst ()) || (Z2 < shortrealfirst ()))
415 XM = YM = ZM = (float ) shortreallast ();
416 xm = ym = zm = (float ) shortrealfirst ();
421 float x1=X1,y1=Y1,z1=Z1,x2=X2,y2=Y2,z2=Z2;
422 if (!myImmediateMatIsIdentity)
424 call_util_transform_pt (&x1, &y1, &z1);
425 call_util_transform_pt (&x2, &y2, &z2);
427 if (x1 > XM) XM = x1;
428 if (x1 < xm) xm = x1;
429 if (y1 > YM) YM = y1;
430 if (y1 < ym) ym = y1;
431 if (z1 > ZM) ZM = z1;
432 if (z1 < zm) zm = z1;
434 if (x2 > XM) XM = x2;
435 if (x2 < xm) xm = x2;
436 if (y2 > YM) YM = y2;
437 if (y2 < ym) ym = y2;
438 if (z2 > ZM) ZM = z2;
439 if (z2 < zm) zm = z2;
440 if (partial != 0) partial = 1;
444 /*----------------------------------------------------------------------*/
449 /*----------------------------------------------------------------------*/
451 Transform the point pt
453 static void call_util_transform_pt ( float *x, float *y, float *z )
456 pt[0] = *x, pt[1] = *y, pt[2] = *z, pt[3] = 1.0;
459 for (i = 0; i < 4; i++)
462 for (j = 0; j < 4; j++)
463 sum += myImmediateMat[i][j] * pt[j];
467 *x = tpt[0], *y = tpt[1], *z = tpt[2];
470 /*----------------------------------------------------------------------*/
472 void call_util_transpose_mat (tmat, mat)
476 Transpose mat and returns tmat.
479 static void call_util_transpose_mat (float tmat[16], float mat[4][4])
485 tmat[j*4+i] = mat[i][j];