Subversion Repositories seema-scanner

Rev

Rev 1 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 205
1
#include "OpenGLContext.h"
1
#include "OpenGLContext.h"
2
 
2
 
3
#include <windows.h>
3
#include <windows.h>
4
#include <winuser.h>
4
#include <winuser.h>
5
#include <tchar.h>
5
#include <tchar.h>
6
#include <ShObjIdl.h>
6
#include <ShObjIdl.h>
7
#include <GL/gl.h>
7
#include <GL/gl.h>
8
 
8
 
9
#include <stdio.h>
9
#include <stdio.h>
10
 
10
 
11
BOOL CALLBACK monitorEnumerator(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData){
11
BOOL CALLBACK monitorEnumerator(HMONITOR hMonitor, HDC, LPRECT, LPARAM dwData){
12
 
12
 
13
    MONITORINFOEX monitorInfo;
13
    MONITORINFOEX monitorInfo;
14
    ZeroMemory(&monitorInfo, sizeof(MONITORINFOEX));
14
    ZeroMemory(&monitorInfo, sizeof(MONITORINFOEX));
15
    monitorInfo.cbSize = sizeof(MONITORINFOEX);
15
    monitorInfo.cbSize = sizeof(MONITORINFOEX);
16
 
16
 
17
    GetMonitorInfo(hMonitor, &monitorInfo);
17
    GetMonitorInfo(hMonitor, &monitorInfo);
18
 
18
 
19
    ScreenInfo screen;
19
    ScreenInfo screen;
20
    char deviceName[32];
20
    char deviceName[32];
21
    wcstombs((char *)deviceName, (wchar_t*)monitorInfo.szDevice, 32);
21
    wcstombs((char *)deviceName, (wchar_t*)monitorInfo.szDevice, 32);
22
    screen.name = std::string(deviceName);
22
    screen.name = std::string(deviceName);
23
 
23
 
24
    RECT rect = monitorInfo.rcMonitor;
24
    RECT rect = monitorInfo.rcMonitor;
25
    screen.resX = rect.right - rect.left;
25
    screen.resX = rect.right - rect.left;
26
    screen.resY = rect.bottom - rect.top;
26
    screen.resY = rect.bottom - rect.top;
27
 
27
 
28
    screen.posX = rect.left;
28
    screen.posX = rect.left;
29
    screen.posY = rect.top;
29
    screen.posY = rect.top;
30
 
30
 
31
    std::vector<ScreenInfo> *ret = reinterpret_cast<std::vector<ScreenInfo>*>(dwData);
31
    std::vector<ScreenInfo> *ret = reinterpret_cast<std::vector<ScreenInfo>*>(dwData);
32
    ret->push_back(screen);
32
    ret->push_back(screen);
33
 
33
 
34
    return true;
34
    return true;
35
}
35
}
36
 
36
 
37
 
37
 
38
std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
38
std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
39
 
39
 
40
    std::vector<ScreenInfo> ret;
40
    std::vector<ScreenInfo> ret;
41
 
41
 
42
    EnumDisplayMonitors(NULL, NULL, monitorEnumerator, (LPARAM)&ret);
42
    EnumDisplayMonitors(NULL, NULL, monitorEnumerator, (LPARAM)&ret);
43
 
43
 
44
 
44
 
45
//    DISPLAY_DEVICE dd;
45
//    DISPLAY_DEVICE dd;
46
//    ZeroMemory(&dd, sizeof(DISPLAY_DEVICE));
46
//    ZeroMemory(&dd, sizeof(DISPLAY_DEVICE));
47
//    dd.cb = sizeof(dd);
47
//    dd.cb = sizeof(dd);
48
 
48
 
49
//    for(int i=0; EnumDisplayDevices(NULL, i, &dd, 0); i++){
49
//    for(int i=0; EnumDisplayDevices(NULL, i, &dd, 0); i++){
50
 
50
 
51
//        if ((dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) || !(dd.StateFlags & DISPLAY_DEVICE_ACTIVE))
51
//        if ((dd.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) || !(dd.StateFlags & DISPLAY_DEVICE_ACTIVE))
52
//            continue;
52
//            continue;
53
 
53
 
54
//        // Get additional info
54
//        // Get additional info
55
//        DISPLAY_DEVICE display;
55
//        DISPLAY_DEVICE display;
56
//        ZeroMemory(&display, sizeof(DISPLAY_DEVICE));
56
//        ZeroMemory(&display, sizeof(DISPLAY_DEVICE));
57
//        display.cb = sizeof(DISPLAY_DEVICE);
57
//        display.cb = sizeof(DISPLAY_DEVICE);
58
 
58
 
