Subversion Repositories seema-scanner

Rev

Details | Last modification | View Log | RSS feed

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