问题描述
有人告诉我将旧配置文件从其他stackoverflow帖子切换到核心配置文件,但我似乎找不到解决方法.因此,我将发布一个更新后的错误信息,以帮助我找到解决方法.
I've been told to switch legacy profile to core profile from other stackoverflow posts but I can't seem to find a way to do that. So I'm positing a more updated error post to help me figure out a way.
代码:
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 = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader,GL_FRAGMENT_SHADER))
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, 36, triangle, GL_STATIC_DRAW)
position = glGetAttribLocation(shader, "position")
glVertexAttribPoint(position, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(position)
glUseProgram(shader)
glClearColor(0.2, 0.3, 0.2, 1.0)
while not glfw.window_should_close(window):
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT)
glDrawArrays(GL_TRIANGLES, 0, 3)
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()
我不断收到此错误
File "/Users/Datboi/Desktop/Python/Opengl/main.py", line 71, in <module>
main()
File "/Users/Datboi/Desktop/Python/Opengl/main.py", line 43, in main
shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/OpenGL/GL/shaders.py", line 226, in compileShader
shaderType,
RuntimeError: ('Shader compile failure (0): b"ERROR: 0:2: \'\' : version \'460\' is not supported\\n"', [b'\n #version 460\n in vec3 position;\n \n void main()\n {\n gl_Position = position;\n }\n\n '], GL_VERTEX_SHADER)
我似乎找不到任何解决此问题的方法.我是OpenGL/PyOpenGL的新手,找不到任何提出大致相同问题的帖子
I can't seem to find anything to fix this issue. I'm new to OpenGL/PyOpenGL and couldn't find any posts that have asked roughly the same question
推荐答案
是由于创建窗口之前未指定OpenGL版本而引起的.由于您的系统仅支持OpenGL verison 4.1,因此您必须添加以下代码行.另请参见在OS X上进行OpenGL开发:
is caused because you don't specify the OpenGL version before you create the window. Since your system only support OpenGL verison 4.1, you have to add the follwong lines of code. See also OpenGL Development on OS X:
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
window = glfw.create_window(800,600,"My OpenGL Window", None, None)
如果您解决了该问题,那么您将面临下一个问题:
If you solve that issue, then you'll face the next issue:
gl_Position
是同质坐标,即顶点着色器的输入变量 position
的类型为 vec3
.
While gl_Position
is a Homogeneous coordinates, the vertex shader input variable position
is of type vec3
.
gl_Position = position
的分配导致错误:
您可以通过更改顶点着色器来解决此问题.要么更改分配
You can solve this issue by changing the vertex shader. Either change the assignment
#version 410 core
in vec3 position;
void main()
{
gl_Position = vec4(position, 1.0);
}
或更改输入变量的类型:
or change the type of the input variable:
#version 410 core
in vec4 position;
void main()
{
gl_Position = position;
}
由于在核心版本4.60中已弃用 gl_FragColor
,因此您必须声明一个片段着色器输出变量,而不是使用已弃用的内置输出变量 gl_FragColor
:
Since gl_FragColor
is deprecated in core version 4.60, you have to declare a fragment shader output variable instead of using the deprecated built in output variable gl_FragColor
:
#version 410 core
out vec4 frag_color;
void main()
{
frag_color = vec4(1.0f,0.0f,0.0f,1.0f);
}
此外,您的程序中有错字.它必须是 glVertexAttribPointer
代替 glVertexAttribPoint
.
Further there is a typo in your program. It has to be glVertexAttribPointer
instead of glVertexAttribPoint
.
请注意,在核心配置文件 OpenGL上下文中,您必须使用顶点数组对象,因为默认的顶点数组对象 0 在核心个人资料中无效.
Note, in a core profile OpenGL Context, you have to use a Vertex Array Object, because the default vertex array object 0 is not valid in a core profile.
在OpenGL上下文成为当前上下文之后创建并绑定顶点数组对象:
Create and bind vertex array object after the OpenGL context has become current:
glfw.make_context_current(window)
VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)
shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader,GL_FRAGMENT_SHADER))
这篇关于MacOS上的PyOpenGL OpenGL版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!