| 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.43 2009/01/13 09:47:05 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 |
|
|---|
| 39 | #include "G4OpenGLFontBaseStore.hh"
|
|---|
| 40 |
|
|---|
| 41 | #include <sstream>
|
|---|
| 42 |
|
|---|
| 43 | #include "G4VisExtent.hh"
|
|---|
| 44 | #include "G4LogicalVolume.hh"
|
|---|
| 45 | #include "G4VSolid.hh"
|
|---|
| 46 | #include "G4Point3D.hh"
|
|---|
| 47 | #include "G4Normal3D.hh"
|
|---|
| 48 |
|
|---|
| 49 | #include <X11/Xatom.h>
|
|---|
| 50 | #include <X11/Xutil.h>
|
|---|
| 51 |
|
|---|
| 52 | #include <assert.h>
|
|---|
| 53 |
|
|---|
| 54 | int G4OpenGLXViewer::snglBuf_RGBA[12] =
|
|---|
| 55 | { GLX_RGBA,
|
|---|
| 56 | GLX_RED_SIZE, 1,
|
|---|
| 57 | GLX_GREEN_SIZE, 1,
|
|---|
| 58 | GLX_BLUE_SIZE, 1,
|
|---|
| 59 | GLX_DEPTH_SIZE, 1,
|
|---|
| 60 | GLX_STENCIL_SIZE, 1,
|
|---|
| 61 | None };
|
|---|
| 62 |
|
|---|
| 63 | int G4OpenGLXViewer::dblBuf_RGBA[13] =
|
|---|
| 64 | { GLX_RGBA,
|
|---|
| 65 | GLX_RED_SIZE, 1,
|
|---|
| 66 | GLX_GREEN_SIZE, 1,
|
|---|
| 67 | GLX_BLUE_SIZE, 1,
|
|---|
| 68 | GLX_DOUBLEBUFFER,
|
|---|
| 69 | GLX_DEPTH_SIZE, 1,
|
|---|
| 70 | GLX_STENCIL_SIZE, 1,
|
|---|
| 71 | None };
|
|---|
| 72 |
|
|---|
| 73 | #define NewString(str) \
|
|---|
| 74 | ((str) != NULL ? (strcpy((char*)malloc((unsigned)strlen(str) + 1), str)) : (char*)NULL)
|
|---|
| 75 |
|
|---|
| 76 | #define USE_DEFAULT_COLORMAP 1
|
|---|
| 77 | #define USE_STANDARD_COLORMAP 0
|
|---|
| 78 |
|
|---|
| 79 | XVisualInfo* G4OpenGLXViewer::vi_single_buffer = 0;
|
|---|
| 80 | XVisualInfo* G4OpenGLXViewer::vi_double_buffer = 0;
|
|---|
| 81 |
|
|---|
| 82 | extern "C" {
|
|---|
| 83 | static Bool G4OpenGLXViewerWaitForNotify (Display*, XEvent* e, char* arg) {
|
|---|
| 84 | #ifdef G4DEBUG
|
|---|
| 85 | printf("G4OpenGLXViewerWaitForNotify\n");
|
|---|
| 86 | #endif
|
|---|
| 87 | return (e->type == MapNotify) && (e->xmap.window == (Window) arg);
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void G4OpenGLXViewer::SetView () {
|
|---|
| 92 | glXMakeCurrent (dpy, win, cx);
|
|---|
| 93 | G4OpenGLViewer::SetView ();
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void G4OpenGLXViewer::ShowView () {
|
|---|
| 97 | glXWaitGL (); //Wait for effects of all previous OpenGL commands to
|
|---|
| 98 | //be propagated before progressing.
|
|---|
| 99 | glFlush ();
|
|---|
| 100 |
|
|---|
| 101 | if (fVP.IsPicking()) {
|
|---|
| 102 | G4cout <<
|
|---|
| 103 | "Window activated for picking (left-mouse), exit (middle-mouse)."
|
|---|
| 104 | << G4endl;
|
|---|
| 105 | while (true) {
|
|---|
| 106 | if (XPending(dpy)) {
|
|---|
| 107 | XNextEvent(dpy, &event);
|
|---|
| 108 | if (event.type == ButtonPress && event.xbutton.button == 1) {
|
|---|
| 109 | Pick(event.xbutton.x, event.xbutton.y);
|
|---|
| 110 | }
|
|---|
| 111 | else if (event.type == ButtonPress && event.xbutton.button == 2) break;
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|
| 114 | }
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | void G4OpenGLXViewer::GetXConnection () {
|
|---|
| 118 | // get a connection.
|
|---|
| 119 | dpy = XOpenDisplay (0);
|
|---|
| 120 | if (!dpy) {
|
|---|
| 121 | fViewId = -1; // This flags an error.
|
|---|
| 122 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't open display." << G4endl;
|
|---|
| 123 | return;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | // make sure OpenGL is supported and installed properly.
|
|---|
| 127 | if (!glXQueryExtension (dpy, &errorBase, &eventBase)) {
|
|---|
| 128 | fViewId = -1; // This flags an error.
|
|---|
| 129 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer X Server has no GLX extension."
|
|---|
| 130 | << G4endl;
|
|---|
| 131 | return;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | void G4OpenGLXViewer::CreateGLXContext (XVisualInfo* v) {
|
|---|
| 137 |
|
|---|
| 138 | vi = v;
|
|---|
| 139 | // get window's attributes
|
|---|
| 140 | if (!XGetWindowAttributes(dpy, XRootWindow (dpy, vi -> screen), &xwa)) {
|
|---|
| 141 | fViewId = -1; // This flags an error.
|
|---|
| 142 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't return window attributes"
|
|---|
| 143 | << G4endl;
|
|---|
| 144 | return;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | // create a GLX context
|
|---|
| 148 | cx = glXCreateContext (dpy, vi, 0, true);
|
|---|
| 149 | if (!cx) {
|
|---|
| 150 | fViewId = -1; // This flags an error.
|
|---|
| 151 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer couldn't create context."
|
|---|
| 152 | << G4endl;
|
|---|
| 153 | return;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | // New stab at getting a colormap
|
|---|
| 157 |
|
|---|
| 158 | Status status;
|
|---|
| 159 | XStandardColormap *standardCmaps = XAllocStandardColormap ();
|
|---|
| 160 | int i, numCmaps;
|
|---|
| 161 |
|
|---|
| 162 | status = XmuLookupStandardColormap (dpy,
|
|---|
| 163 | vi -> screen,
|
|---|
| 164 | vi -> visualid,
|
|---|
| 165 | vi -> depth,
|
|---|
| 166 | XA_RGB_DEFAULT_MAP,
|
|---|
| 167 | False,
|
|---|
| 168 | True);
|
|---|
| 169 |
|
|---|
| 170 | if (status == 1) {
|
|---|
| 171 | cmap = 0;
|
|---|
| 172 | status = XGetRGBColormaps (dpy,
|
|---|
| 173 | XRootWindow (dpy, vi -> screen),
|
|---|
| 174 | &standardCmaps,
|
|---|
| 175 | &numCmaps,
|
|---|
| 176 | XA_RGB_DEFAULT_MAP);
|
|---|
| 177 | if (status == 1)
|
|---|
| 178 | for (i = 0; i < numCmaps; i++) {
|
|---|
| 179 | if (standardCmaps[i].visualid == vi -> visualid) {
|
|---|
| 180 | cmap = standardCmaps[i].colormap;
|
|---|
| 181 | XFree (standardCmaps);
|
|---|
| 182 | break;
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | if (!cmap) {
|
|---|
| 186 | fViewId = -1; // This flags an error.
|
|---|
| 187 | G4cerr <<
|
|---|
| 188 | "G4OpenGLViewer::G4OpenGLViewer failed to allocate a standard colormap."
|
|---|
| 189 | << G4endl;
|
|---|
| 190 | return;
|
|---|
| 191 | }
|
|---|
| 192 | G4cout << "Got standard cmap" << G4endl;
|
|---|
| 193 | } else {
|
|---|
| 194 | cmap = XCreateColormap (dpy,
|
|---|
| 195 | XRootWindow(dpy, vi -> screen),
|
|---|
| 196 | vi -> visual,
|
|---|
| 197 | AllocNone);
|
|---|
| 198 | G4cout << "Created own cmap" << G4endl;
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | if (!cmap) {
|
|---|
| 202 | fViewId = -1; // This flags an error.
|
|---|
| 203 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to allocate a Colormap."
|
|---|
| 204 | << G4endl;
|
|---|
| 205 | return;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void G4OpenGLXViewer::CreateMainWindow () {
|
|---|
| 211 |
|
|---|
| 212 | // create a window
|
|---|
| 213 | swa.colormap = cmap;
|
|---|
| 214 | swa.border_pixel = 0;
|
|---|
| 215 | swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
|
|---|
| 216 | swa.backing_store = WhenMapped;
|
|---|
| 217 |
|
|---|
| 218 | // Window size and position...
|
|---|
| 219 | size_hints = XAllocSizeHints();
|
|---|
| 220 |
|
|---|
| 221 | fWinSize_x = fVP.GetWindowSizeHintX();
|
|---|
| 222 | fWinSize_y = fVP.GetWindowSizeHintY();
|
|---|
| 223 | G4int x_origin = fVP.GetWindowAbsoluteLocationHintX(DisplayWidth(dpy, vi -> screen));
|
|---|
| 224 | G4int y_origin = fVP.GetWindowAbsoluteLocationHintY(DisplayHeight(dpy, vi -> screen));
|
|---|
| 225 |
|
|---|
| 226 | size_hints->base_width = fWinSize_x;
|
|---|
| 227 | size_hints->base_height = fWinSize_y;
|
|---|
| 228 | size_hints->x = x_origin;
|
|---|
| 229 | size_hints->y = y_origin;
|
|---|
| 230 | size_hints->flags |= PSize | PPosition;
|
|---|
| 231 | #ifdef G4DEBUG
|
|---|
| 232 | printf("G4OpenGLXViewer::CreateMainWindow CreateWindow Size:W:%d H:%d X:%d Y:%d \n",fWinSize_x,fWinSize_y,x_origin,y_origin);
|
|---|
| 233 | #endif
|
|---|
| 234 |
|
|---|
| 235 | G4cout << "Window name: " << fName << G4endl;
|
|---|
| 236 | strncpy (charViewName, fName, 100);
|
|---|
| 237 | char *window_name = charViewName;
|
|---|
| 238 | char *icon_name = charViewName;
|
|---|
| 239 | //char tmpatom[] = "XA_WM_NORMAL_HINTS";
|
|---|
| 240 | wm_hints = XAllocWMHints();
|
|---|
| 241 | class_hints = XAllocClassHint();
|
|---|
| 242 |
|
|---|
| 243 | XStringListToTextProperty (&window_name, 1, &windowName);
|
|---|
| 244 | XStringListToTextProperty (&icon_name, 1, &iconName);
|
|---|
| 245 |
|
|---|
| 246 | wm_hints -> initial_state = NormalState;
|
|---|
| 247 | wm_hints -> input = True;
|
|---|
| 248 | wm_hints -> icon_pixmap = icon_pixmap;
|
|---|
| 249 | wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
|
|---|
| 250 |
|
|---|
| 251 | class_hints -> res_name = NewString("G4OpenGL");
|
|---|
| 252 | class_hints -> res_class = NewString("G4OpenGL");
|
|---|
| 253 |
|
|---|
| 254 | win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
|
|---|
| 255 | y_origin, fWinSize_x, fWinSize_y, 0, vi -> depth,
|
|---|
| 256 | InputOutput, vi -> visual,
|
|---|
| 257 | CWBorderPixel | CWColormap |
|
|---|
| 258 | CWEventMask | CWBackingStore,
|
|---|
| 259 | &swa);
|
|---|
| 260 |
|
|---|
| 261 | XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
|
|---|
| 262 | size_hints, wm_hints, class_hints);
|
|---|
| 263 |
|
|---|
| 264 | // request X to Draw window on screen.
|
|---|
| 265 | XMapWindow (dpy, win);
|
|---|
| 266 |
|
|---|
| 267 | // Wait for window to appear (wait for an "expose" event).
|
|---|
| 268 | XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
|
|---|
| 269 |
|
|---|
| 270 | // connect the context to a window
|
|---|
| 271 | Bool success = glXMakeCurrent (dpy, win, cx);
|
|---|
| 272 | if (!success) {
|
|---|
| 273 | fViewId = -1; // This flags an error.
|
|---|
| 274 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
|
|---|
| 275 | << G4endl;
|
|---|
| 276 | GLint error = GL_NO_ERROR;
|
|---|
| 277 | while ((error = glGetError()) != GL_NO_ERROR) {
|
|---|
| 278 | G4cout << "GL Error: " << gluErrorString(error) << G4endl;
|
|---|
| 279 | }
|
|---|
| 280 | return;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | void G4OpenGLXViewer::CreateFontLists () {
|
|---|
| 286 |
|
|---|
| 287 | std::map<G4double,G4String> fonts; // G4VMarker screen size and font name.
|
|---|
| 288 | fonts[10.] = "-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
|
|---|
| 289 | fonts[11.] = "-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
|
|---|
| 290 | fonts[12.] = "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
|
|---|
| 291 | fonts[13.] = "fixed";
|
|---|
| 292 | fonts[14.] = "-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
|
|---|
| 293 | fonts[17.] = "-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
|
|---|
| 294 | fonts[18.] = "-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
|
|---|
| 295 | fonts[20.] = "-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
|
|---|
| 296 | fonts[24.] = "-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
|
|---|
| 297 | fonts[25.] = "-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
|
|---|
| 298 | fonts[34.] = "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
|
|---|
| 299 | std::map<G4double,G4String>::const_iterator i;
|
|---|
| 300 | for (i = fonts.begin(); i != fonts.end(); ++i) {
|
|---|
| 301 | XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
|
|---|
| 302 | if (!font_info) {
|
|---|
| 303 | G4cerr <<
|
|---|
| 304 | "G4OpenGLXViewer: XLoadQueryFont failed for font\n "
|
|---|
| 305 | << i->second
|
|---|
| 306 | << G4endl;
|
|---|
| 307 | continue;
|
|---|
| 308 | }
|
|---|
| 309 | G4int font_base = glGenLists(256);
|
|---|
| 310 | if (!font_base) {
|
|---|
| 311 | G4cerr << "G4OpenGLXViewer: out of display lists for fonts."
|
|---|
| 312 | << G4endl;
|
|---|
| 313 | continue;
|
|---|
| 314 | }
|
|---|
| 315 | G4int first = font_info->min_char_or_byte2;
|
|---|
| 316 | G4int last = font_info->max_char_or_byte2;
|
|---|
| 317 | glXUseXFont(font_info->fid, first, last-first+1,font_base+first);
|
|---|
| 318 | G4OpenGLFontBaseStore::AddFontBase(this,font_base,i->first,i->second);
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
|
|---|
| 323 | G4VViewer (scene, -1),
|
|---|
| 324 | G4OpenGLViewer (scene),
|
|---|
| 325 | vi_immediate (0),
|
|---|
| 326 | vi_stored (0),
|
|---|
| 327 | vi (0),
|
|---|
| 328 | cmap (0)
|
|---|
| 329 | {
|
|---|
| 330 | GetXConnection ();
|
|---|
| 331 | if (fViewId < 0) return;
|
|---|
| 332 |
|
|---|
| 333 | // Try for a visual suitable for OpenGLImmediate..
|
|---|
| 334 | // first try for a single buffered RGB window
|
|---|
| 335 | if (!vi_single_buffer) {
|
|---|
| 336 | vi_single_buffer =
|
|---|
| 337 | glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
|
|---|
| 338 | }
|
|---|
| 339 | if (!vi_double_buffer) {
|
|---|
| 340 | vi_double_buffer =
|
|---|
| 341 | glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | if (vi_single_buffer || vi_double_buffer) {
|
|---|
| 345 | if (!vi_double_buffer) {
|
|---|
| 346 | G4cout <<
|
|---|
| 347 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
|
|---|
| 348 | "\n Working with a single buffer."
|
|---|
| 349 | << G4endl;
|
|---|
| 350 | }
|
|---|
| 351 | } else {
|
|---|
| 352 | if (!vi_single_buffer) {
|
|---|
| 353 | G4cout <<
|
|---|
| 354 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
|
|---|
| 355 | << G4endl;
|
|---|
| 356 | }
|
|---|
| 357 | if (!vi_double_buffer) {
|
|---|
| 358 | G4cout <<
|
|---|
| 359 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
|
|---|
| 360 | << G4endl;
|
|---|
| 361 | }
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | if (vi_single_buffer) {
|
|---|
| 365 | vi_immediate = vi_single_buffer;
|
|---|
| 366 | attributeList = snglBuf_RGBA;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | if (!vi_immediate){
|
|---|
| 370 | // next try for a double buffered RGB, but Draw to top buffer
|
|---|
| 371 | if (vi_double_buffer) {
|
|---|
| 372 | vi_immediate = vi_double_buffer;
|
|---|
| 373 | attributeList = dblBuf_RGBA;
|
|---|
| 374 | }
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | // Now try for a visual suitable for OpenGLStored...
|
|---|
| 378 | // Try for a double buffered RGB window
|
|---|
| 379 | if (vi_double_buffer) {
|
|---|
| 380 | vi_stored = vi_double_buffer;
|
|---|
| 381 | attributeList = dblBuf_RGBA;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | if (!vi_immediate || !vi_stored) {
|
|---|
| 385 | G4cout <<
|
|---|
| 386 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
|
|---|
| 387 | << G4endl;
|
|---|
| 388 | fViewId = -1; // This flags an error.
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | // glClearColor (0., 0., 0., 0.);
|
|---|
| 392 | // glClearDepth (1.);
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | G4OpenGLXViewer::~G4OpenGLXViewer () {
|
|---|
| 396 | if (fViewId >= 0) {
|
|---|
| 397 | //Close a window from here
|
|---|
| 398 | glXMakeCurrent (dpy, None, NULL);
|
|---|
| 399 | glXDestroyContext (dpy, cx);
|
|---|
| 400 | if (win) XDestroyWindow (dpy, win); // ...if already deleted in
|
|---|
| 401 | // sub-class G4OpenGLXmViewer.
|
|---|
| 402 | XFlush (dpy);
|
|---|
| 403 | }
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | void G4OpenGLXViewer::print() {
|
|---|
| 407 |
|
|---|
| 408 | //using namespace std;
|
|---|
| 409 | //cout << "print_col_callback requested with file name: " << print_string << G4endl;
|
|---|
| 410 |
|
|---|
| 411 | if (vectored_ps) {
|
|---|
| 412 |
|
|---|
| 413 | G4OpenGLViewer::print();
|
|---|
| 414 |
|
|---|
| 415 | } else {
|
|---|
| 416 |
|
|---|
| 417 | XVisualInfo* pvi;
|
|---|
| 418 | GLXContext pcx = create_GL_print_context(pvi);
|
|---|
| 419 |
|
|---|
| 420 | if (!pcx) {
|
|---|
| 421 | G4cout << "Unable to create print context." << G4endl;
|
|---|
| 422 | return;
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | GLXContext tmp_cx;
|
|---|
| 426 | tmp_cx = cx;
|
|---|
| 427 | cx=pcx;
|
|---|
| 428 |
|
|---|
| 429 | Pixmap pmap = XCreatePixmap (dpy,
|
|---|
| 430 | XRootWindow (dpy, pvi->screen),
|
|---|
| 431 | fWinSize_x, fWinSize_y,
|
|---|
| 432 | pvi->depth);
|
|---|
| 433 |
|
|---|
| 434 | GLXPixmap glxpmap = glXCreateGLXPixmap (dpy,
|
|---|
| 435 | pvi,
|
|---|
| 436 | pmap);
|
|---|
| 437 |
|
|---|
| 438 | GLXDrawable tmp_win;
|
|---|
| 439 | tmp_win=win;
|
|---|
| 440 | win=glxpmap;
|
|---|
| 441 |
|
|---|
| 442 | glXMakeCurrent (dpy,
|
|---|
| 443 | win,
|
|---|
| 444 | cx);
|
|---|
| 445 |
|
|---|
| 446 | // glViewport (0, 0, fWinSize_x, fWinSize_y);
|
|---|
| 447 |
|
|---|
| 448 | ClearView ();
|
|---|
| 449 | SetView ();
|
|---|
| 450 | DrawView ();
|
|---|
| 451 |
|
|---|
| 452 | generateEPS (print_string,
|
|---|
| 453 | print_colour,
|
|---|
| 454 | fWinSize_x, fWinSize_y);
|
|---|
| 455 |
|
|---|
| 456 | win=tmp_win;
|
|---|
| 457 | cx=tmp_cx;
|
|---|
| 458 |
|
|---|
| 459 | glXMakeCurrent (dpy,
|
|---|
| 460 | win,
|
|---|
| 461 | cx);
|
|---|
| 462 |
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | GLubyte* G4OpenGLXViewer::grabPixels (int inColor, unsigned int width, unsigned int height) {
|
|---|
| 468 |
|
|---|
| 469 | GLubyte* buffer;
|
|---|
| 470 | GLint swapbytes, lsbfirst, rowlength;
|
|---|
| 471 | GLint skiprows, skippixels, alignment;
|
|---|
| 472 | GLenum format;
|
|---|
| 473 | int size;
|
|---|
| 474 |
|
|---|
| 475 | if (inColor) {
|
|---|
| 476 | format = GL_RGB;
|
|---|
| 477 | size = width*height*3;
|
|---|
| 478 | } else {
|
|---|
| 479 | format = GL_LUMINANCE;
|
|---|
| 480 | size = width*height*1;
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | buffer = new GLubyte[size];
|
|---|
| 484 | if (buffer == NULL)
|
|---|
| 485 | return NULL;
|
|---|
| 486 |
|
|---|
| 487 | glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
|
|---|
| 488 | glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
|
|---|
| 489 | glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
|
|---|
| 490 |
|
|---|
| 491 | glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
|
|---|
| 492 | glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
|
|---|
| 493 | glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
|
|---|
| 494 |
|
|---|
| 495 | glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
|
|---|
| 496 | glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
|
|---|
| 497 | glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
|
|---|
| 498 |
|
|---|
| 499 | glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
|
|---|
| 500 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
|
|---|
| 501 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
|
|---|
| 502 |
|
|---|
| 503 | glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
|
|---|
| 504 |
|
|---|
| 505 | glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
|
|---|
| 506 | glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
|
|---|
| 507 | glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
|
|---|
| 508 |
|
|---|
| 509 | glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
|
|---|
| 510 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
|
|---|
| 511 | glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
|
|---|
| 512 |
|
|---|
| 513 | return buffer;
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | int G4OpenGLXViewer::generateEPS (char* filnam,
|
|---|
| 517 | int inColour,
|
|---|
| 518 | unsigned int width,
|
|---|
| 519 | unsigned int height) {
|
|---|
| 520 |
|
|---|
| 521 | FILE* fp;
|
|---|
| 522 | GLubyte* pixels;
|
|---|
| 523 | GLubyte* curpix;
|
|---|
| 524 | int components, pos, i;
|
|---|
| 525 |
|
|---|
| 526 | pixels = grabPixels (inColour, width, height);
|
|---|
| 527 |
|
|---|
| 528 | if (pixels == NULL)
|
|---|
| 529 | return 1;
|
|---|
| 530 | if (inColour) {
|
|---|
| 531 | components = 3;
|
|---|
| 532 | } else {
|
|---|
| 533 | components = 1;
|
|---|
| 534 | }
|
|---|
| 535 |
|
|---|
| 536 | fp = fopen (filnam, "w");
|
|---|
| 537 | if (fp == NULL) {
|
|---|
| 538 | return 2;
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
|
|---|
| 542 | fprintf (fp, "%%%%Title: %s\n", filnam);
|
|---|
| 543 | fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
|
|---|
| 544 | fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
|
|---|
| 545 | fprintf (fp, "%%%%EndComments\n");
|
|---|
| 546 | fprintf (fp, "gsave\n");
|
|---|
| 547 | fprintf (fp, "/bwproc {\n");
|
|---|
| 548 | fprintf (fp, " rgbproc\n");
|
|---|
| 549 | fprintf (fp, " dup length 3 idiv string 0 3 0 \n");
|
|---|
| 550 | fprintf (fp, " 5 -1 roll {\n");
|
|---|
| 551 | fprintf (fp, " add 2 1 roll 1 sub dup 0 eq\n");
|
|---|
| 552 | fprintf (fp, " { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
|
|---|
| 553 | fprintf (fp, " 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
|
|---|
| 554 | fprintf (fp, " { 2 1 roll } ifelse\n");
|
|---|
| 555 | fprintf (fp, " }forall\n");
|
|---|
| 556 | fprintf (fp, " pop pop pop\n");
|
|---|
| 557 | fprintf (fp, "} def\n");
|
|---|
| 558 | fprintf (fp, "systemdict /colorimage known not {\n");
|
|---|
| 559 | fprintf (fp, " /colorimage {\n");
|
|---|
| 560 | fprintf (fp, " pop\n");
|
|---|
| 561 | fprintf (fp, " pop\n");
|
|---|
| 562 | fprintf (fp, " /rgbproc exch def\n");
|
|---|
| 563 | fprintf (fp, " { bwproc } image\n");
|
|---|
| 564 | fprintf (fp, " } def\n");
|
|---|
| 565 | fprintf (fp, "} if\n");
|
|---|
| 566 | fprintf (fp, "/picstr %d string def\n", width * components);
|
|---|
| 567 | fprintf (fp, "%d %d scale\n", width, height);
|
|---|
| 568 | fprintf (fp, "%d %d %d\n", width, height, 8);
|
|---|
| 569 | fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
|
|---|
| 570 | fprintf (fp, "{currentfile picstr readhexstring pop}\n");
|
|---|
| 571 | fprintf (fp, "false %d\n", components);
|
|---|
| 572 | fprintf (fp, "colorimage\n");
|
|---|
| 573 |
|
|---|
| 574 | curpix = (GLubyte*) pixels;
|
|---|
| 575 | pos = 0;
|
|---|
| 576 | for (i = width*height*components; i>0; i--) {
|
|---|
| 577 | fprintf (fp, "%02hx ", *(curpix++));
|
|---|
| 578 | if (++pos >= 32) {
|
|---|
| 579 | fprintf (fp, "\n");
|
|---|
| 580 | pos = 0;
|
|---|
| 581 | }
|
|---|
| 582 | }
|
|---|
| 583 | if (pos)
|
|---|
| 584 | fprintf (fp, "\n");
|
|---|
| 585 |
|
|---|
| 586 | fprintf (fp, "grestore\n");
|
|---|
| 587 | fprintf (fp, "showpage\n");
|
|---|
| 588 | delete pixels;
|
|---|
| 589 | fclose (fp);
|
|---|
| 590 | return 0;
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | GLXContext G4OpenGLXViewer::create_GL_print_context(XVisualInfo*& pvi) {
|
|---|
| 594 |
|
|---|
| 595 | pvi = glXChooseVisual (dpy,
|
|---|
| 596 | XDefaultScreen (dpy),
|
|---|
| 597 | snglBuf_RGBA);
|
|---|
| 598 |
|
|---|
| 599 | if (!pvi) {
|
|---|
| 600 | pvi = glXChooseVisual (dpy,
|
|---|
| 601 | XDefaultScreen (dpy),
|
|---|
| 602 | dblBuf_RGBA);
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | return glXCreateContext (dpy,
|
|---|
| 606 | pvi,
|
|---|
| 607 | NULL,
|
|---|
| 608 | False);
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | #endif
|
|---|