2 * Mesa 3-D graphics library
5 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 * This program opens two windows, renders into one and uses
28 * glCopyPixels to copy the image from the first window into the
29 * second by means of the extension function wglMakeContextCurrentARB().
34 * Created: March 1, 2013
35 * Copyright (c) 2013, Attachmate Corporation All Rights Reserved
42 static HGLRC Context
= NULL
;
43 static HWND Win
[2] = {NULL
, NULL
};
44 static int Width
[2] = {0, 0}, Height
[2] = {0, 0};
45 static float Angle
= 0.0f
;
49 Redraw(BOOL DrawFront
)
51 assert(Context
!= NULL
);
52 if (!wglExtMakeContextCurrent(Win
[0], Win
[0], Context
))
57 glDrawBuffer(GL_FRONT
);
58 glReadBuffer(GL_FRONT
);
61 glDrawBuffer(GL_BACK
);
62 glReadBuffer(GL_BACK
);
65 glViewport(0, 0, Width
[0], Height
[0]);
66 glMatrixMode(GL_PROJECTION
);
68 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
69 glMatrixMode(GL_MODELVIEW
);
70 glShadeModel(GL_FLAT
);
71 glClearColor(0.5, 0.5, 0.5, 0.0);
72 glClear(GL_COLOR_BUFFER_BIT
);
75 glColor3f(0.3f
, 0.3f
, 1.0f
);
77 glRotatef(Angle
, 0, 0, 1);
79 glVertex2f(-0.5, -0.25);
80 glVertex2f(0.5, -0.25);
81 glVertex2f(0.5, 0.25);
82 glVertex2f(-0.5, 0.25);
89 wglExtSwapBuffers(Win
[0]);
91 /* copy image from window 0 to window 1 */
92 if (!wglExtMakeContextCurrent(Win
[1], Win
[0], Context
))
95 /* copy the image between windows */
96 glClearColor(0.0, 0.0, 0.0, 0.0);
97 glClear(GL_COLOR_BUFFER_BIT
);
98 glCopyPixels(0, 0, Width
[0], Height
[0], GL_COLOR
);
102 wglExtSwapBuffers(Win
[1]);
108 Resize(HWND win
, int width
, int height
)
118 if ((hDC
= (HDC
)(INT_PTR
)GetWindowLongPtr(win
, GWLP_USERDATA
)) != NULL
) {
121 if ((ret
= wglMakeCurrent(hDC
, Context
)) != FALSE
) {
125 assert(ret
!= FALSE
);
129 static LRESULT CALLBACK
130 EventLoop(HWND hWnd
, UINT msg
, WPARAM wParam
, LPARAM lParam
)
136 static BOOL drawFront
= FALSE
;
140 ret
= (LRESULT
)storeDC(hWnd
);
145 GetUpdateRect(hWnd
, &rect
, FALSE
);
150 case WM_WINDOWPOSCHANGED
:
151 pWinPos
= (LPWINDOWPOS
)lParam
;
152 //if (!(pWinPos->flags&SWP_NOSIZE))
153 Resize(hWnd
, pWinPos
->cx
, pWinPos
->cy
);
158 if (wParam
== 'f') { // 'f' key
159 drawFront
= !drawFront
;
162 else if (wParam
== VK_ESCAPE
)
168 if ((hDC
= fetchDC(hWnd
)) != NULL
) {
169 ret
= ReleaseDC(hWnd
, hDC
);
170 assert(ret
!= FALSE
);
176 ret
= DefWindowProc(hWnd
, msg
, wParam
, lParam
);
184 Init(HINSTANCE hInst
, int show
)
188 if (wglExtInit(hInst
, EventLoop
) != FALSE
) {
189 const int attribList
[] = {
190 WGL_DRAW_TO_WINDOW
, GL_TRUE
,
191 WGL_SUPPORT_OPENGL
, GL_TRUE
,
192 WGL_DOUBLE_BUFFER
, GL_TRUE
,
193 WGL_PIXEL_TYPE
, WGL_TYPE_RGBA
,
199 if ((format
= wglExtChoosePixelFormat(attribList
)) != 0) {
200 if ((Win
[0] = getWindow(hInst
, 0, 0, 300, 300, format
)) != NULL
) {
201 if ((Win
[1] = getWindow(hInst
, 350, 0, 300, 300, format
)) != NULL
) {
202 ShowWindow(Win
[0], show
);
203 ShowWindow(Win
[1], show
);
214 WinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPSTR lpCmdLine
, int nCmdShow
)
218 if (Init(hInst
, nCmdShow
) != FALSE
) {
222 if ((hDC
= fetchDC(Win
[0])) != NULL
) {
223 if ((Context
= wglCreateContext(hDC
)) != NULL
) {
226 while ((ret
= GetMessage(&msg
, NULL
, 0, 0)) != 0) {
227 TranslateMessage(&msg
);
228 DispatchMessage(&msg
);
230 if (Win
[0] != NULL
) {
231 rval
= DestroyWindow(Win
[0]);
232 assert(rval
!= FALSE
);
234 if (Win
[1] != NULL
) {
235 rval
= DestroyWindow(Win
[1]);
236 assert(rval
!= FALSE
);
238 rval
= wglDeleteContext(Context
);
239 assert(rval
!= FALSE
);
242 wglExtDispose(hInst
);
243 ret
= (int)msg
.wParam
;
246 wglDeleteContext(Context
);