为 OpenGL 编写着色器

问题描述 投票:0回答:1

我正在用 python 写下我自己的 OpenGL 着色器:

我已经写下了这两个着色器:

顶点着色器:

#version 330 core

layout(location=0) in vec3 vertexPos;

out vec4 position;

void main()
{
position = vec4(vertexPos,1.0);
}

片段着色器:

#version 330 core

layout(location=1) in vec3 vertexColor;

out vec4 color;

void main()
{
color = vec4(vertexColor,1.0);
}

着色器是正确的吗? OpenGL 不会产生错误,对吗?

编辑:

我已经调试了它,它打印出这条消息:

OpenGL.GL.shaders.ShaderCompilationError: ('Shader compile failure (0): b\'Fragment shader failed to compile with the following errors:\\nERROR: 2:1: error(#5) Extension: "explicit attribute location in fragment shader is only supported for the out storage qualifier"\\nERROR: error(#273) 1 compilation errors.  No code generated\\n\\n\'', [b'#version 330 core\n', b'\n', b'layout(location=1) in vec3 vertexColor;\n', b'\n', b'out vec4 color;\n', b'\n', b'void main()\n', b'{\n', b'color = vec4(vertexColor,1.0);\n', b'}\n', b'\n'], GL_FRAGMENT_SHADER)

但是为什么顶点着色器可以编译而片段着色器不可以编译呢?

python opengl glsl
1个回答
0
投票

片段着色器无法直接处理顶点属性。您需要将属性从顶点传递到片段着色器。此外,顶点着色器必须写入

gl_Position
(无论着色器的版本如何):

顶点着色器:

#version 330 core

layout(location=0) in vec3 vertexPos;
layout(location=1) in vec3 vertexColor;

out vec3 vColor;

void main()
{
    vColor = vertexColor;
    gl_Position = vec4(vertexPos,1.0);
}

片段着色器:

#version 330 core

in vec3 vColor;
out vec3 color;

void main()
{
    color = vec4(vColor,1.0);
}
© www.soinside.com 2019 - 2024. All rights reserved.