34 #ifdef G4VIS_BUILD_OPENGLX_DRIVER
51 #include <X11/Xatom.h>
52 #include <X11/Xutil.h>
53 #include <X11/Xmu/StdCmap.h>
60 int G4OpenGLXViewer::snglBuf_RGBA[12] =
69 int G4OpenGLXViewer::dblBuf_RGBA[13] =
79 #define NewString(str) \
80 ((str) != 0 ? (strncpy((char*)malloc((unsigned)strlen(str) + 1), str, (unsigned)strlen(str) + 1)) : (char*)0)
82 #define USE_DEFAULT_COLORMAP 1
83 #define USE_STANDARD_COLORMAP 0
85 XVisualInfo* G4OpenGLXViewer::vi_single_buffer = 0;
86 XVisualInfo* G4OpenGLXViewer::vi_double_buffer = 0;
89 static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent*
e,
char* arg) {
90 return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
94 void G4OpenGLXViewer::SetView () {
95 #ifdef G4MULTITHREADED
97 glXMakeCurrent (dpy, win, cxMaster);
99 glXMakeCurrent (dpy, win, cxVisSubThread);
102 glXMakeCurrent (dpy, win, cxMaster);
104 G4OpenGLViewer::SetView ();
107 void G4OpenGLXViewer::ShowView () {
108 #ifdef G4MULTITHREADED
116 if (fVP.IsPicking()) {
118 "Window activated for picking (left-mouse), exit (middle-mouse)."
122 XNextEvent(dpy, &event);
123 if (event.type == ButtonPress && event.xbutton.button == 1) {
124 G4cout << Pick(event.xbutton.x, event.xbutton.y) <<
G4endl;
126 else if (event.type == ButtonPress && event.xbutton.button == 2)
break;
128 std::this_thread::sleep_for(std::chrono::milliseconds(100));
133 #ifdef G4MULTITHREADED
135 void G4OpenGLXViewer::SwitchToVisSubThread()
138 cxVisSubThread = glXCreateContext (dpy, vi, cxMaster,
true);
139 glXMakeCurrent (dpy, win, cxVisSubThread);
142 void G4OpenGLXViewer::SwitchToMasterThread()
145 glXMakeCurrent (dpy, win, cxMaster);
147 glXDestroyContext (dpy, cxVisSubThread);
152 void G4OpenGLXViewer::GetXConnection () {
154 dpy = XOpenDisplay (0);
157 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't open display." <<
G4endl;
162 if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
164 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer X Server has no GLX extension."
171 void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
175 if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
177 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't return window attributes"
183 cxMaster = glXCreateContext (dpy, vi, 0,
true);
186 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't create context."
194 XStandardColormap *standardCmaps = XAllocStandardColormap ();
197 status = XmuLookupStandardColormap (dpy,
207 status = XGetRGBColormaps (dpy,
208 XRootWindow (dpy, vi -> screen),
213 for (i = 0; i < numCmaps; i++) {
214 if (standardCmaps[i].visualid == vi -> visualid) {
215 cmap = standardCmaps[i].colormap;
216 XFree (standardCmaps);
224 "G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a standard colormap."
231 cmap = XCreateColormap (dpy,
232 XRootWindow(dpy, vi -> screen),
242 G4cout <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a Colormap."
249 void G4OpenGLXViewer::CreateMainWindow () {
253 swa.border_pixel = 0;
254 swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
255 swa.backing_store = WhenMapped;
258 size_hints = XAllocSizeHints();
260 ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
262 G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
266 G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
268 size_hints->base_width = getWinWidth();
269 size_hints->base_height = getWinHeight();
270 size_hints->x = x_origin;
271 size_hints->y = y_origin;
272 if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
273 size_hints->flags |= PSize | PPosition;
274 }
else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
275 size_hints->flags |= PSize;
276 }
else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
277 size_hints->flags |= PPosition;
281 strncpy (charViewName,
fName, 99); charViewName[99] =
'\0';
282 char *window_name = charViewName;
283 char *icon_name = charViewName;
285 wm_hints = XAllocWMHints();
286 class_hints = XAllocClassHint();
288 XStringListToTextProperty (&window_name, 1, &windowName);
289 XStringListToTextProperty (&icon_name, 1, &iconName);
291 wm_hints -> initial_state = NormalState;
292 wm_hints -> input = True;
293 wm_hints -> icon_pixmap = icon_pixmap;
294 wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
296 class_hints -> res_name =
NewString(
"G4OpenGL");
297 class_hints -> res_class =
NewString(
"G4OpenGL");
299 win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
300 y_origin, getWinWidth(), getWinHeight(), 0, vi -> depth,
301 InputOutput, vi -> visual,
302 CWBorderPixel | CWColormap |
303 CWEventMask | CWBackingStore,
306 XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
307 size_hints, wm_hints, class_hints);
310 XMapWindow (dpy, win);
313 XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (
char*) win);
316 Bool success = glXMakeCurrent (dpy, win, cxMaster);
319 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to attach a GLX context."
321 GLint
error = GL_NO_ERROR;
322 while ((error = glGetError()) != GL_NO_ERROR) {
324 case GL_INVALID_ENUM :
326 case GL_INVALID_VALUE :
328 case GL_INVALID_OPERATION :
329 G4cout <<
"GL Error: GL_INVALID_OPERATION" <<
G4endl;
break;
330 case GL_OUT_OF_MEMORY :
332 case GL_STACK_UNDERFLOW :
333 G4cout <<
"GL Error: GL_STACK_UNDERFLOW" <<
G4endl;
break;
334 case GL_STACK_OVERFLOW :
335 G4cout <<
"GL Error: GL_STACK_OVERFLOW" <<
G4endl;
break;
344 void G4OpenGLXViewer::CreateFontLists()
346 std::map<G4double,G4String> fonts;
347 fonts[10.] =
"-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
348 fonts[11.] =
"-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
349 fonts[12.] =
"-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
350 fonts[13.] =
"fixed";
351 fonts[14.] =
"-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
352 fonts[17.] =
"-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
353 fonts[18.] =
"-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
354 fonts[20.] =
"-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
355 fonts[24.] =
"-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
356 fonts[25.] =
"-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
357 fonts[34.] =
"-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
358 std::map<G4double,G4String>::const_iterator i;
359 for (i = fonts.begin(); i != fonts.end(); ++i) {
360 XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
363 "G4OpenGLXViewer::CreateFontLists XLoadQueryFont failed for font\n "
368 G4int font_base = glGenLists(256);
371 "G4OpenGLXViewer::CreateFontLists out of display lists for fonts."
376 G4int last = font_info->max_char_or_byte2;
377 glXUseXFont(font_info->fid, first, last-first+1, font_base + first);
380 (
this, font_base, i->first, i->second, width);
384 void G4OpenGLXViewer::DrawText(
const G4Text& g4text)
386 if (isGl2psWriting()) {
388 G4OpenGLViewer::DrawText(g4text);
393 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
398 static G4int callCount = 0;
401 if (callCount <= 1) {
403 "G4OpenGLXViewer::DrawText: No fonts available for \""
412 const G4Colour& c = fSceneHandler.GetTextColour(g4text);
418 const char* textCString = textString.c_str();
421 glRasterPos3d(position.x(),position.y(),position.z());
423 glPushAttrib(GL_LIST_BIT);
439 glBitmap(0,0,0,0,xmove,ymove,0);
443 glCallLists(strlen(textCString),GL_UNSIGNED_BYTE,(GLubyte*)textCString);
449 G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
451 G4OpenGLViewer (scene),
461 windowName.value = 0;
464 if (fViewId < 0)
return;
468 if (!vi_single_buffer) {
470 glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
472 if (!vi_double_buffer) {
474 glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
477 if (vi_single_buffer || vi_double_buffer) {
478 if (!vi_double_buffer) {
480 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
481 "\n Working with a single buffer."
485 if (!vi_single_buffer) {
487 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
490 if (!vi_double_buffer) {
492 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
497 if (vi_single_buffer) {
498 vi_immediate = vi_single_buffer;
499 attributeList = snglBuf_RGBA;
504 if (vi_double_buffer) {
505 vi_immediate = vi_double_buffer;
506 attributeList = dblBuf_RGBA;
512 if (vi_double_buffer) {
513 vi_stored = vi_double_buffer;
514 attributeList = dblBuf_RGBA;
517 if (!vi_immediate || !vi_stored) {
519 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
528 G4OpenGLXViewer::~G4OpenGLXViewer () {
531 glXMakeCurrent (dpy, None, NULL);
532 glXDestroyContext (dpy, cxMaster);
533 if (win) XDestroyWindow (dpy, win);
static const FontInfo & GetFontInfo(G4VViewer *, G4double size)
G4double GetXOffset() const
static Verbosity GetVerbosity()
G4GLOB_DLL std::ostream G4cerr
static G4String Status(G4StepStatus stps)
G4double GetAlpha() const
G4GLOB_DLL std::ostream G4cout
G4double GetYOffset() const
static void AddFontBase(G4VViewer *, G4int fontBase, G4double size, const G4String &fontName, G4int width)
static PROLOG_HANDLER error
G4Point3D GetPosition() const
G4double GetGreen() const