59
//        EnumDisplayDevices(dd.DeviceName, 0, &display, 0);
59
//        EnumDisplayDevices(dd.DeviceName, 0, &display, 0);
60
//        HDC dc = CreateDC(L"DISPLAY", display.DeviceString, NULL, NULL);
60
//        HDC dc = CreateDC(L"DISPLAY", display.DeviceString, NULL, NULL);
61
 
61
 
62
//        ScreenInfo screen;
62
//        ScreenInfo screen;
63
 
63
 
64
//        char deviceName[32];
64
//        char deviceName[32];
65
//        wcstombs(deviceName, display.DeviceName, 32);
65
//        wcstombs(deviceName, display.DeviceName, 32);
66
//        screen.name = std::string(deviceName);
66
//        screen.name = std::string(deviceName);
67
 
67
 
68
//        screen.resX = GetDeviceCaps(dc, HORZRES);
68
//        screen.resX = GetDeviceCaps(dc, HORZRES);
69
//        screen.resY = GetDeviceCaps(dc, VERTRES);
69
//        screen.resY = GetDeviceCaps(dc, VERTRES);
70
 
70
 
71
//        ret.push_back(screen);
71
//        ret.push_back(screen);
72
 
72
 
73
 
73
 
74
//        DEVMODE settings;
74
//        DEVMODE settings;
75
//        ZeroMemory(&settings, sizeof(DEVMODE));
75
//        ZeroMemory(&settings, sizeof(DEVMODE));
76
//        settings.dmSize = sizeof(DEVMODE);
76
//        settings.dmSize = sizeof(DEVMODE);
77
 
77
 
78
//        EnumDisplaySettings(display.DeviceName, ENUM_CURRENT_SETTINGS, &settings);
78
//        EnumDisplaySettings(display.DeviceName, ENUM_CURRENT_SETTINGS, &settings);
79
 
79
 
80
//        int xpos = settings.dmPosition.x;
80
//        int xpos = settings.dmPosition.x;
81
//        int ypos = settings.dmPosition.y;
81
//        int ypos = settings.dmPosition.y;
82
 
82
 
83
//        DeleteDC(dc);
83
//        DeleteDC(dc);
84
//    }
84
//    }
85
 
85
 
86
    return ret;
86
    return ret;
87
}
87
}
88
 
88
 
89
// Window Function
89
// Window Function
90
LONG WINAPI MainWndProc (
90
LONG WINAPI MainWndProc (
91
    HWND    hWnd,
91
    HWND    hWnd,
92
    UINT    uMsg,
92
    UINT    uMsg,
93
    WPARAM  wParam,
93
    WPARAM  wParam,
94
    LPARAM  lParam)
94
    LPARAM  lParam)
95
{
95
{
96
    LONG lRet = 1;
96
    LONG lRet = 1;
97
    PAINTSTRUCT ps;
97
    PAINTSTRUCT ps;
98
 
98
 
99
    switch (uMsg) {
99
    switch (uMsg) {
100
 
100
 
101
    case WM_PAINT:
101
    case WM_PAINT:
102
        BeginPaint(hWnd, &ps);
102
        BeginPaint(hWnd, &ps);
103
        EndPaint(hWnd, &ps);
103
        EndPaint(hWnd, &ps);
104
        break;
104
        break;
105
    default:
105
    default:
106
        lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
106
        lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
107
        break;
107
        break;
108
    }
108
    }
109
 
109
 
110
    return lRet;
110
    return lRet;
111
}
111
}
112
 
112
 
113
struct OpenGLContext::OpenGLContextInfo{
113
struct OpenGLContext::OpenGLContextInfo{
114
    HDC hdc;     // device OpenGLContext handle
114
    HDC hdc;     // device OpenGLContext handle
115
    HGLRC hglrc; // OpenGL rendering OpenGLContext
115
    HGLRC hglrc; // OpenGL rendering OpenGLContext
116
    HWND hwnd;	 // window handle
116
    HWND hwnd;	 // window handle
117
    HINSTANCE hinstance; // application instance handle
117
    HINSTANCE hinstance; // application instance handle
118
 
118
 
119
    OpenGLContextInfo() : hdc(NULL), hglrc(NULL), hwnd(NULL){}
119
    OpenGLContextInfo() : hdc(NULL), hglrc(NULL), hwnd(NULL){}
120
};
120
};
121
 
121
 
