G4OpenGLImmediateSceneHandler.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // Andrew Walkden  10th February 1997
00031 // OpenGL immediate scene - draws immediately to buffer
00032 //                           (saving space on server).
00033 
00034 #ifdef G4VIS_BUILD_OPENGL_DRIVER
00035 
00036 // Included here - problems with HP compiler if not before other includes?
00037 #include "G4NURBS.hh"
00038 
00039 // Here follows a special for Mesa, the OpenGL emulator.  Does not affect
00040 // other OpenGL's, as far as I'm aware.   John Allison 18/9/96.
00041 #define CENTERLINE_CLPP  /* CenterLine C++ workaround: */
00042 // Also seems to be required for HP's CC and AIX xlC, at least.
00043 
00044 #include "G4OpenGLImmediateSceneHandler.hh"
00045 
00046 #include "G4OpenGLViewer.hh"
00047 #include "G4OpenGLTransform3D.hh"
00048 #include "G4Polyline.hh"
00049 #include "G4Polymarker.hh"
00050 #include "G4Text.hh"
00051 #include "G4Circle.hh"
00052 #include "G4Square.hh"
00053 #include "G4Scale.hh"
00054 #include "G4Polyhedron.hh"
00055 #include "G4AttHolder.hh"
00056 
00057 #include <typeinfo>
00058 
00059 G4OpenGLImmediateSceneHandler::G4OpenGLImmediateSceneHandler
00060 (G4VGraphicsSystem& system,const G4String& name):
00061   G4OpenGLSceneHandler (system, fSceneIdCount++, name)
00062 {}
00063 
00064 G4OpenGLImmediateSceneHandler::~G4OpenGLImmediateSceneHandler ()
00065 {}
00066 
00067 #include <iomanip>
00068 
00069 G4bool G4OpenGLImmediateSceneHandler::AddPrimitivePreamble(const G4Visible& visible)
00070 {
00071   const G4Colour& c = GetColour (visible);
00072   G4double opacity = c.GetAlpha ();
00073   
00074   G4bool transparency_enabled = true;
00075   G4bool isMarkerNotHidden = true;
00076   G4OpenGLViewer* pViewer = dynamic_cast<G4OpenGLViewer*>(fpViewer);
00077   if (pViewer) {
00078     transparency_enabled = pViewer->transparency_enabled;
00079     isMarkerNotHidden = pViewer->fVP.IsMarkerNotHidden();
00080   }
00081   
00082   G4bool isMarker = false;
00083   try {
00084     (void) dynamic_cast<const G4VMarker&>(visible);
00085     isMarker = true;
00086   }
00087   catch (std::bad_cast) {}
00088   
00089   G4bool isPolyline = false;
00090   try {
00091     (void) dynamic_cast<const G4Polyline&>(visible);
00092     isPolyline = true;
00093   }
00094   catch (std::bad_cast) {}
00095   
00096   G4bool isMarkerOrPolyline = isMarker || isPolyline;
00097   G4bool treatAsTransparent = transparency_enabled && opacity < 1.;
00098   G4bool treatAsNotHidden = isMarkerNotHidden && (isMarker || isPolyline);
00099   
00100   if (fProcessing2D) glDisable (GL_DEPTH_TEST);
00101   else {
00102     if (isMarkerOrPolyline && isMarkerNotHidden)
00103       glDisable (GL_DEPTH_TEST);
00104     else {glEnable (GL_DEPTH_TEST); glDepthFunc (GL_LEQUAL);}
00105   }
00106 
00107   if (fThreePassCapable) {
00108     
00109     // Ensure transparent objects are drawn opaque ones and before
00110     // non-hidden markers.  The problem of blending/transparency/alpha
00111     // is quite a tricky one - see History of opengl-V07-01-01/2/3.
00112     if (!(fSecondPassForTransparency || fThirdPassForNonHiddenMarkers)) {
00113       // First pass...
00114       if (treatAsTransparent) {  // Request pass for transparent objects...
00115         fSecondPassForTransparencyRequested = true;
00116       }
00117       if (treatAsNotHidden) {    // Request pass for non-hidden markers...
00118         fThirdPassForNonHiddenMarkersRequested = true;
00119       }
00120       // On first pass, transparent objects and non-hidden markers are not drawn...
00121       if (treatAsTransparent || treatAsNotHidden) {
00122         return false;
00123       }
00124     }
00125     
00126     // On second pass, only transparent objects are drawn...
00127     if (fSecondPassForTransparency) {
00128       if (!treatAsTransparent) {
00129         return false;
00130       }
00131     }
00132     
00133     // On third pass, only non-hidden markers are drawn...
00134     if (fThirdPassForNonHiddenMarkers) {
00135       if (!treatAsNotHidden) {
00136         return false;
00137       }
00138     }
00139   }  // fThreePassCapable
00140   
00141   // Loads G4Atts for picking...
00142   if (fpViewer->GetViewParameters().IsPicking()) {
00143     glLoadName(++fPickName);
00144     G4AttHolder* holder = new G4AttHolder;
00145     LoadAtts(visible, holder);
00146     fPickMap[fPickName] = holder;
00147   }
00148 
00149   if (transparency_enabled) {
00150     glColor4d(c.GetRed(),c.GetGreen(),c.GetBlue(),c.GetAlpha());
00151   } else {
00152     glColor3d(c.GetRed(),c.GetGreen(),c.GetBlue());    
00153   }
00154 
00155   return true;
00156 }
00157 
00158 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyline& polyline)
00159 {
00160   G4bool furtherprocessing = AddPrimitivePreamble(polyline);
00161   if (furtherprocessing) {
00162     G4OpenGLSceneHandler::AddPrimitive(polyline);
00163   }
00164 }
00165 
00166 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polymarker& polymarker)
00167 {
00168   G4bool furtherprocessing = AddPrimitivePreamble(polymarker);
00169   if (furtherprocessing) {
00170     G4OpenGLSceneHandler::AddPrimitive(polymarker);
00171   }
00172 }
00173 
00174 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Text& text)
00175 {
00176   // Note: colour is still handled in
00177   // G4OpenGLSceneHandler::AddPrimitive(const G4Text&).
00178   G4bool furtherprocessing = AddPrimitivePreamble(text);
00179   if (furtherprocessing) {
00180     G4OpenGLSceneHandler::AddPrimitive(text);
00181   }
00182 }
00183 
00184 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Circle& circle)
00185 {
00186   G4bool furtherprocessing = AddPrimitivePreamble(circle);
00187   if (furtherprocessing) {
00188     G4OpenGLSceneHandler::AddPrimitive(circle);
00189   }
00190 }
00191 
00192 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Square& square)
00193 {
00194   G4bool furtherprocessing = AddPrimitivePreamble(square);
00195   if (furtherprocessing) {
00196     G4OpenGLSceneHandler::AddPrimitive(square);
00197   }
00198 }
00199 
00200 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Scale& scale)
00201 {
00202   G4bool furtherprocessing = AddPrimitivePreamble(scale);
00203   if (furtherprocessing) {
00204     G4OpenGLSceneHandler::AddPrimitive(scale);
00205   }
00206 }
00207 
00208 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4Polyhedron& polyhedron)
00209 {
00210   // Note: colour is still handled in
00211   // G4OpenGLSceneHandler::AddPrimitive(const G4Polyhedron&).
00212   G4bool furtherprocessing = AddPrimitivePreamble(polyhedron);
00213   if (furtherprocessing) {
00214     G4OpenGLSceneHandler::AddPrimitive(polyhedron);
00215   }
00216 }
00217 
00218 void G4OpenGLImmediateSceneHandler::AddPrimitive (const G4NURBS& nurbs)
00219 {
00220   // Note: colour is still handled in
00221   // G4OpenGLSceneHandler::AddPrimitive(const G4NURBS&).
00222   G4bool furtherprocessing = AddPrimitivePreamble(nurbs);
00223   if (furtherprocessing) {
00224     G4OpenGLSceneHandler::AddPrimitive(nurbs);
00225   }
00226 }
00227 
00228 void G4OpenGLImmediateSceneHandler::BeginPrimitives
00229 (const G4Transform3D& objectTransformation)
00230 {
00231   G4OpenGLSceneHandler::BeginPrimitives (objectTransformation);
00232 
00233   G4OpenGLTransform3D oglt (objectTransformation);
00234 
00235   glPushMatrix();
00236 
00237   /*************************** Check matrix.
00238   const GLdouble* m = oglt.GetGLMatrix ();
00239   G4cout << "G4OpenGLTransform3D matrix:";
00240   for (int i = 0; i < 16; i++) {
00241     if ((i % 4) == 0) G4cout << '\n';
00242     G4cout << std::setw (15) << m[i];
00243   }
00244   G4cout << G4endl;
00245   *****************************************/
00246 
00247   glMultMatrixd (oglt.GetGLMatrix ());
00248 }
00249 
00250 void G4OpenGLImmediateSceneHandler::EndPrimitives ()
00251 {
00252   glPopMatrix();
00253 
00254   // See all primitives immediately...  At least soon...
00255   ScaledFlush();
00256 
00257   G4OpenGLSceneHandler::EndPrimitives ();
00258 }
00259 
00260 void G4OpenGLImmediateSceneHandler::BeginPrimitives2D
00261 (const G4Transform3D& objectTransformation)
00262 {
00263   G4OpenGLSceneHandler::BeginPrimitives2D(objectTransformation);
00264 
00265   // Push current 3D world matrices and load identity to define screen
00266   // coordinates...
00267   glMatrixMode (GL_PROJECTION);
00268   glPushMatrix();
00269   glLoadIdentity();
00270   glOrtho (-1., 1., -1., 1., -G4OPENGL_FLT_BIG, G4OPENGL_FLT_BIG);
00271   glMatrixMode (GL_MODELVIEW);
00272   glPushMatrix();
00273   glLoadIdentity();
00274   G4OpenGLTransform3D oglt (objectTransformation);
00275   glMultMatrixd (oglt.GetGLMatrix ());
00276   glDisable(GL_DEPTH_TEST);  // But see parent scene handler!!  In
00277   glDisable (GL_LIGHTING);   // some cases, we need to re-iterate this.
00278 }
00279 
00280 void G4OpenGLImmediateSceneHandler::EndPrimitives2D()
00281 {
00282   // Pop current 3D world matrices back again...
00283   glMatrixMode (GL_PROJECTION);
00284   glPopMatrix();
00285   glMatrixMode (GL_MODELVIEW);
00286   glPopMatrix();
00287 
00288   // See all primitives immediately...
00289   glFlush ();
00290 
00291   G4OpenGLSceneHandler::EndPrimitives2D ();
00292 }
00293 
00294 void G4OpenGLImmediateSceneHandler::BeginModeling () {
00295   G4VSceneHandler::BeginModeling();
00296 }
00297 
00298 void G4OpenGLImmediateSceneHandler::EndModeling () {
00299   G4VSceneHandler::EndModeling ();
00300 }
00301 
00302 void G4OpenGLImmediateSceneHandler::ClearTransientStore ()
00303 {
00304   // Nothing to do except redraw the scene ready for the next event.
00305   if (fpViewer) {
00306     fpViewer -> SetView ();
00307     fpViewer -> ClearView ();
00308     fpViewer -> DrawView ();
00309   }
00310 }
00311 
00312 G4int G4OpenGLImmediateSceneHandler::fSceneIdCount = 0;
00313 
00314 #endif

Generated on Mon May 27 17:49:09 2013 for Geant4 by  doxygen 1.4.7