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: geant4-09-04-beta-01 $ |
---|
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 | |
---|
59 | int 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 | |
---|
68 | int 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 | |
---|
84 | XVisualInfo* G4OpenGLXViewer::vi_single_buffer = 0; |
---|
85 | XVisualInfo* G4OpenGLXViewer::vi_double_buffer = 0; |
---|
86 | |
---|
87 | extern "C" { |
---|
88 | static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent* e, char* arg) { |
---|
89 | return (e->type == MapNotify) && (e->xmap.window == (Window) arg); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | void 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 | |
---|
108 | void 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 | |
---|
129 | void 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 | |
---|
148 | void 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 | |
---|
222 | void 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 | |
---|
302 | void 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 | |
---|
339 | G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene): |
---|
340 | G4VViewer (scene, -1), |
---|
341 | G4OpenGLViewer (scene), |
---|
342 | vi_immediate (0), |
---|
343 | vi_stored (0), |
---|
344 | vi (0), |
---|
345 | cmap (0) |
---|
346 | { |
---|
347 | GetXConnection (); |
---|
348 | if (fViewId < 0) return; |
---|
349 | |
---|
350 | // Try for a visual suitable for OpenGLImmediate.. |
---|
351 | // first try for a single buffered RGB window |
---|
352 | if (!vi_single_buffer) { |
---|
353 | vi_single_buffer = |
---|
354 | glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA); |
---|
355 | } |
---|
356 | if (!vi_double_buffer) { |
---|
357 | vi_double_buffer = |
---|
358 | glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA); |
---|
359 | } |
---|
360 | |
---|
361 | if (vi_single_buffer || vi_double_buffer) { |
---|
362 | if (!vi_double_buffer) { |
---|
363 | G4cout << |
---|
364 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual." |
---|
365 | "\n Working with a single buffer." |
---|
366 | << G4endl; |
---|
367 | } |
---|
368 | } else { |
---|
369 | if (!vi_single_buffer) { |
---|
370 | G4cout << |
---|
371 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual." |
---|
372 | << G4endl; |
---|
373 | } |
---|
374 | if (!vi_double_buffer) { |
---|
375 | G4cout << |
---|
376 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual." |
---|
377 | << G4endl; |
---|
378 | } |
---|
379 | } |
---|
380 | |
---|
381 | if (vi_single_buffer) { |
---|
382 | vi_immediate = vi_single_buffer; |
---|
383 | attributeList = snglBuf_RGBA; |
---|
384 | } |
---|
385 | |
---|
386 | if (!vi_immediate){ |
---|
387 | // next try for a double buffered RGB, but Draw to top buffer |
---|
388 | if (vi_double_buffer) { |
---|
389 | vi_immediate = vi_double_buffer; |
---|
390 | attributeList = dblBuf_RGBA; |
---|
391 | } |
---|
392 | } |
---|
393 | |
---|
394 | // Now try for a visual suitable for OpenGLStored... |
---|
395 | // Try for a double buffered RGB window |
---|
396 | if (vi_double_buffer) { |
---|
397 | vi_stored = vi_double_buffer; |
---|
398 | attributeList = dblBuf_RGBA; |
---|
399 | } |
---|
400 | |
---|
401 | if (!vi_immediate || !vi_stored) { |
---|
402 | G4cout << |
---|
403 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals." |
---|
404 | << G4endl; |
---|
405 | fViewId = -1; // This flags an error. |
---|
406 | } |
---|
407 | |
---|
408 | // glClearColor (0., 0., 0., 0.); |
---|
409 | // glClearDepth (1.); |
---|
410 | } |
---|
411 | |
---|
412 | G4OpenGLXViewer::~G4OpenGLXViewer () { |
---|
413 | if (fViewId >= 0) { |
---|
414 | //Close a window from here |
---|
415 | glXMakeCurrent (dpy, None, NULL); |
---|
416 | glXDestroyContext (dpy, cx); |
---|
417 | if (win) XDestroyWindow (dpy, win); // ...if already deleted in |
---|
418 | // sub-class G4OpenGLXmViewer. |
---|
419 | XFlush (dpy); |
---|
420 | } |
---|
421 | } |
---|
422 | |
---|
423 | |
---|
424 | #endif |
---|