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