Geant4_10
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 66373 2012-12-18 09:41:34Z 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
53  G4OpenGLSceneHandler (system, fSceneIdCount++, name)
54 {}
55 
56 G4OpenGLImmediateSceneHandler::~G4OpenGLImmediateSceneHandler ()
57 {}
58 
59 #include <iomanip>
60 
61 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Visible& visible)
62 {
63  const G4Colour& c = GetColour (visible);
64  G4double opacity = c.GetAlpha ();
65 
66  G4bool transparency_enabled = true;
67  G4bool isMarkerNotHidden = true;
68  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
69  if (pViewer) {
70  transparency_enabled = pViewer->transparency_enabled;
71  isMarkerNotHidden = pViewer->fVP.IsMarkerNotHidden();
72  }
73 
74  G4bool isMarker = false;
75  try {
76  (void) dynamic_cast<const G4VMarker&>(visible);
77  isMarker = true;
78  }
79  catch (std::bad_cast) {}
80 
81  G4bool isPolyline = false;
82  try {
83  (void) dynamic_cast<const G4Polyline&>(visible);
84  isPolyline = true;
85  }
86  catch (std::bad_cast) {}
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  glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
253  glMatrixMode (GL_MODELVIEW);
254  glPushMatrix();
255  glLoadIdentity();
256  G4OpenGLTransform3D oglt (objectTransformation);
257  glMultMatrixd (oglt.GetGLMatrix ());
258  glDisable(GL_DEPTH_TEST); // But see parent scene handler!! In
259  glDisable (GL_LIGHTING); // some cases, we need to re-iterate this.
260 }
261 
262 void G4OpenGLImmediateSceneHandler::EndPrimitives2D()
263 {
264  // Pop current 3D world matrices back again...
265  glMatrixMode (GL_PROJECTION);
266  glPopMatrix();
267  glMatrixMode (GL_MODELVIEW);
268  glPopMatrix();
269 
270  // See all primitives immediately...
271  glFlush ();
272 
273  G4OpenGLSceneHandler::EndPrimitives2D ();
274 }
275 
276 void G4OpenGLImmediateSceneHandler::BeginModeling () {
278 }
279 
280 void G4OpenGLImmediateSceneHandler::EndModeling () {
282 }
283 
284 void G4OpenGLImmediateSceneHandler::ClearTransientStore ()
285 {
286  // Nothing to do except redraw the scene ready for the next event.
287  if (fpViewer) {
288  fpViewer -> SetView ();
289  fpViewer -> ClearView ();
290  fpViewer -> DrawView ();
291  }
292 }
293 
294 G4int G4OpenGLImmediateSceneHandler::fSceneIdCount = 0;
295 
296 #endif
Definition: G4Text.hh:73
G4double GetAlpha() const
Definition: G4Colour.hh:142
virtual void BeginModeling()
typedef void(XMLCALL *XML_ElementDeclHandler)(void *userData
const XML_Char * name
Definition: expat.h:151
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()
Double_t scale
Definition: plot.C:11
system("rm -rf dna.root")
double G4double
Definition: G4Types.hh:76
tuple c
Definition: test.py:13