Geant4  10.02.p01
G4OpenGLImmediateSceneHandler.cc
Go to the documentation of this file.
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 // $Id: G4OpenGLImmediateSceneHandler.cc 87695 2014-12-17 09:35:24Z gcosmo $
28 //
29 //
30 // Andrew Walkden 10th February 1997
31 // OpenGL immediate scene - draws immediately to buffer
32 // (saving space on server).
33 
34 #ifdef G4VIS_BUILD_OPENGL_DRIVER
35 
37 
38 #include "G4OpenGLViewer.hh"
39 #include "G4OpenGLTransform3D.hh"
40 #include "G4Polyline.hh"
41 #include "G4Polymarker.hh"
42 #include "G4Text.hh"
43 #include "G4Circle.hh"
44 #include "G4Square.hh"
45 #include "G4Scale.hh"
46 #include "G4Polyhedron.hh"
47 #include "G4AttHolder.hh"
48 
49 #include <typeinfo>
50 
51 G4OpenGLImmediateSceneHandler::G4OpenGLImmediateSceneHandler
52 (G4VGraphicsSystem& system,const G4String& name):
53  G4OpenGLSceneHandler (system, fSceneIdCount++, name)
54 {}
55 
56 G4OpenGLImmediateSceneHandler::~G4OpenGLImmediateSceneHandler ()
57 {}
58 
59 #include <iomanip>
60 
61 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4VMarker& visible)
62 {
63  return AddPrimitivePreambleInternal(visible, true, false);
64 }
65 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Polyline& visible)
66 {
67  return AddPrimitivePreambleInternal(visible, false, true);
68 }
69 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Polyhedron& visible)
70 {
71  return AddPrimitivePreambleInternal(visible, false, false);
72 }
73 
74 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreambleInternal(const G4Visible& visible, bool isMarker, bool isPolyline)
75 {
76  const G4Colour& c = GetColour (visible);
77  G4double opacity = c.GetAlpha ();
78 
79  G4bool transparency_enabled = true;
80  G4bool isMarkerNotHidden = true;
81  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
82  if (pViewer) {
83  transparency_enabled = pViewer->transparency_enabled;
84  isMarkerNotHidden = pViewer->fVP.IsMarkerNotHidden();
85  }
86 
87  G4bool isMarkerOrPolyline = isMarker || isPolyline;
88  G4bool treatAsTransparent = transparency_enabled && opacity < 1.;
89  G4bool treatAsNotHidden = isMarkerNotHidden && (isMarker || isPolyline);
90 
91  if (fProcessing2D) glDisable (GL_DEPTH_TEST);
92  else {
93  if (isMarkerOrPolyline && isMarkerNotHidden)
94  glDisable (GL_DEPTH_TEST);
95  else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);}
96  }
97 
98  if (fThreePassCapable) {
99 
100  // Ensure transparent objects are drawn opaque ones and before
101  // non-hidden markers. The problem of blending/transparency/alpha
102  // is quite a tricky one - see History of opengl-V07-01-01/2/3.
103  if (!(fSecondPassForTransparency || fThirdPassForNonHiddenMarkers)) {
104  // First pass...
105  if (treatAsTransparent) { // Request pass for transparent objects...
106  fSecondPassForTransparencyRequested = true;
107  }
108  if (treatAsNotHidden) { // Request pass for non-hidden markers...
109  fThirdPassForNonHiddenMarkersRequested = true;
110  }
111  // On first pass, transparent objects and non-hidden markers are not drawn...
112  if (treatAsTransparent || treatAsNotHidden) {
113  return false;
114  }
115  }
116 
117  // On second pass, only transparent objects are drawn...
118  if (fSecondPassForTransparency) {
119  if (!treatAsTransparent) {
120  return false;
121  }
122  }
123 
124  // On third pass, only non-hidden markers are drawn...
125  if (fThirdPassForNonHiddenMarkers) {
126  if (!treatAsNotHidden) {
127  return false;
128  }
129  }
130  } // fThreePassCapable
131 
132  // Loads G4Atts for picking...
133  if (fpViewer->GetViewParameters().IsPicking()) {
134  glLoadName(++fPickName);
135  G4AttHolder* holder = new G4AttHolder;
136  LoadAtts(visible, holder);
137  fPickMap[fPickName] = holder;
138  }
139 
140  if (transparency_enabled) {
141  glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
142  } else {
143  glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
144  }
145 
146  return true;
147 }
148 
149 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyline& polyline)
150 {
151  G4bool furtherprocessing = AddPrimitivePreamble(polyline);
152  if (furtherprocessing) {
153  G4OpenGLSceneHandler::AddPrimitive(polyline);
154  }
155 }
156 
157 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
158 {
159  G4bool furtherprocessing = AddPrimitivePreamble(polymarker);
160  if (furtherprocessing) {
161  G4OpenGLSceneHandler::AddPrimitive(polymarker);
162  }
163 }
164 
165 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Text& text)
166 {
167  // Note: colour is still handled in
168  // G4OpenGLSceneHandler::AddPrimitive(const G4Text&).
169  G4bool furtherprocessing = AddPrimitivePreamble(text);
170  if (furtherprocessing) {
171  G4OpenGLSceneHandler::AddPrimitive(text);
172  }
173 }
174 
175 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Circle& circle)
176 {
177  G4bool furtherprocessing = AddPrimitivePreamble(circle);
178  if (furtherprocessing) {
179  G4OpenGLSceneHandler::AddPrimitive(circle);
180  }
181 }
182 
183 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Square& square)
184 {
185  G4bool furtherprocessing = AddPrimitivePreamble(square);
186  if (furtherprocessing) {
187  G4OpenGLSceneHandler::AddPrimitive(square);
188  }
189 }
190 
191 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Scale& scale)
192 {
193  G4bool furtherprocessing = AddPrimitivePreamble(scale);
194  if (furtherprocessing) {
195  G4OpenGLSceneHandler::AddPrimitive(scale);
196  }
197 }
198 
199 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
200 {
201  // Note: colour is still handled in
202  // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&).
203  G4bool furtherprocessing = AddPrimitivePreamble(polyhedron);
204  if (furtherprocessing) {
205  G4OpenGLSceneHandler::AddPrimitive(polyhedron);
206  }
207 }
208 
209 void G4OpenGLImmediateSceneHandler::BeginPrimitives
210 (const G4Transform3D& objectTransformation)
211 {
212  G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
213 
214  G4OpenGLTransform3D oglt (objectTransformation);
215 
216  glPushMatrix();
217 
218  /*************************** Check matrix.
219  const GLdouble* m = oglt.GetGLMatrix ();
220  G4cout << "G4OpenGLTransform3D matrix:";
221  for (int i = 0; i < 16; i++) {
222  if ((i % 4) == 0) G4cout << '\n';
223  G4cout << std::setw (15) << m[i];
224  }
225  G4cout << G4endl;
226  *****************************************/
227 
228  glMultMatrixd (oglt.GetGLMatrix ());
229 }
230 
231 void G4OpenGLImmediateSceneHandler::EndPrimitives ()
232 {
233  glPopMatrix();
234 
235  // See all primitives immediately... At least soon...
236  ScaledFlush();
237 
238  G4OpenGLSceneHandler::EndPrimitives ();
239 }
240 
241 void G4OpenGLImmediateSceneHandler::BeginPrimitives2D
242 (const G4Transform3D& objectTransformation)
243 {
244  G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
245 
246  // Push current 3D world matrices and load identity to define screen
247  // coordinates...
248  glMatrixMode (GL_PROJECTION);
249  glPushMatrix();
250  glLoadIdentity();
251  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
252  if (pViewer) {
253  pViewer->g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
254  }
255  glMatrixMode (GL_MODELVIEW);
256  glPushMatrix();
257  glLoadIdentity();
258  G4OpenGLTransform3D oglt (objectTransformation);
259  glMultMatrixd (oglt.GetGLMatrix ());
260  glDisable(GL_DEPTH_TEST); // But see parent scene handler!! In
261 #ifndef G4OPENGL_VERSION_2
262  glDisable (GL_LIGHTING); // some cases, we need to re-iterate this.
263 #endif
264 }
265 
266 void G4OpenGLImmediateSceneHandler::EndPrimitives2D()
267 {
268  // Pop current 3D world matrices back again...
269  glMatrixMode (GL_PROJECTION);
270  glPopMatrix();
271  glMatrixMode (GL_MODELVIEW);
272  glPopMatrix();
273 
274  // See all primitives immediately...
275  glFlush ();
276 
277  G4OpenGLSceneHandler::EndPrimitives2D ();
278 }
279 
280 void G4OpenGLImmediateSceneHandler::BeginModeling () {
282 }
283 
284 void G4OpenGLImmediateSceneHandler::EndModeling () {
286 }
287 
288 void G4OpenGLImmediateSceneHandler::ClearTransientStore ()
289 {
290  // Nothing to do except redraw the scene ready for the next event.
291  if (fpViewer) {
292  fpViewer -> SetView ();
293  fpViewer -> ClearView ();
294  fpViewer -> DrawView ();
295  }
296 }
297 
298 G4int G4OpenGLImmediateSceneHandler::fSceneIdCount = 0;
299 
300 #endif
Definition: G4Text.hh:73
G4double GetAlpha() const
Definition: G4Colour.hh:142
virtual void BeginModeling()
G4String name
Definition: TRTMaterials.hh:40
G4double GetBlue() const
Definition: G4Colour.hh:141
int G4int
Definition: G4Types.hh:78
G4double GetRed() const
Definition: G4Colour.hh:139
bool G4bool
Definition: G4Types.hh:79
G4double GetGreen() const
Definition: G4Colour.hh:140
virtual void EndModeling()
HepGeom::Transform3D G4Transform3D
double G4double
Definition: G4Types.hh:76