root / rgbdslam / external / siftgpu / src / TestWin / GLTestWnd.cpp @ 9240aaa3
History | View | Annotate | Download (7.06 KB)
1 |
////////////////////////////////////////////////////////////////////////////
|
---|---|
2 |
// File: GLTestWnd.cpp
|
3 |
// Author: Changchang Wu
|
4 |
// Description : implementation of the GLTestWnd class.
|
5 |
//
|
6 |
//
|
7 |
//
|
8 |
// Copyright (c) 2007 University of North Carolina at Chapel Hill
|
9 |
// All Rights Reserved
|
10 |
//
|
11 |
// Permission to use, copy, modify and distribute this software and its
|
12 |
// documentation for educational, research and non-profit purposes, without
|
13 |
// fee, and without a written agreement is hereby granted, provided that the
|
14 |
// above copyright notice and the following paragraph appear in all copies.
|
15 |
//
|
16 |
// The University of North Carolina at Chapel Hill make no representations
|
17 |
// about the suitability of this software for any purpose. It is provided
|
18 |
// 'as is' without express or implied warranty.
|
19 |
//
|
20 |
// Please send BUG REPORTS to ccwu@cs.unc.edu
|
21 |
//
|
22 |
////////////////////////////////////////////////////////////////////////////
|
23 |
|
24 |
|
25 |
#define WIN32_LEAN_AND_MEAN
|
26 |
|
27 |
#include <windows.h> |
28 |
#include <windowsx.h> |
29 |
#include "GL/gl.h" |
30 |
#include <stdlib.h> |
31 |
|
32 |
/////////////////////////
|
33 |
#include "BasicTestWin.h" |
34 |
#include "GLTestWnd.h" |
35 |
|
36 |
#ifdef _WINDOWS
|
37 |
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
38 |
LPSTR lpCmdLine, int nCmdShow)
|
39 |
{ |
40 |
//////////////////////////////////////////
|
41 |
//create a window
|
42 |
GLTestWnd win(lpCmdLine); |
43 |
#else
|
44 |
int main(int argc, char** argv) |
45 |
{ |
46 |
GLTestWnd win(argc, argv); |
47 |
#endif
|
48 |
|
49 |
///////////////////////////////////////
|
50 |
MSG msg; //
|
51 |
while (GetMessage(&msg, NULL, 0, 0)) |
52 |
{ |
53 |
TranslateMessage(&msg); |
54 |
DispatchMessage(&msg); |
55 |
//on idle
|
56 |
if(PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE)==0)SendMessage(GetActiveWindow(), WM_MY_IDLE, 0, 0); |
57 |
} |
58 |
return (int) msg.wParam; |
59 |
} |
60 |
|
61 |
//////////////////////////////////////////////////////////////////////
|
62 |
// Construction/Destruction
|
63 |
//////////////////////////////////////////////////////////////////////
|
64 |
|
65 |
|
66 |
GLTestWnd::GLTestWnd(LPSTR cmd) |
67 |
{ |
68 |
///////////////////////
|
69 |
//Parse Command Line
|
70 |
if(cmd) ParseCommandLine(cmd);
|
71 |
|
72 |
//////////////////////
|
73 |
//create the window
|
74 |
CreateWindowGL(); |
75 |
} |
76 |
|
77 |
GLTestWnd::GLTestWnd(int argc, char**argv) |
78 |
{ |
79 |
BasicTestWin::ParseSiftParam(argc, argv); |
80 |
CreateWindowGL(); |
81 |
} |
82 |
|
83 |
GLTestWnd::~GLTestWnd() |
84 |
{ |
85 |
|
86 |
} |
87 |
|
88 |
void GLTestWnd::CreateWindowGL()
|
89 |
{ |
90 |
_hWndMain = NULL;
|
91 |
RegisterWindowClass(); |
92 |
HWND hWnd = CreateWindow("SIFT_GPU_WND", "SIFT_GPU", |
93 |
WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN, |
94 |
CW_USEDEFAULT, CW_USEDEFAULT, |
95 |
600, 450, NULL, NULL, NULL, this); |
96 |
ShowWindow(hWnd, SW_SHOW); |
97 |
UpdateWindow(hWnd); |
98 |
|
99 |
} |
100 |
LRESULT CALLBACK GLTestWnd::___WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) |
101 |
{ |
102 |
static int nWnd=0; |
103 |
GLTestWnd *pWnd; |
104 |
if(message==WM_CREATE)
|
105 |
{ |
106 |
LPCREATESTRUCT cs = (LPCREATESTRUCT)lParam; |
107 |
pWnd = (GLTestWnd*)cs->lpCreateParams; |
108 |
pWnd->_hWndMain = hWnd; |
109 |
SetWindowLong(hWnd,0,(long)(pWnd)); |
110 |
nWnd++; |
111 |
|
112 |
}else if(message== WM_DESTROY) |
113 |
{ |
114 |
pWnd=(GLTestWnd*)GetWindowLong(hWnd,0);
|
115 |
delete pWnd;
|
116 |
SetWindowLong(hWnd,0,0); |
117 |
nWnd--; |
118 |
if(nWnd==0) |
119 |
PostQuitMessage(0);
|
120 |
pWnd = NULL;
|
121 |
}else
|
122 |
{ |
123 |
pWnd=(GLTestWnd*)GetWindowLong(hWnd,0);
|
124 |
} |
125 |
if(pWnd)
|
126 |
return pWnd->_WndProc(message,wParam,lParam);
|
127 |
else
|
128 |
return DefWindowProc(hWnd, message, wParam, lParam);
|
129 |
|
130 |
} |
131 |
|
132 |
void GLTestWnd::RegisterWindowClass()
|
133 |
{ |
134 |
static int registered = 0; |
135 |
if(registered) return; |
136 |
WNDCLASSEX wcex; |
137 |
wcex.cbSize = sizeof(WNDCLASSEX);
|
138 |
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; |
139 |
wcex.lpfnWndProc = (WNDPROC)___WndProc; |
140 |
wcex.cbClsExtra = 0;
|
141 |
wcex.cbWndExtra = 4;
|
142 |
wcex.hInstance = 0;
|
143 |
wcex.hIcon = NULL;
|
144 |
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
|
145 |
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
|
146 |
wcex.lpszMenuName = NULL;;
|
147 |
wcex.lpszClassName = "SIFT_GPU_WND";
|
148 |
wcex.hIconSm = NULL;
|
149 |
RegisterClassEx(&wcex); |
150 |
registered = 1;
|
151 |
} |
152 |
|
153 |
|
154 |
LRESULT GLTestWnd::_WndProc(UINT message, WPARAM wParam, LPARAM lParam) |
155 |
{ |
156 |
|
157 |
|
158 |
switch (message)
|
159 |
{ |
160 |
case WM_CREATE:
|
161 |
{ |
162 |
if(_win_x !=-1) |
163 |
{ |
164 |
LPCREATESTRUCT cs = (LPCREATESTRUCT)lParam; |
165 |
MoveWindow(_hWndMain, _win_x, _win_y, cs->cx, cs->cy, 0);
|
166 |
} |
167 |
SetDisplaySize(600, 450); |
168 |
HDC hdc = GetDC(_hWndMain); |
169 |
glCreateRC(hdc); |
170 |
if(_hglrc == NULL) exit(0); |
171 |
ReleaseDC(_hWndMain,hdc); |
172 |
|
173 |
} |
174 |
break;
|
175 |
case WM_SIZE:
|
176 |
{ |
177 |
glResize(LOWORD(lParam),HIWORD(lParam)); |
178 |
} |
179 |
break;
|
180 |
case WM_PAINT:
|
181 |
{ |
182 |
PAINTSTRUCT ps; |
183 |
HDC hdc = BeginPaint(_hWndMain, &ps); |
184 |
///
|
185 |
glPaint(hdc); |
186 |
///
|
187 |
EndPaint(_hWndMain, &ps); |
188 |
} |
189 |
break;
|
190 |
case WM_CHAR:
|
191 |
KeyInput((int) wParam);
|
192 |
InvalidateRect(_hWndMain, NULL, FALSE);
|
193 |
break;
|
194 |
case WM_LBUTTONDOWN:
|
195 |
{ |
196 |
int xPos = GET_X_LPARAM(lParam);
|
197 |
int yPos = GET_Y_LPARAM(lParam);
|
198 |
StartMotion(xPos, yPos); |
199 |
} |
200 |
break;
|
201 |
case WM_LBUTTONUP:
|
202 |
EndMotion(); |
203 |
break;
|
204 |
case WM_MOUSEMOVE:
|
205 |
if( wParam & MK_LBUTTON)
|
206 |
{ |
207 |
int xPos = GET_X_LPARAM(lParam);
|
208 |
int yPos = GET_Y_LPARAM(lParam);
|
209 |
MoveMouse(xPos, yPos); |
210 |
} |
211 |
case WM_ERASEBKGND:
|
212 |
return TRUE;
|
213 |
case WM_MY_IDLE:
|
214 |
OnIdle(); |
215 |
return TRUE;
|
216 |
case WM_DESTROY:
|
217 |
PostQuitMessage(0);
|
218 |
break;
|
219 |
default:
|
220 |
return DefWindowProc(_hWndMain, message, wParam, lParam);
|
221 |
} |
222 |
return 0; |
223 |
} |
224 |
void GLTestWnd::glCreateRC(HDC hdc)
|
225 |
{ |
226 |
int pixelformat;
|
227 |
PIXELFORMATDESCRIPTOR pfd = |
228 |
{ |
229 |
sizeof(PIXELFORMATDESCRIPTOR),1, |
230 |
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER, |
231 |
PFD_TYPE_RGBA,24,0, 0, 0, 0, 0, 0,0,0,0,0, 0, 0, 0,16,0,0, |
232 |
PFD_MAIN_PLANE,0,0, 0, 0 |
233 |
}; |
234 |
if ((pixelformat = ChoosePixelFormat(hdc, &pfd)) ==0)return ; |
235 |
if (SetPixelFormat(hdc, pixelformat, &pfd) == FALSE)return ; |
236 |
pixelformat =::GetPixelFormat(hdc); |
237 |
::DescribePixelFormat(hdc, pixelformat, sizeof(pfd), &pfd);
|
238 |
|
239 |
//
|
240 |
_hglrc = wglCreateContext(hdc); |
241 |
wglMakeCurrent(hdc, _hglrc); |
242 |
|
243 |
glMatrixMode(GL_MODELVIEW); |
244 |
glLoadIdentity(); |
245 |
|
246 |
RunSiftGPU(); |
247 |
|
248 |
wglMakeCurrent(NULL, NULL); |
249 |
|
250 |
} |
251 |
|
252 |
void GLTestWnd::glResize(int w, int h) |
253 |
{ |
254 |
ReShape(w, h); |
255 |
} |
256 |
|
257 |
void GLTestWnd::glPaint(HDC hdc)
|
258 |
{ |
259 |
wglMakeCurrent(hdc,HGLRC(_hglrc)); |
260 |
Display(); |
261 |
SwapBuffers(hdc); |
262 |
} |
263 |
|
264 |
void GLTestWnd::ParseCommandLine(LPSTR cmd)
|
265 |
{ |
266 |
int argc=0; |
267 |
char**argv = new char*[256]; |
268 |
if(*cmd == 0) return; |
269 |
do
|
270 |
{ |
271 |
while(*cmd ==' ') cmd++; |
272 |
if(*cmd)
|
273 |
{ |
274 |
argv[argc++] = cmd; |
275 |
} |
276 |
while(*cmd && *cmd != ' ') cmd++; |
277 |
if(*cmd==' ') *cmd++ = 0; |
278 |
|
279 |
}while(*cmd && argc <256); |
280 |
BasicTestWin::ParseSiftParam(argc, argv); |
281 |
} |
282 |
|
283 |
void GLTestWnd::SetDisplaySize(int w, int h) |
284 |
{ |
285 |
RECT rc; int dw, dh;
|
286 |
GetClientRect(_hWndMain, &rc); |
287 |
|
288 |
dw = w - rc.right; |
289 |
dh = h - rc.bottom; |
290 |
GetWindowRect(_hWndMain, &rc); |
291 |
|
292 |
MoveWindow(_hWndMain, rc.left, rc.top, rc.right - rc.left + dw, |
293 |
rc.bottom - rc.top + dh, TRUE); |
294 |
|
295 |
} |
296 |
|
297 |
void GLTestWnd::SetWindowTitle(char *title) |
298 |
{ |
299 |
SetWindowText(_hWndMain, title); |
300 |
} |
301 |
|
302 |
void GLTestWnd::UpdateDisplay()
|
303 |
{ |
304 |
InvalidateRect(_hWndMain, NULL,0); |
305 |
} |
306 |
|