1 // Created on: 2011-08-01
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.
21 #include <OpenGl_GlCore11.hxx>
23 #include <OpenGl_Structure.hxx>
25 #include <OpenGl_Polyline.hxx>
26 #include <OpenGl_Workspace.hxx>
27 #include <OpenGl_View.hxx>
29 #include <OpenGl_telem_util.hxx>
32 /*----------------------------------------------------------------------*/
34 static void call_util_transpose_mat (float tmat[16], float mat[4][4])
40 tmat[j*4+i] = mat[i][j];
43 /*----------------------------------------------------------------------*/
45 OpenGl_Structure::OpenGl_Structure ()
46 : myTransformation(NULL),
53 myHighlightColor(NULL),
59 /*----------------------------------------------------------------------*/
61 OpenGl_Structure::~OpenGl_Structure()
63 Release (Handle(OpenGl_Context)());
64 delete myTransformation; myTransformation = NULL;
65 delete myTransPers; myTransPers = NULL;
68 /*----------------------------------------------------------------------*/
70 void OpenGl_Structure::SetTransformation(const float *AMatrix)
72 if (!myTransformation)
73 myTransformation = new OpenGl_Matrix();
75 matcpy( myTransformation->mat, AMatrix );
78 /*----------------------------------------------------------------------*/
80 void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers)
83 myTransPers = new TEL_TRANSFORM_PERSISTENCE;
85 myTransPers->mode = ATransPers.Flag;
86 myTransPers->pointX = ATransPers.Point.x;
87 myTransPers->pointY = ATransPers.Point.y;
88 myTransPers->pointZ = ATransPers.Point.z;
91 /*----------------------------------------------------------------------*/
93 void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &AContext)
96 myAspectLine = new OpenGl_AspectLine();
97 myAspectLine->SetContext( AContext );
100 /*----------------------------------------------------------------------*/
102 void OpenGl_Structure::SetAspectFace (const Handle(OpenGl_Context)& theCtx,
103 const CALL_DEF_CONTEXTFILLAREA& theAspect)
107 myAspectFace = new OpenGl_AspectFace();
109 myAspectFace->Init (theCtx, theAspect);
112 /*----------------------------------------------------------------------*/
114 void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER &AContext)
117 myAspectMarker = new OpenGl_AspectMarker();
118 myAspectMarker->SetContext( AContext );
121 /*----------------------------------------------------------------------*/
123 void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &AContext)
126 myAspectText = new OpenGl_AspectText();
127 myAspectText->SetContext( AContext );
130 /*----------------------------------------------------------------------*/
132 void OpenGl_Structure::SetHighlightBox (const Handle(OpenGl_Context)& theGlCtx,
133 const CALL_DEF_BOUNDBOX& theBoundBox)
135 if (myHighlightBox != NULL)
137 myHighlightBox->Release (theGlCtx);
141 myHighlightBox = new OpenGl_Group();
144 CALL_DEF_CONTEXTLINE aContextLine;
145 aContextLine.Color = theBoundBox.Color;
146 aContextLine.LineType = Aspect_TOL_SOLID;
147 aContextLine.Width = 1.0f;
148 myHighlightBox->SetAspectLine (aContextLine);
150 #define CALL_MAX_BOUNDBOXSIZE 16
152 Graphic3d_Array1OfVertex aPoints (1, CALL_MAX_BOUNDBOXSIZE);
153 const float Xm = theBoundBox.Pmin.x;
154 const float Ym = theBoundBox.Pmin.y;
155 const float Zm = theBoundBox.Pmin.z;
156 const float XM = theBoundBox.Pmax.x;
157 const float YM = theBoundBox.Pmax.y;
158 const float ZM = theBoundBox.Pmax.z;
159 aPoints( 1).SetCoord (Xm, Ym, Zm);
160 aPoints( 2).SetCoord (Xm, Ym, ZM);
161 aPoints( 3).SetCoord (Xm, YM, ZM);
162 aPoints( 4).SetCoord (Xm, YM, Zm);
163 aPoints( 5).SetCoord (Xm, Ym, Zm);
164 aPoints( 6).SetCoord (XM, Ym, Zm);
165 aPoints( 7).SetCoord (XM, Ym, ZM);
166 aPoints( 8).SetCoord (XM, YM, ZM);
167 aPoints( 9).SetCoord (XM, YM, Zm);
168 aPoints(10).SetCoord (XM, Ym, Zm);
169 aPoints(11).SetCoord (XM, YM, Zm);
170 aPoints(12).SetCoord (Xm, YM, Zm);
171 aPoints(13).SetCoord (Xm, YM, ZM);
172 aPoints(14).SetCoord (XM, YM, ZM);
173 aPoints(15).SetCoord (XM, Ym, ZM);
174 aPoints(16).SetCoord (Xm, Ym, ZM);
176 OpenGl_Polyline* aPolyline = new OpenGl_Polyline (aPoints);
177 myHighlightBox->AddElement (TelPolyline, aPolyline);
180 /*----------------------------------------------------------------------*/
182 void OpenGl_Structure::ClearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
184 if (myHighlightBox != NULL)
186 OpenGl_Element::Destroy (theGlCtx, myHighlightBox);
190 /*----------------------------------------------------------------------*/
192 void OpenGl_Structure::SetHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
193 const Standard_ShortReal R,
194 const Standard_ShortReal G,
195 const Standard_ShortReal B)
197 ClearHighlightBox (theGlCtx);
198 if (myHighlightColor == NULL)
200 myHighlightColor = new TEL_COLOUR();
203 myHighlightColor->rgb[0] = R;
204 myHighlightColor->rgb[1] = G;
205 myHighlightColor->rgb[2] = B;
206 myHighlightColor->rgb[3] = 1.F;
209 /*----------------------------------------------------------------------*/
211 void OpenGl_Structure::ClearHighlightColor (const Handle(OpenGl_Context)& theGlCtx)
213 ClearHighlightBox(theGlCtx);
214 delete myHighlightColor;
215 myHighlightColor = NULL;
218 /*----------------------------------------------------------------------*/
220 void OpenGl_Structure::Connect (const OpenGl_Structure *AStructure)
222 Disconnect (AStructure);
223 myConnected.Append(AStructure);
226 /*----------------------------------------------------------------------*/
228 void OpenGl_Structure::Disconnect (const OpenGl_Structure *AStructure)
230 OpenGl_ListOfStructure::Iterator its(myConnected);
233 // Check for the given structure
234 if (its.Value() == AStructure)
236 myConnected.Remove(its);
243 /*----------------------------------------------------------------------*/
245 OpenGl_Group * OpenGl_Structure::AddGroup ()
248 OpenGl_Group *g = new OpenGl_Group;
253 /*----------------------------------------------------------------------*/
255 void OpenGl_Structure::RemoveGroup (const Handle(OpenGl_Context)& theGlCtx,
256 const OpenGl_Group* theGroup)
258 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
260 // Check for the given group
261 if (anIter.Value() == theGroup)
264 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
265 myGroups.Remove (anIter);
271 /*----------------------------------------------------------------------*/
273 void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
276 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
279 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
284 /*----------------------------------------------------------------------*/
286 void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
288 // Process the structure only if visible
289 if ( myNamedStatus & OPENGL_NS_HIDE )
292 // Render named status
293 const Standard_Integer named_status = AWorkspace->NamedStatus;
294 AWorkspace->NamedStatus |= myNamedStatus;
296 // Is rendering in ADD or IMMEDIATE mode?
297 const Standard_Boolean isImmediate = (AWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0;
299 // Apply local transformation
300 GLint matrix_mode = 0;
301 const OpenGl_Matrix *local_trsf = NULL;
302 if (myTransformation)
307 call_util_transpose_mat (mat16, myTransformation->mat);
308 glGetIntegerv (GL_MATRIX_MODE, &matrix_mode);
309 glMatrixMode (GL_MODELVIEW);
311 glScalef (1.F, 1.F, 1.F);
312 glMultMatrixf (mat16);
316 glMatrixMode (GL_MODELVIEW);
319 local_trsf = AWorkspace->SetStructureMatrix(myTransformation);
323 // Apply transform persistence
324 const TEL_TRANSFORM_PERSISTENCE *trans_pers = NULL;
325 if ( myTransPers && myTransPers->mode != 0 )
327 trans_pers = AWorkspace->ActiveView()->BeginTransformPersistence( myTransPers );
331 const OpenGl_AspectLine *aspect_line = AWorkspace->AspectLine(Standard_False);
332 const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace(Standard_False);
333 const OpenGl_AspectMarker *aspect_marker = AWorkspace->AspectMarker(Standard_False);
334 const OpenGl_AspectText *aspect_text = AWorkspace->AspectText(Standard_False);
336 AWorkspace->SetAspectLine(myAspectLine);
338 AWorkspace->SetAspectFace(myAspectFace);
340 AWorkspace->SetAspectMarker(myAspectMarker);
342 AWorkspace->SetAspectText(myAspectText);
344 // Apply highlight box
346 myHighlightBox->Render( AWorkspace );
348 // Apply highlight color
349 const TEL_COLOUR *highlight_color = AWorkspace->HighlightColor;
350 if (myHighlightColor)
351 AWorkspace->HighlightColor = myHighlightColor;
353 // Render connected structures
354 OpenGl_ListOfStructure::Iterator its(myConnected);
357 its.Value()->Render(AWorkspace);
362 OpenGl_ListOfGroup::Iterator itg(myGroups);
365 itg.Value()->Render(AWorkspace);
369 // Restore highlight color
370 AWorkspace->HighlightColor = highlight_color;
373 AWorkspace->SetAspectLine(aspect_line);
374 AWorkspace->SetAspectFace(aspect_face);
375 AWorkspace->SetAspectMarker(aspect_marker);
376 AWorkspace->SetAspectText(aspect_text);
378 // Restore transform persistence
379 if ( myTransPers && myTransPers->mode != 0 )
381 AWorkspace->ActiveView()->BeginTransformPersistence( trans_pers );
384 // Restore local transformation
385 if (myTransformation)
390 glMatrixMode (matrix_mode);
394 AWorkspace->SetStructureMatrix(local_trsf);
396 glMatrixMode (GL_MODELVIEW);
401 // Restore named status
402 AWorkspace->NamedStatus = named_status;
405 // =======================================================================
406 // function : Release
408 // =======================================================================
409 void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
413 OpenGl_Element::Destroy (theGlCtx, myAspectLine);
414 OpenGl_Element::Destroy (theGlCtx, myAspectFace);
415 OpenGl_Element::Destroy (theGlCtx, myAspectMarker);
416 OpenGl_Element::Destroy (theGlCtx, myAspectText);
417 ClearHighlightColor (theGlCtx);
420 // =======================================================================
421 // function : ReleaseGlResources
423 // =======================================================================
424 void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx)
426 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
428 OpenGl_Group* aGroup = const_cast<OpenGl_Group*& > (anIter.ChangeValue());
431 aGroup->Release (theGlCtx);
434 if (myAspectLine != NULL)
436 myAspectLine->Release (theGlCtx);
438 if (myAspectFace != NULL)
440 myAspectFace->Release (theGlCtx);
442 if (myAspectMarker != NULL)
444 myAspectMarker->Release (theGlCtx);
446 if (myAspectText != NULL)
448 myAspectText->Release (theGlCtx);
450 if (myHighlightBox != NULL)
452 myHighlightBox->Release (theGlCtx);
456 //=======================================================================
457 //function : SetZLayer
459 //=======================================================================
461 void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex)
463 myZLayer = theLayerIndex;
466 //=======================================================================
467 //function : GetZLayer
469 //=======================================================================
471 Standard_Integer OpenGl_Structure::GetZLayer () const