打开全屏OpenGL窗口

7
我正在尝试在Linux Red-hat上使用GLFW打开一个OpenGL全屏窗口。我的桌面跨越两个监视器,总分辨率为3840*1080。
我有两个问题: 1. 窗口只在一个监视器上打开,最大窗口宽度为1920(单个监视器的宽度)。 2. 窗口的最大高度为1003(我认为是屏幕减去任务栏和顶部栏的高度)。
这是我用来打开窗口的代码:
if (glfwInit() == GL_FALSE)
    std::cout<< "Unable to initialize GLFW\n";
glfwOpenWindowHint(GLFW_STEREO, GL_FALSE);
if (glfwOpenWindow(3840,1080,8,8,8,0,24,0,GLFW_FULLSCREEN) == GL_FALSE)
    std::cout<< "Unable to open window\n";
int width, height;
glfwGetWindowSize(&width, &height);
std::cout << "width = " << width << " height = " << height << "\n";

输出:宽度 = 1920 高度 = 1003

编辑: 我使用xrandr检查可用的屏幕模式,得到了以下结果:

屏幕0:最小3840 x 1080,当前3840 x 1080,最大3840 x 1080 默认连接3840x1080+0+0 0毫米 x 0毫米 3840x1080 50.0*

编辑2: 我已经更改了我的代码,使用X11打开窗口。

int doubleBufferAttributes[] = {
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE,   GLX_RGBA_BIT,
GLX_DOUBLEBUFFER,  True,  /* Request a double-buffered color buffer with */
GLX_RED_SIZE,      1,     /* the maximum number of bits per component    */
GLX_GREEN_SIZE,    1, 
    GLX_BLUE_SIZE,     1,
    None
};

