-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtriangleApp.cpp
423 lines (326 loc) · 12.3 KB
/
triangleApp.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#include "triangleApp.h"
#include <Dshow.h>
//The first device we want to open
int dev;
double tOld;
BOOL CALLBACK MonitorEnumProc(
HMONITOR hMonitor, // handle to display monitor
HDC hdcMonitor, // handle to monitor DC
LPRECT lprcMonitor, // monitor intersection rectangle
LPARAM dwData // data
);
//empty constructor
triangleApp::triangleApp() {
for(int i=0; i<sizeof(m_maxVal)/sizeof(int); i++) m_maxVal[i]=-1;
m_brightness=-1;
}
void triangleApp::init(int dev_){
dev=dev_;
//optional static function to list devices
//for silent listDevices use listDevices(true);
int numDevices = videoInput::listDevices();
//uncomment for silent setup
//videoInput::setVerbose(false);
videoInput::setVerbose(true);
//we allocate our openGL texture objects
//we give them a ma size of 2048 by 2048 pixels
// IT = new imageTexture(2048,2048, GL_RGB);
// IT2 = new imageTexture(2048,2048, GL_RGB);
//by default we use a callback method
//this updates whenever a new frame
//arrives if you are only ocassionally grabbing frames
//you might want to set this to false as the callback caches the last
//frame for performance reasons.
// VI.setUseCallback(true); // default: true
//try and setup device with id 0 and id 1
//if only one device is found the second
//setupDevice should return false
//if you want to capture at a different frame rate (default is 30)
//specify it here, you are not guaranteed to get this fps though.
//VI.setIdealFramerate(dev, 60);
VI.setIdealFramerate(dev, 1);
//we can specifiy the dimensions we want to capture at
//if those sizes are not possible VI will look for the next nearest matching size
// VI.setupDevice(dev, 320, 240, VI_COMPOSITE);
VI.setupDevice(dev, 3, 3);
//VI.setupDevice(dev+1, 640, 480, VI_COMPOSITE);
IT=new imageTexture(VI.getWidth(dev),VI.getHeight(dev), GL_RGB);
//once the device is setup you can try and
//set the format - this is useful if your device
//doesn't remember what format you set it to
//VI.setFormat(dev, VI_NTSC_M); //optional set the format
//we allocate our buffer based on the number
//of pixels in each frame - this will be width * height * 3
frame = new unsigned char[VI.getSize(dev)];
VI.setVideoSettingFilter(dev, VI.propColorEnable, 0, VideoProcAmp_Flags_Manual);
}
///////////////////////////////////////////////////////////////////////
// Name: AllocateMonitorHandles
// Description: Get handles to the physical monitors.
///////////////////////////////////////////////////////////////////////
BOOL triangleApp::AllocateMonitorHandles()
{
// Enumerate the display monitors. The callback will do the work.
return EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)this);
}
///////////////////////////////////////////////////////////////////////
// Name: MonitorEnumProc
// Description: Callback for EnumDisplayMonitors function.
//
// The callback data is a pointer to the MainDialog class.
///////////////////////////////////////////////////////////////////////
BOOL CALLBACK MonitorEnumProc(
HMONITOR hMonitor, // handle to display monitor
HDC hdcMonitor, // handle to monitor DC
LPRECT lprcMonitor, // monitor intersection rectangle
LPARAM dwData // data
)
{
TRACE((L"MonitorEnumProc"));
printf("MonitorEnumProc(0x%X, 0x%X, ...)\n", hMonitor, hdcMonitor);
triangleApp *pDialog = (triangleApp*)dwData;
BOOL bContinue = FALSE;
if (pDialog)
{
bContinue = pDialog->OnEnumMonitor(hMonitor);
}
return bContinue;
}
///////////////////////////////////////////////////////////////////////
// Name: OnEnumMonitor
// Description: Callback for EnumDisplayMonitors function.
//
// This function is where we get the physical monitor handles.
///////////////////////////////////////////////////////////////////////
BOOL triangleApp::OnEnumMonitor(HMONITOR hMonitor)
{
TRACE((L"OnEnumMonitor: Handle = 0x%X", hMonitor));
printf("OnEnumMonitor: Handle = 0x%X\n", hMonitor);
BOOL bSuccess = FALSE;
DWORD cMonitors = 0;
PHYSICAL_MONITOR *pPhysicalMonitors = NULL;
// Free any monitor handles that we allocated previously.
FreeMonitorHandles();
// Get the number of physical monitors.
bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(
hMonitor,
&cMonitors
);
TRACE_IF_FAILED(L"GetNumberOfPhysicalMonitorsFromHMONITOR", bSuccess);
printf("GetNumberOfPhysicalMonitorsFromHMONITOR: cMonitors=%i; bSuccess=%i\n", cMonitors, bSuccess);
// Allocate the array of PHYSICAL_MONITOR structs.
if (bSuccess)
{
pPhysicalMonitors = new PHYSICAL_MONITOR[cMonitors];
if (pPhysicalMonitors == NULL)
{
bSuccess = FALSE;
}
}
// Get the array of monior handles.
if (bSuccess)
{
bSuccess = GetPhysicalMonitorsFromHMONITOR( //!
hMonitor, cMonitors, pPhysicalMonitors);
TRACE_IF_FAILED(L"GetPhysicalMonitorsFromHMONITOR", bSuccess);
}
printf("GetPhysicalMonitorsFromHMONITOR: bSuccess=%i\n", bSuccess);
if (bSuccess)
{
m_pPhysicalMonitors = pPhysicalMonitors;
m_NumPhysicalMonitors = cMonitors;
}
// Add the display string to the combo box.
// Also add the index of the array as user data.
if (bSuccess)
{
for (DWORD i = 0; i < cMonitors; i++)
//for (DWORD i = cMonitors-1; i >= 0; i--)
{
TRACE((L"Physical monitor: %s (handle = 0x%X)",
pPhysicalMonitors[i].szPhysicalMonitorDescription,
pPhysicalMonitors[i].hPhysicalMonitor
));
wprintf(L"Physical monitor %i: \"%s\" (handle = 0x%X)\n", i, pPhysicalMonitors[i].szPhysicalMonitorDescription, pPhysicalMonitors[i].hPhysicalMonitor);
DWORD minVal = 0, maxVal = 0, current = 0;
if(m_maxVal[m_noMonitor]==-1) {
bSuccess = GetMonitorBrightness(pPhysicalMonitors[i].hPhysicalMonitor, &minVal, ¤t, &maxVal);
if(!bSuccess) {
printf("Catalyst 11.2 override!!!!!!!!!!!!!!!!!!!!\n");
if(m_noMonitor==0) maxVal=255; else maxVal=100;
bSuccess=true;
}
printf("Brightness: %d (%d - %d) -> %d\n", current, minVal, maxVal, bSuccess);
m_maxVal[m_noMonitor]=maxVal;
}
char szNoMonitor[256]; sprintf(szNoMonitor, "%i", m_noMonitor);
char szPhysicalMonitorDescription[256]; sprintf(szPhysicalMonitorDescription, "%ws", pPhysicalMonitors[i].szPhysicalMonitorDescription);
if (m_maxVal[m_noMonitor]>0 && (
/*m_targetMonitor==*/strcmp(m_monitor, "-1") == 0 ||
/*m_targetMonitor==*/strcmp(m_monitor, szNoMonitor) == 0 ||
strcmp(m_monitor, szPhysicalMonitorDescription) == 0
)) {
SetMonitorBrightness(pPhysicalMonitors[i].hPhysicalMonitor, m_brightness*m_maxVal[m_noMonitor]/100);
printf("\nSetMonitorBrightness(%d)\n", m_brightness*m_maxVal[m_noMonitor]/100);
}
//SetMonitorBrightness(pPhysicalMonitors[i].hPhysicalMonitor, m_brightness);
m_noMonitor++;
/*bSuccess = GetMonitorBrightness(pPhysicalMonitors[i].hPhysicalMonitor, &minVal, ¤t, &maxVal);
printf("Brightness: %d (%d - %d) -> %d; ", current, minVal, maxVal, bSuccess);*/
/* bSuccess = comboMonitors.AddString(
pPhysicalMonitors[i].szPhysicalMonitorDescription, // string
(DWORD_PTR)i // user data = array index
);*/
if (!bSuccess)
{
break;
}
}
}
/* // Select the first one in the list.
if (bSuccess)
{
comboMonitors.Select(0);
}*/
return bSuccess;
}
///////////////////////////////////////////////////////////////////////
// Name: FreeMonitorHandles
//
// Description: Release the array of monitor handles.
///////////////////////////////////////////////////////////////////////
BOOL triangleApp::FreeMonitorHandles()
{
BOOL bSuccess = TRUE;
if (m_NumPhysicalMonitors > 0)
{
// assert(m_pPhysicalMonitors != NULL);
bSuccess = DestroyPhysicalMonitors(m_NumPhysicalMonitors, m_pPhysicalMonitors);
}
SAFE_ARRAY_DELETE(m_pPhysicalMonitors);
return bSuccess;
}
void triangleApp::setBrightness(/*int*/char* monitor, int brightness) {
if(brightness>100) brightness=100;
else if(brightness<0) brightness=0;
if(brightness!=m_brightness) {
m_brightness=brightness;
m_noMonitor=0;
// m_targetMonitor = monitor;
m_monitor = monitor;
AllocateMonitorHandles();
if (strcmp(m_monitor, "-1")==0) {
char lpParameters[256]; sprintf(lpParameters, "SetBrightness %i", brightness);
ShellExecuteA(NULL, "open", "nircmd", lpParameters, NULL, SW_SHOWDEFAULT);
/*char lpParameters[256]; sprintf(lpParameters, "nircmd SetBrightness %i", brightness);
system(lpParameters);*/
/*STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
if (CreateProcessA("nircmd", lpParameters, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}*/
}
}
}
bool triangleApp::idle(){
//check to see if we have got a new frame
if( VI.isFrameNew(dev) )
{
//we get the pixels by passing in out buffer which gets filled
double t0=glfwGetTime();
if(t0-tOld>.5) {
VI.getPixels(dev,frame, true);
int count=0;
m_avg=0;
count=0;
for(int y=0; y<VI.getHeight(dev)/*/2*/; y++) {
for(int x=0; x<VI.getWidth(dev); x++) {
//for(int i=0;i<VI.getSize(dev);i++) {
//for(int i=0;i<VI.getSize(dev)/2;i++) {
m_avg+=frame[(VI.getHeight(dev)-y-1)*VI.getWidth(dev)*3+x*3+0]; count++;
m_avg+=frame[(VI.getHeight(dev)-y-1)*VI.getWidth(dev)*3+x*3+1]; count++;
m_avg+=frame[(VI.getHeight(dev)-y-1)*VI.getWidth(dev)*3+x*3+2]; count++;
//m_avg+=frame[i];
}
}
//m_avg/=VI.getSize(dev);
m_avg/=count;
printf("avg=%i; ", m_avg);
/*m_avg2=0;
count=0;
for(int y=VI.getHeight(dev)/2; y<VI.getHeight(dev); y++) {
for(int x=VI.getWidth(dev)/3;x<VI.getWidth(dev)*2/3;x++) {
//for(int i=VI.getSize(dev)/2;i<VI.getSize(dev);i++) {
m_avg2+=frame[(VI.getHeight(dev)-y-1)*VI.getWidth(dev)*3+x*3+0]; count++;
m_avg2+=frame[(VI.getHeight(dev)-y-1)*VI.getWidth(dev)*3+x*3+1]; count++;
m_avg2+=frame[(VI.getHeight(dev)-y-1)*VI.getWidth(dev)*3+x*3+2]; count++;
//m_avg2+=frame[i];
}
}
m_avg2/=count;
printf("avg2=%i\n", m_avg2);
if(m_avg2<90) m_avg=0;*/
// EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)this);
//BOOL bSuccess = TRUE;
//SetMonitorBrightness(m_hMonitor, avg/2);
/*m_brightness=100*avg/255+glfwGetMouseWheel();
m_noMonitor=0;
AllocateMonitorHandles();*/
setBrightness("-1", 100*m_avg/255+m_shift);
//we then load them into our texture
IT->loadImageData(frame, VI.getWidth(dev), VI.getHeight(dev),GL_RGB);
tOld=t0;
return true;
}
}
return false;
//check to see if we have got a new frame
/*if( VI.isFrameNew(dev+1) )
{
//here we are directly return the pixels into our texture
//use VI.getWidth getHeight etc so that you don't get a crash
IT2->loadImageData(VI.getPixels(dev+1, true), VI.getWidth(dev+1), VI.getHeight(dev+1), GL_RGB);
}*/
}
void triangleApp::draw(){
setupScreen();
IT->renderTexture(0, 0, VI.getWidth(dev), VI.getHeight(dev));
//IT2->renderTexture(VI.getWidth(dev), 0, VI.getWidth(dev+1), VI.getHeight(dev+1));
}
void triangleApp::keyDown (char c){
//some options hooked up to key commands
if(c=='S')VI.showSettingsWindow(dev);
//if(c=='D')VI.showSettingsWindow(dev+1);
if(c=='R')VI.restartDevice(dev);
//if(c=='T')VI.restartDevice(dev+1);
if(c == '1')VI.setVideoSettingCameraPct(0, VI.propExposure, 0.1, 2);
if(c == '2')VI.setVideoSettingCameraPct(0, VI.propExposure, 0.9, 2);
if(c == '5')VI.setVideoSettingFilterPct(0, VI.propWhiteBalance, 0.12, 2);
if(c == '6')VI.setVideoSettingFilterPct(0, VI.propWhiteBalance, 0.88, 2);
if(c=='Q') VI.stopDevice(dev);
}
void triangleApp::mousewheelRotated(int position) {
int diff=position-m_oldPosition;
m_shift+=diff;
if(m_shift>100) m_shift=100;
else if(m_shift<-100) m_shift=-100;
printf("\nm_brightness=%d; m_shift=%d\n", m_brightness, m_shift);
/*m_brightness+=diff;
AllocateMonitorHandles();*/
setBrightness("-1", 100*m_avg/255+m_shift);
m_oldPosition=position;
}
void triangleApp::mouseMove( float x, float y ){
}
void triangleApp::mouseDrag( float x, float y ){
}
void triangleApp::mouseDown( float x, float y, int button ){
}
void triangleApp::mouseUp ( float x, float y, int button ){
}