File: vbglcnv.cpp

package info (click to toggle)
libv1.22 1.22-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,380 kB
  • ctags: 9,765
  • sloc: cpp: 58,097; ansic: 6,814; makefile: 1,597; java: 515; sh: 24
file content (260 lines) | stat: -rw-r--r-- 7,821 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//===============================================================
// vbaseGLCanvas - a basic GL window canvas for drawing
//
// Copyright (C) 1995-1998  Bruce E. Wampler
//
// This file is part of the V C++ GUI Framework, and is covered
// under the terms of the GNU Library General Public License,
// Version 2. This library has NO WARRANTY. See the source file
// vapp.cxx for more complete information about license terms.
//===============================================================

#include <v/vwin32.h>
#include <v/vbglcnv.h>		// our header

#include <v/vapp.h>
#include <v/vwindow.h>

#include <stdlib.h>

// OpenGL init
    int vBaseGLCanvasPane::_pixelFormat = 0;
    int vBaseGLCanvasPane::_doubleBuffer = 0;	// if we have double buffering
    unsigned int vBaseGLCanvasPane::_vGLmode = 0;

//================>>> vBaseGLCanvasPane::vBaseGLCanvasPane <<<========================
  vBaseGLCanvasPane::vBaseGLCanvasPane(unsigned int vGLmode,
   PaneType pt) : vCanvasPane(pt)
  {
    SysDebug(Constructor,"vBaseGLCanvasPane::vBaseGLCanvasPane() constructor\n")

    const unsigned int defGLmode = (vGL_RGB | vGL_DoubleBuffer | vGL_Depth);

    _vGLmode = ((vGLmode == vGL_Default) ? defGLmode : vGLmode);
    _hDC = 0;
    _hGLRC = 0;
    _hPalette = 0;

  }

//================>>> vBaseGLCanvasPane::~vBaseGLCanvasPane <<<===================
  vBaseGLCanvasPane::~vBaseGLCanvasPane()
  {
    SysDebug(Destructor,"vBaseGLCanvasPane::~vBaseGLCanvasPane() destructor\n")
    // OpenGL: finish OpenGL rendering

    if (_hDC)
	::ReleaseDC(theApp->winHwnd(),_hDC);
    if (_hGLRC)
      {
	wglMakeCurrent(NULL, NULL);
	wglDeleteContext(_hGLRC);
      }
    if (_hPalette)
      {
	DeleteObject(_hPalette);
      }
  }

//==================>>> vBaseGLCanvasPane::initialize <<<==========================
  void vBaseGLCanvasPane::initialize(vWindow* pWindow, HWND pWidget)
  {
    vCanvasPane::initialize(pWindow, pWidget);	// init std canvas
	// the initialize will call CreateDC, which we override to
	// create the DC we need for OpenGL

    if (!_hDC)
      {
        // Create the GL DC for it to use.
        // Initialize OpenGL rendering

        _hDC = ::GetDC(_drawWindow);

	// Set up the rendering pixel format
	_setUpPixelFormat();
	// Set up Palette
	_setUpPalette();
	_hGLRC = wglCreateContext(_hDC);
      }

    // initialize graphic
    graphicsInit();
  }

#ifdef USECREATEDC
//==================>>> vBaseGLCanvasPane::CreateDC <<<========================
  void vBaseGLCanvasPane::CreateDC(void)
  {
    // This overrides the CreateDC of vCanvasPane and creates
    // an OpenGL compatible DC, and then sets up the rendering stuff

    vCanvasPane::CreateDC();		// we can have both regular DC
    					// and a OpenGL DC.
  }
#endif

