34 #ifdef G4VIS_BUILD_OPENGLX_DRIVER
53 #include <X11/Xatom.h>
54 #include <X11/Xutil.h>
55 #include <X11/Xmu/StdCmap.h>
59 int G4OpenGLXViewer::snglBuf_RGBA[12] =
68 int G4OpenGLXViewer::dblBuf_RGBA[13] =
78 #define NewString(str) \
79 ((str) != 0 ? (strncpy((char*)malloc((unsigned)strlen(str) + 1), str, (unsigned)strlen(str) + 1)) : (char*)0)
81 #define USE_DEFAULT_COLORMAP 1
82 #define USE_STANDARD_COLORMAP 0
84 XVisualInfo* G4OpenGLXViewer::vi_single_buffer = 0;
85 XVisualInfo* G4OpenGLXViewer::vi_double_buffer = 0;
88 static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent* e,
char* arg) {
89 return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
93 void G4OpenGLXViewer::SetView () {
94 Bool success = glXMakeCurrent (dpy, win, cx);
97 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to attach a GLX context."
99 GLint
error = GL_NO_ERROR;
100 while ((error = glGetError()) != GL_NO_ERROR) {
102 case GL_INVALID_ENUM :
104 case GL_INVALID_VALUE :
106 case GL_INVALID_OPERATION :
107 G4cout <<
"GL Error: GL_INVALID_OPERATION" <<
G4endl;
break;
108 case GL_OUT_OF_MEMORY :
110 case GL_STACK_UNDERFLOW :
111 G4cout <<
"GL Error: GL_STACK_UNDERFLOW" <<
G4endl;
break;
112 case GL_STACK_OVERFLOW :
113 G4cout <<
"GL Error: GL_STACK_OVERFLOW" <<
G4endl;
break;
120 G4OpenGLViewer::SetView ();
123 void G4OpenGLXViewer::ShowView () {
128 if (fVP.IsPicking()) {
130 "Window activated for picking (left-mouse), exit (middle-mouse)."
134 XNextEvent(dpy, &event);
135 if (event.type == ButtonPress && event.xbutton.button == 1) {
136 G4cout << Pick(event.xbutton.x, event.xbutton.y) <<
G4endl;
138 else if (event.type == ButtonPress && event.xbutton.button == 2)
break;
144 void G4OpenGLXViewer::GetXConnection () {
146 dpy = XOpenDisplay (0);
149 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't open display." <<
G4endl;
154 if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
156 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer X Server has no GLX extension."
163 void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
167 if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
169 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't return window attributes"
175 cx = glXCreateContext (dpy, vi, 0,
true);
178 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't create context."
186 XStandardColormap *standardCmaps = XAllocStandardColormap ();
189 status = XmuLookupStandardColormap (dpy,
199 status = XGetRGBColormaps (dpy,
200 XRootWindow (dpy, vi -> screen),
205 for (i = 0; i < numCmaps; i++) {
206 if (standardCmaps[i].visualid == vi -> visualid) {
207 cmap = standardCmaps[i].colormap;
208 XFree (standardCmaps);
216 "G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a standard colormap."
223 cmap = XCreateColormap (dpy,
224 XRootWindow(dpy, vi -> screen),
234 G4cout <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a Colormap."
241 void G4OpenGLXViewer::CreateMainWindow () {
245 swa.border_pixel = 0;
246 swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
247 swa.backing_store = WhenMapped;
250 size_hints = XAllocSizeHints();
252 ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
254 G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
258 G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
260 size_hints->base_width = getWinWidth();
261 size_hints->base_height = getWinHeight();
262 size_hints->x = x_origin;
263 size_hints->y = y_origin;
264 if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
265 size_hints->flags |= PSize | PPosition;
266 }
else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
267 size_hints->flags |= PSize;
268 }
else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
269 size_hints->flags |= PPosition;
273 strncpy (charViewName,
fName, 99); charViewName[99] =
'\0';
274 char *window_name = charViewName;
275 char *icon_name = charViewName;
277 wm_hints = XAllocWMHints();
278 class_hints = XAllocClassHint();
280 XStringListToTextProperty (&window_name, 1, &windowName);
281 XStringListToTextProperty (&icon_name, 1, &iconName);
283 wm_hints -> initial_state = NormalState;
284 wm_hints -> input = True;
285 wm_hints -> icon_pixmap = icon_pixmap;
286 wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
288 class_hints -> res_name =
NewString(
"G4OpenGL");
289 class_hints -> res_class =
NewString(
"G4OpenGL");
291 win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
292 y_origin, getWinWidth(), getWinHeight(), 0, vi -> depth,
293 InputOutput, vi -> visual,
294 CWBorderPixel | CWColormap |
295 CWEventMask | CWBackingStore,
298 XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
299 size_hints, wm_hints, class_hints);
302 XMapWindow (dpy, win);
305 XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (
char*) win);
308 Bool success = glXMakeCurrent (dpy, win, cx);
311 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to attach a GLX context."
313 GLint error = GL_NO_ERROR;
314 while ((error = glGetError()) != GL_NO_ERROR) {
316 case GL_INVALID_ENUM :
318 case GL_INVALID_VALUE :
320 case GL_INVALID_OPERATION :
321 G4cout <<
"GL Error: GL_INVALID_OPERATION" <<
G4endl;
break;
322 case GL_OUT_OF_MEMORY :
324 case GL_STACK_UNDERFLOW :
325 G4cout <<
"GL Error: GL_STACK_UNDERFLOW" <<
G4endl;
break;
326 case GL_STACK_OVERFLOW :
327 G4cout <<
"GL Error: GL_STACK_OVERFLOW" <<
G4endl;
break;
337 void G4OpenGLXViewer::CreateFontLists()
339 std::map<G4double,G4String> fonts;
340 fonts[10.] =
"-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
341 fonts[11.] =
"-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
342 fonts[12.] =
"-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
343 fonts[13.] =
"fixed";
344 fonts[14.] =
"-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
345 fonts[17.] =
"-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
346 fonts[18.] =
"-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
347 fonts[20.] =
"-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
348 fonts[24.] =
"-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
349 fonts[25.] =
"-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
350 fonts[34.] =
"-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
351 std::map<G4double,G4String>::const_iterator i;
352 for (i = fonts.begin(); i != fonts.end(); ++i) {
353 XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
356 "G4OpenGLXViewer::CreateFontLists XLoadQueryFont failed for font\n "
361 G4int font_base = glGenLists(256);
364 "G4OpenGLXViewer::CreateFontLists out of display lists for fonts."
368 G4int first = font_info->min_char_or_byte2;
369 G4int last = font_info->max_char_or_byte2;
370 glXUseXFont(font_info->fid, first, last-first+1, font_base + first);
373 (
this, font_base, i->first, i->second, width);
377 void G4OpenGLXViewer::DrawText(
const G4Text& g4text)
379 if (isGl2psWriting()) {
381 G4OpenGLViewer::DrawText(g4text);
386 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
391 static G4int callCount = 0;
394 if (callCount <= 1) {
396 "G4OpenGLXViewer::DrawText: No fonts available for \""
405 const G4Colour& c = fSceneHandler.GetTextColour(g4text);
411 const char* textCString = textString.c_str();
414 glRasterPos3d(position.x(),position.y(),position.z());
416 glPushAttrib(GL_LIST_BIT);
432 glBitmap(0,0,0,0,xmove,ymove,0);
436 glCallLists(strlen(textCString),GL_UNSIGNED_BYTE,(GLubyte*)textCString);
442 G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
444 G4OpenGLViewer (scene),
454 windowName.value = 0;
457 if (fViewId < 0)
return;
461 if (!vi_single_buffer) {
463 glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
465 if (!vi_double_buffer) {
467 glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
470 if (vi_single_buffer || vi_double_buffer) {
471 if (!vi_double_buffer) {
473 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
474 "\n Working with a single buffer."
478 if (!vi_single_buffer) {
480 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
483 if (!vi_double_buffer) {
485 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
490 if (vi_single_buffer) {
491 vi_immediate = vi_single_buffer;
492 attributeList = snglBuf_RGBA;
497 if (vi_double_buffer) {
498 vi_immediate = vi_double_buffer;
499 attributeList = dblBuf_RGBA;
505 if (vi_double_buffer) {
506 vi_stored = vi_double_buffer;
507 attributeList = dblBuf_RGBA;
510 if (!vi_immediate || !vi_stored) {
512 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
521 G4OpenGLXViewer::~G4OpenGLXViewer () {
524 glXMakeCurrent (dpy, None, NULL);
525 glXDestroyContext (dpy, cx);
526 if (win) XDestroyWindow (dpy, win);
G4double GetAlpha() const
HepGeom::Point3D< G4double > G4Point3D
G4Point3D GetPosition() const
G4double GetYOffset() const
G4GLOB_DLL std::ostream G4cout
static const FontInfo & GetFontInfo(G4VViewer *, G4double size)
G4double GetGreen() const
static void AddFontBase(G4VViewer *, G4int fontBase, G4double size, const G4String &fontName, G4int width)
G4double GetXOffset() const
static Verbosity GetVerbosity()
static G4String Status(G4StepStatus stps)
static PROLOG_HANDLER error
G4GLOB_DLL std::ostream G4cerr