| 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 | //
|
|---|
| 27 | // $Id: G4OpenGLXViewer.cc,v 1.52 2009/04/08 16:55:44 lgarnier Exp $
|
|---|
| 28 | // GEANT4 tag $Name: $
|
|---|
| 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"
|
|---|
| 38 | #include "G4VViewer.hh"
|
|---|
| 39 | #include "G4OpenGLSceneHandler.hh"
|
|---|
| 40 | #include <GL/glu.h>
|
|---|
| 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"
|
|---|
| 51 | #include "G4StateManager.hh"
|
|---|
| 52 |
|
|---|
| 53 | #include <X11/Xatom.h>
|
|---|
| 54 | #include <X11/Xutil.h>
|
|---|
| 55 | #include <X11/Xmu/StdCmap.h>
|
|---|
| 56 |
|
|---|
| 57 | #include <assert.h>
|
|---|
| 58 |
|
|---|
| 59 | int 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 |
|
|---|
| 68 | int 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 |
|
|---|
| 84 | XVisualInfo* G4OpenGLXViewer::vi_single_buffer = 0;
|
|---|
| 85 | XVisualInfo* G4OpenGLXViewer::vi_double_buffer = 0;
|
|---|
| 86 |
|
|---|
| 87 | extern "C" {
|
|---|
| 88 | static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent* e, char* arg) {
|
|---|
| 89 | return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void G4OpenGLXViewer::SetView () {
|
|---|
| 94 | glXMakeCurrent (dpy, win, cx);
|
|---|
| 95 | G4OpenGLViewer::SetView ();
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void G4OpenGLXViewer::ShowView () {
|
|---|
| 99 | glXWaitGL (); //Wait for effects of all previous OpenGL commands to
|
|---|
| 100 | //be propagated before progressing.
|
|---|
| 101 | glFlush ();
|
|---|
| 102 |
|
|---|
| 103 | if (fVP.IsPicking()) {
|
|---|
| 104 | G4cout <<
|
|---|
| 105 | "Window activated for picking (left-mouse), exit (middle-mouse)."
|
|---|
| 106 | << G4endl;
|
|---|
| 107 | while (true) {
|
|---|
| 108 | if (XPending(dpy)) {
|
|---|
| 109 | XNextEvent(dpy, &event);
|
|---|
| 110 | if (event.type == ButtonPress && event.xbutton.button == 1) {
|
|---|
| 111 | Pick(event.xbutton.x, event.xbutton.y);
|
|---|
| 112 | }
|
|---|
| 113 | else if (event.type == ButtonPress && event.xbutton.button == 2) break;
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | void G4OpenGLXViewer::GetXConnection () {
|
|---|
| 120 | // get a connection.
|
|---|
| 121 | dpy = XOpenDisplay (0);
|
|---|
| 122 | if (!dpy) {
|
|---|
| 123 | fViewId = -1; // This flags an error.
|
|---|
| 124 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't open display." << G4endl;
|
|---|
| 125 | return;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | // make sure OpenGL is supported and installed properly.
|
|---|
| 129 | if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
|
|---|
| 130 | fViewId = -1; // This flags an error.
|
|---|
| 131 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer X Server has no GLX extension."
|
|---|
| 132 | << G4endl;
|
|---|
| 133 | return;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
|
|---|
| 139 |
|
|---|
| 140 | vi = v;
|
|---|
| 141 | // get window's attributes
|
|---|
| 142 | if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
|
|---|
| 143 | fViewId = -1; // This flags an error.
|
|---|
| 144 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't return window attributes"
|
|---|
| 145 | << G4endl;
|
|---|
| 146 | return;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | // create a GLX context
|
|---|
| 150 | cx = glXCreateContext (dpy, vi, 0, true);
|
|---|
| 151 | if (!cx) {
|
|---|
| 152 | fViewId = -1; // This flags an error.
|
|---|
| 153 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't create context."
|
|---|
| 154 | << G4endl;
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | // New stab at getting a colormap
|
|---|
| 159 |
|
|---|
| 160 | Status status;
|
|---|
| 161 | XStandardColormap *standardCmaps = XAllocStandardColormap ();
|
|---|
| 162 | int i, numCmaps;
|
|---|
| 163 |
|
|---|
| 164 | status = XmuLookupStandardColormap (dpy,
|
|---|
| 165 | vi -> screen,
|
|---|
| 166 | vi -> visualid,
|
|---|
| 167 | vi -> depth,
|
|---|
| 168 | XA_RGB_DEFAULT_MAP,
|
|---|
| 169 | False,
|
|---|
| 170 | True);
|
|---|
| 171 |
|
|---|
| 172 | if (status == 1) {
|
|---|
| 173 | cmap = 0;
|
|---|
| 174 | status = XGetRGBColormaps (dpy,
|
|---|
| 175 | XRootWindow (dpy, vi -> screen),
|
|---|
| 176 | &standardCmaps,
|
|---|
| 177 | &numCmaps,
|
|---|
| 178 | XA_RGB_DEFAULT_MAP);
|
|---|
| 179 | if (status == 1)
|
|---|
| 180 | for (i = 0; i < numCmaps; i++) {
|
|---|
| 181 | if (standardCmaps[i].visualid == vi -> visualid) {
|
|---|
| 182 | cmap = standardCmaps[i].colormap;
|
|---|
| 183 | XFree (standardCmaps);
|
|---|
| 184 | break;
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | if (!cmap) {
|
|---|
| 188 | fViewId = -1; // This flags an error.
|
|---|
| 189 | G4cerr <<
|
|---|
| 190 | "G4OpenGLViewer::G4OpenGLViewer failed to allocate a standard colormap."
|
|---|
| 191 | << G4endl;
|
|---|
| 192 | return;
|
|---|
| 193 | }
|
|---|
| 194 | G4cout << "Got standard cmap" << G4endl;
|
|---|
| 195 | } else {
|
|---|
| 196 | cmap = XCreateColormap (dpy,
|
|---|
| 197 | XRootWindow(dpy, vi -> screen),
|
|---|
| 198 | vi -> visual,
|
|---|
| 199 | AllocNone);
|
|---|
| 200 | G4cout << "Created own cmap" << G4endl;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | if (!cmap) {
|
|---|
| 204 | fViewId = -1; // This flags an error.
|
|---|
| 205 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to allocate a Colormap."
|
|---|
| 206 | << G4endl;
|
|---|
| 207 | return;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | void G4OpenGLXViewer::CreateMainWindow () {
|
|---|
| 213 |
|
|---|
| 214 | // create a window
|
|---|
| 215 | swa.colormap = cmap;
|
|---|
| 216 | swa.border_pixel = 0;
|
|---|
| 217 | swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
|
|---|
| 218 | swa.backing_store = WhenMapped;
|
|---|
| 219 |
|
|---|
| 220 | // Window size and position...
|
|---|
| 221 | size_hints = XAllocSizeHints();
|
|---|
| 222 |
|
|---|
| 223 | fWinSize_x = fVP.GetWindowSizeHintX();
|
|---|
| 224 | fWinSize_y = fVP.GetWindowSizeHintY();
|
|---|
| 225 | G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
|
|---|
| 226 |
|
|---|
| 227 | // FIXME, screen size != window size on MAC, but I don't know have to get the menuBar
|
|---|
| 228 | // size on MAC. L.Garnier 01/2009
|
|---|
| 229 | G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
|
|---|
| 230 |
|
|---|
| 231 | size_hints->base_width = fWinSize_x;
|
|---|
| 232 | size_hints->base_height = fWinSize_y;
|
|---|
| 233 | size_hints->x = x_origin;
|
|---|
| 234 | size_hints->y = y_origin;
|
|---|
| 235 | if (fVP.IsWindowSizeHintX () && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
|
|---|
| 236 | size_hints->flags |= PSize | PPosition;
|
|---|
| 237 | } else if (fVP.IsWindowSizeHintX () && !(fVP.IsWindowLocationHintX () || fVP.IsWindowLocationHintY ())) {
|
|---|
| 238 | size_hints->flags |= PSize;
|
|---|
| 239 | } else if ((!fVP.IsWindowSizeHintX ()) && fVP.IsWindowLocationHintX () && fVP.IsWindowLocationHintY ()) {
|
|---|
| 240 | size_hints->flags |= PPosition;
|
|---|
| 241 | }
|
|---|
| 242 | G4cout << "Window name: " << fName << G4endl;
|
|---|
| 243 | strncpy (charViewName, fName, 100);
|
|---|
| 244 | char *window_name = charViewName;
|
|---|
| 245 | char *icon_name = charViewName;
|
|---|
| 246 | //char tmpatom[] = "XA_WM_NORMAL_HINTS";
|
|---|
| 247 | wm_hints = XAllocWMHints();
|
|---|
| 248 | class_hints = XAllocClassHint();
|
|---|
| 249 |
|
|---|
| 250 | XStringListToTextProperty (&window_name, 1, &windowName);
|
|---|
| 251 | XStringListToTextProperty (&icon_name, 1, &iconName);
|
|---|
| 252 |
|
|---|
| 253 | wm_hints -> initial_state = NormalState;
|
|---|
| 254 | wm_hints -> input = True;
|
|---|
| 255 | wm_hints -> icon_pixmap = icon_pixmap;
|
|---|
| 256 | wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
|
|---|
| 257 |
|
|---|
| 258 | class_hints -> res_name = NewString("G4OpenGL");
|
|---|
| 259 | class_hints -> res_class = NewString("G4OpenGL");
|
|---|
| 260 |
|
|---|
| 261 | win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
|
|---|
| 262 | y_origin, fWinSize_x, fWinSize_y, 0, vi -> depth,
|
|---|
| 263 | InputOutput, vi -> visual,
|
|---|
| 264 | CWBorderPixel | CWColormap |
|
|---|
| 265 | CWEventMask | CWBackingStore,
|
|---|
| 266 | &swa);
|
|---|
| 267 |
|
|---|
| 268 | XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
|
|---|
| 269 | size_hints, wm_hints, class_hints);
|
|---|
| 270 |
|
|---|
| 271 | // request X to Draw window on screen.
|
|---|
| 272 | XMapWindow (dpy, win);
|
|---|
| 273 |
|
|---|
| 274 | // Wait for window to appear (wait for an "expose" event).
|
|---|
| 275 | XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
|
|---|
| 276 |
|
|---|
| 277 | // connect the context to a window
|
|---|
| 278 | Bool success = glXMakeCurrent (dpy, win, cx);
|
|---|
| 279 | if (!success) {
|
|---|
| 280 | fViewId = -1; // This flags an error.
|
|---|
| 281 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
|
|---|
| 282 | << G4endl;
|
|---|
| 283 | GLint error = GL_NO_ERROR;
|
|---|
| 284 | while ((error = glGetError()) != GL_NO_ERROR) {
|
|---|
| 285 | G4cout << "GL Error: " << gluErrorString(error) << G4endl;
|
|---|
| 286 | }
|
|---|
| 287 | return;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | void G4OpenGLXViewer::CreateFontLists () {
|
|---|
| 293 |
|
|---|
| 294 | std::map<G4double,G4String> fonts; // G4VMarker screen size and font name.
|
|---|
| 295 | fonts[10.] = "-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
|
|---|
| 296 | fonts[11.] = "-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
|
|---|
| 297 | fonts[12.] = "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
|
|---|
| 298 | fonts[13.] = "fixed";
|
|---|
| 299 | fonts[14.] = "-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
|
|---|
| 300 | fonts[17.] = "-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
|
|---|
| 301 | fonts[18.] = "-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
|
|---|
| 302 | fonts[20.] = "-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
|
|---|
| 303 | fonts[24.] = "-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
|
|---|
| 304 | fonts[25.] = "-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
|
|---|
| 305 | fonts[34.] = "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
|
|---|
| 306 | std::map<G4double,G4String>::const_iterator i;
|
|---|
| 307 | for (i = fonts.begin(); i != fonts.end(); ++i) {
|
|---|
| 308 | XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
|
|---|
| 309 | if (!font_info) {
|
|---|
| 310 | G4cerr <<
|
|---|
| 311 | "G4OpenGLXViewer: XLoadQueryFont failed for font\n "
|
|---|
| 312 | << i->second
|
|---|
| 313 | << G4endl;
|
|---|
| 314 | continue;
|
|---|
| 315 | }
|
|---|
| 316 | G4int font_base = glGenLists(256);
|
|---|
| 317 | if (!font_base) {
|
|---|
| 318 | G4cerr << "G4OpenGLXViewer: out of display lists for fonts."
|
|---|
| 319 | << G4endl;
|
|---|
| 320 | continue;
|
|---|
| 321 | }
|
|---|
| 322 | G4int first = font_info->min_char_or_byte2;
|
|---|
| 323 | G4int last = font_info->max_char_or_byte2;
|
|---|
| 324 | glXUseXFont(font_info->fid, first, last-first+1,font_base+first);
|
|---|
| 325 | G4OpenGLFontBaseStore::AddFontBase(this,font_base,i->first,i->second);
|
|---|
| 326 | }
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
|
|---|
| 330 | G4VViewer (scene, -1),
|
|---|
| 331 | G4OpenGLViewer (scene),
|
|---|
| 332 | vi_immediate (0),
|
|---|
| 333 | vi_stored (0),
|
|---|
| 334 | vi (0),
|
|---|
| 335 | cmap (0)
|
|---|
| 336 | {
|
|---|
| 337 | GetXConnection ();
|
|---|
| 338 | if (fViewId < 0) return;
|
|---|
| 339 |
|
|---|
| 340 | // Try for a visual suitable for OpenGLImmediate..
|
|---|
| 341 | // first try for a single buffered RGB window
|
|---|
| 342 | if (!vi_single_buffer) {
|
|---|
| 343 | vi_single_buffer =
|
|---|
| 344 | glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
|
|---|
| 345 | }
|
|---|
| 346 | if (!vi_double_buffer) {
|
|---|
| 347 | vi_double_buffer =
|
|---|
| 348 | glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | if (vi_single_buffer || vi_double_buffer) {
|
|---|
| 352 | if (!vi_double_buffer) {
|
|---|
| 353 | G4cout <<
|
|---|
| 354 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
|
|---|
| 355 | "\n Working with a single buffer."
|
|---|
| 356 | << G4endl;
|
|---|
| 357 | }
|
|---|
| 358 | } else {
|
|---|
| 359 | if (!vi_single_buffer) {
|
|---|
| 360 | G4cout <<
|
|---|
| 361 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
|
|---|
| 362 | << G4endl;
|
|---|
| 363 | }
|
|---|
| 364 | if (!vi_double_buffer) {
|
|---|
| 365 | G4cout <<
|
|---|
| 366 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
|
|---|
| 367 | << G4endl;
|
|---|
| 368 | }
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | if (vi_single_buffer) {
|
|---|
| 372 | vi_immediate = vi_single_buffer;
|
|---|
| 373 | attributeList = snglBuf_RGBA;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | if (!vi_immediate){
|
|---|
| 377 | // next try for a double buffered RGB, but Draw to top buffer
|
|---|
| 378 | if (vi_double_buffer) {
|
|---|
| 379 | vi_immediate = vi_double_buffer;
|
|---|
| 380 | attributeList = dblBuf_RGBA;
|
|---|
| 381 | }
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | // Now try for a visual suitable for OpenGLStored...
|
|---|
| 385 | // Try for a double buffered RGB window
|
|---|
| 386 | if (vi_double_buffer) {
|
|---|
| 387 | vi_stored = vi_double_buffer;
|
|---|
| 388 | attributeList = dblBuf_RGBA;
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | if (!vi_immediate || !vi_stored) {
|
|---|
| 392 | G4cout <<
|
|---|
| 393 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
|
|---|
| 394 | << G4endl;
|
|---|
| 395 | fViewId = -1; // This flags an error.
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | // glClearColor (0., 0., 0., 0.);
|
|---|
| 399 | // glClearDepth (1.);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | G4OpenGLXViewer::~G4OpenGLXViewer () {
|
|---|
| 403 | if (fViewId >= 0) {
|
|---|
| 404 | //Close a window from here
|
|---|
| 405 | glXMakeCurrent (dpy, None, NULL);
|
|---|
| 406 | glXDestroyContext (dpy, cx);
|
|---|
| 407 | if (win) XDestroyWindow (dpy, win); // ...if already deleted in
|
|---|
| 408 | // sub-class G4OpenGLXmViewer.
|
|---|
| 409 | XFlush (dpy);
|
|---|
| 410 | }
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | void G4OpenGLXViewer::printEPS() {
|
|---|
| 414 |
|
|---|
| 415 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 416 | printf("G4OpenGLXViewer::print \n");
|
|---|
| 417 | #endif
|
|---|
| 418 |
|
|---|
| 419 | //using namespace std;
|
|---|
| 420 | //cout << "print_col_callback requested with file name: " << fPrintFilename << G4endl;
|
|---|
| 421 |
|
|---|
| 422 | if (fVectoredPs) {
|
|---|
| 423 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 424 | printf("G4OpenGLXViewer::print Vectored\n");
|
|---|
| 425 | #endif
|
|---|
| 426 | G4OpenGLViewer::printEPS();
|
|---|
| 427 | } else {
|
|---|
| 428 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 429 | printf("G4OpenGLXViewer::print non Vectored\n");
|
|---|
| 430 | #endif
|
|---|
| 431 | // G4StateManager* stateManager = G4StateManager::GetStateManager();
|
|---|
| 432 | // G4ApplicationState oldState = stateManager->GetCurrentState();
|
|---|
| 433 | // stateManager->SetNewState(G4State_Idle);
|
|---|
| 434 |
|
|---|
| 435 | // printEPS ();
|
|---|
| 436 |
|
|---|
| 437 | // stateManager->SetNewState(oldState);
|
|---|
| 438 |
|
|---|
| 439 | // // fPrintFilename = fPrintFilename+"-Gl2ps.ps";
|
|---|
| 440 | // return;
|
|---|
| 441 |
|
|---|
| 442 | // save context before
|
|---|
| 443 | tmp_cx = cx;
|
|---|
| 444 | XVisualInfo* pvi;
|
|---|
| 445 | GLXContext pcx = create_GL_print_context(pvi);
|
|---|
| 446 |
|
|---|
| 447 | if (!pcx) {
|
|---|
| 448 | G4cout << "Unable to create print context." << G4endl;
|
|---|
| 449 | return;
|
|---|
| 450 | }
|
|---|
| 451 |
|
|---|
| 452 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 453 | printf("G4OpenGLXViewer::print Create pixmap Size :%d %d\n",fWinSize_x, fWinSize_y);
|
|---|
| 454 | #endif
|
|---|
| 455 | cx=pcx;
|
|---|
| 456 |
|
|---|
| 457 | Pixmap pmap = XCreatePixmap (dpy,
|
|---|
| 458 | XRootWindow (dpy, pvi->screen),
|
|---|
| 459 | fWinSize_x, fWinSize_y,
|
|---|
| 460 | pvi->depth);
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 | if (!pmap) {
|
|---|
| 464 | G4cout << "Unable to create pixmap." << G4endl;
|
|---|
| 465 | return;
|
|---|
| 466 | }
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 | GLXPixmap glxpmap = glXCreateGLXPixmap (dpy,
|
|---|
| 470 | pvi,
|
|---|
| 471 | pmap);
|
|---|
| 472 |
|
|---|
| 473 | if (!glxpmap) {
|
|---|
| 474 | G4cout << "Unable to create glx pixmap." << G4endl;
|
|---|
| 475 | return;
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 |
|
|---|
| 479 | GLXDrawable tmp_win;
|
|---|
| 480 | tmp_win=win;
|
|---|
| 481 | win=glxpmap;
|
|---|
| 482 |
|
|---|
| 483 | // int winX=fWinSize_x;
|
|---|
| 484 | // int winY=fWinSize_y;
|
|---|
| 485 |
|
|---|
| 486 | //#define CHECK_MULTIPLE_PRINT 1
|
|---|
| 487 | #ifdef CHECK_MULTIPLE_PRINT
|
|---|
| 488 | for (int tstX = 3000-2;tstX <=3000+2;tstX++) {
|
|---|
| 489 | for (int tstY = 3000-2;tstY <=3000+2;tstY++) {
|
|---|
| 490 | fWinSize_y = tstY;
|
|---|
| 491 | fWinSize_x = tstX;
|
|---|
| 492 | std::string file = "G4OpenGL_XPixmap";
|
|---|
| 493 | file += tstX;
|
|---|
| 494 | file +="x";
|
|---|
| 495 | file +=tstY;
|
|---|
| 496 | file +=".eps";
|
|---|
| 497 | #else
|
|---|
| 498 | std::string file = "G4OpenGL_XPixmapTest.eps";
|
|---|
| 499 | #endif
|
|---|
| 500 |
|
|---|
| 501 | // clear the buffers and window.
|
|---|
| 502 | SetView();
|
|---|
| 503 | ClearView ();
|
|---|
| 504 |
|
|---|
| 505 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 506 | printf("G4OpenGLXViewer::print Call DrawView \n");
|
|---|
| 507 | #endif
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
|
|---|
| 511 | // Need to force redraw for SXm mode
|
|---|
| 512 | NeedKernelVisit ();
|
|---|
| 513 |
|
|---|
| 514 | // Need to change state to IDLE
|
|---|
| 515 | G4StateManager* stateManager = G4StateManager::GetStateManager();
|
|---|
| 516 | G4ApplicationState oldState = stateManager->GetCurrentState();
|
|---|
| 517 | stateManager->SetNewState(G4State_Idle);
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
|
|---|
| 521 | DrawView (); // Will make current glX
|
|---|
| 522 |
|
|---|
| 523 | // Restore state
|
|---|
| 524 | stateManager->SetNewState(oldState);
|
|---|
| 525 |
|
|---|
| 526 |
|
|---|
| 527 |
|
|---|
| 528 |
|
|---|
| 529 | generateEPSX (file.c_str(),
|
|---|
| 530 | fPrintColour,
|
|---|
| 531 | fWinSize_x, fWinSize_y);
|
|---|
| 532 |
|
|---|
| 533 | #ifdef CHECK_MULTIPLE_PRINT
|
|---|
| 534 | }
|
|---|
| 535 | }
|
|---|
| 536 | fWinSize_y = winY;
|
|---|
| 537 | fWinSize_x = winX;
|
|---|
| 538 | #endif
|
|---|
| 539 | win=tmp_win;
|
|---|
| 540 | cx=tmp_cx;
|
|---|
| 541 |
|
|---|
| 542 | // bool er2 = glXMakeCurrent (dpy,
|
|---|
| 543 | // win,
|
|---|
| 544 | // cx);
|
|---|
| 545 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 546 | printf("G4OpenGLXViewer::print Restored \n");
|
|---|
| 547 | #endif
|
|---|
| 548 | // printf("Error 1:%d 2:%d\n",er1,er2);
|
|---|
| 549 |
|
|---|
| 550 | // Free print context
|
|---|
| 551 | XFreePixmap(dpy,pmap);
|
|---|
| 552 | glXDestroyContext(dpy,pcx);
|
|---|
| 553 | glXDestroyGLXPixmap(dpy,glxpmap);
|
|---|
| 554 |
|
|---|
| 555 | // Restore view in display context
|
|---|
| 556 | SetView();
|
|---|
| 557 |
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 | GLXContext G4OpenGLXViewer::create_GL_print_context(XVisualInfo*& pvi) {
|
|---|
| 564 |
|
|---|
| 565 | pvi = glXChooseVisual (dpy,
|
|---|
| 566 | XDefaultScreen (dpy),
|
|---|
| 567 | snglBuf_RGBA);
|
|---|
| 568 |
|
|---|
| 569 | if (!pvi) {
|
|---|
| 570 | pvi = glXChooseVisual (dpy,
|
|---|
| 571 | XDefaultScreen (dpy),
|
|---|
| 572 | dblBuf_RGBA);
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| 575 | // GLXFBConfig *fbc;
|
|---|
| 576 | // XVisualInfo *vi;
|
|---|
| 577 | // Colormap cmap;
|
|---|
| 578 | // XSetWindowAttributes swa;
|
|---|
| 579 | // Window win;
|
|---|
| 580 | // GLXContext cx;
|
|---|
| 581 | // GLXWindow gwin;
|
|---|
| 582 | // XEvent event;
|
|---|
| 583 | // int nelements;
|
|---|
| 584 |
|
|---|
| 585 | // /* Find a FBConfig that uses RGBA. Note that no attribute list is */
|
|---|
| 586 | // /* needed since GLX_RGBA_BIT is a default attribute. */
|
|---|
| 587 | // fbc = glXChooseFBConfig(dpy,XRootWindow (dpy, pvi->screen), 0, &nelements);
|
|---|
| 588 | // vi = glXGetVisualFromFBConfig(dpy, fbc[0]);
|
|---|
| 589 |
|
|---|
| 590 | // /* Create a GLX context using the first FBConfig in the list. */
|
|---|
| 591 | // return glXCreateNewContext(dpy, fbc[0], GLX_RGBA_TYPE, 0, GL_FALSE);
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 | return glXCreateContext (dpy,
|
|---|
| 596 | pvi,
|
|---|
| 597 | 0,
|
|---|
| 598 | False);
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 | bool G4OpenGLXViewer::grabPixelsX (unsigned int width, unsigned int height,GLenum format,GLubyte* buffer) {
|
|---|
| 603 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 604 | GLint viewport[4];
|
|---|
| 605 | glGetIntegerv(GL_VIEWPORT, viewport);
|
|---|
| 606 | printf("G4OpenGLXViewer::grabPixelsX %d %d viewport %d %d %d %d\n",width,height,viewport[0],viewport[1],viewport[2],viewport[3]);
|
|---|
| 607 | #endif
|
|---|
| 608 |
|
|---|
| 609 | GLint swapbytes, lsbfirst, rowlength;
|
|---|
| 610 | GLint skiprows, skippixels, alignment;
|
|---|
| 611 |
|
|---|
| 612 | unsigned int lineSize = 0;
|
|---|
| 613 | if (format == GL_RGB){
|
|---|
| 614 | lineSize = width*3;
|
|---|
| 615 | } else {
|
|---|
| 616 | lineSize = width*1;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | GLubyte* lineBuffer = new GLubyte[lineSize];
|
|---|
| 620 | if (lineBuffer == NULL)
|
|---|
| 621 | return false;
|
|---|
| 622 | for (unsigned int y = 0; y<lineSize; y++) {
|
|---|
| 623 | lineBuffer[y] = 0;
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
|
|---|
| 627 | glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
|
|---|
| 628 | glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
|
|---|
| 629 |
|
|---|
| 630 | glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
|
|---|
| 631 | glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
|
|---|
| 632 | glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
|
|---|
| 633 |
|
|---|
| 634 | glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
|
|---|
| 635 | glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
|
|---|
| 636 | glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
|
|---|
| 637 |
|
|---|
| 638 | glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
|
|---|
| 639 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
|
|---|
| 640 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
|
|---|
| 641 |
|
|---|
| 642 | // FIXME L.Garnier 25 03 2009
|
|---|
| 643 | // Try to read at first a block of width by height
|
|---|
| 644 | // But on mac osX 10.5.6 on some window size their is
|
|---|
| 645 | // some stranges effects:
|
|---|
| 646 | // Pixels should came 3 values by 3 values but on border
|
|---|
| 647 | // it seems that with some window size there is a lack of
|
|---|
| 648 | // 1 or 2 values. For example a gray level picture like
|
|---|
| 649 | // (1,1,1) (2,2,2) (3,3,3)
|
|---|
| 650 | // (4,4,4) (5,5,5) (6,6,6)
|
|---|
| 651 | // (7,7,7) (8,8,8) (9,9,9)
|
|---|
| 652 | // appear as (with all buffer values init to 999) :
|
|---|
| 653 | // (1,1,1) (2,2,2) (3,3,3)
|
|---|
| 654 | // (999,999,4) (4,4,5) (5,5,6)
|
|---|
| 655 | // (6,6,999) (999,999,7) (7,7,8)
|
|---|
| 656 | // STRANGE EFFECT....
|
|---|
| 657 |
|
|---|
| 658 | // Then we read line by line...and NO problem !
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 | for (unsigned int i=0;i<height;i++) {
|
|---|
| 662 | glReadPixels (0, i, (GLsizei)width, 1, format, GL_UNSIGNED_BYTE, (GLvoid*) lineBuffer);
|
|---|
| 663 | glXWaitGL (); //Wait for effects of all previous OpenGL commands to
|
|---|
| 664 | // printf("\n%d ",i);
|
|---|
| 665 | for (unsigned int j=0;j<lineSize;j++) {
|
|---|
| 666 | buffer[j+i*lineSize] = lineBuffer[j];
|
|---|
| 667 | // printf("%d ",buffer[j]);
|
|---|
| 668 | }
|
|---|
| 669 | }
|
|---|
| 670 | // //be propagated before progressing.
|
|---|
| 671 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 672 | printf("G4OpenGLXViewer::GrabPixelX flush\n");
|
|---|
| 673 | #endif
|
|---|
| 674 | glFlush ();
|
|---|
| 675 |
|
|---|
| 676 | delete [] lineBuffer;
|
|---|
| 677 |
|
|---|
| 678 | glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
|
|---|
| 679 | glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
|
|---|
| 680 | glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
|
|---|
| 681 |
|
|---|
| 682 | glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
|
|---|
| 683 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
|
|---|
| 684 | glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
|
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 | return true;
|
|---|
| 688 | }
|
|---|
| 689 |
|
|---|
| 690 | int G4OpenGLXViewer::generateEPSX (const char* filnam,
|
|---|
| 691 | int inColour,
|
|---|
| 692 | unsigned int width,
|
|---|
| 693 | unsigned int height) {
|
|---|
| 694 |
|
|---|
| 695 | FILE* fp;
|
|---|
| 696 | GLubyte* pixels;
|
|---|
| 697 | GLubyte* curpix;
|
|---|
| 698 | int components;
|
|---|
| 699 |
|
|---|
| 700 | GLenum format;
|
|---|
| 701 | int size;
|
|---|
| 702 |
|
|---|
| 703 | if (inColour) {
|
|---|
| 704 | format = GL_RGB;
|
|---|
| 705 | size = width*height*3;
|
|---|
| 706 | } else {
|
|---|
| 707 | format = GL_LUMINANCE;
|
|---|
| 708 | size = width*height*1;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | pixels = new GLubyte[size];
|
|---|
| 712 | if (pixels == NULL)
|
|---|
| 713 | return NULL;
|
|---|
| 714 | for (int y = 0; y<size; y++) {
|
|---|
| 715 | pixels[y] = 0;
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 720 | printf("G4OpenGLXViewer::generateEPSX\n");
|
|---|
| 721 | #endif
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 | if (!grabPixelsX (width, height,format,pixels)) {
|
|---|
| 725 | return 1;
|
|---|
| 726 | }
|
|---|
| 727 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 728 | printf("--\n--\n--\n");
|
|---|
| 729 | #endif
|
|---|
| 730 |
|
|---|
| 731 | if (inColour) {
|
|---|
| 732 | components = 3;
|
|---|
| 733 | } else {
|
|---|
| 734 | components = 1;
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | fp = fopen (filnam, "w");
|
|---|
| 738 | if (fp == NULL) {
|
|---|
| 739 | return 2;
|
|---|
| 740 | }
|
|---|
| 741 |
|
|---|
| 742 | fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
|
|---|
| 743 | fprintf (fp, "%%%%Title: %s\n", filnam);
|
|---|
| 744 | fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
|
|---|
| 745 | fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
|
|---|
| 746 | fprintf (fp, "%%%%EndComments\n");
|
|---|
| 747 | fprintf (fp, "gsave\n");
|
|---|
| 748 | fprintf (fp, "/bwproc {\n");
|
|---|
| 749 | fprintf (fp, " rgbproc\n");
|
|---|
| 750 | fprintf (fp, " dup length 3 idiv string 0 3 0 \n");
|
|---|
| 751 | fprintf (fp, " 5 -1 roll {\n");
|
|---|
| 752 | fprintf (fp, " add 2 1 roll 1 sub dup 0 eq\n");
|
|---|
| 753 | fprintf (fp, " { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
|
|---|
| 754 | fprintf (fp, " 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
|
|---|
| 755 | fprintf (fp, " { 2 1 roll } ifelse\n");
|
|---|
| 756 | fprintf (fp, " }forall\n");
|
|---|
| 757 | fprintf (fp, " pop pop pop\n");
|
|---|
| 758 | fprintf (fp, "} def\n");
|
|---|
| 759 | fprintf (fp, "systemdict /colorimage known not {\n");
|
|---|
| 760 | fprintf (fp, " /colorimage {\n");
|
|---|
| 761 | fprintf (fp, " pop\n");
|
|---|
| 762 | fprintf (fp, " pop\n");
|
|---|
| 763 | fprintf (fp, " /rgbproc exch def\n");
|
|---|
| 764 | fprintf (fp, " { bwproc } image\n");
|
|---|
| 765 | fprintf (fp, " } def\n");
|
|---|
| 766 | fprintf (fp, "} if\n");
|
|---|
| 767 | fprintf (fp, "/picstr %d string def\n", width * components);
|
|---|
| 768 | fprintf (fp, "%d %d scale\n", width, height);
|
|---|
| 769 | fprintf (fp, "%d %d %d\n", width, height, 8);
|
|---|
| 770 | fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
|
|---|
| 771 | fprintf (fp, "{currentfile picstr readhexstring pop}\n");
|
|---|
| 772 | fprintf (fp, "false %d\n", components);
|
|---|
| 773 | fprintf (fp, "colorimage\n");
|
|---|
| 774 |
|
|---|
| 775 | curpix = (GLubyte*) pixels;
|
|---|
| 776 | unsigned int pos = 0;
|
|---|
| 777 | for (unsigned int i = width*height*components; i>0; i--) {
|
|---|
| 778 | fprintf (fp, "%02hx ", *(curpix++));
|
|---|
| 779 | if (++pos >= width) {
|
|---|
| 780 | // if (++pos >= 32) {
|
|---|
| 781 | fprintf (fp, "\n");
|
|---|
| 782 | pos = 0;
|
|---|
| 783 | }
|
|---|
| 784 | }
|
|---|
| 785 | if (pos)
|
|---|
| 786 | fprintf (fp, "\n");
|
|---|
| 787 |
|
|---|
| 788 | fprintf (fp, "grestore\n");
|
|---|
| 789 | fprintf (fp, "showpage\n");
|
|---|
| 790 | delete pixels;
|
|---|
| 791 | fclose (fp);
|
|---|
| 792 |
|
|---|
| 793 | #ifdef G4DEBUG_VIS_OGL
|
|---|
| 794 | printf("G4OpenGLXViewer::generateEPSX END\n");
|
|---|
| 795 | #endif
|
|---|
| 796 |
|
|---|
| 797 | return 0;
|
|---|
| 798 | }
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 | #endif
|
|---|