source: trunk/source/visualization/OpenGL/src/G4OpenGLXViewer.cc@ 915

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

print improvments begin, see History file

  • Property svn:mime-type set to text/cpp
File size: 13.9 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.48 2009/02/04 16:48:41 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
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 glXMakeCurrent (dpy, win, cx);
94 G4OpenGLViewer::SetView ();
95}
96
97void G4OpenGLXViewer::ShowView () {
98 glXWaitGL (); //Wait for effects of all previous OpenGL commands to
99 //be propagated before progressing.
100 glFlush ();
101
102 if (fVP.IsPicking()) {
103 G4cout <<
104 "Window activated for picking (left-mouse), exit (middle-mouse)."
105 << G4endl;
106 while (true) {
107 if (XPending(dpy)) {
108 XNextEvent(dpy, &event);
109 if (event.type == ButtonPress && event.xbutton.button == 1) {
110 Pick(event.xbutton.x, event.xbutton.y);
111 }
112 else if (event.type == ButtonPress && event.xbutton.button == 2) break;
113 }
114 }
115 }
116}
117
118void G4OpenGLXViewer::GetXConnection () {
119// get a connection.
120 dpy = XOpenDisplay (0);
121 if (!dpy) {
122 fViewId = -1; // This flags an error.
123 G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't open display." << G4endl;
124 return;
125 }
126
127// make sure OpenGL is supported and installed properly.
128 if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
129 fViewId = -1; // This flags an error.
130 G4cerr << "G4OpenGLViewer::G4OpenGLViewer X Server has no GLX extension."
131 << G4endl;
132 return;
133 }
134
135}
136
137void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
138
139 vi = v;
140// get window's attributes
141 if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
142 fViewId = -1; // This flags an error.
143 G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't return window attributes"
144 << G4endl;
145 return;
146 }
147
148// create a GLX context
149 cx = glXCreateContext (dpy, vi, 0, true);
150 if (!cx) {
151 fViewId = -1; // This flags an error.
152 G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't create context."
153 << G4endl;
154 return;
155 }
156
157// New stab at getting a colormap
158
159 Status status;
160 XStandardColormap *standardCmaps = XAllocStandardColormap ();
161 int i, numCmaps;
162
163 status = XmuLookupStandardColormap (dpy,
164 vi -> screen,
165 vi -> visualid,
166 vi -> depth,
167 XA_RGB_DEFAULT_MAP,
168 False,
169 True);
170
171 if (status == 1) {
172 cmap = 0;
173 status = XGetRGBColormaps (dpy,
174 XRootWindow (dpy, vi -> screen),
175 &standardCmaps,
176 &numCmaps,
177 XA_RGB_DEFAULT_MAP);
178 if (status == 1)
179 for (i = 0; i < numCmaps; i++) {
180 if (standardCmaps[i].visualid == vi -> visualid) {
181 cmap = standardCmaps[i].colormap;
182 XFree (standardCmaps);
183 break;
184 }
185 }
186 if (!cmap) {
187 fViewId = -1; // This flags an error.
188 G4cerr <<
189 "G4OpenGLViewer::G4OpenGLViewer failed to allocate a standard colormap."
190 << G4endl;
191 return;
192 }
193 G4cout << "Got standard cmap" << G4endl;
194 } else {
195 cmap = XCreateColormap (dpy,
196 XRootWindow(dpy, vi -> screen),
197 vi -> visual,
198 AllocNone);
199 G4cout << "Created own cmap" << G4endl;
200 }
201
202 if (!cmap) {
203 fViewId = -1; // This flags an error.
204 G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to allocate a Colormap."
205 << G4endl;
206 return;
207 }
208
209}
210
211void G4OpenGLXViewer::CreateMainWindow () {
212
213// create a window
214 swa.colormap = cmap;
215 swa.border_pixel = 0;
216 swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
217 swa.backing_store = WhenMapped;
218
219 // Window size and position...
220 size_hints = XAllocSizeHints();
221
222 fWinSize_x = fVP.GetWindowSizeHintX();
223 fWinSize_y = fVP.GetWindowSizeHintY();
224 G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
225
226 // FIXME, screen size != window size on MAC, but I don't know have to get the menuBar
227 // size on MAC. L.Garnier 01/2009
228 G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
229
230 size_hints->base_width = fWinSize_x;
231 size_hints->base_height = fWinSize_y;
232 size_hints->x = x_origin;
233 size_hints->y = y_origin;
234 if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
235 size_hints->flags |= PSize | PPosition;
236 } else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
237 size_hints->flags |= PSize;
238 } else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
239 size_hints->flags |= PPosition;
240 }
241 G4cout << "Window name: " << fName << G4endl;
242 strncpy (charViewName, fName, 100);
243 char *window_name = charViewName;
244 char *icon_name = charViewName;
245 //char tmpatom[] = "XA_WM_NORMAL_HINTS";
246 wm_hints = XAllocWMHints();
247 class_hints = XAllocClassHint();
248
249 XStringListToTextProperty (&window_name, 1, &windowName);
250 XStringListToTextProperty (&icon_name, 1, &iconName);
251
252 wm_hints -> initial_state = NormalState;
253 wm_hints -> input = True;
254 wm_hints -> icon_pixmap = icon_pixmap;
255 wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
256
257 class_hints -> res_name = NewString("G4OpenGL");
258 class_hints -> res_class = NewString("G4OpenGL");
259
260 win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
261 y_origin, fWinSize_x, fWinSize_y, 0, vi -> depth,
262 InputOutput, vi -> visual,
263 CWBorderPixel | CWColormap |
264 CWEventMask | CWBackingStore,
265 &swa);
266
267 XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
268 size_hints, wm_hints, class_hints);
269
270// request X to Draw window on screen.
271 XMapWindow (dpy, win);
272
273// Wait for window to appear (wait for an "expose" event).
274 XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
275
276// connect the context to a window
277 Bool success = glXMakeCurrent (dpy, win, cx);
278 if (!success) {
279 fViewId = -1; // This flags an error.
280 G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
281 << G4endl;
282 GLint error = GL_NO_ERROR;
283 while ((error = glGetError()) != GL_NO_ERROR) {
284 G4cout << "GL Error: " << gluErrorString(error) << G4endl;
285 }
286 return;
287 }
288
289}
290
291void G4OpenGLXViewer::CreateFontLists () {
292
293 std::map<G4double,G4String> fonts; // G4VMarker screen size and font name.
294 fonts[10.] = "-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
295 fonts[11.] = "-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
296 fonts[12.] = "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
297 fonts[13.] = "fixed";
298 fonts[14.] = "-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
299 fonts[17.] = "-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
300 fonts[18.] = "-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
301 fonts[20.] = "-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
302 fonts[24.] = "-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
303 fonts[25.] = "-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
304 fonts[34.] = "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
305 std::map<G4double,G4String>::const_iterator i;
306 for (i = fonts.begin(); i != fonts.end(); ++i) {
307 XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
308 if (!font_info) {
309 G4cerr <<
310 "G4OpenGLXViewer: XLoadQueryFont failed for font\n "
311 << i->second
312 << G4endl;
313 continue;
314 }
315 G4int font_base = glGenLists(256);
316 if (!font_base) {
317 G4cerr << "G4OpenGLXViewer: out of display lists for fonts."
318 << G4endl;
319 continue;
320 }
321 G4int first = font_info->min_char_or_byte2;
322 G4int last = font_info->max_char_or_byte2;
323 glXUseXFont(font_info->fid, first, last-first+1,font_base+first);
324 G4OpenGLFontBaseStore::AddFontBase(this,font_base,i->first,i->second);
325 }
326}
327
328G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
329G4VViewer (scene, -1),
330G4OpenGLViewer (scene),
331vi_immediate (0),
332vi_stored (0),
333vi (0),
334cmap (0)
335{
336 GetXConnection ();
337 if (fViewId < 0) return;
338
339 // Try for a visual suitable for OpenGLImmediate..
340 // first try for a single buffered RGB window
341 if (!vi_single_buffer) {
342 vi_single_buffer =
343 glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
344 }
345 if (!vi_double_buffer) {
346 vi_double_buffer =
347 glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
348 }
349
350 if (vi_single_buffer || vi_double_buffer) {
351 if (!vi_double_buffer) {
352 G4cout <<
353 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
354 "\n Working with a single buffer."
355 << G4endl;
356 }
357 } else {
358 if (!vi_single_buffer) {
359 G4cout <<
360 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
361 << G4endl;
362 }
363 if (!vi_double_buffer) {
364 G4cout <<
365 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
366 << G4endl;
367 }
368 }
369
370 if (vi_single_buffer) {
371 vi_immediate = vi_single_buffer;
372 attributeList = snglBuf_RGBA;
373 }
374
375 if (!vi_immediate){
376 // next try for a double buffered RGB, but Draw to top buffer
377 if (vi_double_buffer) {
378 vi_immediate = vi_double_buffer;
379 attributeList = dblBuf_RGBA;
380 }
381 }
382
383 // Now try for a visual suitable for OpenGLStored...
384 // Try for a double buffered RGB window
385 if (vi_double_buffer) {
386 vi_stored = vi_double_buffer;
387 attributeList = dblBuf_RGBA;
388 }
389
390 if (!vi_immediate || !vi_stored) {
391 G4cout <<
392 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
393 << G4endl;
394 fViewId = -1; // This flags an error.
395 }
396
397 // glClearColor (0., 0., 0., 0.);
398 // glClearDepth (1.);
399}
400
401G4OpenGLXViewer::~G4OpenGLXViewer () {
402 if (fViewId >= 0) {
403 //Close a window from here
404 glXMakeCurrent (dpy, None, NULL);
405 glXDestroyContext (dpy, cx);
406 if (win) XDestroyWindow (dpy, win); // ...if already deleted in
407 // sub-class G4OpenGLXmViewer.
408 XFlush (dpy);
409 }
410}
411
412void G4OpenGLXViewer::print() {
413
414 //using namespace std;
415 //cout << "print_col_callback requested with file name: " << print_string << G4endl;
416
417 if (vectored_ps) {
418
419 G4OpenGLViewer::print();
420
421 } else {
422
423 XVisualInfo* pvi;
424 GLXContext pcx = create_GL_print_context(pvi);
425
426 if (!pcx) {
427 G4cout << "Unable to create print context." << G4endl;
428 return;
429 }
430
431 GLXContext tmp_cx;
432 tmp_cx = cx;
433 cx=pcx;
434
435 Pixmap pmap = XCreatePixmap (dpy,
436 XRootWindow (dpy, pvi->screen),
437 fWinSize_x, fWinSize_y,
438 pvi->depth);
439
440 GLXPixmap glxpmap = glXCreateGLXPixmap (dpy,
441 pvi,
442 pmap);
443
444 GLXDrawable tmp_win;
445 tmp_win=win;
446 win=glxpmap;
447
448 glXMakeCurrent (dpy,
449 win,
450 cx);
451
452 ClearView ();
453 SetView ();
454 DrawView ();
455
456 generateEPS (print_string,
457 print_colour,
458 fWinSize_x, fWinSize_y);
459
460 win=tmp_win;
461 cx=tmp_cx;
462
463 glXMakeCurrent (dpy,
464 win,
465 cx);
466
467 }
468
469}
470
471
472GLXContext G4OpenGLXViewer::create_GL_print_context(XVisualInfo*& pvi) {
473
474 pvi = glXChooseVisual (dpy,
475 XDefaultScreen (dpy),
476 snglBuf_RGBA);
477
478 if (!pvi) {
479 pvi = glXChooseVisual (dpy,
480 XDefaultScreen (dpy),
481 dblBuf_RGBA);
482 }
483
484 return glXCreateContext (dpy,
485 pvi,
486 NULL,
487 False);
488}
489
490#endif
Note: See TracBrowser for help on using the repository browser.