Project

General

Profile

Statistics
| Branch: | Revision:

root / rgbdslam / external / siftgpu / src / SiftGPU / LiteWindow.h @ 9240aaa3

History | View | Annotate | Download (5.43 KB)

1 9240aaa3 Alex
#ifndef LITE_WINDOW_H
2
#define LITE_WINDOW_H
3
4
//#define WINDOW_PREFER_GLUT
5
6
#if defined(WINDOW_PREFER_GLUT)
7
8
#ifdef __APPLE__
9
        #include "GLUT/glut.h"
10
#else
11
        #include "GL/glut.h"
12
#endif
13
//for apple, use GLUT to create the window..
14
class LiteWindow
15
{
16
    int glut_id;
17
public:
18
    LiteWindow()            {  glut_id = 0;         }
19
    int IsValid()           {  return glut_id > 0; }        
20
    virtual ~LiteWindow()   {  if(glut_id > 0) glutDestroyWindow(glut_id);  }
21
    void MakeCurrent()      {  glutSetWindow(glut_id);    }
22
    void Create(int x = -1, int y = -1, const char* display = NULL)
23
    {
24
            static int _glut_init_called = 0;
25
        if(glut_id != 0) return;
26
27
            //see if there is an existing window
28
            if(_glut_init_called) glut_id = glutGetWindow();
29
30
            //create one if no glut window exists
31
            if(glut_id != 0) return;
32
33
            if(_glut_init_called == 0)
34
            {
35
                    int argc = 1;
36
                    char * argv[4] = { "-iconic", 0 , 0, 0};
37
            if(display) 
38
            {
39
                argc = 3;
40
                argv[1] = "-display";
41
                argv[2] = (char*) display;
42
            }
43
                    glutInit(&argc, argv);
44
                    glutInitDisplayMode (GLUT_RGBA ); 
45
                    _glut_init_called = 1; 
46
            }
47
            if(x != -1) glutInitWindowPosition(x, y);
48
        if(display || x != -1) std::cout << "Using display ["
49
            << (display? display : "\0" )<< "] at (" << x << "," << y << ")\n";
50
            glut_id = glutCreateWindow ("SIFT_GPU_GLUT");
51
            glutHideWindow();
52
    }
53
};
54
#elif defined( _WIN32)
55
56
#ifndef _INC_WINDOWS
57
#ifndef WIN32_LEAN_AND_MEAN
58
        #define WIN32_LEAN_AND_MEAN
