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),
48 myDegenerateModel(NULL),
54 myHighlightColor(NULL),
60 /*----------------------------------------------------------------------*/
62 OpenGl_Structure::~OpenGl_Structure()
64 Release (Handle(OpenGl_Context)());
65 delete myTransformation; myTransformation = NULL;
66 delete myTransPers; myTransPers = NULL;
67 delete myDegenerateModel; myDegenerateModel = NULL;
70 /*----------------------------------------------------------------------*/
72 void OpenGl_Structure::SetTransformation(const float *AMatrix)
74 if (!myTransformation)
75 myTransformation = new OpenGl_Matrix();
77 matcpy( myTransformation->mat, AMatrix );
80 /*----------------------------------------------------------------------*/
82 void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers)
85 myTransPers = new TEL_TRANSFORM_PERSISTENCE;
87 myTransPers->mode = ATransPers.Flag;
88 myTransPers->pointX = ATransPers.Point.x;
89 myTransPers->pointY = ATransPers.Point.y;
90 myTransPers->pointZ = ATransPers.Point.z;
93 /*----------------------------------------------------------------------*/
95 void OpenGl_Structure::SetDegenerateModel (const Standard_Integer AMode, const float ASkipRatio)
97 if (!myDegenerateModel)
98 myDegenerateModel = new DEGENERATION;
100 myDegenerateModel->mode = AMode;
101 myDegenerateModel->skipRatio = ASkipRatio;
104 /*----------------------------------------------------------------------*/
106 void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &AContext)
109 myAspectLine = new OpenGl_AspectLine();
110 myAspectLine->SetContext( AContext );
113 /*----------------------------------------------------------------------*/
115 void OpenGl_Structure::SetAspectFace (const CALL_DEF_CONTEXTFILLAREA &AContext)
118 myAspectFace = new OpenGl_AspectFace();
119 myAspectFace->SetContext( AContext );
122 /*----------------------------------------------------------------------*/
124 void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER &AContext)
127 myAspectMarker = new OpenGl_AspectMarker();
128 myAspectMarker->SetContext( AContext );
131 /*----------------------------------------------------------------------*/
133 void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &AContext)
136 myAspectText = new OpenGl_AspectText();
137 myAspectText->SetContext( AContext );
140 /*----------------------------------------------------------------------*/
142 void OpenGl_Structure::SetHighlightBox (const Handle(OpenGl_Context)& theGlCtx,
143 const CALL_DEF_BOUNDBOX& theBoundBox)
145 if (myHighlightBox != NULL)
147 myHighlightBox->Release (theGlCtx);
151 myHighlightBox = new OpenGl_Group();
154 CALL_DEF_CONTEXTLINE aContextLine;
155 aContextLine.Color = theBoundBox.Color;
156 aContextLine.LineType = Aspect_TOL_SOLID;
157 aContextLine.Width = 1.0f;
158 myHighlightBox->SetAspectLine (aContextLine);
160 #define CALL_MAX_BOUNDBOXSIZE 16
162 Graphic3d_Array1OfVertex aPoints (1, CALL_MAX_BOUNDBOXSIZE);
163 const float Xm = theBoundBox.Pmin.x;
164 const float Ym = theBoundBox.Pmin.y;
165 const float Zm = theBoundBox.Pmin.z;
166 const float XM = theBoundBox.Pmax.x;
167 const float YM = theBoundBox.Pmax.y;
168 const float ZM = theBoundBox.Pmax.z;
169 aPoints( 1).SetCoord (Xm, Ym, Zm);
170 aPoints( 2).SetCoord (Xm, Ym, ZM);
171 aPoints( 3).SetCoord (Xm, YM, ZM);
172 aPoints( 4).SetCoord (Xm, YM, Zm);
173 aPoints( 5).SetCoord (Xm, Ym, Zm);
174 aPoints( 6).SetCoord (XM, Ym, Zm);
175 aPoints( 7).SetCoord (XM, Ym, ZM);
176 aPoints( 8).SetCoord (XM, YM, ZM);
177 aPoints( 9).SetCoord (XM, YM, Zm);
178 aPoints(10).SetCoord (XM, Ym, Zm);
179 aPoints(11).SetCoord (XM, YM, Zm);
180 aPoints(12).SetCoord (Xm, YM, Zm);
181 aPoints(13).SetCoord (Xm, YM, ZM);
182 aPoints(14).SetCoord (XM, YM, ZM);
183 aPoints(15).SetCoord (XM, Ym, ZM);
184 aPoints(16).SetCoord (Xm, Ym, ZM);
186 OpenGl_Polyline* aPolyline = new OpenGl_Polyline (aPoints);
187 myHighlightBox->AddElement (TelPolyline, aPolyline);
190 /*----------------------------------------------------------------------*/
192 void OpenGl_Structure::ClearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
194 if (myHighlightBox != NULL)
196 OpenGl_Element::Destroy (theGlCtx, myHighlightBox);
200 /*----------------------------------------------------------------------*/
202 void OpenGl_Structure::SetHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
203 const Standard_ShortReal R,
204 const Standard_ShortReal G,
205 const Standard_ShortReal B)
207 ClearHighlightBox (theGlCtx);
208 if (myHighlightColor == NULL)
210 myHighlightColor = new TEL_COLOUR();
213 myHighlightColor->rgb[0] = R;
214 myHighlightColor->rgb[1] = G;
215 myHighlightColor->rgb[2] = B;
216 myHighlightColor->rgb[3] = 1.F;
219 /*----------------------------------------------------------------------*/
221 void OpenGl_Structure::ClearHighlightColor (const Handle(OpenGl_Context)& theGlCtx)
223 ClearHighlightBox(theGlCtx);
224 delete myHighlightColor;
225 myHighlightColor = NULL;
228 /*----------------------------------------------------------------------*/
230 void OpenGl_Structure::Connect (const OpenGl_Structure *AStructure)
232 Disconnect (AStructure);
233 myConnected.Append(AStructure);
236 /*----------------------------------------------------------------------*/
238 void OpenGl_Structure::Disconnect (const OpenGl_Structure *AStructure)
240 OpenGl_ListOfStructure::Iterator its(myConnected);
243 // Check for the given structure
244 if (its.Value() == AStructure)
246 myConnected.Remove(its);
253 /*----------------------------------------------------------------------*/
255 OpenGl_Group * OpenGl_Structure::AddGroup ()
258 OpenGl_Group *g = new OpenGl_Group;
263 /*----------------------------------------------------------------------*/
265 void OpenGl_Structure::RemoveGroup (const Handle(OpenGl_Context)& theGlCtx,
266 const OpenGl_Group* theGroup)
268 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
270 // Check for the given group
271 if (anIter.Value() == theGroup)
274 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
275 myGroups.Remove (anIter);
281 /*----------------------------------------------------------------------*/
283 void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
286 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
289 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
294 /*----------------------------------------------------------------------*/
296 void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
298 // Process the structure only if visible
299 if ( myNamedStatus & OPENGL_NS_HIDE )
302 // Render named status
303 const Standard_Integer named_status = AWorkspace->NamedStatus;
304 AWorkspace->NamedStatus |= myNamedStatus;
306 // Is rendering in ADD or IMMEDIATE mode?
307 const Standard_Boolean isImmediate = (AWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0;
309 // Apply local transformation
310 GLint matrix_mode = 0;
311 const OpenGl_Matrix *local_trsf = NULL;
312 if (myTransformation)
317 call_util_transpose_mat (mat16, myTransformation->mat);
318 glGetIntegerv (GL_MATRIX_MODE, &matrix_mode);
319 glMatrixMode (GL_MODELVIEW);
321 glScalef (1.F, 1.F, 1.F);
322 glMultMatrixf (mat16);
326 glMatrixMode (GL_MODELVIEW);
329 local_trsf = AWorkspace->SetStructureMatrix(myTransformation);
333 // Apply transform persistence
334 const TEL_TRANSFORM_PERSISTENCE *trans_pers = NULL;
335 if ( myTransPers && myTransPers->mode != 0 )
337 trans_pers = AWorkspace->ActiveView()->BeginTransformPersistence( myTransPers );
340 // Apply degeneration
341 if (myDegenerateModel)
343 if ( AWorkspace->NamedStatus & OPENGL_NS_DEGENERATION )
345 AWorkspace->DegenerateModel = myDegenerateModel->mode;
346 switch ( AWorkspace->DegenerateModel )
352 glDisable ( GL_LINE_STIPPLE );
355 AWorkspace->SkipRatio = myDegenerateModel->skipRatio;
361 const OpenGl_AspectLine *aspect_line = AWorkspace->AspectLine(Standard_False);
362 const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace(Standard_False);
363 const OpenGl_AspectMarker *aspect_marker = AWorkspace->AspectMarker(Standard_False);
364 const OpenGl_AspectText *aspect_text = AWorkspace->AspectText(Standard_False);
366 AWorkspace->SetAspectLine(myAspectLine);
368 AWorkspace->SetAspectFace(myAspectFace);
370 AWorkspace->SetAspectMarker(myAspectMarker);
372 AWorkspace->SetAspectText(myAspectText);
374 // Apply highlight box
376 myHighlightBox->Render( AWorkspace );
378 // Apply highlight color
379 const TEL_COLOUR *highlight_color = AWorkspace->HighlightColor;
380 if (myHighlightColor)
381 AWorkspace->HighlightColor = myHighlightColor;
383 // Render connected structures
384 OpenGl_ListOfStructure::Iterator its(myConnected);
387 its.Value()->Render(AWorkspace);
392 OpenGl_ListOfGroup::Iterator itg(myGroups);
395 itg.Value()->Render(AWorkspace);
399 // Restore highlight color
400 AWorkspace->HighlightColor = highlight_color;
403 AWorkspace->SetAspectLine(aspect_line);
404 AWorkspace->SetAspectFace(aspect_face);
405 AWorkspace->SetAspectMarker(aspect_marker);
406 AWorkspace->SetAspectText(aspect_text);
408 // Restore transform persistence
409 if ( myTransPers && myTransPers->mode != 0 )
411 AWorkspace->ActiveView()->BeginTransformPersistence( trans_pers );
414 // Restore local transformation
415 if (myTransformation)
420 glMatrixMode (matrix_mode);
424 AWorkspace->SetStructureMatrix(local_trsf);
426 glMatrixMode (GL_MODELVIEW);
431 // Restore named status
432 AWorkspace->NamedStatus = named_status;
435 // =======================================================================
436 // function : Release
438 // =======================================================================
439 void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
443 OpenGl_Element::Destroy (theGlCtx, myAspectLine);
444 OpenGl_Element::Destroy (theGlCtx, myAspectFace);
445 OpenGl_Element::Destroy (theGlCtx, myAspectMarker);
446 OpenGl_Element::Destroy (theGlCtx, myAspectText);
447 ClearHighlightColor (theGlCtx);
450 // =======================================================================
451 // function : ReleaseGlResources
453 // =======================================================================
454 void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx)
456 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
458 OpenGl_Group* aGroup = const_cast<OpenGl_Group*& > (anIter.ChangeValue());
461 aGroup->Release (theGlCtx);
464 if (myAspectLine != NULL)
466 myAspectLine->Release (theGlCtx);
468 if (myAspectFace != NULL)
470 myAspectFace->Release (theGlCtx);
472 if (myAspectMarker != NULL)
474 myAspectMarker->Release (theGlCtx);
476 if (myAspectText != NULL)
478 myAspectText->Release (theGlCtx);
480 if (myHighlightBox != NULL)
482 myHighlightBox->Release (theGlCtx);
486 //=======================================================================
487 //function : SetZLayer
489 //=======================================================================
491 void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex)
493 myZLayer = theLayerIndex;
496 //=======================================================================
497 //function : GetZLayer
499 //=======================================================================
501 Standard_Integer OpenGl_Structure::GetZLayer () const