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