34#ifdef G4VIS_BUILD_OPENGLX_DRIVER
55#include <X11/Xmu/StdCmap.h>
59int G4OpenGLXViewer::snglBuf_RGBA[12] =
68int 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
84XVisualInfo* G4OpenGLXViewer::vi_single_buffer = 0;
85XVisualInfo* G4OpenGLXViewer::vi_double_buffer = 0;
88 static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent* e,
char* arg) {
89 return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
93void 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) {
101 G4cout <<
"GL Error: " << gluErrorString(error) <<
G4endl;
105 G4OpenGLViewer::SetView ();
108void G4OpenGLXViewer::ShowView () {
113 if (fVP.IsPicking()) {
115 "Window activated for picking (left-mouse), exit (middle-mouse)."
119 XNextEvent(dpy, &event);
120 if (event.type == ButtonPress && event.xbutton.button == 1) {
121 Pick(event.xbutton.x, event.xbutton.y);
123 else if (event.type == ButtonPress && event.xbutton.button == 2)
break;
129void G4OpenGLXViewer::GetXConnection () {
131 dpy = XOpenDisplay (0);
134 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't open display." <<
G4endl;
139 if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
141 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer X Server has no GLX extension."
148void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
152 if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
154 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't return window attributes"
160 cx = glXCreateContext (dpy, vi, 0,
true);
163 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't create context."
171 XStandardColormap *standardCmaps = XAllocStandardColormap ();
174 status = XmuLookupStandardColormap (dpy,
184 status = XGetRGBColormaps (dpy,
185 XRootWindow (dpy, vi -> screen),
190 for (i = 0; i < numCmaps; i++) {
191 if (standardCmaps[i].visualid == vi -> visualid) {
192 cmap = standardCmaps[i].colormap;
193 XFree (standardCmaps);
201 "G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a standard colormap."
208 cmap = XCreateColormap (dpy,
209 XRootWindow(dpy, vi -> screen),
219 G4cout <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a Colormap."
226void G4OpenGLXViewer::CreateMainWindow () {
230 swa.border_pixel = 0;
231 swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
232 swa.backing_store = WhenMapped;
235 size_hints = XAllocSizeHints();
237 ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
239 G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
243 G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
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;
258 strncpy (charViewName, fName, 99); charViewName[99] =
'\0';
259 char *window_name = charViewName;
260 char *icon_name = charViewName;
262 wm_hints = XAllocWMHints();
263 class_hints = XAllocClassHint();
265 XStringListToTextProperty (&window_name, 1, &windowName);
266 XStringListToTextProperty (&icon_name, 1, &iconName);
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;
273 class_hints -> res_name =
NewString(
"G4OpenGL");
274 class_hints -> res_class =
NewString(
"G4OpenGL");
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,
283 XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
284 size_hints, wm_hints, class_hints);
287 XMapWindow (dpy, win);
290 XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (
char*) win);
293 Bool success = glXMakeCurrent (dpy, win, cx);
296 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to attach a GLX context."
298 GLint error = GL_NO_ERROR;
299 while ((error = glGetError()) != GL_NO_ERROR) {
300 G4cout <<
"GL Error: " << gluErrorString(error) <<
G4endl;
307void G4OpenGLXViewer::CreateFontLists()
309 std::map<G4double,G4String> fonts;
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);
326 "G4OpenGLXViewer::CreateFontLists XLoadQueryFont failed for font\n "
331 G4int font_base = glGenLists(256);
334 "G4OpenGLXViewer::CreateFontLists out of display lists for fonts."
338 G4int first = font_info->min_char_or_byte2;
339 G4int last = font_info->max_char_or_byte2;
340 glXUseXFont(font_info->fid, first, last-first+1, font_base + first);
341 G4int width = font_info->max_bounds.width;
343 (
this, font_base, i->first, i->second, width);
347void G4OpenGLXViewer::DrawText(
const G4Text& g4text)
349 if (isGl2psWriting()) {
351 G4OpenGLViewer::DrawText(g4text);
356 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
361 static G4int callCount = 0;
364 if (callCount <= 1) {
366 "G4OpenGLXViewer::DrawText: No fonts available for \""
375 const G4Colour& c = fSceneHandler.GetTextColour(g4text);
381 const char* textCString = textString.c_str();
386 glPushAttrib(GL_LIST_BIT);
402 glBitmap(0,0,0,0,xmove,ymove,0);
406 glCallLists(strlen(textCString),GL_UNSIGNED_BYTE,(GLubyte*)textCString);
412G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
414G4OpenGLViewer (scene),
424 windowName.value = 0;
427 if (fViewId < 0)
return;
431 if (!vi_single_buffer) {
433 glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
435 if (!vi_double_buffer) {
437 glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
440 if (vi_single_buffer || vi_double_buffer) {
441 if (!vi_double_buffer) {
443 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
444 "\n Working with a single buffer."
448 if (!vi_single_buffer) {
450 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
453 if (!vi_double_buffer) {
455 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
460 if (vi_single_buffer) {
461 vi_immediate = vi_single_buffer;
462 attributeList = snglBuf_RGBA;
467 if (vi_double_buffer) {
468 vi_immediate = vi_double_buffer;
469 attributeList = dblBuf_RGBA;
475 if (vi_double_buffer) {
476 vi_stored = vi_double_buffer;
477 attributeList = dblBuf_RGBA;
480 if (!vi_immediate || !vi_stored) {
482 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
491G4OpenGLXViewer::~G4OpenGLXViewer () {
494 glXMakeCurrent (dpy, None, NULL);
495 glXDestroyContext (dpy, cx);
496 if (win) XDestroyWindow (dpy, win);
G4DLLIMPORT std::ostream G4cerr
G4DLLIMPORT std::ostream G4cout
G4double GetAlpha() const
G4double GetGreen() const
static void AddFontBase(G4VViewer *, G4int fontBase, G4double size, const G4String &fontName, G4int width)
static const FontInfo & GetFontInfo(G4VViewer *, G4double size)
G4double GetYOffset() const
G4double GetXOffset() const
G4Point3D GetPosition() const
static Verbosity GetVerbosity()