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

Last change on this file since 1354 was 1348, checked in by garnier, 15 years ago

update

  • Property svn:mime-type set to text/cpp
File size: 14.6 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.57 2010/12/11 17:04:07 allison 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#include "G4VisManager.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) != 0 ? (strncpy((char*)malloc((unsigned)strlen(str) + 1), str, (unsigned)strlen(str) + 1)) : (char*)0)
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 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
200 G4cerr <<
201 "G4OpenGLViewer::G4OpenGLViewer failed to allocate a standard colormap."
202 << G4endl;
203 return;
204 }
205 if (G4VisManager::GetVerbosity() >= G4VisManager::confirmations)
206 G4cout << "Got standard cmap" << G4endl;
207 } else {
208 cmap = XCreateColormap (dpy,
209 XRootWindow(dpy, vi -> screen),
210 vi -> visual,
211 AllocNone);
212 if (G4VisManager::GetVerbosity() >= G4VisManager::confirmations)
213 G4cout << "Created own cmap" << G4endl;
214 }
215
216 if (!cmap) {
217 fViewId = -1; // This flags an error.
218 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
219 G4cout << "G4OpenGLViewer::G4OpenGLViewer failed to allocate a Colormap."
220 << G4endl;
221 return;
222 }
223
224}
225
226void G4OpenGLXViewer::CreateMainWindow () {
227
228// create a window
229 swa.colormap = cmap;
230 swa.border_pixel = 0;
231 swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
232 swa.backing_store = WhenMapped;
233
234 // Window size and position...
235 size_hints = XAllocSizeHints();
236
237 ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
238
239 G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
240
241 // FIXME, screen size != window size on MAC, but I don't know have to get the menuBar
242 // size on MAC. L.Garnier 01/2009
243 G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
244
245 size_hints->base_width = getWinWidth();
246 size_hints->base_height = getWinHeight();
247 size_hints->x = x_origin;
248 size_hints->y = y_origin;
249 if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
250 size_hints->flags |= PSize | PPosition;
251 } else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
252 size_hints->flags |= PSize;
253 } else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
254 size_hints->flags |= PPosition;
255 }
256 if (G4VisManager::GetVerbosity() >= G4VisManager::confirmations)
257 G4cout << "Window name: " << fName << G4endl;
258 strncpy (charViewName, fName, 99); charViewName[99] = '\0';
259 char *window_name = charViewName;
260 char *icon_name = charViewName;
261 //char tmpatom[] = "XA_WM_NORMAL_HINTS";
262 wm_hints = XAllocWMHints();
263 class_hints = XAllocClassHint();
264
265 XStringListToTextProperty (&window_name, 1, &windowName);
266 XStringListToTextProperty (&icon_name, 1, &iconName);
267
268 wm_hints -> initial_state = NormalState;
269 wm_hints -> input = True;
270 wm_hints -> icon_pixmap = icon_pixmap;
271 wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
272
273 class_hints -> res_name = NewString("G4OpenGL");
274 class_hints -> res_class = NewString("G4OpenGL");
275
276 win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
277 y_origin, getWinWidth(), getWinHeight(), 0, vi -> depth,
278 InputOutput, vi -> visual,
279 CWBorderPixel | CWColormap |
280 CWEventMask | CWBackingStore,
281 &swa);
282
283 XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
284 size_hints, wm_hints, class_hints);
285
286// request X to Draw window on screen.
287 XMapWindow (dpy, win);
288
289// Wait for window to appear (wait for an "expose" event).
290 XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
291
292// connect the context to a window
293 Bool success = glXMakeCurrent (dpy, win, cx);
294 if (!success) {
295 fViewId = -1; // This flags an error.
296 G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
297 << G4endl;
298 GLint error = GL_NO_ERROR;
299 while ((error = glGetError()) != GL_NO_ERROR) {
300 G4cout << "GL Error: " << gluErrorString(error) << G4endl;
301 }
302 return;
303 }
304
305}
306
307void G4OpenGLXViewer::CreateFontLists () {
308
309 std::map<G4double,G4String> fonts; // G4VMarker screen size and font name.
310 fonts[10.] = "-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
311 fonts[11.] = "-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
312 fonts[12.] = "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
313 fonts[13.] = "fixed";
314 fonts[14.] = "-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
315 fonts[17.] = "-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
316 fonts[18.] = "-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
317 fonts[20.] = "-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
318 fonts[24.] = "-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
319 fonts[25.] = "-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
320 fonts[34.] = "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
321 std::map<G4double,G4String>::const_iterator i;
322 for (i = fonts.begin(); i != fonts.end(); ++i) {
323 XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
324 if (!font_info) {
325 G4cerr <<
326 "G4OpenGLXViewer: XLoadQueryFont failed for font\n "
327 << i->second
328 << G4endl;
329 continue;
330 }
331 G4int font_base = glGenLists(256);
332 if (!font_base) {
333 G4cerr << "G4OpenGLXViewer: out of display lists for fonts."
334 << G4endl;
335 continue;
336 }
337 G4int first = font_info->min_char_or_byte2;
338 G4int last = font_info->max_char_or_byte2;
339 glXUseXFont(font_info->fid, first, last-first+1,font_base+first);
340 G4OpenGLFontBaseStore::AddFontBase(this,font_base,i->first,i->second);
341 }
342}
343
344void G4OpenGLXViewer::drawText(const char * textString,int x,int y,int z, int size){
345
346 CreateFontLists ();
347 // gl2ps or GL window ?
348 if (! drawGl2psText(textString,size)) {
349
350 G4int font_base = G4OpenGLFontBaseStore::GetFontBase(this,size);
351 if (font_base < 0) {
352 static G4int callCount = 0;
353 ++callCount;
354 if (callCount <= 10 || callCount%100 == 0) {
355 G4cout <<
356 "G4OpenGLSceneHandler::AddPrimitive (const G4Text&) call count "
357 << callCount <<
358 "\n No fonts available."
359 "\n Called with text \""
360 << textString
361 << "\"\n at "
362 << "x:"<< x
363 << "y:"<< y
364 << "z:"<< z
365 << ", size " << size
366 // << ", offsets " << text.GetXOffset () << ", " << text.GetYOffset ()
367 << G4endl;
368 }
369 return;
370 }
371 // No action on offset or layout at present.
372 glPushAttrib(GL_LIST_BIT);
373 glListBase(font_base);
374 glCallLists(strlen(textString), GL_UNSIGNED_BYTE, (GLubyte *)textString);
375 glPopAttrib();
376 }
377}
378
379
380G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
381G4VViewer (scene, -1),
382G4OpenGLViewer (scene),
383vi_immediate (0),
384vi_stored (0),
385vi (0),
386cmap (0)
387{
388 // To satisfy Coverity
389 xwa.visual = 0;
390 iconName.value = 0;
391 xwa.screen = 0;
392 windowName.value = 0;
393
394 GetXConnection ();
395 if (fViewId < 0) return;
396
397 // Try for a visual suitable for OpenGLImmediate..
398 // first try for a single buffered RGB window
399 if (!vi_single_buffer) {
400 vi_single_buffer =
401 glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
402 }
403 if (!vi_double_buffer) {
404 vi_double_buffer =
405 glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
406 }
407
408 if (vi_single_buffer || vi_double_buffer) {
409 if (!vi_double_buffer) {
410 G4cout <<
411 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
412 "\n Working with a single buffer."
413 << G4endl;
414 }
415 } else {
416 if (!vi_single_buffer) {
417 G4cout <<
418 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
419 << G4endl;
420 }
421 if (!vi_double_buffer) {
422 G4cout <<
423 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
424 << G4endl;
425 }
426 }
427
428 if (vi_single_buffer) {
429 vi_immediate = vi_single_buffer;
430 attributeList = snglBuf_RGBA;
431 }
432
433 if (!vi_immediate){
434 // next try for a double buffered RGB, but Draw to top buffer
435 if (vi_double_buffer) {
436 vi_immediate = vi_double_buffer;
437 attributeList = dblBuf_RGBA;
438 }
439 }
440
441 // Now try for a visual suitable for OpenGLStored...
442 // Try for a double buffered RGB window
443 if (vi_double_buffer) {
444 vi_stored = vi_double_buffer;
445 attributeList = dblBuf_RGBA;
446 }
447
448 if (!vi_immediate || !vi_stored) {
449 G4cout <<
450 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
451 << G4endl;
452 fViewId = -1; // This flags an error.
453 }
454
455 // glClearColor (0., 0., 0., 0.);
456 // glClearDepth (1.);
457}
458
459G4OpenGLXViewer::~G4OpenGLXViewer () {
460 if (fViewId >= 0) {
461 //Close a window from here
462 glXMakeCurrent (dpy, None, NULL);
463 glXDestroyContext (dpy, cx);
464 if (win) XDestroyWindow (dpy, win); // ...if already deleted in
465 // sub-class G4OpenGLXmViewer.
466 XFlush (dpy);
467 }
468}
469
470
471#endif
Note: See TracBrowser for help on using the repository browser.