0024138: Exception during projection of the point on the face
[occt.git] / src / OpenGl / OpenGl_Structure.cxx
CommitLineData
b311480e 1// Created on: 2011-08-01
2// Created by: Sergey ZERCHANINOV
3// Copyright (c) 2011-2012 OPEN CASCADE SAS
4//
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.
9//
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.
12//
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.
19
5f8b738e 20#include <OpenGl_GlCore11.hxx>
21
2166f0fa
SK
22#include <OpenGl_Structure.hxx>
23
2166f0fa 24#include <OpenGl_Workspace.hxx>
27eed937 25#include <OpenGl_Vec.hxx>
2166f0fa
SK
26#include <OpenGl_View.hxx>
27
2166f0fa
SK
28#include <OpenGl_telem_util.hxx>
29
27eed937 30//! Auxiliary class for bounding box presentation
31class OpenGl_BndBoxPrs : public OpenGl_Element
32{
33
34public:
35
36 //! Main constructor
37 OpenGl_BndBoxPrs (const CALL_DEF_BOUNDBOX& theBndBox)
38 {
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);
61 }
62
63 //! Render presentation
64 virtual void Render (const Handle(OpenGl_Workspace)& theWorkspace) const
65 {
66 // Apply line aspect
67 const OpenGl_AspectLine* anAspectLine = theWorkspace->AspectLine (Standard_True);
68 const Handle(OpenGl_Texture) aPrevTexture = theWorkspace->DisableTexture();
69
70 glDisable (GL_LIGHTING);
71 if ((theWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0)
72 {
73 glDepthMask (GL_FALSE);
74 }
75
76 // Use highlight colors
77 glColor3fv ((theWorkspace->NamedStatus & OPENGL_NS_HIGHLIGHT) ? theWorkspace->HighlightColor->rgb : anAspectLine->Color().rgb);
78
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);
83
84 // restore aspects
85 if (!aPrevTexture.IsNull())
86 {
87 theWorkspace->EnableTexture (aPrevTexture);
88 }
89 }
90
91 //! Release graphical resources
92 virtual void Release (const Handle(OpenGl_Context)& )
93 {
94 //
95 }
96
97protected:
98
99 //! Protected destructor
100 virtual ~OpenGl_BndBoxPrs() {}
101
102private:
103
104 OpenGl_Vec3 myVerts[16]; //!< vertices array
105
106public:
107
108 DEFINE_STANDARD_ALLOC
109
110};
2166f0fa
SK
111
112/*----------------------------------------------------------------------*/
113
114static void call_util_transpose_mat (float tmat[16], float mat[4][4])
115{
116 int i, j;
117
118 for (i=0; i<4; i++)
119 for (j=0; j<4; j++)
120 tmat[j*4+i] = mat[i][j];
121}
122
123/*----------------------------------------------------------------------*/
124
125OpenGl_Structure::OpenGl_Structure ()
126: myTransformation(NULL),
127 myTransPers(NULL),
2166f0fa
SK
128 myAspectLine(NULL),
129 myAspectFace(NULL),
130 myAspectMarker(NULL),
131 myAspectText(NULL),
132 myHighlightBox(NULL),
133 myHighlightColor(NULL),
59f45b7c 134 myNamedStatus(0),
135 myZLayer(0)
2166f0fa
SK
136{
137}
138
139/*----------------------------------------------------------------------*/
140
5e27df78 141OpenGl_Structure::~OpenGl_Structure()
2166f0fa 142{
5e27df78 143 Release (Handle(OpenGl_Context)());
144 delete myTransformation; myTransformation = NULL;
145 delete myTransPers; myTransPers = NULL;
2166f0fa
SK
146}
147
148/*----------------------------------------------------------------------*/
149
150void OpenGl_Structure::SetTransformation(const float *AMatrix)
151{
152 if (!myTransformation)
5e27df78 153 myTransformation = new OpenGl_Matrix();
2166f0fa
SK
154
155 matcpy( myTransformation->mat, AMatrix );
156}
157
158/*----------------------------------------------------------------------*/
159
160void OpenGl_Structure::SetTransformPersistence(const CALL_DEF_TRANSFORM_PERSISTENCE &ATransPers)
161{
162 if (!myTransPers)
163 myTransPers = new TEL_TRANSFORM_PERSISTENCE;
164
165 myTransPers->mode = ATransPers.Flag;
166 myTransPers->pointX = ATransPers.Point.x;
167 myTransPers->pointY = ATransPers.Point.y;
168 myTransPers->pointZ = ATransPers.Point.z;
169}
170
171/*----------------------------------------------------------------------*/
172
2166f0fa
SK
173void OpenGl_Structure::SetAspectLine (const CALL_DEF_CONTEXTLINE &AContext)
174{
175 if (!myAspectLine)
5e27df78 176 myAspectLine = new OpenGl_AspectLine();
2166f0fa
SK
177 myAspectLine->SetContext( AContext );
178}
179
180/*----------------------------------------------------------------------*/
181
bf75be98 182void OpenGl_Structure::SetAspectFace (const Handle(OpenGl_Context)& theCtx,
183 const CALL_DEF_CONTEXTFILLAREA& theAspect)
2166f0fa
SK
184{
185 if (!myAspectFace)
bf75be98 186 {
5e27df78 187 myAspectFace = new OpenGl_AspectFace();
bf75be98 188 }
189 myAspectFace->Init (theCtx, theAspect);
2166f0fa
SK
190}
191
192/*----------------------------------------------------------------------*/
193
194void OpenGl_Structure::SetAspectMarker (const CALL_DEF_CONTEXTMARKER &AContext)
195{
196 if (!myAspectMarker)
5e27df78 197 myAspectMarker = new OpenGl_AspectMarker();
2166f0fa
SK
198 myAspectMarker->SetContext( AContext );
199}
200
201/*----------------------------------------------------------------------*/
202
203void OpenGl_Structure::SetAspectText (const CALL_DEF_CONTEXTTEXT &AContext)
204{
205 if (!myAspectText)
5e27df78 206 myAspectText = new OpenGl_AspectText();
2166f0fa
SK
207 myAspectText->SetContext( AContext );
208}
209
210/*----------------------------------------------------------------------*/
211
5e27df78 212void OpenGl_Structure::SetHighlightBox (const Handle(OpenGl_Context)& theGlCtx,
213 const CALL_DEF_BOUNDBOX& theBoundBox)
2166f0fa 214{
5e27df78 215 if (myHighlightBox != NULL)
216 {
217 myHighlightBox->Release (theGlCtx);
218 }
2166f0fa 219 else
5e27df78 220 {
221 myHighlightBox = new OpenGl_Group();
222 }
2166f0fa 223
5e27df78 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);
2166f0fa 229
27eed937 230 OpenGl_BndBoxPrs* aBndBoxPrs = new OpenGl_BndBoxPrs (theBoundBox);
231 myHighlightBox->AddElement (TelParray, aBndBoxPrs);
2166f0fa
SK
232}
233
234/*----------------------------------------------------------------------*/
235
5e27df78 236void OpenGl_Structure::ClearHighlightBox (const Handle(OpenGl_Context)& theGlCtx)
2166f0fa 237{
5e27df78 238 if (myHighlightBox != NULL)
2166f0fa 239 {
5e27df78 240 OpenGl_Element::Destroy (theGlCtx, myHighlightBox);
2166f0fa
SK
241 }
242}
243
244/*----------------------------------------------------------------------*/
245
5e27df78 246void OpenGl_Structure::SetHighlightColor (const Handle(OpenGl_Context)& theGlCtx,
247 const Standard_ShortReal R,
248 const Standard_ShortReal G,
249 const Standard_ShortReal B)
2166f0fa 250{
5e27df78 251 ClearHighlightBox (theGlCtx);
252 if (myHighlightColor == NULL)
253 {
254 myHighlightColor = new TEL_COLOUR();
255 }
2166f0fa
SK
256
257 myHighlightColor->rgb[0] = R;
258 myHighlightColor->rgb[1] = G;
259 myHighlightColor->rgb[2] = B;
260 myHighlightColor->rgb[3] = 1.F;
261}
262
263/*----------------------------------------------------------------------*/
264
5e27df78 265void OpenGl_Structure::ClearHighlightColor (const Handle(OpenGl_Context)& theGlCtx)
2166f0fa 266{
5e27df78 267 ClearHighlightBox(theGlCtx);
268 delete myHighlightColor;
269 myHighlightColor = NULL;
2166f0fa
SK
270}
271
272/*----------------------------------------------------------------------*/
273
274void OpenGl_Structure::Connect (const OpenGl_Structure *AStructure)
275{
276 Disconnect (AStructure);
277 myConnected.Append(AStructure);
278}
279
280/*----------------------------------------------------------------------*/
281
282void OpenGl_Structure::Disconnect (const OpenGl_Structure *AStructure)
283{
284 OpenGl_ListOfStructure::Iterator its(myConnected);
285 while (its.More())
286 {
287 // Check for the given structure
288 if (its.Value() == AStructure)
289 {
290 myConnected.Remove(its);
291 return;
292 }
293 its.Next();
294 }
295}
296
297/*----------------------------------------------------------------------*/
298
299OpenGl_Group * OpenGl_Structure::AddGroup ()
300{
301 // Create new group
302 OpenGl_Group *g = new OpenGl_Group;
303 myGroups.Append(g);
304 return g;
305}
306
307/*----------------------------------------------------------------------*/
308
5e27df78 309void OpenGl_Structure::RemoveGroup (const Handle(OpenGl_Context)& theGlCtx,
310 const OpenGl_Group* theGroup)
2166f0fa 311{
5e27df78 312 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
2166f0fa
SK
313 {
314 // Check for the given group
5e27df78 315 if (anIter.Value() == theGroup)
2166f0fa
SK
316 {
317 // Delete object
5e27df78 318 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
319 myGroups.Remove (anIter);
2166f0fa
SK
320 return;
321 }
2166f0fa
SK
322 }
323}
324
325/*----------------------------------------------------------------------*/
326
5e27df78 327void OpenGl_Structure::Clear (const Handle(OpenGl_Context)& theGlCtx)
2166f0fa 328{
5e27df78 329 // Release groups
330 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
2166f0fa
SK
331 {
332 // Delete objects
5e27df78 333 OpenGl_Element::Destroy (theGlCtx, const_cast<OpenGl_Group*& > (anIter.ChangeValue()));
2166f0fa
SK
334 }
335 myGroups.Clear();
336}
337
338/*----------------------------------------------------------------------*/
339
340void OpenGl_Structure::Render (const Handle(OpenGl_Workspace) &AWorkspace) const
341{
342 // Process the structure only if visible
343 if ( myNamedStatus & OPENGL_NS_HIDE )
344 return;
345
346 // Render named status
347 const Standard_Integer named_status = AWorkspace->NamedStatus;
348 AWorkspace->NamedStatus |= myNamedStatus;
349
350 // Is rendering in ADD or IMMEDIATE mode?
351 const Standard_Boolean isImmediate = (AWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0;
352
353 // Apply local transformation
354 GLint matrix_mode = 0;
355 const OpenGl_Matrix *local_trsf = NULL;
356 if (myTransformation)
357 {
358 if (isImmediate)
359 {
360 float mat16[16];
361 call_util_transpose_mat (mat16, myTransformation->mat);
362 glGetIntegerv (GL_MATRIX_MODE, &matrix_mode);
363 glMatrixMode (GL_MODELVIEW);
364 glPushMatrix ();
365 glScalef (1.F, 1.F, 1.F);
366 glMultMatrixf (mat16);
367 }
368 else
369 {
370 glMatrixMode (GL_MODELVIEW);
371 glPushMatrix();
372
373 local_trsf = AWorkspace->SetStructureMatrix(myTransformation);
374 }
375 }
376
377 // Apply transform persistence
378 const TEL_TRANSFORM_PERSISTENCE *trans_pers = NULL;
379 if ( myTransPers && myTransPers->mode != 0 )
380 {
381 trans_pers = AWorkspace->ActiveView()->BeginTransformPersistence( myTransPers );
382 }
383
2166f0fa
SK
384 // Apply aspects
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);
389 if (myAspectLine)
390 AWorkspace->SetAspectLine(myAspectLine);
391 if (myAspectFace)
392 AWorkspace->SetAspectFace(myAspectFace);
393 if (myAspectMarker)
394 AWorkspace->SetAspectMarker(myAspectMarker);
395 if (myAspectText)
396 AWorkspace->SetAspectText(myAspectText);
397
398 // Apply highlight box
399 if (myHighlightBox)
400 myHighlightBox->Render( AWorkspace );
401
402 // Apply highlight color
403 const TEL_COLOUR *highlight_color = AWorkspace->HighlightColor;
404 if (myHighlightColor)
405 AWorkspace->HighlightColor = myHighlightColor;
406
407 // Render connected structures
408 OpenGl_ListOfStructure::Iterator its(myConnected);
409 while (its.More())
410 {
411 its.Value()->Render(AWorkspace);
412 its.Next();
413 }
414
415 // Render groups
416 OpenGl_ListOfGroup::Iterator itg(myGroups);
417 while (itg.More())
418 {
419 itg.Value()->Render(AWorkspace);
420 itg.Next();
421 }
422
423 // Restore highlight color
424 AWorkspace->HighlightColor = highlight_color;
425
426 // Restore aspects
427 AWorkspace->SetAspectLine(aspect_line);
428 AWorkspace->SetAspectFace(aspect_face);
429 AWorkspace->SetAspectMarker(aspect_marker);
430 AWorkspace->SetAspectText(aspect_text);
431
432 // Restore transform persistence
433 if ( myTransPers && myTransPers->mode != 0 )
434 {
435 AWorkspace->ActiveView()->BeginTransformPersistence( trans_pers );
436 }
437
438 // Restore local transformation
439 if (myTransformation)
440 {
441 if (isImmediate)
442 {
443 glPopMatrix ();
444 glMatrixMode (matrix_mode);
445 }
446 else
447 {
448 AWorkspace->SetStructureMatrix(local_trsf);
449
450 glMatrixMode (GL_MODELVIEW);
451 glPopMatrix();
452 }
453 }
454
455 // Restore named status
456 AWorkspace->NamedStatus = named_status;
457}
458
5e27df78 459// =======================================================================
460// function : Release
461// purpose :
462// =======================================================================
463void OpenGl_Structure::Release (const Handle(OpenGl_Context)& theGlCtx)
464{
465 // Release groups
466 Clear (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);
472}
473
dd8a4ce9 474// =======================================================================
475// function : ReleaseGlResources
476// purpose :
477// =======================================================================
478void OpenGl_Structure::ReleaseGlResources (const Handle(OpenGl_Context)& theGlCtx)
479{
480 for (OpenGl_ListOfGroup::Iterator anIter (myGroups); anIter.More(); anIter.Next())
481 {
482 OpenGl_Group* aGroup = const_cast<OpenGl_Group*& > (anIter.ChangeValue());
483 if (aGroup != NULL)
484 {
485 aGroup->Release (theGlCtx);
486 }
487 }
488 if (myAspectLine != NULL)
489 {
490 myAspectLine->Release (theGlCtx);
491 }
492 if (myAspectFace != NULL)
493 {
494 myAspectFace->Release (theGlCtx);
495 }
496 if (myAspectMarker != NULL)
497 {
498 myAspectMarker->Release (theGlCtx);
499 }
500 if (myAspectText != NULL)
501 {
502 myAspectText->Release (theGlCtx);
503 }
504 if (myHighlightBox != NULL)
505 {
506 myHighlightBox->Release (theGlCtx);
507 }
508}
509
59f45b7c 510//=======================================================================
511//function : SetZLayer
bf75be98 512//purpose :
59f45b7c 513//=======================================================================
514
515void OpenGl_Structure::SetZLayer (const Standard_Integer theLayerIndex)
516{
517 myZLayer = theLayerIndex;
518}
519
520//=======================================================================
521//function : GetZLayer
bf75be98 522//purpose :
59f45b7c 523//=======================================================================
524
525Standard_Integer OpenGl_Structure::GetZLayer () const
526{
527 return myZLayer;
528}