| 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.42 2007/05/25 10:47:17 allison Exp $
|
|---|
| 28 | // GEANT4 tag $Name: HEAD $
|
|---|
| 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 | 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 ();
|
|---|
| 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 | }
|
|---|
| 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) {
|
|---|
| 168 | cmap = 0;
|
|---|
| 169 | status = XGetRGBColormaps (dpy,
|
|---|
| 170 | XRootWindow (dpy, vi -> screen),
|
|---|
| 171 | &standardCmaps,
|
|---|
| 172 | &numCmaps,
|
|---|
| 173 | XA_RGB_DEFAULT_MAP);
|
|---|
| 174 | if (status == 1)
|
|---|
| 175 | for (i = 0; i < numCmaps; i++) {
|
|---|
| 176 | if (standardCmaps[i].visualid == vi -> visualid) {
|
|---|
| 177 | cmap = standardCmaps[i].colormap;
|
|---|
| 178 | XFree (standardCmaps);
|
|---|
| 179 | break;
|
|---|
| 180 | }
|
|---|
| 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 | }
|
|---|
| 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 | G4int x_origin = 0;
|
|---|
| 217 | G4int y_origin = 0;
|
|---|
| 218 | size_hints = XAllocSizeHints();
|
|---|
| 219 |
|
|---|
| 220 | Window root_return;
|
|---|
| 221 | int x_return, y_return;
|
|---|
| 222 | unsigned int width_return, height_return;
|
|---|
| 223 | unsigned int border_width_return;
|
|---|
| 224 | unsigned int depth_return;
|
|---|
| 225 |
|
|---|
| 226 | // get the parent window's geometry
|
|---|
| 227 | XGetGeometry(dpy, XRootWindow (dpy, vi -> screen), &root_return, &x_return, &y_return,
|
|---|
| 228 | &width_return, &height_return, &border_width_return,
|
|---|
| 229 | &depth_return);
|
|---|
| 230 |
|
|---|
| 231 | fWinSize_x = fVP.GetWindowSizeHintX();
|
|---|
| 232 | fWinSize_y = fVP.GetWindowSizeHintY();
|
|---|
| 233 | x_origin = fVP.GetWindowAbsoluteLocationHintX(width_return);
|
|---|
| 234 | y_origin = fVP.GetWindowAbsoluteLocationHintY(height_return);
|
|---|
| 235 | size_hints->base_width = fWinSize_x;
|
|---|
| 236 | size_hints->base_height = fWinSize_y;
|
|---|
| 237 | size_hints->x = x_origin;
|
|---|
| 238 | size_hints->y = y_origin;
|
|---|
| 239 | size_hints->flags |= PSize | PPosition;
|
|---|
| 240 | #ifdef G4DEBUG
|
|---|
| 241 | printf("G4OpenGLXViewer::CreateMainWindow CreateWindow Size:W:%d H:%d X:%d Y:%d \n",fWinSize_x,fWinSize_y,x_origin,y_origin);
|
|---|
| 242 | #endif
|
|---|
| 243 |
|
|---|
| 244 | G4cout << "Window name: " << fName << G4endl;
|
|---|
| 245 | strncpy (charViewName, fName, 100);
|
|---|
| 246 | char *window_name = charViewName;
|
|---|
| 247 | char *icon_name = charViewName;
|
|---|
| 248 | //char tmpatom[] = "XA_WM_NORMAL_HINTS";
|
|---|
| 249 | wm_hints = XAllocWMHints();
|
|---|
| 250 | class_hints = XAllocClassHint();
|
|---|
| 251 |
|
|---|
| 252 | XStringListToTextProperty (&window_name, 1, &windowName);
|
|---|
| 253 | XStringListToTextProperty (&icon_name, 1, &iconName);
|
|---|
| 254 |
|
|---|
| 255 | wm_hints -> initial_state = NormalState;
|
|---|
| 256 | wm_hints -> input = True;
|
|---|
| 257 | wm_hints -> icon_pixmap = icon_pixmap;
|
|---|
| 258 | wm_hints -> flags = StateHint | IconPixmapHint | InputHint;
|
|---|
| 259 |
|
|---|
| 260 | class_hints -> res_name = NewString("G4OpenGL");
|
|---|
| 261 | class_hints -> res_class = NewString("G4OpenGL");
|
|---|
| 262 |
|
|---|
| 263 | win = XCreateWindow (dpy, XRootWindow (dpy, vi -> screen), x_origin,
|
|---|
| 264 | y_origin, fWinSize_x, fWinSize_y, 0, vi -> depth,
|
|---|
| 265 | InputOutput, vi -> visual,
|
|---|
| 266 | CWBorderPixel | CWColormap |
|
|---|
| 267 | CWEventMask | CWBackingStore,
|
|---|
| 268 | &swa);
|
|---|
| 269 |
|
|---|
| 270 | XSetWMProperties (dpy, win, &windowName, &iconName, 0, 0,
|
|---|
| 271 | size_hints, wm_hints, class_hints);
|
|---|
| 272 |
|
|---|
| 273 | // request X to Draw window on screen.
|
|---|
| 274 | XMapWindow (dpy, win);
|
|---|
| 275 |
|
|---|
| 276 | // Wait for window to appear (wait for an "expose" event).
|
|---|
| 277 | XIfEvent (dpy, &event, G4OpenGLXViewerWaitForNotify, (char*) win);
|
|---|
| 278 |
|
|---|
| 279 | // connect the context to a window
|
|---|
| 280 | Bool success = glXMakeCurrent (dpy, win, cx);
|
|---|
| 281 | if (!success) {
|
|---|
| 282 | fViewId = -1; // This flags an error.
|
|---|
| 283 | G4cerr << "G4OpenGLViewer::G4OpenGLViewer failed to attach a GLX context."
|
|---|
| 284 | << G4endl;
|
|---|
| 285 | GLint error = GL_NO_ERROR;
|
|---|
| 286 | while ((error = glGetError()) != GL_NO_ERROR) {
|
|---|
| 287 | G4cout << "GL Error: " << gluErrorString(error) << G4endl;
|
|---|
| 288 | }
|
|---|
| 289 | return;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | void G4OpenGLXViewer::CreateFontLists () {
|
|---|
| 295 |
|
|---|
| 296 | std::map<G4double,G4String> fonts; // G4VMarker screen size and font name.
|
|---|
| 297 | fonts[10.] = "-adobe-courier-bold-r-normal--10-100-75-75-m-60-iso8859-1";
|
|---|
| 298 | fonts[11.] = "-adobe-courier-bold-r-normal--11-80-100-100-m-60-iso8859-1";
|
|---|
| 299 | fonts[12.] = "-adobe-courier-bold-r-normal--12-120-75-75-m-70-iso8859-1";
|
|---|
| 300 | fonts[13.] = "fixed";
|
|---|
| 301 | fonts[14.] = "-adobe-courier-bold-r-normal--14-100-100-100-m-90-iso8859-1";
|
|---|
| 302 | fonts[17.] = "-adobe-courier-bold-r-normal--17-120-100-100-m-100-iso8859-1";
|
|---|
| 303 | fonts[18.] = "-adobe-courier-bold-r-normal--18-180-75-75-m-110-iso8859-1";
|
|---|
| 304 | fonts[20.] = "-adobe-courier-bold-r-normal--20-140-100-100-m-110-iso8859-1";
|
|---|
| 305 | fonts[24.] = "-adobe-courier-bold-r-normal--24-240-75-75-m-150-iso8859-1";
|
|---|
| 306 | fonts[25.] = "-adobe-courier-bold-r-normal--25-180-100-100-m-150-iso8859-1";
|
|---|
| 307 | fonts[34.] = "-adobe-courier-bold-r-normal--34-240-100-100-m-200-iso8859-1";
|
|---|
| 308 | std::map<G4double,G4String>::const_iterator i;
|
|---|
| 309 | for (i = fonts.begin(); i != fonts.end(); ++i) {
|
|---|
| 310 | XFontStruct* font_info = XLoadQueryFont(dpy, i->second);
|
|---|
| 311 | if (!font_info) {
|
|---|
| 312 | G4cerr <<
|
|---|
| 313 | "G4OpenGLXViewer: XLoadQueryFont failed for font\n "
|
|---|
| 314 | << i->second
|
|---|
| 315 | << G4endl;
|
|---|
| 316 | continue;
|
|---|
| 317 | }
|
|---|
| 318 | G4int font_base = glGenLists(256);
|
|---|
| 319 | if (!font_base) {
|
|---|
| 320 | G4cerr << "G4OpenGLXViewer: out of display lists for fonts."
|
|---|
| 321 | << G4endl;
|
|---|
| 322 | continue;
|
|---|
| 323 | }
|
|---|
| 324 | G4int first = font_info->min_char_or_byte2;
|
|---|
| 325 | G4int last = font_info->max_char_or_byte2;
|
|---|
| 326 | glXUseXFont(font_info->fid, first, last-first+1,font_base+first);
|
|---|
| 327 | G4OpenGLFontBaseStore::AddFontBase(this,font_base,i->first,i->second);
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | G4OpenGLXViewer::G4OpenGLXViewer (G4OpenGLSceneHandler& scene):
|
|---|
| 332 | G4VViewer (scene, -1),
|
|---|
| 333 | G4OpenGLViewer (scene),
|
|---|
| 334 | vi_immediate (0),
|
|---|
| 335 | vi_stored (0),
|
|---|
| 336 | vi (0),
|
|---|
| 337 | cmap (0)
|
|---|
| 338 | {
|
|---|
| 339 | GetXConnection ();
|
|---|
| 340 | if (fViewId < 0) return;
|
|---|
| 341 |
|
|---|
| 342 | // Try for a visual suitable for OpenGLImmediate..
|
|---|
| 343 | // first try for a single buffered RGB window
|
|---|
| 344 | if (!vi_single_buffer) {
|
|---|
| 345 | vi_single_buffer =
|
|---|
| 346 | glXChooseVisual (dpy, XDefaultScreen (dpy), snglBuf_RGBA);
|
|---|
| 347 | }
|
|---|
| 348 | if (!vi_double_buffer) {
|
|---|
| 349 | vi_double_buffer =
|
|---|
| 350 | glXChooseVisual (dpy, XDefaultScreen (dpy), dblBuf_RGBA);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | if (vi_single_buffer || vi_double_buffer) {
|
|---|
| 354 | if (!vi_double_buffer) {
|
|---|
| 355 | G4cout <<
|
|---|
| 356 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
|
|---|
| 357 | "\n Working with a single buffer."
|
|---|
| 358 | << G4endl;
|
|---|
| 359 | }
|
|---|
| 360 | } else {
|
|---|
| 361 | if (!vi_single_buffer) {
|
|---|
| 362 | G4cout <<
|
|---|
| 363 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a single buffer visual."
|
|---|
| 364 | << G4endl;
|
|---|
| 365 | }
|
|---|
| 366 | if (!vi_double_buffer) {
|
|---|
| 367 | G4cout <<
|
|---|
| 368 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get a double buffer visual."
|
|---|
| 369 | << G4endl;
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | if (vi_single_buffer) {
|
|---|
| 374 | vi_immediate = vi_single_buffer;
|
|---|
| 375 | attributeList = snglBuf_RGBA;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | if (!vi_immediate){
|
|---|
| 379 | // next try for a double buffered RGB, but Draw to top buffer
|
|---|
| 380 | if (vi_double_buffer) {
|
|---|
| 381 | vi_immediate = vi_double_buffer;
|
|---|
| 382 | attributeList = dblBuf_RGBA;
|
|---|
| 383 | }
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | // Now try for a visual suitable for OpenGLStored...
|
|---|
| 387 | // Try for a double buffered RGB window
|
|---|
| 388 | if (vi_double_buffer) {
|
|---|
| 389 | vi_stored = vi_double_buffer;
|
|---|
| 390 | attributeList = dblBuf_RGBA;
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | if (!vi_immediate || !vi_stored) {
|
|---|
| 394 | G4cout <<
|
|---|
| 395 | "G4OpenGLXViewer::G4OpenGLXViewer: unable to get required visuals."
|
|---|
| 396 | << G4endl;
|
|---|
| 397 | fViewId = -1; // This flags an error.
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | // glClearColor (0., 0., 0., 0.);
|
|---|
| 401 | // glClearDepth (1.);
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | G4OpenGLXViewer::~G4OpenGLXViewer () {
|
|---|
| 405 | if (fViewId >= 0) {
|
|---|
| 406 | //Close a window from here
|
|---|
| 407 | glXMakeCurrent (dpy, None, NULL);
|
|---|
| 408 | glXDestroyContext (dpy, cx);
|
|---|
| 409 | if (win) XDestroyWindow (dpy, win); // ...if already deleted in
|
|---|
| 410 | // sub-class G4OpenGLXmViewer.
|
|---|
| 411 | XFlush (dpy);
|
|---|
| 412 | }
|
|---|
| 413 | }
|
|---|
| 414 |
|
|---|
| 415 | void G4OpenGLXViewer::print() {
|
|---|
| 416 |
|
|---|
| 417 | //using namespace std;
|
|---|
| 418 | //cout << "print_col_callback requested with file name: " << print_string << G4endl;
|
|---|
| 419 |
|
|---|
| 420 | if (vectored_ps) {
|
|---|
| 421 |
|
|---|
| 422 | G4OpenGLViewer::print();
|
|---|
| 423 |
|
|---|
| 424 | } else {
|
|---|
| 425 |
|
|---|
| 426 | XVisualInfo* pvi;
|
|---|
| 427 | GLXContext pcx = create_GL_print_context(pvi);
|
|---|
| 428 |
|
|---|
| 429 | if (!pcx) {
|
|---|
| 430 | G4cout << "Unable to create print context." << G4endl;
|
|---|
| 431 | return;
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | GLXContext tmp_cx;
|
|---|
| 435 | tmp_cx = cx;
|
|---|
| 436 | cx=pcx;
|
|---|
| 437 |
|
|---|
| 438 | Pixmap pmap = XCreatePixmap (dpy,
|
|---|
| 439 | XRootWindow (dpy, pvi->screen),
|
|---|
| 440 | fWinSize_x, fWinSize_y,
|
|---|
| 441 | pvi->depth);
|
|---|
| 442 |
|
|---|
| 443 | GLXPixmap glxpmap = glXCreateGLXPixmap (dpy,
|
|---|
| 444 | pvi,
|
|---|
| 445 | pmap);
|
|---|
| 446 |
|
|---|
| 447 | GLXDrawable tmp_win;
|
|---|
| 448 | tmp_win=win;
|
|---|
| 449 | win=glxpmap;
|
|---|
| 450 |
|
|---|
| 451 | glXMakeCurrent (dpy,
|
|---|
| 452 | win,
|
|---|
| 453 | cx);
|
|---|
| 454 |
|
|---|
| 455 | glViewport (0, 0, fWinSize_x, fWinSize_y);
|
|---|
| 456 |
|
|---|
| 457 | ClearView ();
|
|---|
| 458 | SetView ();
|
|---|
| 459 | DrawView ();
|
|---|
| 460 |
|
|---|
| 461 | generateEPS (print_string,
|
|---|
| 462 | print_colour,
|
|---|
| 463 | fWinSize_x, fWinSize_y);
|
|---|
| 464 |
|
|---|
| 465 | win=tmp_win;
|
|---|
| 466 | cx=tmp_cx;
|
|---|
| 467 |
|
|---|
| 468 | glXMakeCurrent (dpy,
|
|---|
| 469 | win,
|
|---|
| 470 | cx);
|
|---|
| 471 |
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | GLubyte* G4OpenGLXViewer::grabPixels (int inColor, unsigned int width, unsigned int height) {
|
|---|
| 477 |
|
|---|
| 478 | GLubyte* buffer;
|
|---|
| 479 | GLint swapbytes, lsbfirst, rowlength;
|
|---|
| 480 | GLint skiprows, skippixels, alignment;
|
|---|
| 481 | GLenum format;
|
|---|
| 482 | int size;
|
|---|
| 483 |
|
|---|
| 484 | if (inColor) {
|
|---|
| 485 | format = GL_RGB;
|
|---|
| 486 | size = width*height*3;
|
|---|
| 487 | } else {
|
|---|
| 488 | format = GL_LUMINANCE;
|
|---|
| 489 | size = width*height*1;
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | buffer = new GLubyte[size];
|
|---|
| 493 | if (buffer == NULL)
|
|---|
| 494 | return NULL;
|
|---|
| 495 |
|
|---|
| 496 | glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes);
|
|---|
| 497 | glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst);
|
|---|
| 498 | glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength);
|
|---|
| 499 |
|
|---|
| 500 | glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows);
|
|---|
| 501 | glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels);
|
|---|
| 502 | glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment);
|
|---|
| 503 |
|
|---|
| 504 | glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE);
|
|---|
| 505 | glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE);
|
|---|
| 506 | glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
|
|---|
| 507 |
|
|---|
| 508 | glPixelStorei (GL_UNPACK_SKIP_ROWS, 0);
|
|---|
| 509 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0);
|
|---|
| 510 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
|
|---|
| 511 |
|
|---|
| 512 | glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer);
|
|---|
| 513 |
|
|---|
| 514 | glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes);
|
|---|
| 515 | glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst);
|
|---|
| 516 | glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength);
|
|---|
| 517 |
|
|---|
| 518 | glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows);
|
|---|
| 519 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels);
|
|---|
| 520 | glPixelStorei (GL_UNPACK_ALIGNMENT, alignment);
|
|---|
| 521 |
|
|---|
| 522 | return buffer;
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | int G4OpenGLXViewer::generateEPS (char* filnam,
|
|---|
| 526 | int inColour,
|
|---|
| 527 | unsigned int width,
|
|---|
| 528 | unsigned int height) {
|
|---|
| 529 |
|
|---|
| 530 | FILE* fp;
|
|---|
| 531 | GLubyte* pixels;
|
|---|
| 532 | GLubyte* curpix;
|
|---|
| 533 | int components, pos, i;
|
|---|
| 534 |
|
|---|
| 535 | pixels = grabPixels (inColour, width, height);
|
|---|
| 536 |
|
|---|
| 537 | if (pixels == NULL)
|
|---|
| 538 | return 1;
|
|---|
| 539 | if (inColour) {
|
|---|
| 540 | components = 3;
|
|---|
| 541 | } else {
|
|---|
| 542 | components = 1;
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | fp = fopen (filnam, "w");
|
|---|
| 546 | if (fp == NULL) {
|
|---|
| 547 | return 2;
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n");
|
|---|
| 551 | fprintf (fp, "%%%%Title: %s\n", filnam);
|
|---|
| 552 | fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n");
|
|---|
| 553 | fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height);
|
|---|
| 554 | fprintf (fp, "%%%%EndComments\n");
|
|---|
| 555 | fprintf (fp, "gsave\n");
|
|---|
| 556 | fprintf (fp, "/bwproc {\n");
|
|---|
| 557 | fprintf (fp, " rgbproc\n");
|
|---|
| 558 | fprintf (fp, " dup length 3 idiv string 0 3 0 \n");
|
|---|
| 559 | fprintf (fp, " 5 -1 roll {\n");
|
|---|
| 560 | fprintf (fp, " add 2 1 roll 1 sub dup 0 eq\n");
|
|---|
| 561 | fprintf (fp, " { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n");
|
|---|
| 562 | fprintf (fp, " 3 1 roll 5 -1 roll } put 1 add 3 0 \n");
|
|---|
| 563 | fprintf (fp, " { 2 1 roll } ifelse\n");
|
|---|
| 564 | fprintf (fp, " }forall\n");
|
|---|
| 565 | fprintf (fp, " pop pop pop\n");
|
|---|
| 566 | fprintf (fp, "} def\n");
|
|---|
| 567 | fprintf (fp, "systemdict /colorimage known not {\n");
|
|---|
| 568 | fprintf (fp, " /colorimage {\n");
|
|---|
| 569 | fprintf (fp, " pop\n");
|
|---|
| 570 | fprintf (fp, " pop\n");
|
|---|
| 571 | fprintf (fp, " /rgbproc exch def\n");
|
|---|
| 572 | fprintf (fp, " { bwproc } image\n");
|
|---|
| 573 | fprintf (fp, " } def\n");
|
|---|
| 574 | fprintf (fp, "} if\n");
|
|---|
| 575 | fprintf (fp, "/picstr %d string def\n", width * components);
|
|---|
| 576 | fprintf (fp, "%d %d scale\n", width, height);
|
|---|
| 577 | fprintf (fp, "%d %d %d\n", width, height, 8);
|
|---|
| 578 | fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height);
|
|---|
| 579 | fprintf (fp, "{currentfile picstr readhexstring pop}\n");
|
|---|
| 580 | fprintf (fp, "false %d\n", components);
|
|---|
| 581 | fprintf (fp, "colorimage\n");
|
|---|
| 582 |
|
|---|
| 583 | curpix = (GLubyte*) pixels;
|
|---|
| 584 | pos = 0;
|
|---|
| 585 | for (i = width*height*components; i>0; i--) {
|
|---|
| 586 | fprintf (fp, "%02hx ", *(curpix++));
|
|---|
| 587 | if (++pos >= 32) {
|
|---|
| 588 | fprintf (fp, "\n");
|
|---|
| 589 | pos = 0;
|
|---|
| 590 | }
|
|---|
| 591 | }
|
|---|
| 592 | if (pos)
|
|---|
| 593 | fprintf (fp, "\n");
|
|---|
| 594 |
|
|---|
| 595 | fprintf (fp, "grestore\n");
|
|---|
| 596 | fprintf (fp, "showpage\n");
|
|---|
| 597 | delete pixels;
|
|---|
| 598 | fclose (fp);
|
|---|
| 599 | return 0;
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | GLXContext G4OpenGLXViewer::create_GL_print_context(XVisualInfo*& pvi) {
|
|---|
| 603 |
|
|---|
| 604 | pvi = glXChooseVisual (dpy,
|
|---|
| 605 | XDefaultScreen (dpy),
|
|---|
| 606 | snglBuf_RGBA);
|
|---|
| 607 |
|
|---|
| 608 | if (!pvi) {
|
|---|
| 609 | pvi = glXChooseVisual (dpy,
|
|---|
| 610 | XDefaultScreen (dpy),
|
|---|
| 611 | dblBuf_RGBA);
|
|---|
| 612 | }
|
|---|
| 613 |
|
|---|
| 614 | return glXCreateContext (dpy,
|
|---|
| 615 | pvi,
|
|---|
| 616 | NULL,
|
|---|
| 617 | False);
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | #endif
|
|---|