59
#endif
60
#include <windows.h>
61
#endif 
62
63
class LiteWindow
64
{
65
    HWND hWnd;
66
    HGLRC hContext;
67
    HDC hdc;
68
public:
69
    LiteWindow()
70
    {
71
        hWnd = NULL;
72
        hContext = NULL;
73
        hdc = NULL;
74
    }
75
    virtual ~LiteWindow()
76
    {
77
        if(hContext)wglDeleteContext(hContext);
78
        if(hdc)ReleaseDC(hWnd, hdc);
79
        if(hWnd)DestroyWindow(hWnd);
80
    }
81
    int IsValid()
82
    {  
83
        return hContext != NULL;  
84
    }
85
86
    //display is ignored under Win32
87
    void Create(int x = -1, int y = -1, const char* display = NULL)
88
    {
89
        if(hContext) return;
90
        WNDCLASSEX wcex = { sizeof(WNDCLASSEX),  CS_HREDRAW | CS_VREDRAW,  
91
                            (WNDPROC)DefWindowProc,  0, 4, 0, 0, 0, 0, 0,
92
                            ("SIFT_GPU_LITE"),    0};
93
        RegisterClassEx(&wcex);
94
        hWnd = CreateWindow("SIFT_GPU_LITE", "SIFT_GPU", 0,    
95
                            CW_USEDEFAULT, CW_USEDEFAULT, 
96
                            100, 100, NULL, NULL, 0, 0);
97
98
        //move the window so that it can be on the second monitor
99
        if(x !=-1) 
100
        {
101
            MoveWindow(hWnd, x, y, 100, 100, 0);
102
            std::cout << "CreateWindow at (" << x << "," << y<<")\n";
103
        }
104
105
        ///////////////////////////////////////////////////
106
        PIXELFORMATDESCRIPTOR pfd = 
107
        {
108
            sizeof(PIXELFORMATDESCRIPTOR), 1, 
109
            PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL ,        
110
            PFD_TYPE_RGBA,16,0, 0, 0, 0, 0, 0, 0, 0, 
111
            0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0                     
112
        };
113
        hdc=GetDC(hWnd);
114
        ////////////////////////////////////
115
        int pixelformat = ChoosePixelFormat(hdc, &pfd);
116
        DescribePixelFormat(hdc, pixelformat, sizeof(pfd), &pfd);
117
        SetPixelFormat(hdc, pixelformat, &pfd);    
118
        hContext = wglCreateContext(hdc);
119
120
    }
121
    void MakeCurrent()
122
    {
123
        wglMakeCurrent(hdc, hContext);
124
    }
125
};
126
127
#else
128
129
#include <unistd.h>
130
#include <X11/Xlib.h>
131
#include <GL/glx.h>
132
133
class LiteWindow
134
{
135
    Display*     xDisplay;
136
    XVisualInfo* xVisual;    
137
    Window       xWin;
138
    GLXContext   xContext;
139
    Colormap     xColormap;
140
public:
141
    LiteWindow()
142
    {
143
        xDisplay = NULL;
144
        xVisual = NULL;
145
        xWin = 0;
146
        xColormap = 0;
147
        xContext = NULL;
148
    }
149
    int IsValid ()  
150
    {
151
        return xContext != NULL  && glXIsDirect(xDisplay, xContext);
152
    }
153
    virtual ~LiteWindow()
154
    {
155
        if(xWin) XDestroyWindow(xDisplay, xWin);
156
        if(xContext) glXDestroyContext(xDisplay, xContext);
157
        if(xColormap) XFreeColormap(xDisplay, xColormap);
158
        if(xDisplay) XCloseDisplay(xDisplay);
159
    }
160
    void Create(int x = 0, int y = 0, const char * display = NULL)
161
    {
162
        if(xDisplay) return;
163
        if(display) std::cout << "Using display ["<<display<<"]\n";
164
165
        xDisplay = XOpenDisplay(display && display[0] ? display : NULL);
166
        if(xDisplay == NULL) return;
167
        int attrib[] =  {GLX_RGBA, GLX_RED_SIZE, 1, 
168
                         GLX_GREEN_SIZE, 1, GLX_BLUE_SIZE, 1,  0 }; 
169
        xVisual = glXChooseVisual(xDisplay, DefaultScreen(xDisplay), attrib);
170
        if(xVisual == NULL) return;
171
        xColormap = XCreateColormap(
172
            xDisplay, RootWindow(xDisplay, xVisual->screen), 
173
            xVisual->visual, AllocNone);
174
175
        XSetWindowAttributes wa;
176
        wa.event_mask       = 0;
177
        wa.border_pixel     = 0;
178
        wa.colormap = xColormap;
179
           
180
        xWin = XCreateWindow( xDisplay, RootWindow(xDisplay, xVisual->screen) , 
181
                              x, y, 100, 100, 0, xVisual->depth, 
182
                              InputOutput, xVisual->visual, 
183
                              CWBorderPixel |CWColormap | CWEventMask, &wa);
184
            
185
        xContext = glXCreateContext(xDisplay, xVisual,  0, GL_TRUE); 
186
    }
187
    void MakeCurrent()
188
    {
189
        if(xContext) glXMakeCurrent(xDisplay, xWin, xContext);
190
    }
191
};
192
193
#endif
194
195
196
#endif