PyOpenGL无头渲染

问题描述 投票:4回答:3

我正在使用PyOpenGL + glfw进行渲染。

当尝试在无头机器(例如服务器)上执行相同操作时,glfw.init()失败:

glfw.GLFWError: (65544) b'X11: The DISPLAY environment variable is missing'
Fatal Python error: Couldn't create autoTLSkey mapping
Aborted (core dumped)

我找到了一些关于无头渲染的信息,但仅限于直接使用OpenGL而不是通过python

编辑:我知道也许glfw无法支持它。没有glfw的解决方案,但有其他东西可能也有效......

opengl glfw headless pyopengl
3个回答
4
投票

GLFW根本不支持无头OpenGL。

https://www.glfw.org/docs/latest/context.html#context_offscreen

GLFW不支持在没有关联窗口的情况下创建上下文。

这不是一个不寻常的限制,问题是创建OpenGL上下文的正常方法是使用X服务器。现在有使用EGL的替代品,这是相对较新的。您需要为Python使用EGL包装器。

见:OpenGL without X.org in linux


1
投票

解决方案是使用xvfb作为虚拟帧缓冲区。

问题是使用apt-get install libglfw3 libglfw3-dev在Ubuntu中安装的glfw是旧的并且不合适,所以我们需要从源代码编译它。

这是一个完整的Docker示例:

docker run --name headless_test -ti ubuntu /bin/bash

# Inside the ubuntu shell:
apt update && apt install -y python3 python3-pip git python-opengl xvfb xorg-dev cmake
pip3 install pyopengl glfw
mkdir /projects
git clone https://github.com/glfw/glfw.git /projects/glfw
cd /projects/glfw
cmake -DBUILD_SHARED_LIBS=ON .
make
export PYGLFW_LIBRARY=/projects/glfw/src/libglfw.so
xvfb-run python3 some_script_using_pyopengl_and_glfw.py

以下是PyOpenGL代码的基础:

from OpenGL.GL import *
from OpenGL.GLU import *
import glfw

glfw.init()
# Set window hint NOT visible
glfw.window_hint(glfw.VISIBLE, False)
# Create a windowed mode window and its OpenGL context
window = glfw.create_window(DISPLAY_WIDTH, DISPLAY_HEIGHT, "hidden window", None, None)
# Make the window's context current
glfw.make_context_current(window)

0
投票

如果你想在Linux上使用没有显示环境的OpenGL(例如x服务器),最好的方法是使用EGL。 EGL的作用是将OpenGL上下文管理与窗口系统分开,因此它允许您创建没有显示窗口的上下文。

如果您使用的是Nvidia显卡,则必须安装专有驱动程序才能使用它。与驱动程序一起有一个名为GLVND的库,这是一个库,其中包含您的应用程序需要链接的EGL

请参阅以下链接以了解如何使用EGL

Pro Tip: Linking OpenGL for Server-Side Rendering

EGL Eye: OpenGL Visualization without an X Serve

PS。如果您的EGL api找不到任何设备,您可能链接了错误的EGL库,EGL库必须与驱动程序匹配。

© www.soinside.com 2019 - 2024. All rights reserved.