我是PYopenGL的新手, 实际上,我也不确定PYopenGL是否适合我的任务。
我有一个Wavefront obj文件格式的3D模型。我需要从给定的视图中获取模型的“打印屏幕”。换句话说,我需要渲染模型,而不是显示模型,将其另存为图像(jpg)
我的想法是使用PYopenGL完成这项任务。然而,谷歌搜索我找不到任何建议或示例如何做到这一点。因此,如果PYopenGL是我的任务的正确工具,我开始怀疑。
你们中的某些人已经有过这样的事情,或者知道一个我可以用来学习的例子吗?
提前致谢。
道之
答案 0 :(得分:1)
我的回答(部分基于CodeSurgeon's answer)是问题的第二部分。
离屏渲染(意味着向内部缓冲区渲染内容而不是可见窗口并将渲染图像保存到文件或作为http响应传输到网页上显示)在PyOpenGL中(如在OpenGL本身中一样) )有点棘手,因为到目前为止GLUT所做的一切(创建窗口,初始化opengl上下文等)你现在需要用手做,因为你不需要标准的GLUT窗口弹出甚至闪烁。
因此在OpenGL中有3种离屏渲染方法:
1)使用GLUT进行初始化,但隐藏过剩窗口并渲染到它。此方法完全独立于平台,但GLUT窗口在初始化期间短时间出现,因此它不适合Web服务器,但您仍然可以将其设置为单独的Web服务器,仅在启动时进行初始化并使用某些接口与之通信它
2)手动创建所有内容:隐藏窗口,OpenGL上下文和要渲染的Framebuffer对象。这个方法很好,因为你控制了一切而且没有窗口出现,但是上下文的创建是特定于平台的(下面是Win64的例子)
3)第3种方法与方法2类似,但使用WGL创建的默认Framebuffer,而不是手工创建FBO。与方法2相同的效果,但更简单。如果由于其他原因不需要FBO,可能是最好的原因。
现在我将描述方法2,硬核方法。我的GitHub repository中有更多样本。
因此,屏幕外渲染算法包括以下步骤:
所以下面有完整的例子。
重要! 3.1.1 PyOpenGL实现有BUG!当您导入WGL时,glReadPixels()开始崩溃
ctypes.ArgumentError:参数7 ::错误类型
要避免这种情况,请转到您的包dir \ OpenGL \ raw \ WGL_types.py,找到以下行
HANDLE = POINTER(None) # /home/mcfletch/pylive/OpenGL-ctypes/src/wgl.h:60
# TODO: figure out how to make the handle not appear as a void_p within the code...
HANDLE.final = True
并替换为(对于x64当然,对于x86 UINT32假设)
HANDLE = UINT64
HANDLE.final = True
所以有例子
from win32api import *
from win32con import *
from win32gui import *
from OpenGL.WGL import *
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
from PIL import ImageOps
import uuid
# =========================================
# I left here only necessary constants, it's easy to search for the rest
PFD_TYPE_RGBA = 0
PFD_MAIN_PLANE = 0
PFD_DOUBLEBUFFER = 0x00000001
PFD_DRAW_TO_WINDOW = 0x00000004
PFD_SUPPORT_OPENGL = 0x00000020
# =========================================
# OpenGL context creation helpers
def mywglCreateContext(hWnd):
pfd = PIXELFORMATDESCRIPTOR()
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL
pfd.iPixelType = PFD_TYPE_RGBA
pfd.cColorBits = 32
pfd.cDepthBits = 24
pfd.iLayerType = PFD_MAIN_PLANE
hdc = GetDC(hWnd)
pixelformat = ChoosePixelFormat(hdc, pfd)
SetPixelFormat(hdc, pixelformat, pfd)
oglrc = wglCreateContext(hdc)
wglMakeCurrent(hdc, oglrc)
# check is context created succesfully
# print "OpenGL version:", glGetString(GL_VERSION)
def mywglDeleteContext():
hrc = wglGetCurrentContext()
wglMakeCurrent(0, 0)
if hrc: wglDeleteContext(hrc)
# =========================================
# OpenGL Framebuffer Objects helpers
def myglCreateBuffers(width, height):
fbo = glGenFramebuffers(1)
color_buf = glGenRenderbuffers(1)
depth_buf = glGenRenderbuffers(1)
# binds created FBO to context both for read and draw
glBindFramebuffer(GL_FRAMEBUFFER, fbo)
# bind color render buffer
glBindRenderbuffer(GL_RENDERBUFFER, color_buf)
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_buf)
# bind depth render buffer - no need for 2D, but necessary for real 3D rendering
glBindRenderbuffer(GL_RENDERBUFFER, depth_buf)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buf)
return fbo, color_buf, depth_buf, width, height
def myglDeleteBuffers(buffers):
fbo, color_buf, depth_buf, width, height = buffers
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glDeleteRenderbuffers(1, color_buf)
glDeleteRenderbuffers(1, depth_buf)
glDeleteFramebuffers(1, fbo)
def myglReadColorBuffer(buffers):
fbo, color_buf, depth_buf, width, height = buffers
glPixelStorei(GL_PACK_ALIGNMENT, 1)
glReadBuffer(GL_COLOR_ATTACHMENT0)
data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
return data, width, height
# =========================================
# Scene rendering
def renderInit(width, height):
glClearColor(0.5, 0.5, 0.5, 1.0)
glColor(0.0, 1.0, 0.0)
gluOrtho2D(-1.0, 1.0, -1.0, 1.0)
glViewport(0, 0, width, height)
def render():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# draw xy axis with arrows
glBegin(GL_LINES)
# x
glVertex2d(-1, 0)
glVertex2d(1, 0)
glVertex2d(1, 0)
glVertex2d(0.95, 0.05)
glVertex2d(1, 0)
glVertex2d(0.95, -0.05)
# y
glVertex2d(0, -1)
glVertex2d(0, 1)
glVertex2d(0, 1)
glVertex2d(0.05, 0.95)
glVertex2d(0, 1)
glVertex2d(-0.05, 0.95)
glEnd()
glFlush()
# =========================================
# Windows stuff and main steps
def main():
# Create window first with Win32 API
hInstance = GetModuleHandle(None)
wndClass = WNDCLASS()
wndClass.lpfnWndProc = DefWindowProc
wndClass.hInstance = hInstance
wndClass.hbrBackground = GetStockObject(WHITE_BRUSH)
wndClass.hCursor = LoadCursor(0, IDC_ARROW)
wndClass.lpszClassName = str(uuid.uuid4())
wndClass.style = CS_OWNDC
wndClassAtom = RegisterClass(wndClass)
# don't care about window size, couse we will create independent buffers
hWnd = CreateWindow(wndClassAtom, '', WS_POPUP, 0, 0, 1, 1, 0, 0, hInstance, None)
# Ok, window created, now we can create OpenGL context
mywglCreateContext(hWnd)
# In OpenGL context create Framebuffer Object (FBO) and attach Color and Depth render buffers to it
width, height = 300, 300
buffers = myglCreateBuffers(width, height)
# Init our renderer
renderInit(width, height)
# Now everything is ready for job to be done!
# Render something and save it to file
render()
data, width, height = myglReadColorBuffer(buffers)
image = Image.frombytes("RGBA", (width, height), data)
image = ImageOps.flip(image) # in my case image is flipped top-bottom for some reason
# it's easy to achive antialiasing effect by resizing rendered image
# don't forget to increase initial rendered image resolution and line thikness for 2D
#image = image.resize((width/2, height/2), Image.ANTIALIAS)
image.save("fbo.png", "PNG")
# Shutdown everything
myglDeleteBuffers(buffers)
mywglDeleteContext()
main()
答案 1 :(得分:0)
PyOpenGL可用于您的目的,如果您感兴趣的是将3D模型/场景从特定角度渲染到图像,只要您不介意打开并运行OpenGL窗口即可。
网上有很多来源讨论如何为.obj格式编写解析器。除了查看格式为here的维基百科文章之外,我相信您可以在pygame website上找到固定函数obj加载器的实现。如果你自己制作.obj模型,它会更容易,因为规范非常宽松,编写一个健壮的解析器可能很难。或者,您可以使用Assimp
之类的库来加载模型并提取其数据,这些数据具有python pyAssimp
绑定。
至于保存屏幕截图,您需要将3D场景渲染为纹理。我强烈建议您查看this answer。如果要进行屏幕外渲染,您需要了解如何使用glReadPixels以及如何使用FBO(帧缓冲对象)。
答案 2 :(得分:0)
GLUT隐藏窗口方法更简单,与平台无关,但它会导致窗口闪烁。
为Django设置它,即您可以将渲染器实现为单独的Web服务器,它将在启动时仅按窗口闪烁一次,然后通过http响应返回渲染的图像。
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from PIL import Image
from PIL import ImageOps
import sys
width, height = 300, 300
def init():
glClearColor(0.5, 0.5, 0.5, 1.0)
glColor(0.0, 1.0, 0.0)
gluOrtho2D(-1.0, 1.0, -1.0, 1.0)
glViewport(0, 0, width, height)
def render():
glClear(GL_COLOR_BUFFER_BIT)
# draw xy axis with arrows
glBegin(GL_LINES)
# x
glVertex2d(-1, 0)
glVertex2d(1, 0)
glVertex2d(1, 0)
glVertex2d(0.95, 0.05)
glVertex2d(1, 0)
glVertex2d(0.95, -0.05)
# y
glVertex2d(0, -1)
glVertex2d(0, 1)
glVertex2d(0, 1)
glVertex2d(0.05, 0.95)
glVertex2d(0, 1)
glVertex2d(-0.05, 0.95)
glEnd()
glFlush()
def draw():
render()
glutSwapBuffers()
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutInitWindowSize(300, 300)
glutCreateWindow(b"OpenGL Offscreen")
glutHideWindow()
init()
render()
glPixelStorei(GL_PACK_ALIGNMENT, 1)
data = glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE)
image = Image.frombytes("RGBA", (width, height), data)
image = ImageOps.flip(image) # in my case image is flipped top-bottom for some reason
image.save('glutout.png', 'PNG')
#glutDisplayFunc(draw)
#glutMainLoop()
main()
答案 3 :(得分:0)
除了glutHideWindow()之外,如果需要在服务器上执行此操作,则可以使用虚拟显示。 requirements.txt:pyopengl,枕头,pyvirtualdisplay。 软件包:freeglut3-dev,xvfb。
from pyvirtualdisplay import Display
# before glutInit create virtual display
display = Display(visible=0, size=(HEIGHT, WIDTH))
display.start()