Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 99312 2016-09-13 09:47:18Z 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  // Get applicable vis attributes for all primitives.
77  fpVisAttribs = fpViewer->GetApplicableVisAttributes(visible.GetVisAttributes());
78  const G4Colour& c = GetColour ();
79  G4double opacity = c.GetAlpha ();
80 
81  G4bool transparency_enabled = true;
82  G4bool isMarkerNotHidden = true;
83  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
84  if (pViewer) {
85  transparency_enabled = pViewer->transparency_enabled;
86  isMarkerNotHidden = pViewer->fVP.IsMarkerNotHidden();
87  }
88 
89  G4bool isMarkerOrPolyline = isMarker || isPolyline;
90  G4bool treatAsTransparent = transparency_enabled && opacity < 1.;
91  G4bool treatAsNotHidden = isMarkerNotHidden && (isMarker || isPolyline);
92 
93  if (fProcessing2D) glDisable (GL_DEPTH_TEST);
94  else {
95  if (isMarkerOrPolyline && isMarkerNotHidden)
96  glDisable (GL_DEPTH_TEST);
97  else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);}
98  }
99 
100  if (fThreePassCapable) {
101 
102  // Ensure transparent objects are drawn opaque ones and before
103  // non-hidden markers. The problem of blending/transparency/alpha
104  // is quite a tricky one - see History of opengl-V07-01-01/2/3.
105  if (!(fSecondPassForTransparency || fThirdPassForNonHiddenMarkers)) {
106  // First pass...
107  if (treatAsTransparent) { // Request pass for transparent objects...
108  fSecondPassForTransparencyRequested = true;
109  }
110  if (treatAsNotHidden) { // Request pass for non-hidden markers...
111  fThirdPassForNonHiddenMarkersRequested = true;
112  }
113  // On first pass, transparent objects and non-hidden markers are not drawn...
114  if (treatAsTransparent || treatAsNotHidden) {
115  return false;
116  }
117  }
118 
119  // On second pass, only transparent objects are drawn...
120  if (fSecondPassForTransparency) {
121  if (!treatAsTransparent) {
122  return false;
123  }
124  }
125 
126  // On third pass, only non-hidden markers are drawn...
127  if (fThirdPassForNonHiddenMarkers) {
128  if (!treatAsNotHidden) {
129  return false;
130  }
131  }
132  } // fThreePassCapable
133 
134  // Loads G4Atts for picking...
135  if (fpViewer->GetViewParameters().IsPicking()) {
136  glLoadName(++fPickName);
137  G4AttHolder* holder = new G4AttHolder;
138  LoadAtts(visible, holder);
139  fPickMap[fPickName] = holder;
140  }
141 
142  if (transparency_enabled) {
143  glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
144  } else {
145  glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());
146  }
147 
148  return true;
149 }
150 
151 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyline& polyline)
152 {
153  G4bool furtherprocessing = AddPrimitivePreamble(polyline);
154  if (furtherprocessing) {
155  G4OpenGLSceneHandler::AddPrimitive(polyline);
156  }
157 }
158 
159 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
160 {
161  G4bool furtherprocessing = AddPrimitivePreamble(polymarker);
162  if (furtherprocessing) {
163  G4OpenGLSceneHandler::AddPrimitive(polymarker);
164  }
165 }
166 
167 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Text& text)
168 {
169  // Note: colour is still handled in
170  // G4OpenGLSceneHandler::AddPrimitive(const G4Text&).
171  G4bool furtherprocessing = AddPrimitivePreamble(text);
172  if (furtherprocessing) {
173  G4OpenGLSceneHandler::AddPrimitive(text);
174  }
175 }
176 
177 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Circle& circle)
178 {
179  G4bool furtherprocessing = AddPrimitivePreamble(circle);
180  if (furtherprocessing) {
181  G4OpenGLSceneHandler::AddPrimitive(circle);
182  }
183 }
184 
185 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Square& square)
186 {
187  G4bool furtherprocessing = AddPrimitivePreamble(square);
188  if (furtherprocessing) {
189  G4OpenGLSceneHandler::AddPrimitive(square);
190  }
191 }
192 
193 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Scale& scale)
194 {
195  G4bool furtherprocessing = AddPrimitivePreamble(scale);
196  if (furtherprocessing) {
197  G4OpenGLSceneHandler::AddPrimitive(scale);
198  }
199 }
200 
201 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
202 {
203  // Note: colour is still handled in
204  // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&).
205  G4bool furtherprocessing = AddPrimitivePreamble(polyhedron);
206  if (furtherprocessing) {
207  G4OpenGLSceneHandler::AddPrimitive(polyhedron);
208  }
209 }
210 
211 void G4OpenGLImmediateSceneHandler::BeginPrimitives
212 (const G4Transform3D& objectTransformation)
213 {
214  G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
215 
216  G4OpenGLTransform3D oglt (objectTransformation);
217 
218  glPushMatrix();
219 
220  /*************************** Check matrix.
221  const GLdouble* m = oglt.GetGLMatrix ();
222  G4cout << "G4OpenGLTransform3D matrix:";
223  for (int i = 0; i < 16; i++) {
224  if ((i % 4) == 0) G4cout << '\n';
225  G4cout << std::setw (15) << m[i];
226  }
227  G4cout << G4endl;
228  *****************************************/
229 
230  glMultMatrixd (oglt.GetGLMatrix ());
231 }
232 
233 void G4OpenGLImmediateSceneHandler::EndPrimitives ()
234 {
235  glPopMatrix();
236 
237  // See all primitives immediately... At least soon...
238  ScaledFlush();
239 
240  G4OpenGLSceneHandler::EndPrimitives ();
241 }
242 
243 void G4OpenGLImmediateSceneHandler::BeginPrimitives2D
244 (const G4Transform3D& objectTransformation)
245 {
246  G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
247 
248  // Push current 3D world matrices and load identity to define screen
249  // coordinates...
250  glMatrixMode (GL_PROJECTION);
251  glPushMatrix();
252  glLoadIdentity();
253  G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
254  if (pViewer) {
255  pViewer->g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
256  }
257  glMatrixMode (GL_MODELVIEW);
258  glPushMatrix();
259  glLoadIdentity();
260  G4OpenGLTransform3D oglt (objectTransformation);
261  glMultMatrixd (oglt.GetGLMatrix ());
262  glDisable(GL_DEPTH_TEST); // But see parent scene handler!! In
263 #ifndef G4OPENGL_VERSION_2
264  glDisable (GL_LIGHTING); // some cases, we need to re-iterate this.
265 #endif
266 }
267 
268 void G4OpenGLImmediateSceneHandler::EndPrimitives2D()
269 {
270  // Pop current 3D world matrices back again...
271  glMatrixMode (GL_PROJECTION);
272  glPopMatrix();
273  glMatrixMode (GL_MODELVIEW);
274  glPopMatrix();
275 
276  // See all primitives immediately... At least soon...
277  ScaledFlush();
278 
279  G4OpenGLSceneHandler::EndPrimitives2D ();
280 }
281 
282 void G4OpenGLImmediateSceneHandler::BeginModeling () {
284 }
285 
286 void G4OpenGLImmediateSceneHandler::EndModeling () {
288 }
289 
290 void G4OpenGLImmediateSceneHandler::ClearTransientStore ()
291 {
292  // Nothing to do except redraw the scene ready for the next event.
293  if (fpViewer) {
294  fpViewer -> SetView ();
295  fpViewer -> ClearView ();
296  fpViewer -> DrawView ();
297  }
298 }
299 
300 G4int G4OpenGLImmediateSceneHandler::fSceneIdCount = 0;
301 
302 #endif
const XML_Char * name
Definition: expat.h:151
Definition: G4Text.hh:73
G4double GetAlpha() const
Definition: G4Colour.hh:142
virtual void BeginModeling()
G4double GetBlue() const
Definition: G4Colour.hh:141
const G4VisAttributes * GetVisAttributes() const
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 G4double
Definition: G4Types.hh:76