ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OpenGLImmediateSceneHandler.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4OpenGLImmediateSceneHandler.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 //
28 //
29 // Andrew Walkden 10th February 1997
30 // OpenGL immediate scene - draws immediately to buffer
31 // (saving space on server).
32 
33 #ifdef G4VIS_BUILD_OPENGL_DRIVER
34 
36 
37 #include "G4OpenGLViewer.hh"
38 #include "G4OpenGLTransform3D.hh"
39 #include "G4Polyline.hh"
40 #include "G4Polymarker.hh"
41 #include "G4Text.hh"
42 #include "G4Circle.hh"
43 #include "G4Square.hh"
44 #include "G4Scale.hh"
45 #include "G4Polyhedron.hh"
46 #include "G4AttHolder.hh"
47 
48 #include <typeinfo>
49 
50 G4OpenGLImmediateSceneHandler::G4OpenGLImmediateSceneHandler
52  G4OpenGLSceneHandler (system, fSceneIdCount++, name)
53 {}
54 
55 G4OpenGLImmediateSceneHandler::~G4OpenGLImmediateSceneHandler ()
56 {}
57 
58 #include <iomanip>
59 
60 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4VMarker& visible)
61 {
62  return AddPrimitivePreambleInternal(visible, true, false);
63 }
64 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Polyline& visible)
65 {
66  return AddPrimitivePreambleInternal(visible, false, true);
67 }
68 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Polyhedron& visible)
69 {
70  return AddPrimitivePreambleInternal(visible, false, false);
71 }
72 
73 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreambleInternal(const G4Visible& visible, bool isMarker, bool isPolyline)
74 {
75  // Get applicable vis attributes for all primitives.
76  fpVisAttribs = fpViewer->GetApplicableVisAttributes(visible.GetVisAttributes());
77  const G4Colour& c = GetColour ();
78  G4double opacity = c.GetAlpha ();
79 
80  G4bool transparency_enabled = true;
81  G4bool isMarkerNotHidden = true;
82  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
83  if (pViewer) {
84  transparency_enabled = pViewer->transparency_enabled;
85  isMarkerNotHidden = pViewer->fVP.IsMarkerNotHidden();
86  }
87 
88  G4bool isMarkerOrPolyline = isMarker || isPolyline;
89  G4bool treatAsTransparent = transparency_enabled && opacity < 1.;
90  G4bool treatAsNotHidden = isMarkerNotHidden && (isMarker || isPolyline);
91 
92  if (fProcessing2D) glDisable (GL_DEPTH_TEST);
93  else {
94  if (isMarkerOrPolyline && isMarkerNotHidden)
95  glDisable (GL_DEPTH_TEST);
96  else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);}
97  }
98 
99  if (fThreePassCapable) {
100 
101  // Ensure transparent objects are drawn opaque ones and before
102  // non-hidden markers. The problem of blending/transparency/alpha
103  // is quite a tricky one - see History of opengl-V07-01-01/2/3.
104  if (!(fSecondPassForTransparency || fThirdPassForNonHiddenMarkers)) {
105  // First pass...
106  if (treatAsTransparent) { // Request pass for transparent objects...
107  fSecondPassForTransparencyRequested = true;
108  }
109  if (treatAsNotHidden) { // Request pass for non-hidden markers...
110  fThirdPassForNonHiddenMarkersRequested = true;
111  }
112  // On first pass, transparent objects and non-hidden markers are not drawn...
113  if (treatAsTransparent || treatAsNotHidden) {
114  return false;
115  }
116  }
117 
118  // On second pass, only transparent objects are drawn...
119  if (fSecondPassForTransparency) {
120  if (!treatAsTransparent) {
121  return false;
122  }
123  }
124 
125  // On third pass, only non-hidden markers are drawn...
126  if (fThirdPassForNonHiddenMarkers) {
127  if (!treatAsNotHidden) {
128  return false;
129  }
130  }
131  } // fThreePassCapable
132 
133  // Loads G4Atts for picking...
134  if (fpViewer->GetViewParameters().IsPicking()) {
135  glLoadName(++fPickName);
136  G4AttHolder* holder = new G4AttHolder;
137  LoadAtts(visible, holder);
138  fPickMap[fPickName] = holder;
139  }
140 
141  if (transparency_enabled) {
142  glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
143  } else {
144  glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
145  }
146 
147  return true;
148 }
149 
150 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyline& polyline)
151 {
152  G4bool furtherprocessing = AddPrimitivePreamble(polyline);
153  if (furtherprocessing) {
154  G4OpenGLSceneHandler::AddPrimitive(polyline);
155  }
156 }
157 
158 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
159 {
160  G4bool furtherprocessing = AddPrimitivePreamble(polymarker);
161  if (furtherprocessing) {
162  G4OpenGLSceneHandler::AddPrimitive(polymarker);
163  }
164 }
165 
166 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Text& text)
167 {
168  // Note: colour is still handled in
169  // G4OpenGLSceneHandler::AddPrimitive(const G4Text&).
170  G4bool furtherprocessing = AddPrimitivePreamble(text);
171  if (furtherprocessing) {
172  G4OpenGLSceneHandler::AddPrimitive(text);
173  }
174 }
175 
176 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Circle& circle)
177 {
178  G4bool furtherprocessing = AddPrimitivePreamble(circle);
179  if (furtherprocessing) {
180  G4OpenGLSceneHandler::AddPrimitive(circle);
181  }
182 }
183 
184 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Square& square)
185 {
186  G4bool furtherprocessing = AddPrimitivePreamble(square);
187  if (furtherprocessing) {
188  G4OpenGLSceneHandler::AddPrimitive(square);
189  }
190 }
191 
192 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Scale& scale)
193 {
194  G4bool furtherprocessing = AddPrimitivePreamble(scale);
195  if (furtherprocessing) {
196  G4OpenGLSceneHandler::AddPrimitive(scale);
197  }
198 }
199 
200 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
201 {
202  // Note: colour is still handled in
203  // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&).
204  G4bool furtherprocessing = AddPrimitivePreamble(polyhedron);
205  if (furtherprocessing) {
206  G4OpenGLSceneHandler::AddPrimitive(polyhedron);
207  }
208 }
209 
210 void G4OpenGLImmediateSceneHandler::BeginPrimitives
211 (const G4Transform3D& objectTransformation)
212 {
213  G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
214 
215  G4OpenGLTransform3D oglt (objectTransformation);
216 
217  glPushMatrix();
218 
219  /*************************** Check matrix.
220  const GLdouble* m = oglt.GetGLMatrix ();
221  G4cout << "G4OpenGLTransform3D matrix:";
222  for (int i = 0; i < 16; i++) {
223  if ((i % 4) == 0) G4cout << '\n';
224  G4cout << std::setw (15) << m[i];
225  }
226  G4cout << G4endl;
227  *****************************************/
228 
229  glMultMatrixd (oglt.GetGLMatrix ());
230 }
231 
232 void G4OpenGLImmediateSceneHandler::EndPrimitives ()
233 {
234  glPopMatrix();
235 
236  // See all primitives immediately... At least soon...
237  ScaledFlush();
238 
239  G4OpenGLSceneHandler::EndPrimitives ();
240 }
241 
242 void G4OpenGLImmediateSceneHandler::BeginPrimitives2D
243 (const G4Transform3D& objectTransformation)
244 {
245  G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
246 
247  // Push current 3D world matrices and load identity to define screen
248  // coordinates...
249  glMatrixMode (GL_PROJECTION);
250  glPushMatrix();
251  glLoadIdentity();
252  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
253  if (pViewer) {
254  pViewer->g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
255  }
256  glMatrixMode (GL_MODELVIEW);
257  glPushMatrix();
258  glLoadIdentity();
259  G4OpenGLTransform3D oglt (objectTransformation);
260  glMultMatrixd (oglt.GetGLMatrix ());
261  glDisable(GL_DEPTH_TEST); // But see parent scene handler!! In
262 #ifndef G4OPENGL_VERSION_2
263  glDisable (GL_LIGHTING); // some cases, we need to re-iterate this.
264 #endif
265 }
266 
267 void G4OpenGLImmediateSceneHandler::EndPrimitives2D()
268 {
269  // Pop current 3D world matrices back again...
270  glMatrixMode (GL_PROJECTION);
271  glPopMatrix();
272  glMatrixMode (GL_MODELVIEW);
273  glPopMatrix();
274 
275  // See all primitives immediately... At least soon...
276  ScaledFlush();
277 
278  G4OpenGLSceneHandler::EndPrimitives2D ();
279 }
280 
281 void G4OpenGLImmediateSceneHandler::BeginModeling () {
283 }
284 
285 void G4OpenGLImmediateSceneHandler::EndModeling () {
287 }
288 
289 void G4OpenGLImmediateSceneHandler::ClearTransientStore ()
290 {
291  // Nothing to do except redraw the scene ready for the next event.
292  if (fpViewer) {
293  fpViewer -> SetView ();
294  fpViewer -> ClearView ();
295  fpViewer -> DrawView ();
296  }
297 }
298 
299 G4int G4OpenGLImmediateSceneHandler::fSceneIdCount = 0;
300 
301 #endif