source: trunk/source/visualization/OpenGL/src/#G4OpenGLXViewer.cc# @ 1350

Last change on this file since 1350 was 1350, checked in by garnier, 13 years ago

update to last version 4.9.4

File size: 14.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: G4OpenGLXViewer.cc,v 1.55 2009/05/13 10:28:00 lgarnier Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// Andrew Walkden  7th February 1997
32// G4OpenGLXViewer : Class to provide XWindows specific
33//                 functionality for OpenGL in GEANT4
34
35#ifdef G4VIS_BUILD_OPENGLX_DRIVER
36
37#include "G4OpenGLXViewer.hh"
38#include "G4VViewer.hh"
39#include "G4OpenGLSceneHandler.hh"
40
41#include "G4OpenGLFontBaseStore.hh"
42
43#include <sstream>
44
45#include "G4VisExtent.hh"
46#include "G4LogicalVolume.hh"
47#include "G4VSolid.hh"
48#include "G4Point3D.hh"
49#include "G4Normal3D.hh"
50#include "G4StateManager.hh"
51
52#include <X11/Xatom.h>
53#include <X11/Xutil.h>
54#include <X11/Xmu/StdCmap.h>
55
56#include <assert.h>
57
58int G4OpenGLXViewer::snglBuf_RGBA[12] =
59{ GLX_RGBA,
60  GLX_RED_SIZE, 1,
61  GLX_GREEN_SIZE, 1,
62  GLX_BLUE_SIZE, 1,
63  GLX_DEPTH_SIZE, 1,
64  GLX_STENCIL_SIZE, 1,
65  None };
66
67int G4OpenGLXViewer::dblBuf_RGBA[13] =
68{ GLX_RGBA,
69  GLX_RED_SIZE, 1,
70  GLX_GREEN_SIZE, 1,
71  GLX_BLUE_SIZE, 1,
72  GLX_DOUBLEBUFFER,
73  GLX_DEPTH_SIZE, 1,
74  GLX_STENCIL_SIZE, 1,
75  None };
76
77#define NewString(str) \
78 ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : (char*)NULL)
79
80#define USE_DEFAULT_COLORMAP 1
81#define USE_STANDARD_COLORMAP 0
82
83XVisualInfo*  G4OpenGLXViewer::vi_single_buffer = 0;
84XVisualInfo*  G4OpenGLXViewer::vi_double_buffer = 0;
85
86extern "C" {
87  static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent* e, char* arg) {
88    return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
89  }
90}
91
92void G4OpenGLXViewer::SetView () {
93  Bool success = glXMakeCurrent (dpy, win, cx);
94  if (!success) {
95    fViewId = -1;  // This flags an error.
96    G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
97           << G4endl;
98    GLint error = GL_NO_ERROR;
99    while ((error = glGetError()) != GL_NO_ERROR) {
100      G4cout << "GL Error: " << gluErrorString(error) << G4endl;
101    }
102    return;
103  }
104  G4OpenGLViewer::SetView (); 
105}
106
107void G4OpenGLXViewer::ShowView () {
108  glXWaitGL (); //Wait for effects of all previous OpenGL commands to
109                //be propagated before progressing.
110  glFlush ();
111
112  if (fVP.IsPicking()) {
113    G4cout <<
114      "Window activated for picking (left-mouse), exit (middle-mouse)."
115           << G4endl;
116    while (true) {
117      if (XPending(dpy)) {
118        XNextEvent(dpy, &event);
119        if (event.type == ButtonPress && event.xbutton.button == 1) {
120          Pick(event.xbutton.x, event.xbutton.y);
121        }
122        else if (event.type == ButtonPress && event.xbutton.button == 2) break;
123      }
124    }
125  }
126}
127
128void G4OpenGLXViewer::GetXConnection () {
129// get a connection.
130  dpy = XOpenDisplay (0);
131  if (!dpy) {
132    fViewId = -1;  // This flags an error.
133    G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't open display." << G4endl;
134    return;
135  }
136
137// make sure OpenGL is supported and installed properly.
138  if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
139    fViewId = -1;  // This flags an error.
140    G4cerr << "G4OpenGLViewer::G4OpenGLViewer X Server has no GLX extension."
141         << G4endl;
142    return;
143  }
144
145}
146
147void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
148
149  vi = v;
150// get window's attributes
151  if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
152    fViewId = -1;  // This flags an error.
153    G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't return window attributes"
154         << G4endl;
155    return;
156  }
157 
158// create a GLX context
159  cx = glXCreateContext (dpy, vi, 0, true);
160  if (!cx) {
161    fViewId = -1;  // This flags an error.
162    G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't create context."
163         << G4endl;
164    return;
165  }
166
167// New stab at getting a colormap
168
169  Status status;
170  XStandardColormap *standardCmaps = XAllocStandardColormap ();
171  int i, numCmaps;
172
173  status = XmuLookupStandardColormap (dpy,
174                                      vi -> screen,
175                                      vi -> visualid,
176                                      vi -> depth,
177                                      XA_RGB_DEFAULT_MAP,
178                                      False,
179                                      True);
180 
181  if (status == 1) {
182    cmap = 0;
183    status = XGetRGBColormaps (dpy,
184                               XRootWindow (dpy, vi -> screen),
185                               &standardCmaps,
186                               &numCmaps,
187                               XA_RGB_DEFAULT_MAP);
188    if (status == 1)
189      for (i = 0; i < numCmaps; i++) {
190        if (standardCmaps[i].visualid == vi -> visualid) {
191          cmap = standardCmaps[i].colormap;
192          XFree (standardCmaps);
193          break;
194        }
195      }
196    if (!cmap) {
197      fViewId = -1;  // This flags an error.
198      G4cerr <<
199   "G4OpenGLViewer::G4OpenGLViewer failed to allocate a standard colormap."
200             << G4endl;
201      return;
202    }
203    G4cout << "Got standard cmap" << G4endl;
204  } else {
205    cmap = XCreateColormap (dpy,
206                            XRootWindow(dpy, vi -> screen),
207                            vi -> visual,
208                            AllocNone);
209    G4cout << "Created own cmap" << G4endl;
210  }
211
212  if (!cmap) {
213    fViewId = -1;  // This flags an error.
214    G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to allocate a Colormap."
215         << G4endl;
216    return;
217  }
218
219}
220 
221void G4OpenGLXViewer::CreateMainWindow () {
222 
223// create a window
224  swa.colormap = cmap;
225  swa.border_pixel = 0;
226  swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
227  swa.backing_store = WhenMapped;
228
229  // Window size and position...
230  size_hints = XAllocSizeHints();
231   
232  ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
233
234  G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
235
236  // FIXME,  screen size != window size on MAC, but I don't know have to get the menuBar
237  // size on MAC. L.Garnier 01/2009
238  G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
239
240  size_hints->base_width = getWinWidth();
241  size_hints->base_height = getWinHeight();
242  size_hints->x = x_origin;
243  size_hints->y = y_origin;
244  if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
245    size_hints->flags |= PSize | PPosition;
246  } else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
247    size_hints->flags |= PSize;
248  } else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
249    size_hints->flags |= PPosition;
250  }
251  G4cout << "Window name: " << fName << G4endl;
252  strncpy (charViewName, fName, 100);
253  char *window_name = charViewName;
254  char *icon_name = charViewName;
255  //char tmpatom[] = "XA_WM_NORMAL_HINTS";
256  wm_hints = XAllocWMHints();
257  class_hints = XAllocClassHint();
258
259  XStringListToTextProperty (&window_name, 1, &windowName);
260  XStringListToTextProperty (&icon_name, 1, &iconName);
261
262  wm_hints -> initial_state = NormalState;
263  wm_hints -> input = True;
264  wm_hints -> icon_pixmap = icon_pixmap;
265  wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
266
267  class_hints -> res_name  = NewString("G4OpenGL");
268  class_hints -> res_class = NewString("G4OpenGL");
269
270   win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
271                        y_origin, getWinWidth(), getWinHeight(), 0, vi -> depth,
272                        InputOutput, vi -> visual, 
273                        CWBorderPixel | CWColormap |
274                        CWEventMask | CWBackingStore,
275                        &swa);
276 
277   XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
278                     size_hints, wm_hints, class_hints);
279 
280// request X to Draw window on screen.
281  XMapWindow (dpy, win);
282
283// Wait for window to appear (wait for an "expose" event).
284  XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
285
286// connect the context to a window
287  Bool success = glXMakeCurrent (dpy, win, cx);
288  if (!success) {
289    fViewId = -1;  // This flags an error.
290    G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
291         << G4endl;
292    GLint error = GL_NO_ERROR;
293    while ((error = glGetError()) != GL_NO_ERROR) {
294      G4cout << "GL Error: " << gluErrorString(error) << G4endl;
295    }
296    return;
297  }
298
299}
300
301void G4OpenGLXViewer::CreateFontLists () {
302
303  std::map<G4double,G4String> fonts;  // G4VMarker screen size and font name.
304  fonts[10.] = "-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
305  fonts[11.] = "-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
306  fonts[12.] = "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
307  fonts[13.] = "fixed";
308  fonts[14.] = "-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
309  fonts[17.] = "-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
310  fonts[18.] = "-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
311  fonts[20.] = "-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
312  fonts[24.] = "-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
313  fonts[25.] = "-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
314  fonts[34.] = "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
315  std::map<G4double,G4String>::const_iterator i;
316  for (i = fonts.begin(); i != fonts.end(); ++i) {
317    XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
318    if (!font_info) {
319      G4cerr <<
320        "G4OpenGLXViewer: XLoadQueryFont failed for font\n  "
321             << i->second
322             << G4endl;
323      continue;
324    }
325    G4int font_base = glGenLists(256);
326    if (!font_base) {
327      G4cerr << "G4OpenGLXViewer: out of display lists for fonts."
328             << G4endl;
329      continue;
330    }
331    G4int first = font_info->min_char_or_byte2;
332    G4int last  = font_info->max_char_or_byte2;
333    glXUseXFont(font_info->fid, first, last-first+1,font_base+first);
334    G4OpenGLFontBaseStore::AddFontBase(this,font_base,i->first,i->second);
335  }
336}
337
338void G4OpenGLXViewer::drawText(const char * textString,int x,int y,int z, int size){
339 
340  CreateFontLists ();
341  // gl2ps or GL window ?
342  if (! drawGl2psText(textString,size)) {
343
344    G4int font_base = G4OpenGLFontBaseStore::GetFontBase(this,size);
345    if (font_base < 0) {
346      static G4int callCount = 0;
347      ++callCount;
348      if (callCount <= 10 || callCount%100 == 0) {
349        G4cout <<
350          "G4OpenGLSceneHandler::AddPrimitive (const G4Text&) call count "
351               << callCount <<
352          "\n  No fonts available."
353          "\n  Called with text \""
354               << textString
355               << "\"\n  at "
356               << "x:"<< x
357               << "y:"<< y
358               << "z:"<< z
359               << ", size " << size
360          //               << ", offsets " << text.GetXOffset () << ", " << text.GetYOffset ()
361               << G4endl;
362      }
363      return;
364    }
365    // No action on offset or layout at present.
366    glPushAttrib(GL_LIST_BIT);
367    glListBase(font_base);
368    glCallLists(strlen(textString), GL_UNSIGNED_BYTE, (GLubyte *)textString);
369    glPopAttrib();
370  }
371}
372
373
374G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
375G4VViewer (scene, -1),
376G4OpenGLViewer (scene),
377vi_immediate (0),
378vi_stored (0),
379vi (0),
380cmap (0)
381{
382  GetXConnection ();
383  if (fViewId < 0) return;
384 
385  // Try for a visual suitable for OpenGLImmediate..
386  // first try for a single buffered RGB window
387  if (!vi_single_buffer) {
388    vi_single_buffer =
389      glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
390  }
391  if (!vi_double_buffer) {
392    vi_double_buffer =
393      glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
394  }
395
396  if (vi_single_buffer || vi_double_buffer) {
397    if (!vi_double_buffer) {
398      G4cout <<
399        "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
400        "\n  Working with a single buffer."
401             << G4endl;
402    }
403  } else {
404    if (!vi_single_buffer) {
405      G4cout <<
406        "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
407             << G4endl;
408    }
409    if (!vi_double_buffer) {
410      G4cout <<
411        "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
412             << G4endl;
413    }
414  }
415
416  if (vi_single_buffer) {
417    vi_immediate = vi_single_buffer;
418    attributeList = snglBuf_RGBA;
419  }
420 
421  if (!vi_immediate){
422    // next try for a double buffered RGB, but Draw to top buffer
423    if (vi_double_buffer) {
424      vi_immediate = vi_double_buffer;
425      attributeList = dblBuf_RGBA;
426    }
427  }
428
429  // Now try for a visual suitable for OpenGLStored...
430  // Try for a double buffered RGB window
431  if (vi_double_buffer) {
432    vi_stored = vi_double_buffer;
433    attributeList = dblBuf_RGBA;
434  }
435
436  if (!vi_immediate || !vi_stored) {
437    G4cout <<
438    "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
439           << G4endl;
440    fViewId = -1;  // This flags an error.
441  }
442
443  //  glClearColor (0., 0., 0., 0.);
444  //  glClearDepth (1.);
445}
446
447G4OpenGLXViewer::~G4OpenGLXViewer () {
448  if (fViewId >= 0) {
449    //Close a window from here
450    glXMakeCurrent (dpy, None, NULL);
451    glXDestroyContext (dpy, cx);
452    if (win) XDestroyWindow (dpy, win); // ...if already deleted in
453    // sub-class G4OpenGLXmViewer.
454    XFlush (dpy);
455  }
456}
457       
458
459#endif
Note: See TracBrowser for help on using the repository browser.