python 中没有可见 window 的 OpenGL 渲染视图
OpenGL render view without a visible window in python
我需要渲染一些场景。我设法在 python 中使用 pyopengl 和 pygame 做到了这一点。问题是它会在短时间内创建 window。
我想渲染相同的图像并保存它,而不创建可见的 window(或者可能根本不创建 window,甚至不创建 pygame)。
import cv2
import numpy as np
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def main():
DISPLAY_WIDTH = 900
DISPLAY_HEIGHT = 900
pygame.init()
pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT), DOUBLEBUF | OPENGL)
gluPerspective(90, (DISPLAY_WIDTH / DISPLAY_HEIGHT), 0.01, 12)
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glRotatef(-90, 1, 0, 0) # Straight rotation
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glRotatef(285, 0, 0, 1) # Rotate yaw
glTranslatef(-5, -3, -2) # Move to position
# Draw rectangle
glBegin(GL_QUADS)
glColor3f(1, 0, 0)
glVertex3f(2, 2, 0)
glVertex3f(2, 2, 2)
glVertex3f(2, 6, 2)
glVertex3f(2, 6, 0)
glEnd()
image_buffer = glReadPixels(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, OpenGL.GL.GL_RGB, OpenGL.GL.GL_UNSIGNED_BYTE)
image = np.frombuffer(image_buffer, dtype=np.uint8).reshape(DISPLAY_WIDTH, DISPLAY_HEIGHT, 3)
cv2.imwrite(r"C:\temp\image.png", image)
pygame.quit()
if __name__ == "__main__":
main()
I want to render the same image and save it, without creating a visible window
如果没有任何 window.
,则无法创建 1.0 以上版本的 OpenGL Context
查看问题 Creating OpenGL context without window.
的答案
但是可以使用完全隐藏的window进行"offscreen"渲染。
遗憾的是,无法使用 Pygame.
创建最初隐藏的 window
只有在 pygame.display.iconify()
创建后才能隐藏 window。
另见 Hiding pygame display.
但是可以创建一个最初隐藏的 window 与 GLFW library by setting the window hint VISIBLE
到 False
.
可以在 glfw 1.7.0.
找到 glfw 库
代码可能如下所示:
import cv2
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
import glfw
def main():
DISPLAY_WIDTH = 900
DISPLAY_HEIGHT = 900
# Initialize the library
if not glfw.init():
return
# 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)
if not window:
glfw.terminate()
return
# Make the window's context current
glfw.make_context_current(window)
gluPerspective(90, (DISPLAY_WIDTH / DISPLAY_HEIGHT), 0.01, 12)
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glRotatef(-90, 1, 0, 0) # Straight rotation
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glRotatef(285, 0, 0, 1) # Rotate yaw
glTranslatef(-5, -3, -2) # Move to position
# Draw rectangle
glBegin(GL_QUADS)
glColor3f(1, 0, 0)
glVertex3f(2, 2, 0)
glVertex3f(2, 2, 2)
glVertex3f(2, 6, 2)
glVertex3f(2, 6, 0)
glEnd()
image_buffer = glReadPixels(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, OpenGL.GL.GL_RGB, OpenGL.GL.GL_UNSIGNED_BYTE)
image = np.frombuffer(image_buffer, dtype=np.uint8).reshape(DISPLAY_WIDTH, DISPLAY_HEIGHT, 3)
cv2.imwrite(r"C:\temp\image.png", image)
glfw.destroy_window(window)
glfw.terminate()
if __name__ == "__main__":
main()
我需要渲染一些场景。我设法在 python 中使用 pyopengl 和 pygame 做到了这一点。问题是它会在短时间内创建 window。
我想渲染相同的图像并保存它,而不创建可见的 window(或者可能根本不创建 window,甚至不创建 pygame)。
import cv2
import numpy as np
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
def main():
DISPLAY_WIDTH = 900
DISPLAY_HEIGHT = 900
pygame.init()
pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT), DOUBLEBUF | OPENGL)
gluPerspective(90, (DISPLAY_WIDTH / DISPLAY_HEIGHT), 0.01, 12)
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glRotatef(-90, 1, 0, 0) # Straight rotation
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glRotatef(285, 0, 0, 1) # Rotate yaw
glTranslatef(-5, -3, -2) # Move to position
# Draw rectangle
glBegin(GL_QUADS)
glColor3f(1, 0, 0)
glVertex3f(2, 2, 0)
glVertex3f(2, 2, 2)
glVertex3f(2, 6, 2)
glVertex3f(2, 6, 0)
glEnd()
image_buffer = glReadPixels(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, OpenGL.GL.GL_RGB, OpenGL.GL.GL_UNSIGNED_BYTE)
image = np.frombuffer(image_buffer, dtype=np.uint8).reshape(DISPLAY_WIDTH, DISPLAY_HEIGHT, 3)
cv2.imwrite(r"C:\temp\image.png", image)
pygame.quit()
if __name__ == "__main__":
main()
I want to render the same image and save it, without creating a visible window
如果没有任何 window.
,则无法创建 1.0 以上版本的 OpenGL Context
查看问题 Creating OpenGL context without window.
但是可以使用完全隐藏的window进行"offscreen"渲染。
遗憾的是,无法使用 Pygame.
创建最初隐藏的 window
只有在 pygame.display.iconify()
创建后才能隐藏 window。
另见 Hiding pygame display.
但是可以创建一个最初隐藏的 window 与 GLFW library by setting the window hint VISIBLE
到 False
.
可以在 glfw 1.7.0.
找到 glfw 库代码可能如下所示:
import cv2
import numpy as np
from OpenGL.GL import *
from OpenGL.GLU import *
import glfw
def main():
DISPLAY_WIDTH = 900
DISPLAY_HEIGHT = 900
# Initialize the library
if not glfw.init():
return
# 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)
if not window:
glfw.terminate()
return
# Make the window's context current
glfw.make_context_current(window)
gluPerspective(90, (DISPLAY_WIDTH / DISPLAY_HEIGHT), 0.01, 12)
glEnable(GL_TEXTURE_2D)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glRotatef(-90, 1, 0, 0) # Straight rotation
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glRotatef(285, 0, 0, 1) # Rotate yaw
glTranslatef(-5, -3, -2) # Move to position
# Draw rectangle
glBegin(GL_QUADS)
glColor3f(1, 0, 0)
glVertex3f(2, 2, 0)
glVertex3f(2, 2, 2)
glVertex3f(2, 6, 2)
glVertex3f(2, 6, 0)
glEnd()
image_buffer = glReadPixels(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT, OpenGL.GL.GL_RGB, OpenGL.GL.GL_UNSIGNED_BYTE)
image = np.frombuffer(image_buffer, dtype=np.uint8).reshape(DISPLAY_WIDTH, DISPLAY_HEIGHT, 3)
cv2.imwrite(r"C:\temp\image.png", image)
glfw.destroy_window(window)
glfw.terminate()
if __name__ == "__main__":
main()