[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: G4OpenGLViewer.cc,v 1.50 2009/03/05 16:36:13 lgarnier Exp $ |
---|
[873] | 28 | // GEANT4 tag $Name: $ |
---|
[529] | 29 | // |
---|
| 30 | // |
---|
| 31 | // Andrew Walkden 27th March 1996 |
---|
| 32 | // OpenGL view - opens window, hard copy, etc. |
---|
| 33 | |
---|
| 34 | #ifdef G4VIS_BUILD_OPENGL_DRIVER |
---|
| 35 | |
---|
| 36 | #include "G4ios.hh" |
---|
| 37 | #include "G4OpenGLViewer.hh" |
---|
| 38 | #include "G4OpenGLSceneHandler.hh" |
---|
| 39 | #include "G4OpenGLTransform3D.hh" |
---|
[923] | 40 | #include "G4OpenGL2PSAction.hh" |
---|
[529] | 41 | |
---|
| 42 | #include "G4Scene.hh" |
---|
| 43 | #include "G4VisExtent.hh" |
---|
| 44 | #include "G4LogicalVolume.hh" |
---|
| 45 | #include "G4VSolid.hh" |
---|
| 46 | #include "G4Point3D.hh" |
---|
| 47 | #include "G4Normal3D.hh" |
---|
| 48 | #include "G4Plane3D.hh" |
---|
[593] | 49 | #include "G4AttHolder.hh" |
---|
| 50 | #include "G4AttCheck.hh" |
---|
[918] | 51 | |
---|
[923] | 52 | // GL2PS |
---|
| 53 | #include "Geant4_gl2ps.h" |
---|
| 54 | |
---|
[593] | 55 | #include <sstream> |
---|
[529] | 56 | |
---|
| 57 | G4OpenGLViewer::G4OpenGLViewer (G4OpenGLSceneHandler& scene): |
---|
| 58 | G4VViewer (scene, -1), |
---|
[916] | 59 | fPrintFilename ("G4OpenGL.eps"), |
---|
| 60 | fPrintColour (true), |
---|
| 61 | fVectoredPs (true), |
---|
[941] | 62 | fPrintSizeX(0), |
---|
| 63 | fPrintSizeY(0), |
---|
[593] | 64 | fOpenGLSceneHandler(scene), |
---|
[529] | 65 | background (G4Colour(0.,0.,0.)), |
---|
| 66 | transparency_enabled (true), |
---|
| 67 | antialiasing_enabled (false), |
---|
| 68 | haloing_enabled (false), |
---|
[789] | 69 | fStartTime(-DBL_MAX), |
---|
| 70 | fEndTime(DBL_MAX), |
---|
[529] | 71 | fFadeFactor(0.), |
---|
| 72 | fDisplayHeadTime(false), |
---|
| 73 | fDisplayHeadTimeX(-0.9), |
---|
| 74 | fDisplayHeadTimeY(-0.9), |
---|
| 75 | fDisplayHeadTimeSize(24.), |
---|
| 76 | fDisplayHeadTimeRed(0.), |
---|
| 77 | fDisplayHeadTimeGreen(1.), |
---|
| 78 | fDisplayHeadTimeBlue(1.), |
---|
| 79 | fDisplayLightFront(false), |
---|
| 80 | fDisplayLightFrontX(0.), |
---|
| 81 | fDisplayLightFrontY(0.), |
---|
| 82 | fDisplayLightFrontZ(0.), |
---|
| 83 | fDisplayLightFrontT(0.), |
---|
| 84 | fDisplayLightFrontRed(0.), |
---|
| 85 | fDisplayLightFrontGreen(1.), |
---|
[916] | 86 | fDisplayLightFrontBlue(0.), |
---|
| 87 | fPointSize (0) |
---|
[529] | 88 | { |
---|
| 89 | // Make changes to view parameters for OpenGL... |
---|
| 90 | fVP.SetAutoRefresh(true); |
---|
| 91 | fDefaultVP.SetAutoRefresh(true); |
---|
[897] | 92 | fWinSize_x = fVP.GetWindowSizeHintX(); |
---|
| 93 | fWinSize_y = fVP.GetWindowSizeHintY(); |
---|
[529] | 94 | |
---|
[921] | 95 | fGL2PSAction = new G4OpenGL2PSAction(); |
---|
| 96 | |
---|
[529] | 97 | // glClearColor (0.0, 0.0, 0.0, 0.0); |
---|
| 98 | // glClearDepth (1.0); |
---|
| 99 | // glDisable (GL_BLEND); |
---|
| 100 | // glDisable (GL_LINE_SMOOTH); |
---|
| 101 | // glDisable (GL_POLYGON_SMOOTH); |
---|
| 102 | |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | G4OpenGLViewer::~G4OpenGLViewer () {} |
---|
| 106 | |
---|
| 107 | void G4OpenGLViewer::InitializeGLView () |
---|
| 108 | { |
---|
| 109 | glClearColor (0.0, 0.0, 0.0, 0.0); |
---|
| 110 | glClearDepth (1.0); |
---|
| 111 | glDisable (GL_BLEND); |
---|
| 112 | glDisable (GL_LINE_SMOOTH); |
---|
| 113 | glDisable (GL_POLYGON_SMOOTH); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void G4OpenGLViewer::ClearView () { |
---|
| 117 | glClearColor (background.GetRed(), |
---|
| 118 | background.GetGreen(), |
---|
| 119 | background.GetBlue(), |
---|
| 120 | 1.); |
---|
| 121 | glClearDepth (1.0); |
---|
| 122 | //Below line does not compile with Mesa includes. |
---|
| 123 | //glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
---|
| 124 | glClear (GL_COLOR_BUFFER_BIT); |
---|
| 125 | glClear (GL_DEPTH_BUFFER_BIT); |
---|
| 126 | glClear (GL_STENCIL_BUFFER_BIT); |
---|
| 127 | glFlush (); |
---|
| 128 | } |
---|
| 129 | |
---|
[906] | 130 | |
---|
| 131 | /** |
---|
| 132 | * Set the viewport of the scene |
---|
| 133 | */ |
---|
| 134 | void G4OpenGLViewer::ResizeGLView() |
---|
| 135 | { |
---|
| 136 | int side = fWinSize_x; |
---|
| 137 | if (fWinSize_y < fWinSize_x) side = fWinSize_y; |
---|
| 138 | glViewport((fWinSize_x - side) / 2, (fWinSize_y - side) / 2, side, side); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
[529] | 142 | void G4OpenGLViewer::SetView () { |
---|
[858] | 143 | |
---|
| 144 | if (!fSceneHandler.GetScene()) { |
---|
[908] | 145 | G4cerr << "G4OpenGLStoredViewer: Creating a Viewer without a scene is not allowed. \nPlease use /vis/scene/create before /vis/open/.... " |
---|
[858] | 146 | << G4endl; |
---|
| 147 | return; |
---|
| 148 | } |
---|
[529] | 149 | // Calculates view representation based on extent of object being |
---|
| 150 | // viewed and (initial) viewpoint. (Note: it can change later due |
---|
| 151 | // to user interaction via visualization system's GUI.) |
---|
| 152 | |
---|
| 153 | // Lighting. |
---|
| 154 | GLfloat lightPosition [4]; |
---|
| 155 | lightPosition [0] = fVP.GetActualLightpointDirection().x(); |
---|
| 156 | lightPosition [1] = fVP.GetActualLightpointDirection().y(); |
---|
| 157 | lightPosition [2] = fVP.GetActualLightpointDirection().z(); |
---|
| 158 | lightPosition [3] = 0.; |
---|
| 159 | // Light position is "true" light direction, so must come after gluLookAt. |
---|
| 160 | GLfloat ambient [] = { 0.2, 0.2, 0.2, 1.}; |
---|
| 161 | GLfloat diffuse [] = { 0.8, 0.8, 0.8, 1.}; |
---|
| 162 | glEnable (GL_LIGHT0); |
---|
| 163 | glLightfv (GL_LIGHT0, GL_AMBIENT, ambient); |
---|
| 164 | glLightfv (GL_LIGHT0, GL_DIFFUSE, diffuse); |
---|
| 165 | |
---|
| 166 | // Get radius of scene, etc. |
---|
| 167 | // Note that this procedure properly takes into account zoom, dolly and pan. |
---|
| 168 | const G4Point3D targetPoint |
---|
| 169 | = fSceneHandler.GetScene()->GetStandardTargetPoint() |
---|
| 170 | + fVP.GetCurrentTargetPoint (); |
---|
| 171 | G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius(); |
---|
| 172 | if(radius<=0.) radius = 1.; |
---|
| 173 | const G4double cameraDistance = fVP.GetCameraDistance (radius); |
---|
| 174 | const G4Point3D cameraPosition = |
---|
| 175 | targetPoint + cameraDistance * fVP.GetViewpointDirection().unit(); |
---|
[906] | 176 | const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius); |
---|
| 177 | const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius); |
---|
[529] | 178 | const GLdouble right = fVP.GetFrontHalfHeight (pnear, radius); |
---|
| 179 | const GLdouble left = -right; |
---|
| 180 | const GLdouble bottom = left; |
---|
| 181 | const GLdouble top = right; |
---|
| 182 | |
---|
[906] | 183 | // FIXME |
---|
| 184 | ResizeGLView(); |
---|
[908] | 185 | //SHOULD SetWindowsSizeHint()... |
---|
[906] | 186 | |
---|
[529] | 187 | glMatrixMode (GL_PROJECTION); // set up Frustum. |
---|
| 188 | glLoadIdentity(); |
---|
| 189 | |
---|
[906] | 190 | const G4Vector3D scaleFactor = fVP.GetScaleFactor(); |
---|
| 191 | glScaled(scaleFactor.x(),scaleFactor.y(),scaleFactor.z()); |
---|
[529] | 192 | |
---|
| 193 | if (fVP.GetFieldHalfAngle() == 0.) { |
---|
| 194 | glOrtho (left, right, bottom, top, pnear, pfar); |
---|
| 195 | } |
---|
| 196 | else { |
---|
| 197 | glFrustum (left, right, bottom, top, pnear, pfar); |
---|
[908] | 198 | } |
---|
[906] | 199 | |
---|
[529] | 200 | glMatrixMode (GL_MODELVIEW); // apply further transformations to scene. |
---|
| 201 | glLoadIdentity(); |
---|
| 202 | |
---|
| 203 | const G4Normal3D& upVector = fVP.GetUpVector (); |
---|
| 204 | G4Point3D gltarget; |
---|
| 205 | if (cameraDistance > 1.e-6 * radius) { |
---|
| 206 | gltarget = targetPoint; |
---|
| 207 | } |
---|
| 208 | else { |
---|
| 209 | gltarget = targetPoint - radius * fVP.GetViewpointDirection().unit(); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | const G4Point3D& pCamera = cameraPosition; // An alias for brevity. |
---|
| 213 | gluLookAt (pCamera.x(), pCamera.y(), pCamera.z(), // Viewpoint. |
---|
| 214 | gltarget.x(), gltarget.y(), gltarget.z(), // Target point. |
---|
| 215 | upVector.x(), upVector.y(), upVector.z()); // Up vector. |
---|
[906] | 216 | |
---|
[529] | 217 | // Light position is "true" light direction, so must come after gluLookAt. |
---|
| 218 | glLightfv (GL_LIGHT0, GL_POSITION, lightPosition); |
---|
| 219 | |
---|
| 220 | // OpenGL no longer seems to reconstruct clipped edges, so, when the |
---|
| 221 | // BooleanProcessor is up to it, abandon this and use generic |
---|
| 222 | // clipping in G4OpenGLSceneHandler::CreateSectionPolyhedron. Also, |
---|
| 223 | // force kernel visit on change of clipping plane in |
---|
| 224 | // G4OpenGLStoredViewer::CompareForKernelVisit. |
---|
| 225 | if (fVP.IsSection () ) { // pair of back to back clip planes. |
---|
| 226 | const G4Plane3D& s = fVP.GetSectionPlane (); |
---|
| 227 | double sArray[4]; |
---|
| 228 | sArray[0] = s.a(); |
---|
| 229 | sArray[1] = s.b(); |
---|
| 230 | sArray[2] = s.c(); |
---|
| 231 | sArray[3] = s.d() + radius * 1.e-05; |
---|
| 232 | glClipPlane (GL_CLIP_PLANE0, sArray); |
---|
| 233 | glEnable (GL_CLIP_PLANE0); |
---|
| 234 | sArray[0] = -s.a(); |
---|
| 235 | sArray[1] = -s.b(); |
---|
| 236 | sArray[2] = -s.c(); |
---|
| 237 | sArray[3] = -s.d() + radius * 1.e-05; |
---|
| 238 | glClipPlane (GL_CLIP_PLANE1, sArray); |
---|
| 239 | glEnable (GL_CLIP_PLANE1); |
---|
| 240 | } else { |
---|
| 241 | glDisable (GL_CLIP_PLANE0); |
---|
| 242 | glDisable (GL_CLIP_PLANE1); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | const G4Planes& cutaways = fVP.GetCutawayPlanes(); |
---|
| 246 | size_t nPlanes = cutaways.size(); |
---|
| 247 | if (fVP.IsCutaway() && |
---|
| 248 | fVP.GetCutawayMode() == G4ViewParameters::cutawayIntersection && |
---|
| 249 | nPlanes > 0) { |
---|
| 250 | double a[4]; |
---|
| 251 | a[0] = cutaways[0].a(); |
---|
| 252 | a[1] = cutaways[0].b(); |
---|
| 253 | a[2] = cutaways[0].c(); |
---|
| 254 | a[3] = cutaways[0].d(); |
---|
| 255 | glClipPlane (GL_CLIP_PLANE2, a); |
---|
| 256 | glEnable (GL_CLIP_PLANE2); |
---|
| 257 | if (nPlanes > 1) { |
---|
| 258 | a[0] = cutaways[1].a(); |
---|
| 259 | a[1] = cutaways[1].b(); |
---|
| 260 | a[2] = cutaways[1].c(); |
---|
| 261 | a[3] = cutaways[1].d(); |
---|
| 262 | glClipPlane (GL_CLIP_PLANE3, a); |
---|
| 263 | glEnable (GL_CLIP_PLANE3); |
---|
| 264 | } |
---|
| 265 | if (nPlanes > 2) { |
---|
| 266 | a[0] = cutaways[2].a(); |
---|
| 267 | a[1] = cutaways[2].b(); |
---|
| 268 | a[2] = cutaways[2].c(); |
---|
| 269 | a[3] = cutaways[2].d(); |
---|
| 270 | glClipPlane (GL_CLIP_PLANE4, a); |
---|
| 271 | glEnable (GL_CLIP_PLANE4); |
---|
| 272 | } |
---|
| 273 | } else { |
---|
| 274 | glDisable (GL_CLIP_PLANE2); |
---|
| 275 | glDisable (GL_CLIP_PLANE3); |
---|
| 276 | glDisable (GL_CLIP_PLANE4); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | // Background. |
---|
| 280 | background = fVP.GetBackgroundColour (); |
---|
| 281 | |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | void G4OpenGLViewer::HaloingFirstPass () { |
---|
| 285 | |
---|
| 286 | //To perform haloing, first Draw all information to the depth buffer |
---|
| 287 | //alone, using a chunky line width, and then Draw all info again, to |
---|
| 288 | //the colour buffer, setting a thinner line width an the depth testing |
---|
| 289 | //function to less than or equal, so if two lines cross, the one |
---|
| 290 | //passing behind the other will not pass the depth test, and so not |
---|
| 291 | //get rendered either side of the infront line for a short distance. |
---|
| 292 | |
---|
| 293 | //First, disable writing to the colo(u)r buffer... |
---|
| 294 | glColorMask (GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); |
---|
| 295 | |
---|
| 296 | //Now enable writing to the depth buffer... |
---|
| 297 | glDepthMask (GL_TRUE); |
---|
| 298 | glDepthFunc (GL_LESS); |
---|
| 299 | glClearDepth (1.0); |
---|
| 300 | |
---|
| 301 | //Finally, set the line width to something wide... |
---|
| 302 | glLineWidth (3.0); |
---|
| 303 | |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | void G4OpenGLViewer::HaloingSecondPass () { |
---|
| 307 | |
---|
| 308 | //And finally, turn the colour buffer back on with a sesible line width... |
---|
| 309 | glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); |
---|
| 310 | glDepthFunc (GL_LEQUAL); |
---|
| 311 | glLineWidth (1.0); |
---|
| 312 | |
---|
| 313 | } |
---|
| 314 | |
---|
[593] | 315 | void G4OpenGLViewer::Pick(GLdouble x, GLdouble y) |
---|
| 316 | { |
---|
| 317 | //G4cout << "X: " << x << ", Y: " << y << G4endl; |
---|
| 318 | const G4int BUFSIZE = 512; |
---|
| 319 | GLuint selectBuffer[BUFSIZE]; |
---|
| 320 | glSelectBuffer(BUFSIZE, selectBuffer); |
---|
| 321 | glRenderMode(GL_SELECT); |
---|
| 322 | glInitNames(); |
---|
| 323 | glPushName(0); |
---|
| 324 | glMatrixMode(GL_PROJECTION); |
---|
| 325 | G4double currentProjectionMatrix[16]; |
---|
| 326 | glGetDoublev(GL_PROJECTION_MATRIX, currentProjectionMatrix); |
---|
| 327 | glPushMatrix(); |
---|
| 328 | glLoadIdentity(); |
---|
| 329 | GLint viewport[4]; |
---|
| 330 | glGetIntegerv(GL_VIEWPORT, viewport); |
---|
| 331 | // Define 5x5 pixel pick area |
---|
| 332 | gluPickMatrix(x, viewport[3] - y, 5., 5., viewport); |
---|
| 333 | glMultMatrixd(currentProjectionMatrix); |
---|
| 334 | glMatrixMode(GL_MODELVIEW); |
---|
| 335 | DrawView(); |
---|
| 336 | GLint hits = glRenderMode(GL_RENDER); |
---|
| 337 | if (hits < 0) |
---|
| 338 | G4cout << "Too many hits. Zoom in to reduce overlaps." << G4cout; |
---|
| 339 | else if (hits > 0) { |
---|
| 340 | //G4cout << hits << " hit(s)" << G4endl; |
---|
| 341 | GLuint* p = selectBuffer; |
---|
| 342 | for (GLint i = 0; i < hits; ++i) { |
---|
| 343 | GLuint nnames = *p++; |
---|
| 344 | *p++; //OR GLuint zmin = *p++; |
---|
| 345 | *p++; //OR GLuint zmax = *p++; |
---|
| 346 | //G4cout << "Hit " << i << ": " << nnames << " names" |
---|
| 347 | // << "\nzmin: " << zmin << ", zmax: " << zmax << G4endl; |
---|
| 348 | for (GLuint j = 0; j < nnames; ++j) { |
---|
| 349 | GLuint name = *p++; |
---|
| 350 | //G4cout << "Name " << j << ": PickName: " << name << G4endl; |
---|
| 351 | std::map<GLuint, G4AttHolder*>::iterator iter = |
---|
| 352 | fOpenGLSceneHandler.fPickMap.find(name); |
---|
| 353 | if (iter != fOpenGLSceneHandler.fPickMap.end()) { |
---|
| 354 | G4AttHolder* attHolder = iter->second; |
---|
| 355 | if(attHolder && attHolder->GetAttDefs().size()) { |
---|
| 356 | for (size_t i = 0; i < attHolder->GetAttDefs().size(); ++i) { |
---|
| 357 | G4cout << G4AttCheck(attHolder->GetAttValues()[i], |
---|
| 358 | attHolder->GetAttDefs()[i]); |
---|
| 359 | } |
---|
| 360 | } |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | G4cout << G4endl; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | glMatrixMode(GL_PROJECTION); |
---|
| 367 | glPopMatrix(); |
---|
| 368 | glMatrixMode(GL_MODELVIEW); |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | |
---|
[754] | 372 | |
---|
[593] | 373 | |
---|
[914] | 374 | GLubyte* G4OpenGLViewer::grabPixels (int inColor, unsigned int width, unsigned int height) { |
---|
| 375 | |
---|
| 376 | GLubyte* buffer; |
---|
| 377 | GLint swapbytes, lsbfirst, rowlength; |
---|
| 378 | GLint skiprows, skippixels, alignment; |
---|
| 379 | GLenum format; |
---|
| 380 | int size; |
---|
| 381 | |
---|
| 382 | if (inColor) { |
---|
| 383 | format = GL_RGB; |
---|
| 384 | size = width*height*3; |
---|
| 385 | } else { |
---|
| 386 | format = GL_LUMINANCE; |
---|
| 387 | size = width*height*1; |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | buffer = new GLubyte[size]; |
---|
| 391 | if (buffer == NULL) |
---|
| 392 | return NULL; |
---|
| 393 | |
---|
| 394 | glGetIntegerv (GL_UNPACK_SWAP_BYTES, &swapbytes); |
---|
| 395 | glGetIntegerv (GL_UNPACK_LSB_FIRST, &lsbfirst); |
---|
| 396 | glGetIntegerv (GL_UNPACK_ROW_LENGTH, &rowlength); |
---|
| 397 | |
---|
| 398 | glGetIntegerv (GL_UNPACK_SKIP_ROWS, &skiprows); |
---|
| 399 | glGetIntegerv (GL_UNPACK_SKIP_PIXELS, &skippixels); |
---|
| 400 | glGetIntegerv (GL_UNPACK_ALIGNMENT, &alignment); |
---|
| 401 | |
---|
| 402 | glPixelStorei (GL_UNPACK_SWAP_BYTES, GL_FALSE); |
---|
| 403 | glPixelStorei (GL_UNPACK_LSB_FIRST, GL_FALSE); |
---|
| 404 | glPixelStorei (GL_UNPACK_ROW_LENGTH, 0); |
---|
| 405 | |
---|
| 406 | glPixelStorei (GL_UNPACK_SKIP_ROWS, 0); |
---|
| 407 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, 0); |
---|
| 408 | glPixelStorei (GL_UNPACK_ALIGNMENT, 1); |
---|
| 409 | |
---|
| 410 | glReadPixels (0, 0, (GLsizei)width, (GLsizei)height, format, GL_UNSIGNED_BYTE, (GLvoid*) buffer); |
---|
| 411 | |
---|
| 412 | glPixelStorei (GL_UNPACK_SWAP_BYTES, swapbytes); |
---|
| 413 | glPixelStorei (GL_UNPACK_LSB_FIRST, lsbfirst); |
---|
| 414 | glPixelStorei (GL_UNPACK_ROW_LENGTH, rowlength); |
---|
| 415 | |
---|
| 416 | glPixelStorei (GL_UNPACK_SKIP_ROWS, skiprows); |
---|
| 417 | glPixelStorei (GL_UNPACK_SKIP_PIXELS, skippixels); |
---|
| 418 | glPixelStorei (GL_UNPACK_ALIGNMENT, alignment); |
---|
| 419 | |
---|
| 420 | return buffer; |
---|
| 421 | } |
---|
| 422 | |
---|
[941] | 423 | void G4OpenGLViewer::printEPS() { |
---|
| 424 | bool res; |
---|
[942] | 425 | #ifdef G4DEBUG_VIS_OGL |
---|
| 426 | printf("G4OpenGLViewer::printEPS file:%s Vec:%d\n",fPrintFilename.c_str(),fVectoredPs); |
---|
| 427 | #endif |
---|
[941] | 428 | if (fVectoredPs) { |
---|
| 429 | res = printVectoredEPS(); |
---|
| 430 | } else { |
---|
| 431 | res = printNonVectoredEPS(); |
---|
| 432 | } |
---|
| 433 | if (res == false) { |
---|
| 434 | G4cerr << "Error while saving file... "<<fPrintFilename.c_str()<< G4endl; |
---|
| 435 | } else { |
---|
| 436 | G4cout << "File "<<fPrintFilename.c_str()<<" has been saved " << G4endl; |
---|
| 437 | } |
---|
| 438 | } |
---|
[914] | 439 | |
---|
[941] | 440 | bool G4OpenGLViewer::printVectoredEPS() { |
---|
| 441 | return printGl2PS(); |
---|
| 442 | } |
---|
| 443 | |
---|
| 444 | bool G4OpenGLViewer::printNonVectoredEPS () { |
---|
| 445 | |
---|
| 446 | int width = 0; |
---|
| 447 | int height = 0; |
---|
| 448 | |
---|
| 449 | if (fPrintSizeX == 0) { |
---|
| 450 | width = fWinSize_x; |
---|
| 451 | } else { |
---|
| 452 | width = fPrintSizeX; |
---|
| 453 | } |
---|
| 454 | if (fPrintSizeY == 0) { |
---|
| 455 | height = fWinSize_y; |
---|
| 456 | } else { |
---|
| 457 | height = fPrintSizeY; |
---|
| 458 | } |
---|
| 459 | |
---|
[942] | 460 | #ifdef G4DEBUG_VIS_OGL |
---|
| 461 | printf("G4OpenGLViewer::printNonVectoredEPS file:%s Vec:%d X:%d Y:%d col:%d\n",fPrintFilename.c_str(),fVectoredPs,width,height,fPrintColour); |
---|
| 462 | #endif |
---|
[914] | 463 | FILE* fp; |
---|
| 464 | GLubyte* pixels; |
---|
| 465 | GLubyte* curpix; |
---|
| 466 | int components, pos, i; |
---|
| 467 | |
---|
[941] | 468 | pixels = grabPixels (fPrintColour, width, height); |
---|
[914] | 469 | |
---|
[941] | 470 | if (pixels == NULL) { |
---|
| 471 | G4cerr << "Failed to get pixels from OpenGl viewport" << G4endl; |
---|
| 472 | return false; |
---|
| 473 | } |
---|
| 474 | if (fPrintColour) { |
---|
[914] | 475 | components = 3; |
---|
| 476 | } else { |
---|
| 477 | components = 1; |
---|
| 478 | } |
---|
| 479 | |
---|
[938] | 480 | fp = fopen (fPrintFilename.c_str(), "w"); |
---|
[914] | 481 | if (fp == NULL) { |
---|
[941] | 482 | G4cerr << "Can't open filename " << fPrintFilename.c_str() << G4endl; |
---|
| 483 | return false; |
---|
[914] | 484 | } |
---|
| 485 | |
---|
| 486 | fprintf (fp, "%%!PS-Adobe-2.0 EPSF-1.2\n"); |
---|
[938] | 487 | fprintf (fp, "%%%%Title: %s\n", fPrintFilename.c_str()); |
---|
[914] | 488 | fprintf (fp, "%%%%Creator: OpenGL pixmap render output\n"); |
---|
| 489 | fprintf (fp, "%%%%BoundingBox: 0 0 %d %d\n", width, height); |
---|
| 490 | fprintf (fp, "%%%%EndComments\n"); |
---|
| 491 | fprintf (fp, "gsave\n"); |
---|
| 492 | fprintf (fp, "/bwproc {\n"); |
---|
| 493 | fprintf (fp, " rgbproc\n"); |
---|
| 494 | fprintf (fp, " dup length 3 idiv string 0 3 0 \n"); |
---|
| 495 | fprintf (fp, " 5 -1 roll {\n"); |
---|
| 496 | fprintf (fp, " add 2 1 roll 1 sub dup 0 eq\n"); |
---|
| 497 | fprintf (fp, " { pop 3 idiv 3 -1 roll dup 4 -1 roll dup\n"); |
---|
| 498 | fprintf (fp, " 3 1 roll 5 -1 roll } put 1 add 3 0 \n"); |
---|
| 499 | fprintf (fp, " { 2 1 roll } ifelse\n"); |
---|
| 500 | fprintf (fp, " }forall\n"); |
---|
| 501 | fprintf (fp, " pop pop pop\n"); |
---|
| 502 | fprintf (fp, "} def\n"); |
---|
| 503 | fprintf (fp, "systemdict /colorimage known not {\n"); |
---|
| 504 | fprintf (fp, " /colorimage {\n"); |
---|
| 505 | fprintf (fp, " pop\n"); |
---|
| 506 | fprintf (fp, " pop\n"); |
---|
| 507 | fprintf (fp, " /rgbproc exch def\n"); |
---|
| 508 | fprintf (fp, " { bwproc } image\n"); |
---|
| 509 | fprintf (fp, " } def\n"); |
---|
| 510 | fprintf (fp, "} if\n"); |
---|
| 511 | fprintf (fp, "/picstr %d string def\n", width * components); |
---|
| 512 | fprintf (fp, "%d %d scale\n", width, height); |
---|
| 513 | fprintf (fp, "%d %d %d\n", width, height, 8); |
---|
| 514 | fprintf (fp, "[%d 0 0 %d 0 0]\n", width, height); |
---|
| 515 | fprintf (fp, "{currentfile picstr readhexstring pop}\n"); |
---|
| 516 | fprintf (fp, "false %d\n", components); |
---|
| 517 | fprintf (fp, "colorimage\n"); |
---|
| 518 | |
---|
| 519 | curpix = (GLubyte*) pixels; |
---|
| 520 | pos = 0; |
---|
| 521 | for (i = width*height*components; i>0; i--) { |
---|
| 522 | fprintf (fp, "%02hx ", *(curpix++)); |
---|
| 523 | if (++pos >= 32) { |
---|
| 524 | fprintf (fp, "\n"); |
---|
| 525 | pos = 0; |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | if (pos) |
---|
| 529 | fprintf (fp, "\n"); |
---|
| 530 | |
---|
| 531 | fprintf (fp, "grestore\n"); |
---|
| 532 | fprintf (fp, "showpage\n"); |
---|
| 533 | delete pixels; |
---|
| 534 | fclose (fp); |
---|
[941] | 535 | |
---|
| 536 | // Reset for next time (useful is size change) |
---|
| 537 | fPrintSizeX = 0; |
---|
| 538 | fPrintSizeY = 0; |
---|
| 539 | |
---|
| 540 | return true; |
---|
[914] | 541 | } |
---|
| 542 | |
---|
[918] | 543 | |
---|
[941] | 544 | bool G4OpenGLViewer::printGl2PS() { |
---|
[918] | 545 | |
---|
[941] | 546 | int width = 0; |
---|
| 547 | int height = 0; |
---|
| 548 | |
---|
| 549 | if (fPrintSizeX == 0) { |
---|
| 550 | width = fWinSize_x; |
---|
| 551 | } else { |
---|
| 552 | width = fPrintSizeX; |
---|
| 553 | } |
---|
| 554 | if (fPrintSizeY == 0) { |
---|
| 555 | height = fWinSize_y; |
---|
| 556 | } else { |
---|
| 557 | height = fPrintSizeY; |
---|
| 558 | } |
---|
| 559 | |
---|
[938] | 560 | if (!fGL2PSAction) return false; |
---|
[929] | 561 | |
---|
[938] | 562 | fGL2PSAction->setFileName(fPrintFilename.c_str()); |
---|
| 563 | // try to resize |
---|
| 564 | int X = fWinSize_x; |
---|
| 565 | int Y = fWinSize_y; |
---|
[929] | 566 | |
---|
[938] | 567 | fWinSize_x = width; |
---|
| 568 | fWinSize_y = height; |
---|
| 569 | ResizeGLView(); |
---|
[921] | 570 | if (fGL2PSAction->enableFileWriting()) { |
---|
[931] | 571 | DrawView (); |
---|
[921] | 572 | fGL2PSAction->disableFileWriting(); |
---|
| 573 | } |
---|
[929] | 574 | |
---|
[938] | 575 | fWinSize_x = X; |
---|
| 576 | fWinSize_y = Y; |
---|
| 577 | ResizeGLView(); |
---|
[929] | 578 | |
---|
[941] | 579 | // Reset for next time (useful is size change) |
---|
| 580 | fPrintSizeX = 0; |
---|
| 581 | fPrintSizeY = 0; |
---|
| 582 | |
---|
[938] | 583 | return true; |
---|
[918] | 584 | } |
---|
| 585 | |
---|
[712] | 586 | GLdouble G4OpenGLViewer::getSceneNearWidth() |
---|
| 587 | { |
---|
| 588 | const G4Point3D targetPoint |
---|
| 589 | = fSceneHandler.GetScene()->GetStandardTargetPoint() |
---|
| 590 | + fVP.GetCurrentTargetPoint (); |
---|
| 591 | G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius(); |
---|
| 592 | if(radius<=0.) radius = 1.; |
---|
| 593 | const G4double cameraDistance = fVP.GetCameraDistance (radius); |
---|
| 594 | const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius); |
---|
| 595 | return 2 * fVP.GetFrontHalfHeight (pnear, radius); |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | GLdouble G4OpenGLViewer::getSceneFarWidth() |
---|
| 599 | { |
---|
| 600 | const G4Point3D targetPoint |
---|
| 601 | = fSceneHandler.GetScene()->GetStandardTargetPoint() |
---|
| 602 | + fVP.GetCurrentTargetPoint (); |
---|
| 603 | G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius(); |
---|
| 604 | if(radius<=0.) radius = 1.; |
---|
| 605 | const G4double cameraDistance = fVP.GetCameraDistance (radius); |
---|
| 606 | const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius); |
---|
| 607 | const GLdouble pfar = fVP.GetFarDistance (cameraDistance, pnear, radius); |
---|
| 608 | return 2 * fVP.GetFrontHalfHeight (pfar, radius); |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | |
---|
| 612 | GLdouble G4OpenGLViewer::getSceneDepth() |
---|
| 613 | { |
---|
| 614 | const G4Point3D targetPoint |
---|
| 615 | = fSceneHandler.GetScene()->GetStandardTargetPoint() |
---|
| 616 | + fVP.GetCurrentTargetPoint (); |
---|
| 617 | G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius(); |
---|
| 618 | if(radius<=0.) radius = 1.; |
---|
| 619 | const G4double cameraDistance = fVP.GetCameraDistance (radius); |
---|
| 620 | const GLdouble pnear = fVP.GetNearDistance (cameraDistance, radius); |
---|
| 621 | return fVP.GetFarDistance (cameraDistance, pnear, radius)- pnear; |
---|
| 622 | } |
---|
| 623 | |
---|
| 624 | |
---|
[593] | 625 | |
---|
[798] | 626 | void G4OpenGLViewer::rotateScene(G4double dx, G4double dy,G4double deltaRotation) |
---|
| 627 | { |
---|
| 628 | |
---|
| 629 | G4Vector3D vp; |
---|
| 630 | G4Vector3D up; |
---|
| 631 | |
---|
| 632 | G4Vector3D xprime; |
---|
| 633 | G4Vector3D yprime; |
---|
| 634 | G4Vector3D zprime; |
---|
| 635 | |
---|
| 636 | G4double delta_alpha; |
---|
| 637 | G4double delta_theta; |
---|
| 638 | |
---|
| 639 | G4Vector3D new_vp; |
---|
| 640 | G4Vector3D new_up; |
---|
| 641 | |
---|
| 642 | G4double cosalpha; |
---|
| 643 | G4double sinalpha; |
---|
| 644 | |
---|
| 645 | G4Vector3D a1; |
---|
| 646 | G4Vector3D a2; |
---|
| 647 | G4Vector3D delta; |
---|
| 648 | G4Vector3D viewPoint; |
---|
| 649 | |
---|
| 650 | |
---|
| 651 | //phi spin stuff here |
---|
| 652 | |
---|
| 653 | vp = fVP.GetViewpointDirection ().unit (); |
---|
| 654 | up = fVP.GetUpVector ().unit (); |
---|
| 655 | |
---|
| 656 | yprime = (up.cross(vp)).unit(); |
---|
| 657 | zprime = (vp.cross(yprime)).unit(); |
---|
| 658 | |
---|
| 659 | if (fVP.GetLightsMoveWithCamera()) { |
---|
| 660 | delta_alpha = dy * deltaRotation; |
---|
| 661 | delta_theta = -dx * deltaRotation; |
---|
| 662 | } else { |
---|
| 663 | delta_alpha = -dy * deltaRotation; |
---|
| 664 | delta_theta = dx * deltaRotation; |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | delta_alpha *= deg; |
---|
| 668 | delta_theta *= deg; |
---|
| 669 | |
---|
| 670 | new_vp = std::cos(delta_alpha) * vp + std::sin(delta_alpha) * zprime; |
---|
| 671 | |
---|
| 672 | // to avoid z rotation flipping |
---|
| 673 | // to allow more than 360° rotation |
---|
[847] | 674 | |
---|
[801] | 675 | const G4Point3D targetPoint |
---|
| 676 | = fSceneHandler.GetScene()->GetStandardTargetPoint() |
---|
| 677 | + fVP.GetCurrentTargetPoint (); |
---|
| 678 | G4double radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius(); |
---|
| 679 | if(radius<=0.) radius = 1.; |
---|
| 680 | const G4double cameraDistance = fVP.GetCameraDistance (radius); |
---|
| 681 | const G4Point3D cameraPosition = |
---|
| 682 | targetPoint + cameraDistance * fVP.GetViewpointDirection().unit(); |
---|
| 683 | |
---|
[798] | 684 | if (fVP.GetLightsMoveWithCamera()) { |
---|
| 685 | new_up = (new_vp.cross(yprime)).unit(); |
---|
| 686 | if (new_vp.z()*vp.z() <0) { |
---|
| 687 | new_up.set(new_up.x(),-new_up.y(),new_up.z()); |
---|
| 688 | } |
---|
| 689 | } else { |
---|
| 690 | new_up = up; |
---|
| 691 | if (new_vp.z()*vp.z() <0) { |
---|
| 692 | new_up.set(new_up.x(),-new_up.y(),new_up.z()); |
---|
| 693 | } |
---|
| 694 | } |
---|
| 695 | fVP.SetUpVector(new_up); |
---|
| 696 | //////////////// |
---|
| 697 | // Rotates by fixed azimuthal angle delta_theta. |
---|
| 698 | |
---|
| 699 | cosalpha = new_up.dot (new_vp.unit()); |
---|
| 700 | sinalpha = std::sqrt (1. - std::pow (cosalpha, 2)); |
---|
| 701 | yprime = (new_up.cross (new_vp.unit())).unit (); |
---|
| 702 | xprime = yprime.cross (new_up); |
---|
| 703 | // Projection of vp on plane perpendicular to up... |
---|
| 704 | a1 = sinalpha * xprime; |
---|
| 705 | // Required new projection... |
---|
| 706 | a2 = sinalpha * (std::cos (delta_theta) * xprime + std::sin (delta_theta) * yprime); |
---|
| 707 | // Required Increment vector... |
---|
| 708 | delta = a2 - a1; |
---|
| 709 | // So new viewpoint is... |
---|
| 710 | viewPoint = new_vp.unit() + delta; |
---|
| 711 | |
---|
| 712 | fVP.SetViewAndLights (viewPoint); |
---|
| 713 | } |
---|
| 714 | |
---|
[529] | 715 | #endif |
---|