我最近尝试过使用lwjgl的OpenGL 4.0的子程序功能,但我无法使其工作。当查询GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS时,我得到0,因此子程序是统一的。在查询每个例程的索引时,我总是得到-1,这告诉我它们不“存在”。
以下是我的着色器: 顶点着色器:
#version 400
subroutine vec4 getColor(vec4 color);
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 ModelMatrix;
subroutine uniform getColor myColor;
layout(location = 0) in vec4 inPosition;
layout(location = 1) in vec4 inColor;
// Smooth
out vec4 vColor;
subroutine (getColor) vec4 invertColor(vec4 color){
return vec4(1.0 - color.r, 1.0 - color.g, 1.0 - color.b, color.a);}
subroutine (getColor) vec4 realColor(vec4 color){
return color;}
void main(){
gl_Position = ProjectionMatrix * ViewMatrix * ModelMatrix * inPosition;
vColor = invertColor(inColor);}
片段着色器:
#version 400
// Smooth
in vec4 vColor;
out vec4 pixel;
void main(){
pixel = vColor;}
这是我的代码:
shader.useProgram();
routineInvertColor = GL40.glGetSubroutineIndex(shader.getProgramId(), GL20.GL_VERTEX_SHADER, "invertColor");
routineRealColor = GL40.glGetSubroutineIndex(shader.getProgramId(), GL20.GL_VERTEX_SHADER, "realColor");
routineUniform = GL40.glGetSubroutineUniformLocation(shader.getProgramId(), GL20.GL_VERTEX_SHADER, "myColor");
IntBuffer subroutineBuffer = BufferUtils.createIntBuffer(1).put(routineInvertColor);
System.out.println(GL40.glGetProgramStagei(shader.getProgramId(), GL20.GL_VERTEX_SHADER, GL40.GL_ACTIVE_SUBROUTINE_UNIFORM_LOCATIONS));
System.out.println(routineInvertColor + " " + routineRealColor + " " + routineUniform);
GL40.glUniformSubroutinesu(GL20.GL_VERTEX_SHADER, subroutineBuffer);
答案 0 :(得分:1)
我找到了。我做了一件非常愚蠢的事。但在此之前,我混合了stackoverflow invertColor(inColor)和getColor(inColor):
我在做什么:
getColor(inColor);
我应该做的事情:
myColor(inColor);