Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // >> 27 // $Id$ 27 // 28 // 28 // 29 // 29 // Andrew Walkden 10th February 1997 30 // Andrew Walkden 10th February 1997 30 // OpenGL immediate scene - draws immediately 31 // OpenGL immediate scene - draws immediately to buffer 31 // (saving space on 32 // (saving space on server). 32 33 >> 34 #ifdef G4VIS_BUILD_OPENGL_DRIVER >> 35 33 #include "G4OpenGLImmediateSceneHandler.hh" 36 #include "G4OpenGLImmediateSceneHandler.hh" 34 37 35 #include "G4OpenGLViewer.hh" 38 #include "G4OpenGLViewer.hh" 36 #include "G4OpenGLTransform3D.hh" 39 #include "G4OpenGLTransform3D.hh" 37 #include "G4Polyline.hh" 40 #include "G4Polyline.hh" 38 #include "G4Polymarker.hh" 41 #include "G4Polymarker.hh" 39 #include "G4Text.hh" 42 #include "G4Text.hh" 40 #include "G4Circle.hh" 43 #include "G4Circle.hh" 41 #include "G4Square.hh" 44 #include "G4Square.hh" >> 45 #include "G4Scale.hh" 42 #include "G4Polyhedron.hh" 46 #include "G4Polyhedron.hh" 43 #include "G4AttHolder.hh" 47 #include "G4AttHolder.hh" 44 48 45 #include <typeinfo> 49 #include <typeinfo> 46 50 47 G4OpenGLImmediateSceneHandler::G4OpenGLImmedia 51 G4OpenGLImmediateSceneHandler::G4OpenGLImmediateSceneHandler 48 (G4VGraphicsSystem& system,const G4String& nam 52 (G4VGraphicsSystem& system,const G4String& name): 49 G4OpenGLSceneHandler (system, fSceneIdCount+ 53 G4OpenGLSceneHandler (system, fSceneIdCount++, name) 50 {} 54 {} 51 55 52 G4OpenGLImmediateSceneHandler::~G4OpenGLImmedi 56 G4OpenGLImmediateSceneHandler::~G4OpenGLImmediateSceneHandler () 53 {} 57 {} 54 58 55 #include <iomanip> 59 #include <iomanip> 56 60 57 G4bool G4OpenGLImmediateSceneHandler::AddPrimi 61 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4VMarker& visible) 58 { 62 { 59 return AddPrimitivePreambleInternal(visibl 63 return AddPrimitivePreambleInternal(visible, true, false); 60 } 64 } 61 G4bool G4OpenGLImmediateSceneHandler::AddPrimi 65 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Polyline& visible) 62 { 66 { 63 return AddPrimitivePreambleInternal(visible, 67 return AddPrimitivePreambleInternal(visible, false, true); 64 } 68 } 65 G4bool G4OpenGLImmediateSceneHandler::AddPrimi 69 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Polyhedron& visible) 66 { 70 { 67 return AddPrimitivePreambleInternal(visible, 71 return AddPrimitivePreambleInternal(visible, false, false); 68 } 72 } 69 73 70 G4bool G4OpenGLImmediateSceneHandler::AddPrimi 74 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreambleInternal(const G4Visible& visible, bool isMarker, bool isPolyline) 71 { 75 { 72 // Get applicable vis attributes for all pri 76 // Get applicable vis attributes for all primitives. 73 fpVisAttribs = fpViewer->GetApplicableVisAtt 77 fpVisAttribs = fpViewer->GetApplicableVisAttributes(visible.GetVisAttributes()); 74 const G4Colour& c = GetColour (); 78 const G4Colour& c = GetColour (); 75 G4double opacity = c.GetAlpha (); 79 G4double opacity = c.GetAlpha (); 76 80 77 G4bool transparency_enabled = true; 81 G4bool transparency_enabled = true; 78 G4bool isMarkerNotHidden = true; 82 G4bool isMarkerNotHidden = true; 79 G4OpenGLViewer* pViewer = dynamic_cast<G4Ope 83 G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer); 80 if (pViewer) { 84 if (pViewer) { 81 transparency_enabled = pViewer->transparen 85 transparency_enabled = pViewer->transparency_enabled; 82 isMarkerNotHidden = pViewer->fVP.IsMarkerN 86 isMarkerNotHidden = pViewer->fVP.IsMarkerNotHidden(); 83 } 87 } 84 88 85 G4bool isMarkerOrPolyline = isMarker || isPo 89 G4bool isMarkerOrPolyline = isMarker || isPolyline; 86 G4bool treatAsTransparent = transparency_ena 90 G4bool treatAsTransparent = transparency_enabled && opacity < 1.; 87 G4bool treatAsNotHidden = isMarkerNotHidden 91 G4bool treatAsNotHidden = isMarkerNotHidden && (isMarker || isPolyline); 88 92 89 if (fProcessing2D) glDisable (GL_DEPTH_TEST) 93 if (fProcessing2D) glDisable (GL_DEPTH_TEST); 90 else { 94 else { 91 if (isMarkerOrPolyline && isMarkerNotHidde 95 if (isMarkerOrPolyline && isMarkerNotHidden) 92 glDisable (GL_DEPTH_TEST); 96 glDisable (GL_DEPTH_TEST); 93 else {glEnable (GL_DEPTH_TEST); glDepthFun 97 else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);} 94 } 98 } 95 99 96 if (fThreePassCapable) { 100 if (fThreePassCapable) { 97 101 98 // Ensure transparent objects are drawn op 102 // Ensure transparent objects are drawn opaque ones and before 99 // non-hidden markers. The problem of ble 103 // non-hidden markers. The problem of blending/transparency/alpha 100 // is quite a tricky one - see History of 104 // is quite a tricky one - see History of opengl-V07-01-01/2/3. 101 if (!(fSecondPassForTransparency || fThird 105 if (!(fSecondPassForTransparency || fThirdPassForNonHiddenMarkers)) { 102 // First pass... 106 // First pass... 103 if (treatAsTransparent) { // Request pa 107 if (treatAsTransparent) { // Request pass for transparent objects... 104 fSecondPassForTransparencyRequested = 108 fSecondPassForTransparencyRequested = true; 105 } 109 } 106 if (treatAsNotHidden) { // Request pa 110 if (treatAsNotHidden) { // Request pass for non-hidden markers... 107 fThirdPassForNonHiddenMarkersRequested 111 fThirdPassForNonHiddenMarkersRequested = true; 108 } 112 } 109 // On first pass, transparent objects an 113 // On first pass, transparent objects and non-hidden markers are not drawn... 110 if (treatAsTransparent || treatAsNotHidd 114 if (treatAsTransparent || treatAsNotHidden) { 111 return false; 115 return false; 112 } 116 } 113 } 117 } 114 118 115 // On second pass, only transparent object 119 // On second pass, only transparent objects are drawn... 116 if (fSecondPassForTransparency) { 120 if (fSecondPassForTransparency) { 117 if (!treatAsTransparent) { 121 if (!treatAsTransparent) { 118 return false; 122 return false; 119 } 123 } 120 } 124 } 121 125 122 // On third pass, only non-hidden markers 126 // On third pass, only non-hidden markers are drawn... 123 if (fThirdPassForNonHiddenMarkers) { 127 if (fThirdPassForNonHiddenMarkers) { 124 if (!treatAsNotHidden) { 128 if (!treatAsNotHidden) { 125 return false; 129 return false; 126 } 130 } 127 } 131 } 128 } // fThreePassCapable 132 } // fThreePassCapable 129 133 130 // Loads G4Atts for picking... 134 // Loads G4Atts for picking... 131 if (fpViewer->GetViewParameters().IsPicking( 135 if (fpViewer->GetViewParameters().IsPicking()) { 132 glLoadName(++fPickName); 136 glLoadName(++fPickName); 133 G4AttHolder* holder = new G4AttHolder; 137 G4AttHolder* holder = new G4AttHolder; 134 LoadAtts(visible, holder); 138 LoadAtts(visible, holder); 135 fPickMap[fPickName] = holder; 139 fPickMap[fPickName] = holder; 136 } 140 } 137 141 138 if (transparency_enabled) { 142 if (transparency_enabled) { 139 glColor4d(c.GetRed(),c.GetGreen(),c.GetBlu 143 glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha()); 140 } else { 144 } else { 141 glColor3d(c.GetRed(),c.GetGreen(),c.GetBlu 145 glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue()); 142 } 146 } 143 147 144 return true; 148 return true; 145 } 149 } 146 150 147 void G4OpenGLImmediateSceneHandler::AddPrimiti 151 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyline& polyline) 148 { 152 { 149 G4bool furtherprocessing = AddPrimitivePream 153 G4bool furtherprocessing = AddPrimitivePreamble(polyline); 150 if (furtherprocessing) { 154 if (furtherprocessing) { 151 G4OpenGLSceneHandler::AddPrimitive(polylin 155 G4OpenGLSceneHandler::AddPrimitive(polyline); 152 } 156 } 153 } 157 } 154 158 155 void G4OpenGLImmediateSceneHandler::AddPrimiti 159 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polymarker& polymarker) 156 { 160 { 157 G4bool furtherprocessing = AddPrimitivePream 161 G4bool furtherprocessing = AddPrimitivePreamble(polymarker); 158 if (furtherprocessing) { 162 if (furtherprocessing) { 159 G4OpenGLSceneHandler::AddPrimitive(polymar 163 G4OpenGLSceneHandler::AddPrimitive(polymarker); 160 } 164 } 161 } 165 } 162 166 163 void G4OpenGLImmediateSceneHandler::AddPrimiti 167 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Text& text) 164 { 168 { 165 // Note: colour is still handled in 169 // Note: colour is still handled in 166 // G4OpenGLSceneHandler::AddPrimitive(const 170 // G4OpenGLSceneHandler::AddPrimitive(const G4Text&). 167 G4bool furtherprocessing = AddPrimitivePream 171 G4bool furtherprocessing = AddPrimitivePreamble(text); 168 if (furtherprocessing) { 172 if (furtherprocessing) { 169 G4OpenGLSceneHandler::AddPrimitive(text); 173 G4OpenGLSceneHandler::AddPrimitive(text); 170 } 174 } 171 } 175 } 172 176 173 void G4OpenGLImmediateSceneHandler::AddPrimiti 177 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Circle& circle) 174 { 178 { 175 G4bool furtherprocessing = AddPrimitivePream 179 G4bool furtherprocessing = AddPrimitivePreamble(circle); 176 if (furtherprocessing) { 180 if (furtherprocessing) { 177 G4OpenGLSceneHandler::AddPrimitive(circle) 181 G4OpenGLSceneHandler::AddPrimitive(circle); 178 } 182 } 179 } 183 } 180 184 181 void G4OpenGLImmediateSceneHandler::AddPrimiti 185 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Square& square) 182 { 186 { 183 G4bool furtherprocessing = AddPrimitivePream 187 G4bool furtherprocessing = AddPrimitivePreamble(square); 184 if (furtherprocessing) { 188 if (furtherprocessing) { 185 G4OpenGLSceneHandler::AddPrimitive(square) 189 G4OpenGLSceneHandler::AddPrimitive(square); 186 } 190 } 187 } 191 } 188 192 >> 193 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Scale& scale) >> 194 { >> 195 G4bool furtherprocessing = AddPrimitivePreamble(scale); >> 196 if (furtherprocessing) { >> 197 G4OpenGLSceneHandler::AddPrimitive(scale); >> 198 } >> 199 } >> 200 189 void G4OpenGLImmediateSceneHandler::AddPrimiti 201 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron) 190 { 202 { 191 // Note: colour is still handled in 203 // Note: colour is still handled in 192 // G4OpenGLSceneHandler::AddPrimitive(const 204 // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&). 193 G4bool furtherprocessing = AddPrimitivePream 205 G4bool furtherprocessing = AddPrimitivePreamble(polyhedron); 194 if (furtherprocessing) { 206 if (furtherprocessing) { 195 G4OpenGLSceneHandler::AddPrimitive(polyhed 207 G4OpenGLSceneHandler::AddPrimitive(polyhedron); 196 } 208 } 197 } 209 } 198 210 199 void G4OpenGLImmediateSceneHandler::BeginPrimi 211 void G4OpenGLImmediateSceneHandler::BeginPrimitives 200 (const G4Transform3D& objectTransformation) 212 (const G4Transform3D& objectTransformation) 201 { 213 { 202 G4OpenGLSceneHandler::BeginPrimitives (objec 214 G4OpenGLSceneHandler::BeginPrimitives (objectTransformation); 203 215 204 G4OpenGLTransform3D oglt (objectTransformati 216 G4OpenGLTransform3D oglt (objectTransformation); 205 217 206 glPushMatrix(); 218 glPushMatrix(); 207 219 208 /*************************** Check matrix. 220 /*************************** Check matrix. 209 const GLdouble* m = oglt.GetGLMatrix (); 221 const GLdouble* m = oglt.GetGLMatrix (); 210 G4cout << "G4OpenGLTransform3D matrix:"; 222 G4cout << "G4OpenGLTransform3D matrix:"; 211 for (int i = 0; i < 16; i++) { 223 for (int i = 0; i < 16; i++) { 212 if ((i % 4) == 0) G4cout << '\n'; 224 if ((i % 4) == 0) G4cout << '\n'; 213 G4cout << std::setw (15) << m[i]; 225 G4cout << std::setw (15) << m[i]; 214 } 226 } 215 G4cout << G4endl; 227 G4cout << G4endl; 216 *****************************************/ 228 *****************************************/ 217 229 218 glMultMatrixd (oglt.GetGLMatrix ()); 230 glMultMatrixd (oglt.GetGLMatrix ()); 219 } 231 } 220 232 221 void G4OpenGLImmediateSceneHandler::EndPrimiti 233 void G4OpenGLImmediateSceneHandler::EndPrimitives () 222 { 234 { 223 glPopMatrix(); 235 glPopMatrix(); 224 236 225 // See all primitives immediately... At lea 237 // See all primitives immediately... At least soon... 226 ScaledFlush(); 238 ScaledFlush(); 227 239 228 G4OpenGLSceneHandler::EndPrimitives (); 240 G4OpenGLSceneHandler::EndPrimitives (); 229 } 241 } 230 242 231 void G4OpenGLImmediateSceneHandler::BeginPrimi 243 void G4OpenGLImmediateSceneHandler::BeginPrimitives2D 232 (const G4Transform3D& objectTransformation) 244 (const G4Transform3D& objectTransformation) 233 { 245 { 234 G4OpenGLSceneHandler::BeginPrimitives2D(obje 246 G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation); 235 247 236 // Push current 3D world matrices and load i 248 // Push current 3D world matrices and load identity to define screen 237 // coordinates... 249 // coordinates... 238 glMatrixMode (GL_PROJECTION); 250 glMatrixMode (GL_PROJECTION); 239 glPushMatrix(); 251 glPushMatrix(); 240 glLoadIdentity(); 252 glLoadIdentity(); 241 G4OpenGLViewer* pViewer = dynamic_cast<G4Ope 253 G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer); 242 if (pViewer) { 254 if (pViewer) { 243 pViewer->g4GlOrtho (-1., 1., -1., 1., -G4O 255 pViewer->g4GlOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG); 244 } 256 } 245 glMatrixMode (GL_MODELVIEW); 257 glMatrixMode (GL_MODELVIEW); 246 glPushMatrix(); 258 glPushMatrix(); 247 glLoadIdentity(); 259 glLoadIdentity(); 248 G4OpenGLTransform3D oglt (objectTransformati 260 G4OpenGLTransform3D oglt (objectTransformation); 249 glMultMatrixd (oglt.GetGLMatrix ()); 261 glMultMatrixd (oglt.GetGLMatrix ()); 250 glDisable(GL_DEPTH_TEST); // But see parent 262 glDisable(GL_DEPTH_TEST); // But see parent scene handler!! In >> 263 #ifndef G4OPENGL_VERSION_2 251 glDisable (GL_LIGHTING); // some cases, we 264 glDisable (GL_LIGHTING); // some cases, we need to re-iterate this. >> 265 #endif 252 } 266 } 253 267 254 void G4OpenGLImmediateSceneHandler::EndPrimiti 268 void G4OpenGLImmediateSceneHandler::EndPrimitives2D() 255 { 269 { 256 // Pop current 3D world matrices back again. 270 // Pop current 3D world matrices back again... 257 glMatrixMode (GL_PROJECTION); 271 glMatrixMode (GL_PROJECTION); 258 glPopMatrix(); 272 glPopMatrix(); 259 glMatrixMode (GL_MODELVIEW); 273 glMatrixMode (GL_MODELVIEW); 260 glPopMatrix(); 274 glPopMatrix(); 261 275 262 // See all primitives immediately... At lea 276 // See all primitives immediately... At least soon... 263 ScaledFlush(); 277 ScaledFlush(); 264 278 265 G4OpenGLSceneHandler::EndPrimitives2D (); 279 G4OpenGLSceneHandler::EndPrimitives2D (); 266 } 280 } 267 281 268 void G4OpenGLImmediateSceneHandler::BeginModel 282 void G4OpenGLImmediateSceneHandler::BeginModeling () { 269 G4VSceneHandler::BeginModeling(); 283 G4VSceneHandler::BeginModeling(); 270 } 284 } 271 285 272 void G4OpenGLImmediateSceneHandler::EndModelin 286 void G4OpenGLImmediateSceneHandler::EndModeling () { 273 G4VSceneHandler::EndModeling (); 287 G4VSceneHandler::EndModeling (); 274 } 288 } 275 289 276 void G4OpenGLImmediateSceneHandler::ClearTrans 290 void G4OpenGLImmediateSceneHandler::ClearTransientStore () 277 { 291 { 278 // Nothing to do except redraw the scene rea 292 // Nothing to do except redraw the scene ready for the next event. 279 if (fpViewer) { 293 if (fpViewer) { 280 fpViewer -> SetView (); 294 fpViewer -> SetView (); 281 fpViewer -> ClearView (); 295 fpViewer -> ClearView (); 282 fpViewer -> DrawView (); 296 fpViewer -> DrawView (); 283 } 297 } 284 } 298 } 285 299 286 G4int G4OpenGLImmediateSceneHandler::fSceneIdC 300 G4int G4OpenGLImmediateSceneHandler::fSceneIdCount = 0; >> 301 >> 302 #endif 287 303