122
OpenGLContext::OpenGLContext(unsigned int _screenNum) : screenNum(_screenNum){
122
OpenGLContext::OpenGLContext(unsigned int _screenNum) : screenNum(_screenNum){
123
 
123
 
124
    std::vector<ScreenInfo> screenInfo = OpenGLContext::GetScreenInfo();
124
    std::vector<ScreenInfo> screenInfo = OpenGLContext::GetScreenInfo();
125
    screenResX = screenInfo[screenNum].resX;
125
    screenResX = screenInfo[screenNum].resX;
126
    screenResY = screenInfo[screenNum].resY;
126
    screenResY = screenInfo[screenNum].resY;
127
 
127
 
128
    // New object for OpenGLContext information
128
    // New object for OpenGLContext information
129
    contextInfo = new OpenGLContextInfo();
129
    contextInfo = new OpenGLContextInfo();
130
 
130
 
131
    // Create and register window class
131
    // Create and register window class
132
    WNDCLASS wndclass;
132
    WNDCLASS wndclass;
133
 
133
 
134
    wndclass.style = CS_VREDRAW;
134
    wndclass.style = CS_VREDRAW;
135
    wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
135
    wndclass.lpfnWndProc = (WNDPROC)MainWndProc;
136
    wndclass.cbClsExtra = 0;
136
    wndclass.cbClsExtra = 0;
137
    wndclass.cbWndExtra = 0;
137
    wndclass.cbWndExtra = 0;
138
    wndclass.hInstance = contextInfo->hinstance;
138
    wndclass.hInstance = contextInfo->hinstance;
139
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
139
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
140
    wndclass.hCursor = LoadCursor(NULL, IDC_NO);
140
    wndclass.hCursor = LoadCursor(NULL, IDC_NO);
141
    wndclass.hbrBackground = NULL;
141
    wndclass.hbrBackground = NULL;
142
    wndclass.lpszMenuName = NULL;
142
    wndclass.lpszMenuName = NULL;
143
    wndclass.lpszClassName = L"ProjectorGLClass";
143
    wndclass.lpszClassName = L"ProjectorGLClass";
144
    RegisterClass(&wndclass);
144
    RegisterClass(&wndclass);
145
 
145
 
146
    // create the fullscreen window
146
    // create the fullscreen window
147
    contextInfo->hwnd = CreateWindowEx(WS_EX_TOPMOST,L"ProjectorGLClass", L"Projector",
147
    contextInfo->hwnd = CreateWindowEx(WS_EX_TOPMOST,L"ProjectorGLClass", L"Projector",
148
                                     WS_POPUP|WS_CLIPCHILDREN,
148
                                     WS_POPUP|WS_CLIPCHILDREN,
149
                                     screenInfo[screenNum].posX, screenInfo[screenNum].posY, screenResX, screenResY,
149
                                     screenInfo[screenNum].posX, screenInfo[screenNum].posY, screenResX, screenResY,
150
                                     NULL, NULL, contextInfo->hinstance, NULL);
150
                                     NULL, NULL, contextInfo->hinstance, NULL);
151
 
151
 
152
//    contextInfo->hwnd = CreateWindow(L"ProjectorGLClass", L"Projector",
152
//    contextInfo->hwnd = CreateWindow(L"ProjectorGLClass", L"Projector",
153
//                                     WS_POPUP|WS_EX_TOOLWINDOW,
153
//                                     WS_POPUP|WS_EX_TOOLWINDOW,
154
//                                     screenInfo[screenNum].posX, screenInfo[screenNum].posY, screenResX, screenResY,
154
//                                     screenInfo[screenNum].posX, screenInfo[screenNum].posY, screenResX, screenResY,
155
//                                     GetDesktopWindow(), NULL, contextInfo->hinstance, NULL);
155
//                                     GetDesktopWindow(), NULL, contextInfo->hinstance, NULL);
156
 
156
 
157
    if(!contextInfo->hwnd)
157
    if(!contextInfo->hwnd)
158
        std::cerr << "Could not create window!" << std::endl;
158
        std::cerr << "Could not create window!" << std::endl;
159
 
159
 
160
    //CoTaskbarList list = CoTaskbarList();
160
    //CoTaskbarList list = CoTaskbarList();
161
    // Get window device OpenGLContext
161
    // Get window device OpenGLContext
162
    if ((contextInfo->hdc = GetDC(contextInfo->hwnd)) == NULL)
162
    if ((contextInfo->hdc = GetDC(contextInfo->hwnd)) == NULL)
163
        std::cerr << "Could not get window device OpenGLContext!" << std::endl;
163
        std::cerr << "Could not get window device OpenGLContext!" << std::endl;
164
 
164
 
165
    // Select pixel format description
165
    // Select pixel format description
166
    PIXELFORMATDESCRIPTOR pfd, *ppfd;
166
    PIXELFORMATDESCRIPTOR pfd, *ppfd;
167
    int pixelformat;
167
    int pixelformat;
168
 
168
 
169
    ppfd = &pfd;
169
    ppfd = &pfd;
170
 
170
 
171
    ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
171
    ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
172
    ppfd->nVersion = 1;
172
    ppfd->nVersion = 1;
173
    ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
173
    ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
174
    ppfd->dwLayerMask = PFD_MAIN_PLANE;
174
    ppfd->dwLayerMask = PFD_MAIN_PLANE;
175
    ppfd->iPixelType = PFD_TYPE_COLORINDEX;
175
    ppfd->iPixelType = PFD_TYPE_COLORINDEX;
176
    ppfd->cColorBits = 8;
176
    ppfd->cColorBits = 8;
177
    ppfd->cDepthBits = 16;
177
    ppfd->cDepthBits = 16;
178
    ppfd->cAccumBits = 0;
178
    ppfd->cAccumBits = 0;
179
    ppfd->cStencilBits = 0;
179
    ppfd->cStencilBits = 0;
180
 
180
 
181
    pixelformat = ChoosePixelFormat(contextInfo->hdc, ppfd);
181
    pixelformat = ChoosePixelFormat(contextInfo->hdc, ppfd);
182
 
182
 
183
    if ( (pixelformat = ChoosePixelFormat(contextInfo->hdc, ppfd)) == 0 )
183
    if ( (pixelformat = ChoosePixelFormat(contextInfo->hdc, ppfd)) == 0 )
184
        std::cerr << "Failed to choose pixel format!" << std::endl;
184
        std::cerr << "Failed to choose pixel format!" << std::endl;
185
 
185
 
186
    if (SetPixelFormat(contextInfo->hdc, pixelformat, ppfd) == FALSE)
186
    if (SetPixelFormat(contextInfo->hdc, pixelformat, ppfd) == FALSE)
187
        std::cerr << "Failed to set pixel format!" << std::endl;
187
        std::cerr << "Failed to set pixel format!" << std::endl;
188
 
188
 
189
    // Create the OpenGL rendering OpenGLContext
189
    // Create the OpenGL rendering OpenGLContext
190
    if ((contextInfo->hglrc = wglCreateContext(contextInfo->hdc)) == NULL)
190
    if ((contextInfo->hglrc = wglCreateContext(contextInfo->hdc)) == NULL)
191
        std::cerr << "Could not create OpenGL rendering OpenGLContext!" << std::endl;
191
        std::cerr << "Could not create OpenGL rendering OpenGLContext!" << std::endl;
192
 
192
 
193
    // Set swap interval
193
    // Set swap interval
194
    //wglSwapIntervalEXT(1);
194
    //wglSwapIntervalEXT(1);
195
 
195
 
196
    // Make OpenGLContext current
196
    // Make OpenGLContext current
197
    wglMakeCurrent(contextInfo->hdc, contextInfo->hglrc);
197
    wglMakeCurrent(contextInfo->hdc, contextInfo->hglrc);
198
 
198
 
199
    // Show window
199
    // Show window
200
    ShowWindow(contextInfo->hwnd, SW_SHOW);
200
    ShowWindow(contextInfo->hwnd, SW_SHOW);
201
    UpdateWindow(contextInfo->hwnd);
201
    UpdateWindow(contextInfo->hwnd);
202
 
202
 
203
    // Adjust gamma to one
203
    // Adjust gamma to one
204
    setGamma(1.0);
204
    setGamma(1.0);
205
}
205
}
206
 