//==================>>> vBaseGLCanvasPane::_setUpPixelFormat <<<==========================
  void vBaseGLCanvasPane::_setUpPixelFormat()
  {
    // default pfd values
    PIXELFORMATDESCRIPTOR pfd = {
      sizeof(PIXELFORMATDESCRIPTOR),  /* size */
      1,                              /* version */
      PFD_SUPPORT_OPENGL |
      PFD_DRAW_TO_WINDOW |
      PFD_DOUBLEBUFFER,               /* support double-buffering */
      PFD_TYPE_RGBA,                  /* color type */
      16,                             /* prefered color depth */
      0, 0, 0, 0, 0, 0,               /* color bits (ignored) */
      0,                              /* no alpha buffer */
      0,                              /* alpha bits (ignored) */
      0,                              /* no accumulation buffer */
      0, 0, 0, 0,                     /* accum bits (ignored) */
      24,                             /* depth buffer */
      0,                              /* no stencil buffer */
      0,                              /* no auxiliary buffers */
      PFD_MAIN_PLANE,                 /* main layer */
      0,                              /* reserved */
      0, 0, 0,                        /* no layer, visible, damage masks */
    };

    _doubleBuffer = 1;

    // prepare the mode  
    if (_vGLmode & vGL_Indexed)	// Indexed mode *********************
      {
	pfd.iPixelType = PFD_TYPE_COLORINDEX;

	_pixelFormat = ChoosePixelFormat(_hDC, &pfd);

	// try without double buffer
	if (_pixelFormat == 0 && (_vGLmode & vGL_DoubleBuffer))
	  {
	    pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
	    _pixelFormat = ChoosePixelFormat(_hDC, &pfd);
	    _doubleBuffer = 0;
	  }
      }
    else		// RGBA mode ********************
      {
	_pixelFormat = ChoosePixelFormat(_hDC, &pfd);
	UserDebug(WindowEvents,"vBaseGLCanvasPane::_setUpPixelFormat: OpenGL starts in RGB, Double buffering and Depth mode");

	// try to get rid of this (i.e. try without double buffer)
	// As Alpha mode is not independant on Win32 platform
	// if vGL_Alpha is set, it is considered as vGL_RGB
	if ( (_pixelFormat == 0) && (_vGLmode & vGL_RGB))
	  {
	    UserDebug(WindowEvents,"without Double buffering");

	    pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
	    _pixelFormat = ChoosePixelFormat(_hDC, &pfd);
	    _doubleBuffer = 0;
	  }
	else
	    UserDebug(WindowEvents,"with Double buffering");
      }
  
    if (_pixelFormat == 0)
      {
	::MessageBox(WindowFromDC(_hDC), "ChoosePixelFormat failed.", "Error",
	MB_ICONERROR | MB_OK);
	exit(1);
      }

    if (SetPixelFormat(_hDC, _pixelFormat, &pfd) != TRUE)
      {
	::MessageBox(WindowFromDC(_hDC), "SetPixelFormat failed.", "Error",
	       MB_ICONERROR | MB_OK);
	exit(1);
      }
  
    UserDebug(WindowEvents,"\n");
  }

//==================>>> vBaseGLCanvasPane::_setUpPalette <<<==========================
  void vBaseGLCanvasPane::_setUpPalette()
  {
    //  int pixelFormat = GetPixelFormat(_hDC);
    PIXELFORMATDESCRIPTOR pfd;
    LOGPALETTE* pPal;
    int paletteSize;

    DescribePixelFormat(_hDC, _pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);

    if (pfd.dwFlags & PFD_NEED_PALETTE) 
      {
	paletteSize = 1 << pfd.cColorBits;
      }
    else
      {
	return;
      }

    pPal = (LOGPALETTE*)
    	malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
    pPal->palVersion = 0x300;
    pPal->palNumEntries = paletteSize;

    /* build a simple RGB color palette */
      {
	int redMask = (1 << pfd.cRedBits) - 1;
	int greenMask = (1 << pfd.cGreenBits) - 1;
	int blueMask = (1 << pfd.cBlueBits) - 1;
	int i;

	for (i=0; i<paletteSize; ++i)
	  {
	    pPal->palPalEntry[i].peRed =
	     (((i >> pfd.cRedShift) & redMask) * 255) / redMask;
	    pPal->palPalEntry[i].peGreen =
	      (((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
	    pPal->palPalEntry[i].peBlue =
	      (((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
	    pPal->palPalEntry[i].peFlags = 0;
	  }
      }
  
    _hPalette = CreatePalette(pPal);
    free(pPal);

    if (_hPalette) 
      {
	SelectPalette(_hDC, _hPalette, FALSE);
	RealizePalette(_hDC);
      }
  }

//==================>>> vBaseGLCanvasPane::graphicsInit <<<========================
  void vBaseGLCanvasPane::graphicsInit(void)
  {
    // Perform only the most basic operations, including getting the context

    vglMakeCurrent();
  }

//================>>> vBaseGLCanvasPane::vBaseGLMakeCurrent <<<===============
  void vBaseGLCanvasPane::vglMakeCurrent(void)
  {
    wglMakeCurrent(_hDC, _hGLRC);
  }

//==================>>> vBaseGLCanvasPane::vglFlush <<<========================
  void vBaseGLCanvasPane::vglFlush(void)
  {
    if (_doubleBuffer)
	SwapBuffers(_hDC);
    else
	glFlush();
  }

//=====================>>> vBaseGLCanvasPane::Resize <<<============================
  void vBaseGLCanvasPane::Resize(int newW, int newH)
  {
    // This is the routine the user will override to intercept size changes
    vCanvasPane::Resize(newW,newH);

    vglMakeCurrent();
    glViewport(0,0,newW,newH);

  }