我正在尝试在代码中设置着色器,但未运行
我在Mac上,我的伴侣可以完美运行它,但我不能使用OpenGl 3.3
这是我的代码中发生错误的一部分,我尝试过更改着色器的版本,在不同的环境中运行,使用了PyCharm,空闲的Jupyter笔记本...
if __name__ == "__main__":
# Initialize glfw
if not glfw.init():
sys.exit()
width = 800
height = 600
window = glfw.create_window(width, height, "Window name", None, None)
if not window:
glfw.terminate()
sys.exit()
glfw.make_context_current(window)
# Connecting the callback function 'on_key' to handle keyboard events
glfw.set_key_callback(window, on_key)
# Defining shaders for our pipeline
vertex_shader = """
#version 330
in vec3 position;
in vec3 color;
out vec3 fragColor;
void main()
{
fragColor = color;
gl_Position = vec4(position, 1.0f);
}
"""
fragment_shader = """
#version 330
in vec3 fragColor;
out vec4 outColor;
void main()
{
outColor = vec4(fragColor, 1.0f);
}
"""
vertex_shader2 = """
#version 330
in vec3 position;
in vec3 color;
out vec3 fragColor;
void main()
{
fragColor = color;
gl_Position = vec4(2 * position, 1.0f);
}
"""
fragment_shader2 = """
#version 330
in vec3 fragColor;
out vec4 outColor;
void main()
{
float meanColor = (fragColor.r + fragColor.g + fragColor.b) / 3;
outColor = vec4(meanColor, meanColor, meanColor, 1.0f);
}
"""
# Assembling the shader program (pipeline) with both shaders
shaderProgram = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
shaderProgram2 = OpenGL.GL.shaders.compileProgram(
OpenGL.GL.shaders.compileShader(vertex_shader2, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader2, GL_FRAGMENT_SHADER))
它应该绘制一个三角形,但显示以下错误:
Traceback (most recent call last):
File "/Users/gustavo/PycharmProjects/untitled1/oli.py", line 186, in <module>
OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
File "/Users/gustavo/PycharmProjects/untitled1/venv/lib/python3.7/site-packages/OpenGL/GL/shaders.py", line 226, in compileShader
shaderType,
RuntimeError: ('Shader compile failure (0): b'ERROR: 0:2: '' : version '130' is not supported\n ERROR: 0:11: 'f' : syntax error: syntax error\n '', [b'
#version 130
in vec3 position;
in vec3 color;
out vec3 fragColor;
void main()
{
fragColor = color;
gl_Position = vec4(position, 1.0f);
}
'], GL_VERTEX_SHADER)
答案 0 :(得分:3)
MacOS使用Legacy Profile作为所有创建的OpenGL上下文的默认设置。因此,默认情况下,仅支持OpenGL最高为2.1,GLSL最高为1.20。
要使用OpenGL 3.2+,您需要切换到Core Profile。命名有点混乱,因为它仅统计3.2Core配置文件,但实际上仅统计3.2Core或更高版本(系统/驱动程序支持的向后兼容3.2的每个OpenGL配置文件)
对于glut(如果可用,取决于glut的版本),MacOS上的命令为:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
对不起,我不知道python版本,但应该是类似的功能。