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#include <GL/glu.h>
41
42#include "G4OpenGLFontBaseStore.hh"
43
44#include <sstream>
45
46#include "G4VisExtent.hh"
47#include "G4LogicalVolume.hh"
48#include "G4VSolid.hh"
49#include "G4Point3D.hh"
50#include "G4Normal3D.hh"
51#include "G4StateManager.hh"
52
53#include <X11/Xatom.h>
54#include <X11/Xutil.h>
55#include <X11/Xmu/StdCmap.h>
56
57#include <assert.h>
58
59int G4OpenGLXViewer::snglBuf_RGBA[12] =
60{ GLX_RGBA,
61  GLX_RED_SIZE, 1,
62  GLX_GREEN_SIZE, 1,
63  GLX_BLUE_SIZE, 1,
64  GLX_DEPTH_SIZE, 1,
65  GLX_STENCIL_SIZE, 1,
66  None };
67
68int G4OpenGLXViewer::dblBuf_RGBA[13] =
69{ GLX_RGBA,
70  GLX_RED_SIZE, 1,
71  GLX_GREEN_SIZE, 1,
72  GLX_BLUE_SIZE, 1,
73  GLX_DOUBLEBUFFER,
74  GLX_DEPTH_SIZE, 1,
75  GLX_STENCIL_SIZE, 1,
76  None };
77
78#define NewString(str) \
79 ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : (char*)NULL)
80
81#define USE_DEFAULT_COLORMAP 1
82#define USE_STANDARD_COLORMAP 0
83
84XVisualInfo*  G4OpenGLXViewer::vi_single_buffer = 0;
85XVisualInfo*  G4OpenGLXViewer::vi_double_buffer = 0;
86
87extern "C" {
88  static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent* e, char* arg) {
89    return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
90  }
91}
92
93void G4OpenGLXViewer::SetView () {
94  Bool success = glXMakeCurrent (dpy, win, cx);
95  if (!success) {
96    fViewId = -1;  // This flags an error.
97    G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
98           << G4endl;
99    GLint error = GL_NO_ERROR;
100    while ((error = glGetError()) != GL_NO_ERROR) {
101      G4cout << "GL Error: " << gluErrorString(error) << G4endl;
102    }
103    return;
104  }
105  G4OpenGLViewer::SetView (); 
106}
107
108void G4OpenGLXViewer::ShowView () {
109  glXWaitGL (); //Wait for effects of all previous OpenGL commands to
110                //be propagated before progressing.
111  glFlush ();
112
113  if (fVP.IsPicking()) {
114    G4cout <<
115      "Window activated for picking (left-mouse), exit (middle-mouse)."
116           << G4endl;
117    while (true) {
118      if (XPending(dpy)) {
119        XNextEvent(dpy, &event);
120        if (event.type == ButtonPress && event.xbutton.button == 1) {
121          Pick(event.xbutton.x, event.xbutton.y);
122        }
123        else if (event.type == ButtonPress && event.xbutton.button == 2) break;
124      }
125    }
126  }
127}
128
129void G4OpenGLXViewer::GetXConnection () {
130// get a connection.
131  dpy = XOpenDisplay (0);
132  if (!dpy) {
133    fViewId = -1;  // This flags an error.
134    G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't open display." << G4endl;
135    return;
136  }
137
138// make sure OpenGL is supported and installed properly.
139  if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
140    fViewId = -1;  // This flags an error.
141    G4cerr << "G4OpenGLViewer::G4OpenGLViewer X Server has no GLX extension."
142         << G4endl;
143    return;
144  }
145
146}
147
148void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
149
150  vi = v;
151// get window's attributes
152  if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
153    fViewId = -1;  // This flags an error.
154    G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't return window attributes"
155         << G4endl;
156    return;
157  }
158 
159// create a GLX context
160  cx = glXCreateContext (dpy, vi, 0, true);
161  if (!cx) {
162    fViewId = -1;  // This flags an error.
163    G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't create context."
164         << G4endl;
165    return;
166  }
167
168// New stab at getting a colormap
169
170  Status status;
171  XStandardColormap *standardCmaps = XAllocStandardColormap ();
172  int i, numCmaps;
173
174  status = XmuLookupStandardColormap (dpy,
175                                      vi -> screen,
176                                      vi -> visualid,
177                                      vi -> depth,
178                                      XA_RGB_DEFAULT_MAP,
179                                      False,
180                                      True);
181 
182  if (status == 1) {
183    cmap = 0;
184    status = XGetRGBColormaps (dpy,
185                               XRootWindow (dpy, vi -> screen),
186                               &standardCmaps,
187                               &numCmaps,
188                               XA_RGB_DEFAULT_MAP);
189    if (status == 1)
190      for (i = 0; i < numCmaps; i++) {
191        if (standardCmaps[i].visualid == vi -> visualid) {
192          cmap = standardCmaps[i].colormap;
193          XFree (standardCmaps);
194          break;
195        }
196      }
197    if (!cmap) {
198      fViewId = -1;  // This flags an error.
199      G4cerr <<
200   "G4OpenGLViewer::G4OpenGLViewer failed to allocate a standard colormap."
201             << G4endl;
202      return;
203    }
204    G4cout << "Got standard cmap" << G4endl;
205  } else {
206    cmap = XCreateColormap (dpy,
207                            XRootWindow(dpy, vi -> screen),
208                            vi -> visual,
209                            AllocNone);
210    G4cout << "Created own cmap" << G4endl;
211  }
212
213  if (!cmap) {
214    fViewId = -1;  // This flags an error.
215    G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to allocate a Colormap."
216         << G4endl;
217    return;
218  }
219
220}
221 
222void G4OpenGLXViewer::CreateMainWindow () {
223 
224// create a window
225  swa.colormap = cmap;
226  swa.border_pixel = 0;
227  swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
228  swa.backing_store = WhenMapped;
229
230  // Window size and position...
231  size_hints = XAllocSizeHints();
232   
233  ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
234
235  G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
236
237  // FIXME,  screen size != window size on MAC, but I don't know have to get the menuBar
238  // size on MAC. L.Garnier 01/2009
239  G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
240
241  size_hints->base_width = getWinWidth();
242  size_hints->base_height = getWinHeight();
243  size_hints->x = x_origin;
244  size_hints->y = y_origin;
245  if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
246    size_hints->flags |= PSize | PPosition;
247  } else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
248    size_hints->flags |= PSize;
249  } else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
250    size_hints->flags |= PPosition;
251  }
252  G4cout << "Window name: " << fName << G4endl;
253  strncpy (charViewName, fName, 100);
254  char *window_name = charViewName;
255  char *icon_name = charViewName;
256  //char tmpatom[] = "XA_WM_NORMAL_HINTS";
257  wm_hints = XAllocWMHints();
258  class_hints = XAllocClassHint();
259
260  XStringListToTextProperty (&window_name, 1, &windowName);
261  XStringListToTextProperty (&icon_name, 1, &iconName);
262
263  wm_hints -> initial_state = NormalState;
264  wm_hints -> input = True;
265  wm_hints -> icon_pixmap = icon_pixmap;
266  wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
267
268  class_hints -> res_name  = NewString("G4OpenGL");
269  class_hints -> res_class = NewString("G4OpenGL");
270
271   win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
272                        y_origin, getWinWidth(), getWinHeight(), 0, vi -> depth,
273                        InputOutput, vi -> visual, 
274                        CWBorderPixel | CWColormap |
275                        CWEventMask | CWBackingStore,
276                        &swa);
277 
278   XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
279                     size_hints, wm_hints, class_hints);
280 
281// request X to Draw window on screen.
282  XMapWindow (dpy, win);
283
284// Wait for window to appear (wait for an "expose" event).
285  XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
286
287// connect the context to a window
288  Bool success = glXMakeCurrent (dpy, win, cx);
289  if (!success) {
290    fViewId = -1;  // This flags an error.
291    G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
292         << G4endl;
293    GLint error = GL_NO_ERROR;
294    while ((error = glGetError()) != GL_NO_ERROR) {
295      G4cout << "GL Error: " << gluErrorString(error) << G4endl;
296    }
297    return;
298  }
299
300}
301
302void G4OpenGLXViewer::CreateFontLists () {
303
304  std::map<G4double,G4String> fonts;  // G4VMarker screen size and font name.
305  fonts[10.] = "-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
306  fonts[11.] = "-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
307  fonts[12.] = "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
308  fonts[13.] = "fixed";
309  fonts[14.] = "-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
310  fonts[17.] = "-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
311  fonts[18.] = "-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
312  fonts[20.] = "-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
313  fonts[24.] = "-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
314  fonts[25.] = "-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
315  fonts[34.] = "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
316  std::map<G4double,G4String>::const_iterator i;
317  for (i = fonts.begin(); i != fonts.end(); ++i) {
318    XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
319    if (!font_info) {
320      G4cerr <<
321        "G4OpenGLXViewer: XLoadQueryFont failed for font\n  "
322             << i->second
323             << G4endl;
324      continue;
325    }
326    G4int font_base = glGenLists(256);
327    if (!font_base) {
328      G4cerr << "G4OpenGLXViewer: out of display lists for fonts."
329             << G4endl;
330      continue;
331    }
332    G4int first = font_info->min_char_or_byte2;
333    G4int last  = font_info->max_char_or_byte2;
334    glXUseXFont(font_info->fid, first, last-first+1,font_base+first);
335    G4OpenGLFontBaseStore::AddFontBase(this,font_base,i->first,i->second);
336  }
337}
338
339void G4OpenGLXViewer::drawText(const char * textString,int x,int y,int z, int size){
340 
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.