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