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.
20 #include <OpenGl_GlCore11.hxx>
22 #include <OpenGl_Structure.hxx>
24 #include <OpenGl_Workspace.hxx>
25 #include <OpenGl_Vec.hxx>
26 #include <OpenGl_View.hxx>
28 #include <OpenGl_telem_util.hxx>
30 //! Auxiliary class for bounding box presentation
31 class OpenGl_BndBoxPrs : public OpenGl_Element
37 OpenGl_BndBoxPrs (const CALL_DEF_BOUNDBOX& theBndBox)
39 const float Xm = theBndBox.Pmin.x;
40 const float Ym = theBndBox.Pmin.y;
41 const float Zm = theBndBox.Pmin.z;
42 const float XM = theBndBox.Pmax.x;
43 const float YM = theBndBox.Pmax.y;
44 const float ZM = theBndBox.Pmax.z;
45 myVerts[0] = OpenGl_Vec3 (Xm, Ym, Zm);
46 myVerts[1] = OpenGl_Vec3 (Xm, Ym, ZM);
47 myVerts[2] = OpenGl_Vec3 (Xm, YM, ZM);
48 myVerts[3] = OpenGl_Vec3 (Xm, YM, Zm);
49 myVerts[4] = OpenGl_Vec3 (Xm, Ym, Zm);
50 myVerts[5] = OpenGl_Vec3 (XM, Ym, Zm);
51 myVerts[6] = OpenGl_Vec3 (XM, Ym, ZM);
52 myVerts[7] = OpenGl_Vec3 (XM, YM, ZM);
53 myVerts[8] = OpenGl_Vec3 (XM, YM, Zm);
54 myVerts[9] = OpenGl_Vec3 (XM, Ym, Zm);
55 myVerts[10] = OpenGl_Vec3 (XM, YM, Zm);
56 myVerts[11] = OpenGl_Vec3 (Xm, YM, Zm);
57 myVerts[12] = OpenGl_Vec3 (Xm, YM, ZM);
58 myVerts[13] = OpenGl_Vec3 (XM, YM, ZM);
59 myVerts[14] = OpenGl_Vec3 (XM, Ym, ZM);
60 myVerts[15] = OpenGl_Vec3 (Xm, Ym, ZM);
63 //! Render presentation
64 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
67 const OpenGl_AspectLine* anAspectLine = theWorkspace->AspectLine (Standard_True);
68 const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
70 glDisable (GL_LIGHTING);
71 if ((theWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0)
73 glDepthMask (GL_FALSE);
76 // Use highlight colors
77 glColor3fv ((theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) ? theWorkspace->HighlightColor->rgb : anAspectLine->Color().rgb);
79 glEnableClientState (GL_VERTEX_ARRAY);
80 glVertexPointer (3, GL_FLOAT, 0, (GLfloat* )&myVerts);
81 glDrawArrays (GL_LINE_STRIP, 0, 16);
82 glDisableClientState (GL_VERTEX_ARRAY);
85 if (!aPrevTexture.IsNull())
87 theWorkspace->EnableTexture (aPrevTexture);
91 //! Release graphical resources
92 virtual void Release (const Handle(OpenGl_Context)& )
99 //! Protected destructor
100 virtual ~OpenGl_BndBoxPrs() {}
104 OpenGl_Vec3 myVerts[16]; //!< vertices array
108 DEFINE_STANDARD_ALLOC
112 /*----------------------------------------------------------------------*/
114 static void call_util_transpose_mat (float tmat[16], float mat[4][4])
120 tmat[j*4+i] = mat[i][j];
123 /*----------------------------------------------------------------------*/
125 OpenGl_Structure::OpenGl_Structure ()
126 : myTransformation(NULL),
130 myAspectMarker(NULL),
132 myHighlightBox(NULL),
133 myHighlightColor(NULL),
139 /*----------------------------------------------------------------------*/
141 OpenGl_Structure::~OpenGl_Structure()
143 Release (Handle(OpenGl_Context)());
144 delete myTransformation; myTransformation = NULL;
145 delete myTransPers; myTransPers = NULL;
148 /*----------------------------------------------------------------------*/
150 void OpenGl_Structure::SetTransformation(const float *AMatrix)
152 if (!myTransformation)
153 myTransformation = new OpenGl_Matrix();
155 matcpy( myTransformation->mat, AMatrix );
158 /*----------------------------------------------------------------------*/
160 void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers)
163 myTransPers = new TEL_TRANSFORM_PERSISTENCE;
165 myTransPers->mode = ATransPers.Flag;
166 myTransPers->pointX = ATransPers.Point.x;
167 myTransPers->pointY = ATransPers.Point.y;
168 myTransPers->pointZ = ATransPers.Point.z;
171 /*----------------------------------------------------------------------*/
173 void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &AContext)
176 myAspectLine = new OpenGl_AspectLine();
177 myAspectLine->SetContext( AContext );
180 /*----------------------------------------------------------------------*/
182 void OpenGl_Structure::SetAspectFace (const Handle(OpenGl_Context)& theCtx,
183 const CALL_DEF_CONTEXTFILLAREA& theAspect)
187 myAspectFace = new OpenGl_AspectFace();
189 myAspectFace->Init (theCtx, theAspect);
192 /*----------------------------------------------------------------------*/
194 void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER &AContext)
197 myAspectMarker = new OpenGl_AspectMarker();
198 myAspectMarker->SetContext( AContext );
201 /*----------------------------------------------------------------------*/
203 void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &AContext)
206 myAspectText = new OpenGl_AspectText();
207 myAspectText->SetContext( AContext );
210 /*----------------------------------------------------------------------*/
212 void OpenGl_Structure::SetHighlightBox (const Handle(OpenGl_Context)& theGlCtx,
213 const CALL_DEF_BOUNDBOX& theBoundBox)
215 if (myHighlightBox != NULL)
217 myHighlightBox->Release (theGlCtx);
221 myHighlightBox = new OpenGl_Group();
224 CALL_DEF_CONTEXTLINE aContextLine;
225 aContextLine.Color = theBoundBox.Color;
226 aContextLine.LineType = Aspect_TOL_SOLID;
227 aContextLine.Width = 1.0f;
228 myHighlightBox->SetAspectLine (aContextLine);
230 OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (theBoundBox);
231 myHighlightBox->AddElement (TelParray, aBndBoxPrs);
234 /*----------------------------------------------------------------------*/
236 void OpenGl_Structure::ClearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
238 if (myHighlightBox != NULL)
240 OpenGl_Element::Destroy (theGlCtx, myHighlightBox);
244 /*----------------------------------------------------------------------*/
246 void OpenGl_Structure::SetHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
247 const Standard_ShortReal R,
248 const Standard_ShortReal G,
249 const Standard_ShortReal B)
251 ClearHighlightBox (theGlCtx);
252 if (myHighlightColor == NULL)
254 myHighlightColor = new TEL_COLOUR();
257 myHighlightColor->rgb[0] = R;
258 myHighlightColor->rgb[1] = G;
259 myHighlightColor->rgb[2] = B;
260 myHighlightColor->rgb[3] = 1.F;
263 /*----------------------------------------------------------------------*/
265 void OpenGl_Structure::ClearHighlightColor (const Handle(OpenGl_Context)& theGlCtx)
267 ClearHighlightBox(theGlCtx);
268 delete myHighlightColor;
269 myHighlightColor = NULL;
272 /*----------------------------------------------------------------------*/
274 void OpenGl_Structure::Connect (const OpenGl_Structure *AStructure)
276 Disconnect (AStructure);
277 myConnected.Append(AStructure);
280 /*----------------------------------------------------------------------*/
282 void OpenGl_Structure::Disconnect (const OpenGl_Structure *AStructure)
284 OpenGl_ListOfStructure::Iterator its(myConnected);
287 // Check for the given structure
288 if (its.Value() == AStructure)
290 myConnected.Remove(its);
297 /*----------------------------------------------------------------------*/
299 OpenGl_Group * OpenGl_Structure::AddGroup ()
302 OpenGl_Group *g = new OpenGl_Group;
307 /*----------------------------------------------------------------------*/
309 void OpenGl_Structure::RemoveGroup (const Handle(OpenGl_Context)& theGlCtx,
310 const OpenGl_Group* theGroup)
312 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
314 // Check for the given group
315 if (anIter.Value() == theGroup)
318 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
319 myGroups.Remove (anIter);
325 /*----------------------------------------------------------------------*/
327 void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
330 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
333 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
338 /*----------------------------------------------------------------------*/
340 void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
342 // Process the structure only if visible
343 if ( myNamedStatus & OPENGL_NS_HIDE )
346 // Render named status
347 const Standard_Integer named_status = AWorkspace->NamedStatus;
348 AWorkspace->NamedStatus |= myNamedStatus;
350 // Is rendering in ADD or IMMEDIATE mode?
351 const Standard_Boolean isImmediate = (AWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0;
353 // Apply local transformation
354 GLint matrix_mode = 0;
355 const OpenGl_Matrix *local_trsf = NULL;
356 if (myTransformation)
361 call_util_transpose_mat (mat16, myTransformation->mat);
362 glGetIntegerv (GL_MATRIX_MODE, &matrix_mode);
363 glMatrixMode (GL_MODELVIEW);
365 glScalef (1.F, 1.F, 1.F);
366 glMultMatrixf (mat16);
370 glMatrixMode (GL_MODELVIEW);
373 local_trsf = AWorkspace->SetStructureMatrix(myTransformation);
377 // Apply transform persistence
378 const TEL_TRANSFORM_PERSISTENCE *trans_pers = NULL;
379 if ( myTransPers && myTransPers->mode != 0 )
381 trans_pers = AWorkspace->ActiveView()->BeginTransformPersistence( myTransPers );
385 const OpenGl_AspectLine *aspect_line = AWorkspace->AspectLine(Standard_False);
386 const OpenGl_AspectFace *aspect_face = AWorkspace->AspectFace(Standard_False);
387 const OpenGl_AspectMarker *aspect_marker = AWorkspace->AspectMarker(Standard_False);
388 const OpenGl_AspectText *aspect_text = AWorkspace->AspectText(Standard_False);
390 AWorkspace->SetAspectLine(myAspectLine);
392 AWorkspace->SetAspectFace(myAspectFace);
394 AWorkspace->SetAspectMarker(myAspectMarker);
396 AWorkspace->SetAspectText(myAspectText);
398 // Apply highlight box
400 myHighlightBox->Render( AWorkspace );
402 // Apply highlight color
403 const TEL_COLOUR *highlight_color = AWorkspace->HighlightColor;
404 if (myHighlightColor)
405 AWorkspace->HighlightColor = myHighlightColor;
407 // Render connected structures
408 OpenGl_ListOfStructure::Iterator its(myConnected);
411 its.Value()->Render(AWorkspace);
416 OpenGl_ListOfGroup::Iterator itg(myGroups);
419 itg.Value()->Render(AWorkspace);
423 // Restore highlight color
424 AWorkspace->HighlightColor = highlight_color;
427 AWorkspace->SetAspectLine(aspect_line);
428 AWorkspace->SetAspectFace(aspect_face);
429 AWorkspace->SetAspectMarker(aspect_marker);
430 AWorkspace->SetAspectText(aspect_text);
432 // Restore transform persistence
433 if ( myTransPers && myTransPers->mode != 0 )
435 AWorkspace->ActiveView()->BeginTransformPersistence( trans_pers );
438 // Restore local transformation
439 if (myTransformation)
444 glMatrixMode (matrix_mode);
448 AWorkspace->SetStructureMatrix(local_trsf);
450 glMatrixMode (GL_MODELVIEW);
455 // Restore named status
456 AWorkspace->NamedStatus = named_status;
459 // =======================================================================
460 // function : Release
462 // =======================================================================
463 void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
467 OpenGl_Element::Destroy (theGlCtx, myAspectLine);
468 OpenGl_Element::Destroy (theGlCtx, myAspectFace);
469 OpenGl_Element::Destroy (theGlCtx, myAspectMarker);
470 OpenGl_Element::Destroy (theGlCtx, myAspectText);
471 ClearHighlightColor (theGlCtx);
474 // =======================================================================
475 // function : ReleaseGlResources
477 // =======================================================================
478 void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx)
480 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
482 OpenGl_Group* aGroup = const_cast<OpenGl_Group*& > (anIter.ChangeValue());
485 aGroup->Release (theGlCtx);
488 if (myAspectLine != NULL)
490 myAspectLine->Release (theGlCtx);
492 if (myAspectFace != NULL)
494 myAspectFace->Release (theGlCtx);
496 if (myAspectMarker != NULL)
498 myAspectMarker->Release (theGlCtx);
500 if (myAspectText != NULL)
502 myAspectText->Release (theGlCtx);
504 if (myHighlightBox != NULL)
506 myHighlightBox->Release (theGlCtx);
510 //=======================================================================
511 //function : SetZLayer
513 //=======================================================================
515 void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex)
517 myZLayer = theLayerIndex;
520 //=======================================================================
521 //function : GetZLayer
523 //=======================================================================
525 Standard_Integer OpenGl_Structure::GetZLayer () const