为什么我的制服位置显示不正确?
Why are my uniform locations showing incorrectly?
我试图在我的着色器中获取变量的统一位置。
@Override
public void getAllUniformLocations(){
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
location_lightPosition = super.getUniformLocation("lightPosition");
location_lightColour = super.getUniformLocation("lightColour");
System.out.println(location_transformationMatrix + " | "
+ location_lightPosition + " | " + location_lightColour);
}
public int getUniformLocation(String name){
return GL20.glGetUniformLocation(programID, name);
}
但是打印出来的时候,打印的完全不正确:
0 | -1 | -1
0
用于固定功能管道,因此绑定一些东西会使程序完全崩溃,另外两个只是 returning -1,这是一个空值值。
这是我的着色器加载代码:
public ShaderProgram(String vertexFile,String fragmentFile){
System.out.println("Comiling shader!");
vertexShaderID = loadShader(vertexFile,GL20.GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile,GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexShaderID);
GL20.glAttachShader(programID, fragmentShaderID);
bindAttributes();
getAllUniformLocations();
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
start();
System.out.println(GL11.glGetError());
System.out.println("Comiled shader!");
}
着色器绑定、附加、验证、编译等完全成功,此时游戏成功 运行。但是,缺少统一的位置,所以我无法实现照明之类的东西。
这是我的顶点着色器:
#version 130
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
uniform mat4 transformationMatrix;
uniform vec3 lightPosition;
void main(void){
gl_Position = ftransform();
pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
toLightVector = (vec3(0, 20000, 0)) - (transformationMatrix * vec4(position, 1.0)).xyz;
}
变量 0、1、2 和 3 都绑定到属性,所以我希望数字 return 类似于“4 | 5 | 6”。
这是我的片段着色器:
#version 130
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
out vec4 out_Color;
uniform sampler2D textureSampler;
uniform vec3 lightColour;
void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);
float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.5);
brightness = brightness * 1.5;
vec3 diffuse = brightness * vec3(1, 1, 1);
vec4 text = texture(textureSampler, pass_textureCoords);
vec4 textureColor = vec4(diffuse, 1.0) * text;
if(textureColor.a<0.01){
discard;
}
out_Color = vec4(textureColor.r, textureColor.g, textureColor.b, textureColor.a);
}
您得到的结果是绝对正确的,您对它们的解释是错误的:
0
is for the fixed-function pipeline, so having something bound there
is going to completely crash the program,
没有。 0
是一个完全有效的统一位置,与固定功能管道无关。您似乎以某种方式混淆了程序对象 0
(代表旧版 GL 和兼容性配置文件中的固定功能管道)与 per-program 统一位置。
and the other two are simply
returning -1
, which is a null value.
嗯,或多或少。 -1
表示没有 acivte 同名制服。请注意,使用位置 -1
调用 glUniform*()
函数被明确定义为没有错误 - 这样的调用只是没有效果。
您粘贴的着色器代码根本没有使用 lightPosition
和 lightColour
制服,因此它们不存在。您可能打算在片段着色器中使用它们,但我敢打赌您没有以正确的方式在那里使用它们 - 即即使您在代码中声明并使用它们,它们可能仍然没有对着色器输出的影响,因此仍未激活。
我试图在我的着色器中获取变量的统一位置。
@Override
public void getAllUniformLocations(){
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
location_lightPosition = super.getUniformLocation("lightPosition");
location_lightColour = super.getUniformLocation("lightColour");
System.out.println(location_transformationMatrix + " | "
+ location_lightPosition + " | " + location_lightColour);
}
public int getUniformLocation(String name){
return GL20.glGetUniformLocation(programID, name);
}
但是打印出来的时候,打印的完全不正确:
0 | -1 | -1
0
用于固定功能管道,因此绑定一些东西会使程序完全崩溃,另外两个只是 returning -1,这是一个空值值。
这是我的着色器加载代码:
public ShaderProgram(String vertexFile,String fragmentFile){
System.out.println("Comiling shader!");
vertexShaderID = loadShader(vertexFile,GL20.GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile,GL20.GL_FRAGMENT_SHADER);
programID = GL20.glCreateProgram();
GL20.glAttachShader(programID, vertexShaderID);
GL20.glAttachShader(programID, fragmentShaderID);
bindAttributes();
getAllUniformLocations();
GL20.glLinkProgram(programID);
GL20.glValidateProgram(programID);
start();
System.out.println(GL11.glGetError());
System.out.println("Comiled shader!");
}
着色器绑定、附加、验证、编译等完全成功,此时游戏成功 运行。但是,缺少统一的位置,所以我无法实现照明之类的东西。
这是我的顶点着色器:
#version 130
in vec3 position;
in vec2 textureCoords;
in vec3 normal;
out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
uniform mat4 transformationMatrix;
uniform vec3 lightPosition;
void main(void){
gl_Position = ftransform();
pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal, 0.0)).xyz;
toLightVector = (vec3(0, 20000, 0)) - (transformationMatrix * vec4(position, 1.0)).xyz;
}
变量 0、1、2 和 3 都绑定到属性,所以我希望数字 return 类似于“4 | 5 | 6”。
这是我的片段着色器:
#version 130
in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
out vec4 out_Color;
uniform sampler2D textureSampler;
uniform vec3 lightColour;
void main(void){
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitLightVector = normalize(toLightVector);
float nDot1 = dot(unitNormal, unitLightVector);
float brightness = max(nDot1, 0.5);
brightness = brightness * 1.5;
vec3 diffuse = brightness * vec3(1, 1, 1);
vec4 text = texture(textureSampler, pass_textureCoords);
vec4 textureColor = vec4(diffuse, 1.0) * text;
if(textureColor.a<0.01){
discard;
}
out_Color = vec4(textureColor.r, textureColor.g, textureColor.b, textureColor.a);
}
您得到的结果是绝对正确的,您对它们的解释是错误的:
0
is for the fixed-function pipeline, so having something bound there is going to completely crash the program,
没有。 0
是一个完全有效的统一位置,与固定功能管道无关。您似乎以某种方式混淆了程序对象 0
(代表旧版 GL 和兼容性配置文件中的固定功能管道)与 per-program 统一位置。
and the other two are simply returning
-1
, which is a null value.
嗯,或多或少。 -1
表示没有 acivte 同名制服。请注意,使用位置 -1
调用 glUniform*()
函数被明确定义为没有错误 - 这样的调用只是没有效果。
您粘贴的着色器代码根本没有使用 lightPosition
和 lightColour
制服,因此它们不存在。您可能打算在片段着色器中使用它们,但我敢打赌您没有以正确的方式在那里使用它们 - 即即使您在代码中声明并使用它们,它们可能仍然没有对着色器输出的影响,因此仍未激活。