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