我已经开始使用PyOpenGL 3.0.1b在Python中使用OpenGL。
我查看了一些示例代码,然后开始运行它并对其进行修改等。一切都很好,直到我变得不太懂了。
在http://pyopengl.sourceforge.net/documentation/manual-3.0/index.xhtml上,列出了OpenGL函数以及是否不建议使用它们。因此,我以为自己只需要找到一些不使用所有这些已弃用的废话的最新教程。
数小时后,再没有运气了!不推荐使用的示例代码之后不推荐使用的示例代码...我可以在哪里找到不推荐使用的教程?
我想使用 glClear 和 glClearColor 用包括 alpha 透明度的颜色填充帧缓冲区。然而,当绑定到渲染到屏幕的纹理时,帧缓冲区总是渲染为不透明的。
我希望渲染到帧缓冲区的所有内容都保持其透明度。我只是想改变背景。
请参阅以下代码:
def create_texture(surface):
surface.texture = glGenTextures(1)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity() #Loads model matrix
glBindTexture(GL_TEXTURE_2D, surface.texture) #Binds the current 2D texture to the texture to be drawn
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) #Required to be set for maping the pixel data
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) #Similar as above
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface.surface_size[0], surface.surface_size[1], 0, GL_RGBA,GL_UNSIGNED_BYTE, surface.data) #Put surface pixel data into texture
if surface.data == None:
setup_framebuffer(surface)
c = [float(sc)/255.0 for sc in surface.colour] #Divide colours by 255 …Run Code Online (Sandbox Code Playgroud) 什么是PyOpenGL等价物
#define BUFFER_OFFSET(i) (reinterpret_cast<void*>(i))
glDrawElements(GL_TRIANGLE_STRIP, count, GL_UNSIGNED_SHORT, BUFFER_OFFSET(offset))
Run Code Online (Sandbox Code Playgroud)
如果偏移量为0,那么
glDrawElements(GL_TRIANGLE_STRIP, count, GL_UNSIGNED_SHORT, None)
Run Code Online (Sandbox Code Playgroud)
工作,但我无法弄清楚如何指定缓冲对象的非零偏移.
我有一些代码在过剩窗口中使用PyOpenGL显示图形.在视网膜Macbook Pro上,此窗口以低分辨率模式显示,其中一个OpenGL像素由四个物理像素表示.如果它以完整的原始分辨率显示会更好.
我的问题
有没有办法在Retina显示器上使用过剩或其他方式在Python中获取原生分辨率OpenGL上下文?
问题示例
下面是PyOpenGL程序的最小工作示例.没有什么特别之处 - 任何有效的PyOpenGL代码都会展示这个问题.这是屏幕截图的放大细节.请注意,构成白色三角形的像素是OS X小部件像素大小的四倍.这是不是专门为Retina设备设计的程序的默认设置.我想知道如何为PyOpenGL程序关闭它.