static Bool WaitForNotify( Display *dpy, XEvent *event, XPointer arg ) {
    return (event->type == MapNotify) && (event->xmap.window == (Window) arg);
}
int main( int argc, char *argv[] )
{
    Display              *dpy;
    Window                xWin;
    XEvent                event;
    XVisualInfo          *vInfo;
    XSetWindowAttributes  swa;
    GLXFBConfig          *fbConfigs;
    GLXContext            context;
    GLXWindow             glxWin;
    int                   swaMask;
    int                   numReturned;
    int                   swapFlag = True;

    /* Open a connection to the X server */

dpy = XOpenDisplay( NULL );
if ( dpy == NULL ) {
    printf( "Unable to open a connection to the X server\n" );
    exit( EXIT_FAILURE );
}

/* Request a suitable framebuffer configuration - try for a double 
** buffered configuration first */
fbConfigs = glXChooseFBConfig( dpy, DefaultScreen(dpy),
                               doubleBufferAttributes, &numReturned );

/* Create an X colormap and window with a visual matching the first
** returned framebuffer config */
vInfo = glXGetVisualFromFBConfig( dpy, fbConfigs[0] );

swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask;
swa.colormap = XCreateColormap( dpy, RootWindow(dpy, vInfo->screen),
                                vInfo->visual, AllocNone );

swaMask = CWBorderPixel | CWColormap | CWEventMask;

xWin = XCreateWindow( dpy, RootWindow(dpy, vInfo->screen), 0, 0, 3840, 1080,
                      0, vInfo->depth, InputOutput, vInfo->visual,
                      swaMask, &swa );
XWindowAttributes attt;

XGetWindowAttributes( dpy,xWin, &attt);
std::cout << "he = " << attt.height << " wi = " << attt.width << "\n";

/* Create a GLX context for OpenGL rendering */
context = glXCreateNewContext( dpy, fbConfigs[0], GLX_RGBA_TYPE,
             NULL, True );
XGetWindowAttributes( dpy,xWin, &attt);
std::cout << "2he = " << attt.height << " wi = " << attt.width << "\n";


/* Create a GLX window to associate the frame buffer configuration
** with the created X window */
glxWin = glXCreateWindow( dpy, fbConfigs[0], xWin, NULL );
XGetWindowAttributes( dpy,xWin, &attt);
std::cout << "3he = " << attt.height << " wi = " << attt.width << "\n";

/* Map the window to the screen, and wait for it to appear */
XMapWindow( dpy, xWin );
XGetWindowAttributes( dpy,xWin, &attt);
std::cout << "4he = " << attt.height << " wi = " << attt.width << "\n";

XIfEvent( dpy, &event, WaitForNotify, (XPointer) xWin );
XGetWindowAttributes( dpy,xWin, &attt);
std::cout << "5he = " << attt.height << " wi = " << attt.width << "\n";


/* Bind the GLX context to the Window */
glXMakeContextCurrent( dpy, glxWin, glxWin, context );
XGetWindowAttributes( dpy,xWin, &attt);
std::cout << "6he = " << attt.height << " wi = " << attt.width << "\n";

输出结果为:
he = 1080 wi = 3840
2he = 1080 wi = 3840
3he = 1080 wi = 3840
4he = 1080 wi = 3840
5he = 1003 wi = 1920
6he = 1003 wi = 1920

看起来当窗口显示时它的大小缩小了。


你尝试过枚举可用的视频模式并检查哪些是可用的吗? - Nicol Bolas
我使用GLFW打开窗口,所以不需要。我使用“xrandr”查询视频模式: 屏幕0:最小3840 x 1080,当前3840 x 1080,最大3840 x 1080。我正在更改代码,直接使用X打开窗口。你能指向一个列举可用视频模式的函数吗? 默认连接3840x1080+0+0 0mm x 0mm 3840x1080 50.0* - Tal Darom
@NicolBolas 请查看问题的最新编辑。 - Tal Darom
如果您的窗口在创建后被调整大小,那么您的窗口管理器认为它比您更知道该做什么。在这种情况下,我会更改WM或向WM开发人员提交错误报告。 - datenwolf
2个回答

21

我不清楚GLFW,也许它存在一些问题,但X11全屏窗口并不像那样工作。任何值得一试的窗口管理器都会强制窗口适应(单个,非虚拟)屏幕。

你需要完全绕过窗口管理器(使用OverrideRedirect窗口属性),或者要求您的WM合作(使用窗口属性_NET_WM_STATE_FULLSCREEN)。 第一种方法有很多缺点,因此我们使用第二种方法。以下程序将在您的显示器上显示一个窗口,然后将其切换到全屏模式:

#include <X11/X.h>
#include <X11/Xlib.h>
#include <strings.h>
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>

int main ()
{
    Display* dis = XOpenDisplay(NULL);
    Window win = XCreateSimpleWindow(dis, RootWindow(dis, 0), 0, 0, 10, 10,
                                     0, BlackPixel (dis, 0), BlackPixel(dis, 0));

    Atom wm_state = XInternAtom(dis, "_NET_WM_STATE", False);
    Atom fullscreen = XInternAtom(dis, "_NET_WM_STATE_FULLSCREEN", False);

    XEvent xev;
    memset(&xev, 0, sizeof(xev));
    xev.type = ClientMessage;
    xev.xclient.window = win;
    xev.xclient.message_type = wm_state;
    xev.xclient.format = 32;
    xev.xclient.data.l[0] = 1;
    xev.xclient.data.l[1] = fullscreen;
    xev.xclient.data.l[2] = 0;

    XMapWindow(dis, win);

    XSendEvent (dis, DefaultRootWindow(dis), False,
                    SubstructureRedirectMask | SubstructureNotifyMask, &xev);

    XFlush(dis);
    /*Sleep 5 seconds before closing.*/
    sleep(5);
    return(0);

}

为了避免任何调整大小的动画效果,你可能需要从一开始就使用真实的屏幕尺寸来创建窗口。

我没有在多显示器系统上尝试过,因为我没有这样的设备,但在单个显示器系统上它可以正常工作(覆盖面板,去掉窗口装饰等)。如果它适用于您,请告诉我。

更新 他们说要使多显示器工作,您需要使用_NET_WM_FULLSCREEN_MONITORS属性(请参见此处)。 它是一个由4个整数组成的数组,应该像这样设置:

    Atom fullmons = XInternAtom(dis, "_NET_WM_FULLSCREEN_MONITORS", False);
    XEvent xev;
    memset(&xev, 0, sizeof(xev));
    xev.type = ClientMessage;
    xev.xclient.window = win;
    xev.xclient.message_type = fullmons;
    xev.xclient.format = 32;
    xev.xclient.data.l[0] = 0; /* your topmost monitor number */
    xev.xclient.data.l[1] = 0; /* bottommost */
    xev.xclient.data.l[2] = 0; /* leftmost */
    xev.xclient.data.l[3] = 1; /* rightmost */
    xev.xclient.data.l[4] = 0; /* source indication */

    XSendEvent (dis, DefaultRootWindow(dis), False,
                    SubstructureRedirectMask | SubstructureNotifyMask, &xev);

有了这个,你就能够将你的全屏窗口设置为占据单个显示器、整个桌面或(对于超过两个显示器的情况)介于两者之间的任何位置。

我没有检查过这一点,因为我没有多头系统。


感谢您提供详细的答案。在看到您的回答之前,我尝试使用了OverrideRedirect(您建议的第一种方法)。最终,我在xorg.conf中启用了Xinerama模式(这是一种将WM欺骗成只有一个大监视器的模式)。 - Tal Darom
1
非常感谢,这是唯一在X11全屏问题上对我有效的答案。此外:xev.xclient.data.l[0] = 1; 可以改为=0进行还原或=2进行切换。 - TingQian LI

0

xcb 版本:

    xcb_connection_t *xcbConnection = xcb_connect(":0", &screenp);
    xcb_intern_atom_cookie_t type = xcb_intern_atom(xcbConnection, false, strlen("_NET_WM_FULLSCREEN_MONITORS"), "_NET_WM_FULLSCREEN_MONITORS");
    xcb_intern_atom_reply_t *reply_st = xcb_intern_atom_reply(xcbConnection, type, NULL);

    xcb_client_message_event_t event;
    memset(&event, 0, sizeof(event));
    event.response_type = XCB_CLIENT_MESSAGE;
    event.type = reply_st->atom;
    event.window =  this->winId();
    event.format = 32;
    event.data.data32[0] =  0; // top
    event.data.data32[1] =  0; // bottom
    event.data.data32[2] =  0; // left
    event.data.data32[3] =  1; // right

    event.data.data32[4] = 1;
    uint32_t eventMask = XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT|XCB_EVENT_MASK_SUBSTRUCTURE_NOTIFY;
    xcb_send_event(xcbConnection, false, pScreen0->root, eventMask, reinterpret_cast<const char*>(&event));
    xcb_flush(xcbConnection);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接