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

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

test avec des predefined headers...mais +10% de temps de compilation....

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