Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4OpenGLWin32Viewer.cc
Go to the documentation of this file.
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 //
27 // $Id: G4OpenGLWin32Viewer.cc 97430 2016-06-03 07:20:11Z gcosmo $
28 //
29 //
30 // G4OpenGLWin32Viewer : Class to provide WindowsNT specific
31 // functionality for OpenGL in GEANT4
32 //
33 // 27/06/2003 : G.Barrand : implementation (at last !).
34 
35 #ifdef G4VIS_BUILD_OPENGLWIN32_DRIVER
36 
37 #include "G4OpenGLWin32Viewer.hh"
38 #include "G4VViewer.hh"
39 #include "G4VSceneHandler.hh"
40 #include "G4OpenGLSceneHandler.hh"
41 
42 #include "G4ios.hh"
43 #include "G4VisExtent.hh"
44 #include "G4LogicalVolume.hh"
45 #include "G4VSolid.hh"
46 #include "G4Point3D.hh"
47 #include "G4Normal3D.hh"
48 
49 
51 void G4OpenGLWin32Viewer::SetView (
52 )
53 {
56  if(!fHDC) return;
57  if(!fHGLRC) return;
58  ::wglMakeCurrent(fHDC,fHGLRC);
59  G4OpenGLViewer::SetView ();
60 }
61 
63 void G4OpenGLWin32Viewer::ShowView (
64 )
65 {
68  if(!fHDC) return;
69  glFlush ();
70  // Empty the Windows message queue :
71  MSG event;
72  while ( ::PeekMessage(&event, NULL, 0, 0, PM_REMOVE) ) {
73  ::TranslateMessage(&event);
74  ::DispatchMessage (&event);
75  }
76 }
77 
79 void G4OpenGLWin32Viewer::GetWin32Connection (
80 )
81 {
84 }
85 
87 void G4OpenGLWin32Viewer::CreateGLWin32Context (
88 )
89 {
92 }
93 
95 void G4OpenGLWin32Viewer::CreateMainWindow (
96 )
97 {
100  if(fWindow) return; //Done.
101 
102  // Bill Gates stuff...
103  static const char className[] = "G4OpenGLWin32";
104  static bool done = false;
105  if(done==false) {
106  WNDCLASS wc;
107  wc.style = CS_HREDRAW | CS_VREDRAW;
108  wc.lpfnWndProc = (WNDPROC)WindowProc;
109  wc.cbClsExtra = 0;
110  wc.cbWndExtra = 0;
111  wc.hInstance = ::GetModuleHandle(NULL);
112  wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
113  wc.hCursor = LoadCursor(NULL,IDC_CROSS);
114  wc.hbrBackground = NULL;
115  wc.lpszMenuName = className;
116  wc.lpszClassName = className;
117  ::RegisterClass(&wc);
118  done = true;
119  }
120 
121  ResizeWindow(fVP.GetWindowSizeHintX(),fVP.GetWindowSizeHintY());
122 
123  int x_res=GetSystemMetrics(SM_CXSCREEN);
124  int y_res=GetSystemMetrics(SM_CYSCREEN);
125 
126  //FIXME : NOT tested !
127  fWindow = ::CreateWindow(className,fName.c_str(),
128  WS_OVERLAPPEDWINDOW,
129  //WS_CHILD | WS_VISIBLE,
130  // 0,0,
131  fVP.GetWindowAbsoluteLocationHintX(x_res),
132  fVP.GetWindowAbsoluteLocationHintY(y_res),
133  getWinWidth(), getWinHeight(),
134  NULL, NULL,
135  ::GetModuleHandle(NULL),
136  NULL);
137  if(!fWindow) return;
138 
139  ::SetWindowLongPtr(fWindow,GWLP_USERDATA,LONG_PTR(this));
140 
141  // initialize OpenGL rendering :
142  fHDC = ::GetDC(fWindow);
143  if( fHDC && (SetWindowPixelFormat(fHDC)==TRUE) ) {
144  fHGLRC = ::wglCreateContext(fHDC);
145  }
146 
147  if(fHDC && fHGLRC) {
148  ::wglMakeCurrent(fHDC,fHGLRC);
149  }
150 
151  ::SetForegroundWindow(fWindow);
152  ::ShowWindow(fWindow,SW_SHOWDEFAULT);
153  ::UpdateWindow(fWindow);
154  ::DrawMenuBar(fWindow);
155 }
156 
158 G4OpenGLWin32Viewer::G4OpenGLWin32Viewer (
159  G4OpenGLSceneHandler& scene
160 )
161 :G4VViewer (scene, -1)
162 ,G4OpenGLViewer (scene)
163 ,fWindow(0)
164 ,fHDC(0)
165 ,fHGLRC(0)
168 {
169 }
170 
172 G4OpenGLWin32Viewer::~G4OpenGLWin32Viewer (
173 )
174 {
177  // This is the end (Jim Morisson).
178  if (fViewId >= 0) {
179  if(wglGetCurrentContext()!=NULL) wglMakeCurrent(NULL,NULL);
180  if(fHGLRC) {
181  wglDeleteContext(fHGLRC);
182  fHGLRC = NULL;
183  }
184 
185  if(fWindow) {
186  ::SetWindowLongPtr(fWindow,GWLP_USERDATA,LONG(NULL));
187  if(fHDC) ::ReleaseDC(fWindow,fHDC);
188  ::DestroyWindow(fWindow);
189  }
190  }
191 }
192 
194 LRESULT CALLBACK G4OpenGLWin32Viewer::WindowProc (
195  HWND aWindow
196 ,UINT aMessage
197 ,WPARAM aWParam
198 ,LPARAM aLParam
199 )
202 {
203 /*
204  switch (aMessage) {
205  case WM_PAINT:{
206  printf("debug : PAINT\n");
207  HDC hDC;
208  PAINTSTRUCT ps;
209  hDC = BeginPaint(aWindow,&ps);
210  if(This) {
211  // FIXME : To have an automatic refresh someone have to redraw here.
212  }
213  EndPaint(aWindow, &ps);
214 
215  //FIXME : have to handle WM_RESIZE
216  //pView->fWinSize_x = (G4int) width;
217  //pView->fWinSize_y = (G4int) height;
218  G4OpenGLWin32Viewer* This =
219  (G4OpenGLWin32Viewer*)::GetWindowLong(aWindow,GWL_USERDATA);
220  if(This) {
221  This->SetView();
222  glViewport(0,0,This->fWinSize_x,This->fWinSize_y);
223  This->ClearView();
224  This->DrawView();
225  // WARNING : the below empty the Windows message queue...
226  This->FinishView();
227  }
228  } return 0;
229  default:
230  return DefWindowProc(aWindow,aMessage,aWParam,aLParam);
231  }
232 */
233  return DefWindowProc(aWindow,aMessage,aWParam,aLParam);
234 }
235 
237 bool G4OpenGLWin32Viewer::SetWindowPixelFormat(
238  HDC aHdc
239 )
242 {
243  // The ungessable...
244 
245  PIXELFORMATDESCRIPTOR pfd;
246  pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
247  pfd.nVersion = 1;
248  pfd.dwFlags =
249  PFD_DRAW_TO_WINDOW |
250  PFD_SUPPORT_OPENGL |
251  PFD_DOUBLEBUFFER |
252  PFD_STEREO_DONTCARE;
253  pfd.iPixelType = PFD_TYPE_RGBA;
254  pfd.cColorBits = 32;
255  pfd.cRedBits = 8;
256  pfd.cRedShift = 16;
257  pfd.cGreenBits = 8;
258  pfd.cGreenShift = 8;
259  pfd.cBlueBits = 8;
260  pfd.cBlueShift = 0;
261  pfd.cAlphaBits = 0;
262  pfd.cAlphaShift = 0;
263  pfd.cAccumBits = 64;
264  pfd.cAccumRedBits = 16;
265  pfd.cAccumGreenBits = 16;
266  pfd.cAccumBlueBits = 16;
267  pfd.cAccumAlphaBits = 0;
268  pfd.cDepthBits = 32;
269  pfd.cStencilBits = 8;
270  pfd.cAuxBuffers = 0;
271  pfd.iLayerType = PFD_MAIN_PLANE;
272  pfd.bReserved = 0;
273  pfd.dwLayerMask = 0;
274  pfd.dwVisibleMask = 0;
275  pfd.dwDamageMask = 0;
276 
277  int pixelIndex = ::ChoosePixelFormat(aHdc,&pfd);
278  if (pixelIndex==0) {
279  pixelIndex = 1;
280  if (::DescribePixelFormat(aHdc,
281  pixelIndex,
282  sizeof(PIXELFORMATDESCRIPTOR),
283  &pfd)==0) {
284  return false;
285  }
286  }
287  if (::SetPixelFormat(aHdc,pixelIndex,&pfd)==FALSE) return false;
288  return true;
289 }
290 
291 
292 #endif
G4String fName
Definition: G4AttUtils.hh:55
#define FALSE
Definition: globals.hh:52
#define TRUE
Definition: globals.hh:55