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