我在Ubuntu 14.04上使用GPU Nvidia Titan X(驱动程序版本375.66)以相同的刷新率使用两个具有不同原始分辨率的显示器。我想在第二台显示器上实时显示捕获的图像全屏,同时在主显示器上操作相机GUI。
使用glfw
和OpenGL
的最低复制次数为:
#include <stdio.h>
#include <stdlib.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
GLFWwindow* open_window(const char* title, GLFWmonitor* monitor)
{
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
GLFWwindow* window = glfwCreateWindow(mode->width, mode->height, title, monitor, NULL);
if (!window)
return NULL;
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1);
glfwShowWindow(window);
return window;
}
GLuint create_texture(GLFWmonitor* monitor)
{
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
int width = mode->width;
int hieght = mode->height;
char pixels[width * hieght];
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
for (int y = 0; y < hieght; y++)
for (int x = 0; x < width; x++)
pixels[y * width + x] = rand() % width;
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, width, hieght, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
return texture;
}
void draw_quad(GLuint texture)
{
int width, height;
glfwGetFramebufferSize(glfwGetCurrentContext(), &width, &height);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBegin(GL_QUADS);
glTexCoord2f(0.f, 0.f);
glVertex2f(0.f, 0.f);
glTexCoord2f(1.f, 0.f);
glVertex2f(1.f, 0.f);
glTexCoord2f(1.f, 1.f);
glVertex2f(1.f, 1.f);
glTexCoord2f(0.f, 1.f);
glVertex2f(0.f, 1.f);
glEnd();
}
int main(int argc, char** argv)
{
if (!glfwInit())
exit(EXIT_FAILURE);
// detect and print monitor information
int monitor_count;
GLFWmonitor** monitors = glfwGetMonitors(&monitor_count);
printf("Number of monitors detected: %d\n", monitor_count);
if (monitor_count < 2)
{
printf("The second monitor is not connected.\n");
return false;
}
// open a window fullscreen on second monitor
GLFWwindow *window = open_window("Captured image", monitors[1]);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
// the loop
while (!glfwWindowShouldClose(window))
{
// create the image (say, the captured image via camera App)
GLuint texture = create_texture(monitors[1]);
// show the image
draw_quad(texture);
glfwSwapBuffers(window);
}
glfwTerminate();
exit(EXIT_SUCCESS);
}
问题是,当我将当前焦点切换到相机GUI或终端时,Ubuntu的标题栏会出现。例如: 顶部:专注于窗口(全屏完美)。下图:关注终端;注意现在出现不需要的顶部标题栏(标记为红色)。
X将两个显示器视为一个大屏幕,因此屏幕截图中有额外的黑色像素:
我发现https://bugs.launchpad.net/unity/+bug/853865表示第二个显示屏上的顶部面板无法隐藏,并被视为Unity的待实现功能。
如何在glfw
内克服此问题?如果没有,任何其他OpenGL
框架替代建议?我想保持多平台。