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 G4OpenGLViewer::SetView ();
98 void G4OpenGLXViewer::ShowView () {
99 #ifdef G4MULTITHREADED
107 if (fVP.IsPicking()) {
109 "Window activated for picking (left-mouse), exit (middle-mouse)."
113 XNextEvent(dpy, &event);
114 if (event.type == ButtonPress && event.xbutton.button == 1) {
115 G4cout << Pick(event.xbutton.x, event.xbutton.y) <<
G4endl;
117 else if (event.type == ButtonPress && event.xbutton.button == 2)
break;
119 std::this_thread::sleep_for(std::chrono::milliseconds(100));
124 #ifdef G4MULTITHREADED
126 void G4OpenGLXViewer::SwitchToVisSubThread()
129 cxVisSubThread = glXCreateContext (dpy, vi, cxMaster,
true);
130 glXMakeCurrent (dpy, win, cxVisSubThread);
133 void G4OpenGLXViewer::SwitchToMasterThread()
136 glXMakeCurrent (dpy, win, cxMaster);
138 glXDestroyContext (dpy, cxVisSubThread);
143 void G4OpenGLXViewer::GetXConnection () {
145 dpy = XOpenDisplay (0);
148 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't open display." <<
G4endl;
153 if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
155 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer X Server has no GLX extension."
162 void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
166 if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
168 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't return window attributes"
174 cxMaster = glXCreateContext (dpy, vi, 0,
true);
177 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer couldn't create context."
185 XStandardColormap *standardCmaps = XAllocStandardColormap ();
188 status = XmuLookupStandardColormap (dpy,
198 status = XGetRGBColormaps (dpy,
199 XRootWindow (dpy, vi -> screen),
204 for (i = 0; i < numCmaps; i++) {
205 if (standardCmaps[i].visualid == vi -> visualid) {
206 cmap = standardCmaps[i].colormap;
207 XFree (standardCmaps);
215 "G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a standard colormap."
222 cmap = XCreateColormap (dpy,
223 XRootWindow(dpy, vi -> screen),
233 G4cout <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to allocate a Colormap."
240 void G4OpenGLXViewer::CreateMainWindow () {
244 swa.border_pixel = 0;
245 swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
246 swa.backing_store = WhenMapped;
249 size_hints = XAllocSizeHints();
251 ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
253 G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
257 G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
259 size_hints->base_width = getWinWidth();
260 size_hints->base_height = getWinHeight();
261 size_hints->x = x_origin;
262 size_hints->y = y_origin;
263 if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
264 size_hints->flags |= PSize | PPosition;
265 }
else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
266 size_hints->flags |= PSize;
267 }
else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
268 size_hints->flags |= PPosition;
272 strncpy (charViewName,
fName, 99); charViewName[99] =
'\0';
273 char *window_name = charViewName;
274 char *icon_name = charViewName;
276 wm_hints = XAllocWMHints();
277 class_hints = XAllocClassHint();
279 XStringListToTextProperty (&window_name, 1, &windowName);
280 XStringListToTextProperty (&icon_name, 1, &iconName);
282 wm_hints -> initial_state = NormalState;
283 wm_hints -> input = True;
284 wm_hints -> icon_pixmap = icon_pixmap;
285 wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
287 class_hints -> res_name =
NewString(
"G4OpenGL");
288 class_hints -> res_class =
NewString(
"G4OpenGL");
290 win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
291 y_origin, getWinWidth(), getWinHeight(), 0, vi -> depth,
292 InputOutput, vi -> visual,
293 CWBorderPixel | CWColormap |
294 CWEventMask | CWBackingStore,
297 XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
298 size_hints, wm_hints, class_hints);
301 XMapWindow (dpy, win);
304 XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (
char*) win);
307 Bool success = glXMakeCurrent (dpy, win, cxMaster);
310 G4cerr <<
"G4OpenGLXViewer::G4OpenGLXViewer failed to attach a GLX context."
312 GLint
error = GL_NO_ERROR;
313 while ((error = glGetError()) != GL_NO_ERROR) {
315 case GL_INVALID_ENUM :
317 case GL_INVALID_VALUE :
319 case GL_INVALID_OPERATION :
320 G4cout <<
"GL Error: GL_INVALID_OPERATION" <<
G4endl;
break;
321 case GL_OUT_OF_MEMORY :
323 case GL_STACK_UNDERFLOW :
324 G4cout <<
"GL Error: GL_STACK_UNDERFLOW" <<
G4endl;
break;
325 case GL_STACK_OVERFLOW :
326 G4cout <<
"GL Error: GL_STACK_OVERFLOW" <<
G4endl;
break;
335 void G4OpenGLXViewer::CreateFontLists()
337 std::map<G4double,G4String> fonts;
338 fonts[10.] =
"-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
339 fonts[11.] =
"-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
340 fonts[12.] =
"-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
341 fonts[13.] =
"fixed";
342 fonts[14.] =
"-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
343 fonts[17.] =
"-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
344 fonts[18.] =
"-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
345 fonts[20.] =
"-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
346 fonts[24.] =
"-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
347 fonts[25.] =
"-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
348 fonts[34.] =
"-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
349 std::map<G4double,G4String>::const_iterator i;
350 for (i = fonts.begin(); i != fonts.end(); ++i) {
351 XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
354 "G4OpenGLXViewer::CreateFontLists XLoadQueryFont failed for font\n "
359 G4int font_base = glGenLists(256);
362 "G4OpenGLXViewer::CreateFontLists out of display lists for fonts."
366 G4int first = font_info->min_char_or_byte2;
367 G4int last = font_info->max_char_or_byte2;
368 glXUseXFont(font_info->fid, first, last-first+1, font_base + first);
371 (
this, font_base, i->first, i->second, width);
375 void G4OpenGLXViewer::DrawText(
const G4Text& g4text)
377 if (isGl2psWriting()) {
379 G4OpenGLViewer::DrawText(g4text);
384 G4double size = fSceneHandler.GetMarkerSize(g4text,sizeType);
389 static G4int callCount = 0;
392 if (callCount <= 1) {
394 "G4OpenGLXViewer::DrawText: No fonts available for \""
403 const G4Colour& c = fSceneHandler.GetTextColour(g4text);
409 const char* textCString = textString.c_str();
412 glRasterPos3d(position.x(),position.y(),position.z());
414 glPushAttrib(GL_LIST_BIT);
430 glBitmap(0,0,0,0,xmove,ymove,0);
434 glCallLists(strlen(textCString),GL_UNSIGNED_BYTE,(GLubyte*)textCString);
440 G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
442 G4OpenGLViewer (scene),
452 windowName.value = 0;
455 if (fViewId < 0)
return;
459 if (!vi_single_buffer) {
461 glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
463 if (!vi_double_buffer) {
465 glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
468 if (vi_single_buffer || vi_double_buffer) {
469 if (!vi_double_buffer) {
471 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
472 "\n Working with a single buffer."
476 if (!vi_single_buffer) {
478 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
481 if (!vi_double_buffer) {
483 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
488 if (vi_single_buffer) {
489 vi_immediate = vi_single_buffer;
490 attributeList = snglBuf_RGBA;
495 if (vi_double_buffer) {
496 vi_immediate = vi_double_buffer;
497 attributeList = dblBuf_RGBA;
503 if (vi_double_buffer) {
504 vi_stored = vi_double_buffer;
505 attributeList = dblBuf_RGBA;
508 if (!vi_immediate || !vi_stored) {
510 "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
519 G4OpenGLXViewer::~G4OpenGLXViewer () {
522 glXMakeCurrent (dpy, None, NULL);
523 glXDestroyContext (dpy, cxMaster);
524 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