Geant4  10.02
G4OpenGLViewer.hh
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: G4OpenGLViewer.hh 87695 2014-12-17 09:35:24Z gcosmo $
28 //
29 //
30 // Andrew Walkden 27th March 1996
31 // OpenGL viewer - opens window, hard copy, etc.
32 
33 #ifdef G4VIS_BUILD_OPENGL_DRIVER
34 
35 #ifndef G4OPENGLVIEWER_HH
36 #define G4OPENGLVIEWER_HH
37 
38 #include "G4VViewer.hh"
39 #include "G4OpenGL.hh"
40 #ifdef G4OPENGL_VERSION_2
41 #include "G4OpenGLVboDrawer.hh"
42 #endif
43 
44 class G4OpenGLSceneHandler;
45 class G4OpenGL2PSAction;
46 class G4Text;
47 
48 class G4OpenGLViewerPickMap {
49  public :
50  inline void setName(G4String n) {
51  fName = n;
52  }
53 
54  inline void setHitNumber(G4int n) {
55  fHitNumber = n;
56  }
57 
58  inline void setSubHitNumber(G4int n) {
59  fSubHitNumber = n;
60  }
61  inline void setPickName(G4int n) {
62  fPickName= n;
63  }
64 
65  inline void addAttributes(G4String att) {
66  fAttributes.push_back(att);
67  }
68 
69 
70  inline G4String getName() {
71  return fName;
72  }
73  inline G4int getHitNumber() {
74  return fHitNumber;
75  }
76 
77  inline G4int getSubHitNumber() {
78  return fSubHitNumber;
79  }
80 
81  inline G4int getPickName() {
82  return fPickName;
83  }
84 
85  inline std::vector <G4String > getAttributes() {
86  return fAttributes;
87  }
88 
89  G4String print();
90 
91  private :
93  G4int fHitNumber;
94  G4int fSubHitNumber;
95  G4int fPickName;
96  std::vector <G4String > fAttributes;
97 
98 };
99 
100 // Base class for various OpenGLView classes.
101 class G4OpenGLViewer: virtual public G4VViewer {
102 
103  friend class G4OpenGLSceneHandler;
104  friend class G4OpenGLImmediateSceneHandler;
105  friend class G4OpenGLStoredSceneHandler;
106  friend class G4OpenGLFileSceneHandler;
107  friend class G4OpenGLViewerMessenger;
108 
109 public:
110  void ClearView ();
111  void ClearViewWithoutFlush ();
113  bool printEPS();
114  virtual bool exportImage(std::string name="", int width=-1, int height=-1);
115 
116  bool setExportImageFormat(std::string format,bool quiet = false);
117  // change the export image format according to thoses available for the current viewer
118 
119  // Special case for Wt, we want to have acces to the drawer
120 #ifdef G4OPENGL_VERSION_2
121  inline G4OpenGLVboDrawer* getWtDrawer() {return fVboDrawer;}
122 
123  // Associate the Wt drawer to the OpenGLViewer and the OpenGLSceneHandler
124  void setVboDrawer(G4OpenGLVboDrawer* drawer);
125  G4OpenGLVboDrawer* fVboDrawer;
126 
127  inline bool isInitialized() {
128  return fGlViewInitialized;
129  }
130 #endif
131 
132 protected:
133  G4OpenGLViewer (G4OpenGLSceneHandler& scene);
134  virtual ~G4OpenGLViewer ();
135 
136 private:
137  G4OpenGLViewer(const G4OpenGLViewer&);
138  G4OpenGLViewer& operator= (const G4OpenGLViewer&);
139 
140 protected:
141  void SetView ();
142  void ResetView ();
143 
144  virtual void DrawText(const G4Text&);
145  void ChangePointSize(G4double size);
146  void ChangeLineWidth(G4double width);
147  void HaloingFirstPass ();
148  void HaloingSecondPass ();
149  void HLRFirstPass ();
150  void HLRSecondPass ();
151  void HLRThirdPass ();
152  void InitializeGLView ();
153  void ResizeGLView();
154  void ResizeWindow(unsigned int, unsigned int);
155  virtual G4String Pick(GLdouble x, GLdouble y);
156  std::vector < G4OpenGLViewerPickMap* > GetPickDetails(GLdouble x, GLdouble y);
157  virtual void CreateFontLists () {}
158  void rotateScene (G4double dx, G4double dy);
159  void rotateSceneToggle (G4double dx, G4double dy);
161  // print EPS file. Depending of fVectoredPs, it will print Vectored or not
162  void setExportSize(G4int,G4int);
163  // set the new print size.
164  // -1 means 'print size' = 'window size'
165  // Setting size greater than max OpenGL viewport size will set the size to
166  // maximum
167  bool setExportFilename(G4String name,G4bool inc = true);
168  // set export filename.
169  // if inc, then the filename will be increment by one each time
170  // try to guesss the correct format according to the extention
171 
172  std::string getRealPrintFilename();
173  unsigned int getWinWidth() const;
174  unsigned int getWinHeight() const;
175  G4bool sizeHasChanged();
176  // return true if size has change since last redraw
177  GLdouble getSceneNearWidth();
178  GLdouble getSceneFarWidth();
179  GLdouble getSceneDepth();
180  void addExportImageFormat(std::string format);
181  // add a image format to the available export format list
182  G4bool isGl2psWriting();
183  void g4GluPickMatrix(GLdouble x, GLdouble y, GLdouble width, GLdouble height,
184  GLint viewport[4]);
185  // MESA implementation of gluPickMatrix
186 
187  void g4GluLookAt( GLdouble eyex, GLdouble eyey, GLdouble eyez,
188  GLdouble centerx, GLdouble centery, GLdouble
189  centerz,
190  GLdouble upx, GLdouble upy, GLdouble upz );
191  // MESA implementation of gluLookAt
192  void g4GlOrtho (GLdouble left, GLdouble right,
193  GLdouble bottom, GLdouble top,
194  GLdouble near, GLdouble far);
195  // Redefinition on glOrtho to solve precision issues
196  void g4GlFrustum (GLdouble left, GLdouble right,
197  GLdouble bottom, GLdouble top,
198  GLdouble near, GLdouble far);
199  // Redefinition on glFrustum to solve precision issues
200 
201  G4bool fPrintColour;
202  G4bool fVectoredPs;
203 
204  G4OpenGLSceneHandler& fOpenGLSceneHandler;
205  G4Colour background; //the OpenGL clear colour
206  G4bool
207  transparency_enabled, //is alpha blending enabled?
208  antialiasing_enabled, //is antialiasing enabled?
209  haloing_enabled; //is haloing enabled for wireframe?
210  G4double fStartTime, fEndTime; // Time range (e.g., for trajectory steps).
211  G4double fFadeFactor; // 0: no fade; 1: maximum fade with time within range.
212  G4bool fDisplayHeadTime; // Display head time (fEndTime) in 2D text.
213  G4double fDisplayHeadTimeX, fDisplayHeadTimeY; // 2D screen coords.
214  G4double fDisplayHeadTimeSize; // Screen size.
215  G4double fDisplayHeadTimeRed, fDisplayHeadTimeGreen, fDisplayHeadTimeBlue;
216  G4bool fDisplayLightFront;// Display light front at head time originating at
217  G4double fDisplayLightFrontX, fDisplayLightFrontY, fDisplayLightFrontZ,
218  fDisplayLightFrontT;
219  G4double fDisplayLightFrontRed, fDisplayLightFrontGreen, fDisplayLightFrontBlue;
220  G4OpenGL2PSAction* fGL2PSAction;
221 
222  G4double fRot_sens; // Rotation sensibility in degrees
223  G4double fPan_sens; // Translation sensibility
224  unsigned int fWinSize_x;
225  unsigned int fWinSize_y;
226  std::vector < std::string > fExportImageFormatVector;
227  std::string fDefaultExportImageFormat;
228  std::string fExportImageFormat;
229  int fExportFilenameIndex;
230  G4int fPrintSizeX;
231  G4int fPrintSizeY;
232 
233 
234 private :
235  G4float fPointSize;
236  G4String fExportFilename;
237  G4String fDefaultExportFilename;
238  G4bool fSizeHasChanged;
239  int fGl2psDefaultLineWith;
240  int fGl2psDefaultPointSize;
241  bool fGlViewInitialized;
242 
243  // size of the OpenGL frame
244  void rotateSceneThetaPhi(G4double dx, G4double dy);
245  void rotateSceneInViewDirection (G4double dx, G4double dy);
246  bool printGl2PS();
247  G4int getRealExportWidth();
248  G4int getRealExportHeight();
249  GLubyte* grabPixels (int inColor,
250  unsigned int width,
251  unsigned int height);
252  bool printNonVectoredEPS ();
253  // print non vectored EPS files
254 
255  bool printVectoredEPS();
256  // print vectored EPS files
257 
258  bool fIsGettingPickInfos;
259  // Block SetView() during picking
260 
261 #ifdef G4OPENGL_VERSION_2
262 public:
263 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
264  inline Wt::WGLWidget::Program getShaderProgram() {
265  return fShaderProgram;
266  }
267  inline Wt::WGLWidget::UniformLocation getShaderProjectionMatrix() {
268  return fpMatrixUniform;
269  }
270  inline Wt::WGLWidget::UniformLocation getShaderTransformMatrix() {
271  return ftMatrixUniform;
272  }
273 #else
274  inline GLuint getShaderProgram() {
275  return fShaderProgram;
276  }
277  inline GLuint getShaderProjectionMatrix() {
278  return fpMatrixUniform;
279  }
280  inline GLuint getShaderTransformMatrix() {
281  return ftMatrixUniform;
282  }
283  inline GLuint getShaderViewModelMatrix() {
284  return fmvMatrixUniform;
285  }
286 #endif
287 
288 protected :
289 
290  // define the keyword shader to handle it in a better way for OpenGL and WebGL
291 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
292 #define Shader Wt::WGLWidget::Shader
293 #else
294 #define Shader GLuint
295 #endif
296 
297  // define some attributes and variables for OpenGL and WebGL
298 #ifdef G4VIS_BUILD_OPENGLWT_DRIVER
299  Wt::WGLWidget::Program fShaderProgram;
300 
301  // Program and related variables
302  Wt::WGLWidget::AttribLocation fVertexPositionAttribute;
303  Wt::WGLWidget::AttribLocation fVertexNormalAttribute;
304  Wt::WGLWidget::UniformLocation fpMatrixUniform;
305  Wt::WGLWidget::UniformLocation fcMatrixUniform;
306  Wt::WGLWidget::UniformLocation fmvMatrixUniform;
307  Wt::WGLWidget::UniformLocation fnMatrixUniform;
308  Wt::WGLWidget::UniformLocation ftMatrixUniform;
309 #else
310  GLuint fShaderProgram;
311 
312  // Program and related variables
313  GLuint fVertexPositionAttribute;
314  GLuint fVertexNormalAttribute;
315  GLuint fpMatrixUniform;
316  GLuint fcMatrixUniform;
317  GLuint fmvMatrixUniform;
318  GLuint fnMatrixUniform;
319  GLuint ftMatrixUniform;
320 #endif
321 
322 #endif
323 };
324 
325 #endif
326 
327 #endif
Definition: G4Text.hh:73
virtual void SetView()=0
G4String fName
Definition: G4AttUtils.hh:55
virtual void ClearView()=0
G4String name
Definition: TRTMaterials.hh:40
float G4float
Definition: G4Types.hh:77
#define width
int G4int
Definition: G4Types.hh:78
virtual void ResetView()
bool G4bool
Definition: G4Types.hh:79
const G4int n
void print(G4double elem)
const G4double x[NPOINTSGL]
double G4double
Definition: G4Types.hh:76
G4bool isInitialized()
Check if the generator is initialized.
std::string getName(const ParticleType t)
Get the native INCL name of the particle.