source: trunk/geant4/visualization/OpenGL/src/G4OpenGLWin32Viewer.cc @ 553

Last change on this file since 553 was 529, checked in by garnier, 17 years ago

r658@mac-90108: laurentgarnier | 2007-06-25 12:02:16 +0200
import de visualisation

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