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