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

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

suppression de la fin des methodes de print inutiles

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