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