G4OpenGLWin32Viewer.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 // G4OpenGLWin32Viewer : Class to provide WindowsNT specific
00031 //                     functionality for OpenGL in GEANT4
00032 //
00033 // 27/06/2003 : G.Barrand : implementation (at last !).
00034 
00035 #ifdef G4VIS_BUILD_OPENGLWIN32_DRIVER
00036 
00037 #include "G4OpenGLWin32Viewer.hh"
00038 #include "G4VViewer.hh"
00039 #include "G4VSceneHandler.hh"
00040 #include "G4OpenGLSceneHandler.hh"
00041 
00042 #include "G4ios.hh"
00043 #include "G4VisExtent.hh"
00044 #include "G4LogicalVolume.hh"
00045 #include "G4VSolid.hh"
00046 #include "G4Point3D.hh"
00047 #include "G4Normal3D.hh"
00048 
00049 
00051 void G4OpenGLWin32Viewer::SetView (
00052 ) 
00053 
00054 
00055 {
00056   if(!fHDC) return;
00057   if(!fHGLRC) return;
00058   ::wglMakeCurrent(fHDC,fHGLRC);
00059   G4OpenGLViewer::SetView ();  
00060 }
00061 
00063 void G4OpenGLWin32Viewer::ShowView (
00064 ) 
00065 
00066 
00067 {
00068   if(!fHDC) return;
00069   glFlush ();
00070   // Empty the Windows message queue :
00071   MSG event;
00072   while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
00073     ::TranslateMessage(&event);
00074     ::DispatchMessage (&event);
00075   }
00076 }
00077 
00079 void G4OpenGLWin32Viewer::GetWin32Connection (
00080 ) 
00081 
00082 
00083 {
00084 }
00085 
00087 void G4OpenGLWin32Viewer::CreateGLWin32Context (
00088 ) 
00089 
00090 
00091 {
00092 }
00093 
00095 void G4OpenGLWin32Viewer::CreateMainWindow (
00096 ) 
00097 
00098 
00099 {
00100   if(fWindow) return; //Done.
00101 
00102   // Bill Gates stuff...
00103   static const char className[] = "G4OpenGLWin32";
00104   static bool done = false;
00105   if(done==false) {
00106     WNDCLASS wc;
00107     wc.style = CS_HREDRAW | CS_VREDRAW;
00108     wc.lpfnWndProc = (WNDPROC)WindowProc;
00109     wc.cbClsExtra = 0;
00110     wc.cbWndExtra = 0;
00111     wc.hInstance = ::GetModuleHandle(NULL);
00112     wc.hIcon = LoadIcon  (NULL, IDI_APPLICATION);
00113     wc.hCursor = LoadCursor(NULL,IDC_CROSS);
00114     wc.hbrBackground = NULL;
00115     wc.lpszMenuName = className;
00116     wc.lpszClassName = className;
00117     ::RegisterClass(&wc);
00118     done = true;
00119   }  
00120   
00121   ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
00122 
00123   int x_res=GetSystemMetrics(SM_CXSCREEN);
00124   int y_res=GetSystemMetrics(SM_CYSCREEN);
00125   
00126   //FIXME : NOT tested !
00127   fWindow = ::CreateWindow(className,fName.c_str(), 
00128                            WS_OVERLAPPEDWINDOW,
00129                            //WS_CHILD | WS_VISIBLE,
00130                            //                      0,0,
00131                            fVP.GetWindowAbsoluteLocationHintX(x_res),
00132                            fVP.GetWindowAbsoluteLocationHintY(y_res),
00133                            getWinWidth(), getWinHeight(),
00134                            NULL, NULL, 
00135                            ::GetModuleHandle(NULL),
00136                            NULL);
00137   if(!fWindow) return;
00138 
00139   ::SetWindowLongPtr(fWindow,GWLP_USERDATA,LONG(this));
00140 
00141   // initialize OpenGL rendering :
00142   fHDC = ::GetDC(fWindow);
00143   if( fHDC && (SetWindowPixelFormat(fHDC)==TRUE) ) {
00144     fHGLRC = ::wglCreateContext(fHDC);
00145   }
00146   
00147   if(fHDC && fHGLRC) {
00148     ::wglMakeCurrent(fHDC,fHGLRC);
00149   }
00150 
00151   ::SetForegroundWindow(fWindow);
00152   ::ShowWindow(fWindow,SW_SHOWDEFAULT);
00153   ::UpdateWindow(fWindow);
00154   ::DrawMenuBar(fWindow);
00155 }
00156 
00158 G4OpenGLWin32Viewer::G4OpenGLWin32Viewer (
00159  G4OpenGLSceneHandler& scene
00160 )
00161 :G4VViewer (scene, -1)
00162 ,G4OpenGLViewer (scene)
00163 ,fWindow(0)
00164 ,fHDC(0)
00165 ,fHGLRC(0)
00168 {
00169 }
00170 
00172 G4OpenGLWin32Viewer::~G4OpenGLWin32Viewer (
00173 ) 
00174 
00175 
00176 {
00177   // This is the end (Jim Morisson).
00178   if (fViewId >= 0) {
00179     if(wglGetCurrentContext()!=NULL) wglMakeCurrent(NULL,NULL);
00180     if(fHGLRC)  {
00181       wglDeleteContext(fHGLRC);
00182       fHGLRC = NULL;
00183     }
00184     
00185     if(fWindow) {
00186       ::SetWindowLongPtr(fWindow,GWLP_USERDATA,LONG(NULL));
00187       if(fHDC) ::ReleaseDC(fWindow,fHDC);
00188       ::DestroyWindow(fWindow);
00189     }
00190   }
00191 }
00192 
00194 LRESULT CALLBACK G4OpenGLWin32Viewer::WindowProc ( 
00195  HWND   aWindow
00196 ,UINT   aMessage
00197 ,WPARAM aWParam
00198 ,LPARAM aLParam
00199 )
00202 {
00203 /*
00204   switch (aMessage) { 
00205   case WM_PAINT:{
00206     printf("debug : PAINT\n");
00207     HDC hDC;
00208     PAINTSTRUCT ps;
00209     hDC = BeginPaint(aWindow,&ps);
00210     if(This) {
00211       // FIXME : To have an automatic refresh someone have to redraw here.
00212     }
00213     EndPaint(aWindow, &ps);
00214 
00215     //FIXME : have to handle WM_RESIZE
00216     //pView->fWinSize_x = (G4int) width;
00217     //pView->fWinSize_y = (G4int) height;
00218     G4OpenGLWin32Viewer* This = 
00219       (G4OpenGLWin32Viewer*)::GetWindowLong(aWindow,GWL_USERDATA);
00220     if(This) {
00221       This->SetView();
00222       glViewport(0,0,This->fWinSize_x,This->fWinSize_y);
00223       This->ClearView();
00224       This->DrawView();
00225       // WARNING : the below empty the Windows message queue...
00226       This->FinishView();
00227     }
00228   } return 0;
00229   default:
00230     return DefWindowProc(aWindow,aMessage,aWParam,aLParam);
00231   }
00232 */
00233   return DefWindowProc(aWindow,aMessage,aWParam,aLParam);
00234 }
00235 
00237 bool G4OpenGLWin32Viewer::SetWindowPixelFormat(
00238  HDC aHdc
00239 )
00242 {
00243   // The ungessable...
00244 
00245   PIXELFORMATDESCRIPTOR pfd;
00246   pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
00247   pfd.nVersion = 1;
00248   pfd.dwFlags = 
00249     PFD_DRAW_TO_WINDOW | 
00250     PFD_SUPPORT_OPENGL | 
00251     PFD_DOUBLEBUFFER | 
00252     PFD_STEREO_DONTCARE;  
00253   pfd.iPixelType = PFD_TYPE_RGBA;
00254   pfd.cColorBits = 32;
00255   pfd.cRedBits = 8;
00256   pfd.cRedShift = 16;
00257   pfd.cGreenBits = 8;
00258   pfd.cGreenShift = 8;
00259   pfd.cBlueBits = 8;
00260   pfd.cBlueShift = 0;
00261   pfd.cAlphaBits = 0;
00262   pfd.cAlphaShift = 0;
00263   pfd.cAccumBits = 64;  
00264   pfd.cAccumRedBits = 16;
00265   pfd.cAccumGreenBits = 16;
00266   pfd.cAccumBlueBits = 16;
00267   pfd.cAccumAlphaBits = 0;
00268   pfd.cDepthBits = 32;
00269   pfd.cStencilBits = 8;
00270   pfd.cAuxBuffers = 0;
00271   pfd.iLayerType = PFD_MAIN_PLANE;
00272   pfd.bReserved = 0;
00273   pfd.dwLayerMask = 0;
00274   pfd.dwVisibleMask = 0;
00275   pfd.dwDamageMask = 0;
00276   
00277   int pixelIndex = ::ChoosePixelFormat(aHdc,&pfd);
00278   if (pixelIndex==0) {
00279     pixelIndex = 1;     
00280     if (::DescribePixelFormat(aHdc, 
00281                               pixelIndex, 
00282                               sizeof(PIXELFORMATDESCRIPTOR), 
00283                               &pfd)==0) {
00284       return false;
00285     }
00286   }
00287   if (::SetPixelFormat(aHdc,pixelIndex,&pfd)==FALSE) return false;
00288   return true;
00289 }
00290 
00291 
00292 #endif

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