206
 
207
void OpenGLContext::setGamma(float gamma){
207
void OpenGLContext::setGamma(float gamma){
208
    // Adjust gamma
208
    // Adjust gamma
209
 
209
 
210
}
210
}
211
 
211
 
212
void OpenGLContext::makeContextCurrent(){
212
void OpenGLContext::makeContextCurrent(){
213
    wglMakeCurrent(contextInfo->hdc, contextInfo->hglrc);
213
    wglMakeCurrent(contextInfo->hdc, contextInfo->hglrc);
214
}
214
}
215
 
215
 
216
void OpenGLContext::flush(){
216
void OpenGLContext::flush(){
217
    // Swap buffers
217
    // Swap buffers
218
    SwapBuffers(contextInfo->hdc);
218
    SwapBuffers(contextInfo->hdc);
219
    // Synchronize CPU with vsync buffer swap
219
    // Synchronize CPU with vsync buffer swap
220
    glFinish();
220
    glFinish();
221
}
221
}
222
 
222
 
223
OpenGLContext::~OpenGLContext(){
223
OpenGLContext::~OpenGLContext(){
224
    DestroyWindow(contextInfo->hwnd);
224
    DestroyWindow(contextInfo->hwnd);
225
    wglMakeCurrent(contextInfo->hdc, NULL);	// release device OpenGLContext in use by rc
225
    wglMakeCurrent(contextInfo->hdc, NULL);	// release device OpenGLContext in use by rc
226
    wglDeleteContext(contextInfo->hglrc);	// delete rendering OpenGLContext
226
    wglDeleteContext(contextInfo->hglrc);	// delete rendering OpenGLContext
227
 
227
 
228
    delete contextInfo;
228
    delete contextInfo;
229
}
229
}
230
 
230
 
231
 
231