source: trunk/geant4/visualization/OpenGL/src/G4OpenGLViewer.cc @ 542

Last change on this file since 542 was 529, checked in by garnier, 17 years ago

r658@mac-90108: laurentgarnier | 2007-06-25 12:02:16 +0200
import de visualisation

  • Property svn:mime-type set to text/cpp
File size: 9.1 KB
Line 
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: G4OpenGLViewer.cc,v 1.29 2006/09/19 16:13:15 allison Exp $
28// GEANT4 tag $Name: geant4-08-02-patch-01 $
29//
30//
31// Andrew Walkden  27th March 1996
32// OpenGL view - opens window, hard copy, etc.
33
34#ifdef G4VIS_BUILD_OPENGL_DRIVER
35
36#include "G4ios.hh"
37#include "G4OpenGLViewer.hh"
38#include "G4OpenGLSceneHandler.hh"
39#include "G4OpenGLTransform3D.hh"
40
41#include "G4Scene.hh"
42#include "G4VisExtent.hh"
43#include "G4LogicalVolume.hh"
44#include "G4VSolid.hh"
45#include "G4Point3D.hh"
46#include "G4Normal3D.hh"
47#include "G4Plane3D.hh"
48
49G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene):
50G4VViewer (scene, -1),
51background (G4Colour(0.,0.,0.)),
52transparency_enabled (true),
53antialiasing_enabled (false),
54haloing_enabled (false),
55fStartTime(-DBL_MAX),
56fEndTime(DBL_MAX),
57fFadeFactor(0.),
58fDisplayHeadTime(false),
59fDisplayHeadTimeX(-0.9),
60fDisplayHeadTimeY(-0.9),
61fDisplayHeadTimeSize(24.),
62fDisplayHeadTimeRed(0.),
63fDisplayHeadTimeGreen(1.),
64fDisplayHeadTimeBlue(1.),
65fDisplayLightFront(false),
66fDisplayLightFrontX(0.),
67fDisplayLightFrontY(0.),
68fDisplayLightFrontZ(0.),
69fDisplayLightFrontT(0.),
70fDisplayLightFrontRed(0.),
71fDisplayLightFrontGreen(1.),
72fDisplayLightFrontBlue(0.)
73{
74  // Make changes to view parameters for OpenGL...
75  fVP.SetAutoRefresh(true);
76  fDefaultVP.SetAutoRefresh(true);
77
78  //  glClearColor (0.0, 0.0, 0.0, 0.0);
79  //  glClearDepth (1.0);
80  //  glDisable (GL_BLEND);
81  //  glDisable (GL_LINE_SMOOTH);
82  //  glDisable (GL_POLYGON_SMOOTH);
83
84}
85
86G4OpenGLViewer::~G4OpenGLViewer () {}
87
88void G4OpenGLViewer::InitializeGLView ()
89{
90  glClearColor (0.0, 0.0, 0.0, 0.0);
91  glClearDepth (1.0);
92  glDisable (GL_BLEND);
93  glDisable (GL_LINE_SMOOTH);
94  glDisable (GL_POLYGON_SMOOTH);
95
96
97void G4OpenGLViewer::ClearView () {
98  glClearColor (background.GetRed(),
99                background.GetGreen(),
100                background.GetBlue(),
101                1.);
102  glClearDepth (1.0);
103  //Below line does not compile with Mesa includes.
104  //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105  glClear (GL_COLOR_BUFFER_BIT);
106  glClear (GL_DEPTH_BUFFER_BIT);
107  glClear (GL_STENCIL_BUFFER_BIT);
108  glFlush ();
109}
110
111void G4OpenGLViewer::SetView () {
112 
113  // Calculates view representation based on extent of object being
114  // viewed and (initial) viewpoint.  (Note: it can change later due
115  // to user interaction via visualization system's GUI.)
116 
117  // Lighting.
118  GLfloat lightPosition [4];
119  lightPosition [0] = fVP.GetActualLightpointDirection().x();
120  lightPosition [1] = fVP.GetActualLightpointDirection().y();
121  lightPosition [2] = fVP.GetActualLightpointDirection().z();
122  lightPosition [3] = 0.;
123  // Light position is "true" light direction, so must come after gluLookAt.
124  GLfloat ambient [] = { 0.2, 0.2, 0.2, 1.};
125  GLfloat diffuse [] = { 0.8, 0.8, 0.8, 1.};
126  glEnable (GL_LIGHT0);
127  glLightfv (GL_LIGHT0, GL_AMBIENT, ambient);
128  glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse);
129 
130  // Get radius of scene, etc.
131  // Note that this procedure properly takes into account zoom, dolly and pan.
132  const G4Point3D targetPoint
133    = fSceneHandler.GetScene()->GetStandardTargetPoint()
134    + fVP.GetCurrentTargetPoint ();
135  G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
136  if(radius<=0.) radius = 1.;
137  const G4double cameraDistance = fVP.GetCameraDistance (radius);
138  const G4Point3D cameraPosition =
139    targetPoint + cameraDistance * fVP.GetViewpointDirection().unit();
140  const GLdouble pnear   = fVP.GetNearDistance (cameraDistance, radius);
141  const GLdouble pfar    = fVP.GetFarDistance  (cameraDistance, pnear, radius);
142  const GLdouble right  = fVP.GetFrontHalfHeight (pnear, radius);
143  const GLdouble left   = -right;
144  const GLdouble bottom = left;
145  const GLdouble top    = right;
146 
147  glMatrixMode (GL_PROJECTION); // set up Frustum.
148  glLoadIdentity();
149
150  const G4Vector3D scale = fVP.GetScaleFactor();
151  glScaled(scale.x(),scale.y(),scale.z());
152 
153  if (fVP.GetFieldHalfAngle() == 0.) {
154    glOrtho (left, right, bottom, top, pnear, pfar);
155  }
156  else {
157    glFrustum (left, right, bottom, top, pnear, pfar);
158  }
159 
160  glMatrixMode (GL_MODELVIEW); // apply further transformations to scene.
161  glLoadIdentity();
162 
163  const G4Normal3D& upVector = fVP.GetUpVector (); 
164  G4Point3D gltarget;
165  if (cameraDistance > 1.e-6 * radius) {
166    gltarget = targetPoint;
167  }
168  else {
169    gltarget = targetPoint - radius * fVP.GetViewpointDirection().unit();
170  }
171
172  const G4Point3D& pCamera = cameraPosition;  // An alias for brevity.
173  gluLookAt (pCamera.x(),  pCamera.y(),  pCamera.z(),       // Viewpoint.
174             gltarget.x(), gltarget.y(), gltarget.z(),      // Target point.
175             upVector.x(), upVector.y(), upVector.z());     // Up vector.
176 
177  // Light position is "true" light direction, so must come after gluLookAt.
178  glLightfv (GL_LIGHT0, GL_POSITION, lightPosition);
179
180  // OpenGL no longer seems to reconstruct clipped edges, so, when the
181  // BooleanProcessor is up to it, abandon this and use generic
182  // clipping in G4OpenGLSceneHandler::CreateSectionPolyhedron.  Also,
183  // force kernel visit on change of clipping plane in
184  // G4OpenGLStoredViewer::CompareForKernelVisit.
185  if (fVP.IsSection () ) {  // pair of back to back clip planes.
186    const G4Plane3D& s = fVP.GetSectionPlane ();
187    double sArray[4];
188    sArray[0] = s.a();
189    sArray[1] = s.b();
190    sArray[2] = s.c();
191    sArray[3] = s.d() + radius * 1.e-05;
192    glClipPlane (GL_CLIP_PLANE0, sArray);
193    glEnable (GL_CLIP_PLANE0);
194    sArray[0] = -s.a();
195    sArray[1] = -s.b();
196    sArray[2] = -s.c();
197    sArray[3] = -s.d() + radius * 1.e-05;
198    glClipPlane (GL_CLIP_PLANE1, sArray);
199    glEnable (GL_CLIP_PLANE1);
200  } else {
201    glDisable (GL_CLIP_PLANE0);
202    glDisable (GL_CLIP_PLANE1);
203  }
204
205  const G4Planes& cutaways = fVP.GetCutawayPlanes();
206  size_t nPlanes = cutaways.size();
207  if (fVP.IsCutaway() &&
208      fVP.GetCutawayMode() == G4ViewParameters::cutawayIntersection &&
209      nPlanes > 0) {
210    double a[4];
211    a[0] = cutaways[0].a();
212    a[1] = cutaways[0].b();
213    a[2] = cutaways[0].c();
214    a[3] = cutaways[0].d();
215    glClipPlane (GL_CLIP_PLANE2, a);
216    glEnable (GL_CLIP_PLANE2);
217    if (nPlanes > 1) {
218      a[0] = cutaways[1].a();
219      a[1] = cutaways[1].b();
220      a[2] = cutaways[1].c();
221      a[3] = cutaways[1].d();
222      glClipPlane (GL_CLIP_PLANE3, a);
223      glEnable (GL_CLIP_PLANE3);
224    }
225    if (nPlanes > 2) {
226      a[0] = cutaways[2].a();
227      a[1] = cutaways[2].b();
228      a[2] = cutaways[2].c();
229      a[3] = cutaways[2].d();
230      glClipPlane (GL_CLIP_PLANE4, a);
231      glEnable (GL_CLIP_PLANE4);
232    }
233  } else {
234    glDisable (GL_CLIP_PLANE2);
235    glDisable (GL_CLIP_PLANE3);
236    glDisable (GL_CLIP_PLANE4);
237  }
238
239  // Background.
240  background = fVP.GetBackgroundColour ();
241
242}
243
244void G4OpenGLViewer::HaloingFirstPass () {
245 
246  //To perform haloing, first Draw all information to the depth buffer
247  //alone, using a chunky line width, and then Draw all info again, to
248  //the colour buffer, setting a thinner line width an the depth testing
249  //function to less than or equal, so if two lines cross, the one
250  //passing behind the other will not pass the depth test, and so not
251  //get rendered either side of the infront line for a short distance.
252
253  //First, disable writing to the colo(u)r buffer...
254  glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
255
256  //Now enable writing to the depth buffer...
257  glDepthMask (GL_TRUE);
258  glDepthFunc (GL_LESS);
259  glClearDepth (1.0);
260
261  //Finally, set the line width to something wide...
262  glLineWidth (3.0);
263
264}
265
266void G4OpenGLViewer::HaloingSecondPass () {
267
268  //And finally, turn the colour buffer back on with a sesible line width...
269  glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
270  glDepthFunc (GL_LEQUAL);
271  glLineWidth (1.0);
272
273}
274
275#endif
Note: See TracBrowser for help on using the repository browser.