Subversion Repositories seema-scanner

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jakw 1
#include "OpenGLContext.h"
2
#include <GLFW/glfw3.h>
3
 
4
#ifdef __LINUX__
5
    #define GLFW_EXPOSE_NATIVE_X11
6
    #define GLFW_EXPOSE_NATIVE_GLX
7
    #include <cstring>
8
    #include <GLFW/glfw3native.h>
9
#endif
10
 
11
struct OpenGLContext::OpenGLContextInfo{
12
    GLFWmonitor* monitor;
13
    GLFWwindow* window;
14
    OpenGLContextInfo() : monitor(NULL), window(NULL){}
15
};
16
 
17
void error_callback(int error, const char* description){
18
    std::cerr << "GLFW error code " << error << " " << description << std::endl;
19
}
20
 
21
 
22
std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
23
    std::vector<ScreenInfo> ret;
24
 
25
    if (!glfwInit())
26
        std::cerr << "Could not initialize GLFW!" << std::endl;
27
    glfwSetErrorCallback(error_callback);
28
 
29
    int nScreens = 0;
30
    GLFWmonitor **monitors;
31
    monitors = glfwGetMonitors(&nScreens);
32
 
33
    for(int i=0; i<nScreens; i++){
34
 
35
        GLFWmonitor *monitor = monitors[i];
36
        const GLFWvidmode *videoMode = glfwGetVideoMode(monitor);
37
 
38
        ScreenInfo screen;
39
        screen.resX = videoMode->width;
40
        screen.resY = videoMode->height;
41
        const char *screenName = glfwGetMonitorName(monitor);
42
        screen.name = screenName;
43
 
44
        ret.push_back(screen);
45
    }
46
 
47
    //glfwTerminate();
48
 
49
    return ret;
50
}
51
 
52
OpenGLContext::OpenGLContext(uint _screenNum) : screenNum(_screenNum), screenResX(0), screenResY(0){
53
 
54
    contextInfo = new OpenGLContextInfo();
55
 
56
    if (!glfwInit())
57
        std::cerr << "Could not initialize GLFW!" << std::endl;
58
    glfwSetErrorCallback(error_callback);
59
 
60
    int nScreens = 0;
61
    GLFWmonitor **monitors;
62
    monitors = glfwGetMonitors(&nScreens);
63
 
64
    if((unsigned int)nScreens < screenNum+1)
65
        std::cerr << "Could not open screen " << screenNum << std::endl;
66
 
67
    contextInfo->monitor = monitors[screenNum];
68
    const GLFWvidmode *videoMode = glfwGetVideoMode(contextInfo->monitor);
69
 
70
    screenResX = videoMode->width;
71
    screenResY = videoMode->height;
72
 
73
    // Create fullscreen window
74
    contextInfo->window = glfwCreateWindow(screenResX, screenResY, "GLFW OpenGL Context", contextInfo->monitor, NULL);
75
 
76
#ifdef __unix__
77
    Display *display = glfwGetX11Display();
78
    Window window = glfwGetX11Window(contextInfo->window);
79
 
80
//    // Set swap interval to 1 for standard vsync
81
//    typedef GLvoid (*glXSwapIntervalSGIFunc) (GLint);
82
//    const char *glx_extensions = glXQueryExtensionsString(display, screenNum);
83
//    if (strstr(glx_extensions, "GLX_SGI_swap_control")) {
84
//        PFNGLXSWAPINTERVALSGIPROC SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalSGI");
85
//        SwapIntervalSGI(1);
86
//    } else if (strstr(glx_extensions, "GLX_EXT_swap_control")) {
87
        PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
88
        SwapIntervalEXT(display, window, 1);
89
//    } else {
90
//        std::cerr << "OpenGLContext.Unix Error: Could not access swap interval extension!" << std::endl;
91
//    }
92
#endif
93
 
94
    glfwMakeContextCurrent(contextInfo->window);
95
    // Set vsync (swap interval 1)
96
    glfwSwapInterval(1);
97
 
98
    // Adjust gamma to one
99
    this->setGamma(1.0);
100
}
101
 
102
void OpenGLContext::setGamma(float gamma){
103
    glfwSetGamma(contextInfo->monitor, gamma);
104
}
105
 
106
void OpenGLContext::makeContextCurrent(){
107
    glfwMakeContextCurrent(contextInfo->window);
108
}
109
 
110
void OpenGLContext::flush(){
111
 
112
    // Swap buffers
113
    glfwSwapBuffers(contextInfo->window);
114
 
115
    // Poll events in order not to lock up
116
    glfwPollEvents();
117
    //glfwWaitEvents();
118
    glFinish();
119
}
120
 
121
OpenGLContext::~OpenGLContext(){
122
 
123
    glfwDestroyWindow(contextInfo->window);
124
    glfwTerminate();
125
 
126
    delete contextInfo;
127
}
128