这是代码:
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import os
def initFunc():
glDisable(GL_DEPTH_TEST)
glClearColor(0.0, 0.0, 0.0, 0.0)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0.0, 400.0, 0.0, 400.0)
def displayFunc():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0, 1.0, 1.0)
glBegin(GL_TRIANGLES)
glVertex2f(10.0, 10.0)
glVertex2f(10.0, 100.0)
glVertex2f(100.0, 100.0)
glEnd()
glFlush()
if __name__ == '__main__':
glutInit()
glutInitWindowSize(400,400)
glutCreateWindow("GL test")
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutDisplayFunc(displayFunc)
initFunc()
os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
# …Run Code Online (Sandbox Code Playgroud) 安装Yosemite后,我不得不升级numpy,PyOpenGL等.
现在,一个以前工作的程序给了我以下堆栈跟踪:
file "latebind.pyx", line 44, in OpenGL_accelerate.latebind.Curry.__call__ (src/latebind.c:1201)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/OpenGL/GL/VERSION/GL_1_5.py", line 89, in glBufferData
return baseOperation( target, size, data, usage )
File "latebind.pyx", line 32, in OpenGL_accelerate.latebind.LateBind.__call__ (src/latebind.c:989)
File "wrapper.pyx", line 314, in OpenGL_accelerate.wrapper.Wrapper.__call__ (src/wrapper.c:6505)
File "wrapper.pyx", line 311, in OpenGL_accelerate.wrapper.Wrapper.__call__ (src/wrapper.c:6439)
ctypes.ArgumentError: ("argument 3: <class 'OpenGL.error.CopyError'>: from_param received a non-contiguous array! []", (GL_ARRAY_BUFFER, 0, array([],
dtype=[('time', '<f8'), ('data', [('cluster_index', '<i4'), ('delta_diag_out', '<f8')])]), GL_STREAM_DRAW))
Run Code Online (Sandbox Code Playgroud)
看起来PyOpenGL希望我的数组是连续的.在PyOpenGL中查看numpy_formathandler.pyx中的源代码:
if not PyArray_ISCARRAY( instance ):
raise CopyError(
"""from_param received a non-contiguous array! %s"""%(
working, …Run Code Online (Sandbox Code Playgroud) 升级到ubuntu 14.04并通过pip安装PyOpenGL后.它在ubuntu 12.04下工作正常,但现在我每次尝试使用OpenGL.GLUT中的函数时都会出错,例如
from OpenGL.GLUT import *
print bool(glutInit)
glutInit(sys.argv)
Run Code Online (Sandbox Code Playgroud)
将打印出"True"和错误消息:
File "/usr/local/lib/python2.7/dist-packages/OpenGL/GLUT/special.py", line 333, in glutInit
_base_glutInit( ctypes.byref(count), holder )
File "/usr/local/lib/python2.7/dist-packages/OpenGL/platform/baseplatform.py", line 407, in __call__
self.__name__, self.__name__,
OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling
Run Code Online (Sandbox Code Playgroud)
我不明白,有什么建议吗?谢谢.
编辑:我刚刚通过pip重新安装PyOpenGL来解决这个问题.现在,同一程序按预期工作.谢谢你的努力.
整个问题被重写和澄清.
问题不在于(虽然很奇怪)纹理创建和绑定方法,如评论中所述.
我的实际问题是着色器中的统一变量在调用glDrawArrays()之间更改(是的,从着色器外部更改)时不会更新它们的值.我通过使用不同类型的制服,不同的值并通过渲染到纹理来检查它们的值来验证.
一旦调用glDrawArrays(),看起来制服的值就会被锁定.之后,我只能在清除颜色缓冲区时再次更改它们.
我愿意为任何有效的解决方案提供奖励.在我看来,发布任何源代码都没有帮助,因为问题似乎并不在于代码.
任何建议都是适用的.
我正在使用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)
Run Code Online (Sandbox Code Playgroud)
我发现了有关无头渲染的一些信息,但仅在直接使用OpenGL而不是通过python时才找到
编辑:我了解,也许glfw无法支持它。没有glfw但有其他东西的解决方案也可以工作...
标题是这里的主要问题。我在我的计算机上运行了一些 PyOpenGL 代码,但运行速度有些慢。我意识到我没有安装 PyOpenGL-accelerate。这根本没有改变速度,但是大多数关于 Python OpenGL 绑定的教程都建议应该安装 PyOpenGL-accelerate。
这个模块到底有什么作用?
有人告诉我将旧配置文件从其他 stackoverflow 帖子切换到核心配置文件,但我似乎找不到方法来做到这一点。因此,我发布了一个更新的错误帖子来帮助我找到一种方法。
代码:
import glfw, numpy
from OpenGL.GL import *
import OpenGL.GL.shaders
def main():
if not glfw.init():
return
window = glfw.create_window(800,600,"My OpenGL Window", None, None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
triangle = [-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0]
triangle = numpy.array(triangle, dtype = numpy.float32)
vertex_shader = """
#version 460
in vec3 position;
void main()
{
gl_Position = position;
}
"""
fragment_shader = """
#version 460
void main()
{
gl_FragColor = vec4(1.0f,0.0f,0.0f,1.0f);
}
"""
shader = …Run Code Online (Sandbox Code Playgroud)