cf80e70a45f830eb888d5b09bb89be6e2b8cc9bf
[occt.git] / src / OpenGl / OpenGl_Group.cxx
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
20 #include <OpenGl_Group.hxx>
21
22 #include <OpenGl_PrimitiveArray.hxx>
23 #include <OpenGl_Workspace.hxx>
24
25 /*----------------------------------------------------------------------*/
26
27 OpenGl_Group::OpenGl_Group ()
28 : myAspectLine(NULL),
29   myAspectFace(NULL),
30   myAspectMarker(NULL),
31   myAspectText(NULL),
32   myFirst(NULL), myLast(NULL)
33 {
34 }
35
36 OpenGl_Group::~OpenGl_Group()
37 {
38   Release (Handle(OpenGl_Context)());
39 }
40
41 /*----------------------------------------------------------------------*/
42
43 void OpenGl_Group::SetAspectLine (const CALL_DEF_CONTEXTLINE& theContext,
44                                   const Standard_Boolean theIsGlobal)
45 {
46   if (theIsGlobal || myFirst == NULL)
47   {
48     if (myAspectLine == NULL)
49       myAspectLine = new OpenGl_AspectLine();
50     myAspectLine->SetContext (theContext);
51   }
52   else
53   {
54     OpenGl_AspectLine* anAspectLine = new OpenGl_AspectLine();
55     anAspectLine->SetContext (theContext);
56     AddElement (TelNil/*TelAspectLine*/, anAspectLine);
57   }
58 }
59
60 /*----------------------------------------------------------------------*/
61
62 void OpenGl_Group::SetAspectFace (const Handle(OpenGl_Context)&   theCtx,
63                                   const CALL_DEF_CONTEXTFILLAREA& theAspect,
64                                   const Standard_Boolean          theIsGlobal)
65 {
66   if (theIsGlobal || myFirst == NULL)
67   {
68     if (myAspectFace == NULL)
69     {
70       myAspectFace = new OpenGl_AspectFace();
71     }
72     myAspectFace->Init (theCtx, theAspect);
73   }
74   else
75   {
76     OpenGl_AspectFace* anAspectFace = new OpenGl_AspectFace();
77     anAspectFace->Init (theCtx, theAspect);
78     AddElement (TelNil/*TelAspectFace*/, anAspectFace);
79   }
80 }
81
82 /*----------------------------------------------------------------------*/
83
84 void OpenGl_Group::SetAspectMarker (const CALL_DEF_CONTEXTMARKER& theContext,
85                                     const Standard_Boolean theIsGlobal)
86 {
87   if (theIsGlobal || myFirst == NULL)
88   {
89     if (myAspectMarker == NULL)
90       myAspectMarker = new OpenGl_AspectMarker();
91     myAspectMarker->SetContext (theContext);
92   }
93   else
94   {
95     OpenGl_AspectMarker* anAspectMarker = new OpenGl_AspectMarker();
96     anAspectMarker->SetContext (theContext);
97     AddElement (TelNil/*TelAspectMarker*/, anAspectMarker);
98   }
99 }
100
101 /*----------------------------------------------------------------------*/
102
103 void OpenGl_Group::SetAspectText (const CALL_DEF_CONTEXTTEXT& theContext,
104                                   const Standard_Boolean theIsGlobal)
105 {
106   if (theIsGlobal || myFirst == NULL)
107   {
108     if (myAspectText == NULL)
109       myAspectText = new OpenGl_AspectText();
110     myAspectText->SetContext (theContext);
111   }
112   else
113   {
114     OpenGl_AspectText* anAspectText = new OpenGl_AspectText();
115     anAspectText->SetContext (theContext);
116     AddElement ( TelNil/*TelAspectText*/, anAspectText);
117   }
118 }
119
120 /*----------------------------------------------------------------------*/
121
122 void OpenGl_Group::AddElement (const TelType AType, OpenGl_Element *AElem )
123 {
124   OpenGl_ElementNode *node = new OpenGl_ElementNode();
125
126   node->type = AType;
127   node->elem = AElem;
128   node->next = NULL;
129   (myLast? myLast->next : myFirst) = node;
130   myLast = node;
131 }
132
133 /*----------------------------------------------------------------------*/
134
135 void OpenGl_Group::Render (const Handle(OpenGl_Workspace)& theWorkspace) const
136 {
137   // Is rendering in ADD or IMMEDIATE mode?
138   const Standard_Boolean isImmediate = (theWorkspace->NamedStatus & (OPENGL_NS_ADD | OPENGL_NS_IMMEDIATE)) != 0;
139
140   // Setup aspects
141   const OpenGl_AspectLine*   aBackAspectLine   = theWorkspace->AspectLine   (Standard_False);
142   const OpenGl_AspectFace*   aBackAspectFace   = theWorkspace->AspectFace   (Standard_False);
143   const OpenGl_AspectMarker* aBackAspectMarker = theWorkspace->AspectMarker (Standard_False);
144   const OpenGl_AspectText*   aBackAspectText   = theWorkspace->AspectText   (Standard_False);
145   if (myAspectLine)
146   {
147     theWorkspace->SetAspectLine (myAspectLine);
148   }
149   if (myAspectFace)
150   {
151     theWorkspace->SetAspectFace (myAspectFace);
152   }
153   if (myAspectMarker)
154   {
155     theWorkspace->SetAspectMarker (myAspectMarker);
156   }
157   if (myAspectText)
158   {
159     theWorkspace->SetAspectText (myAspectText);
160   }
161
162   // Render group elements
163   for (OpenGl_ElementNode* aNodeIter = myFirst; aNodeIter != NULL; aNodeIter = aNodeIter->next)
164   {
165     switch (aNodeIter->type)
166     {
167       case TelMarker:
168       case TelMarkerSet:
169       case TelText:
170       {
171         glDisable (GL_LIGHTING);
172         if (isImmediate)
173         {
174           glDepthMask (GL_FALSE);
175         }
176
177         aNodeIter->elem->Render (theWorkspace);
178         break;
179       }
180       default:
181       {
182         aNodeIter->elem->Render (theWorkspace);
183         break;
184       }
185     }
186   }
187
188   // Restore aspects
189   theWorkspace->SetAspectLine   (aBackAspectLine);
190   theWorkspace->SetAspectFace   (aBackAspectFace);
191   theWorkspace->SetAspectMarker (aBackAspectMarker);
192   theWorkspace->SetAspectText   (aBackAspectText);
193 }
194
195 void OpenGl_Group::Release (const Handle(OpenGl_Context)& theGlCtx)
196 {
197   // Delete elements
198   while (myFirst != NULL)
199   {
200     OpenGl_ElementNode* aNext = myFirst->next;
201     OpenGl_Element::Destroy (theGlCtx, myFirst->elem);
202     delete myFirst;
203     myFirst = aNext;
204   }
205   myLast = NULL;
206
207   OpenGl_Element::Destroy (theGlCtx, myAspectLine);
208   OpenGl_Element::Destroy (theGlCtx, myAspectFace);
209   OpenGl_Element::Destroy (theGlCtx, myAspectMarker);
210   OpenGl_Element::Destroy (theGlCtx, myAspectText);
211 }