java - 为什么我的制服位置显示不正确?

标签 java opengl glsl lwjgl

我正在尝试获取着色器中变量的统一位置。

@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 用于固定功能管道,因此绑定(bind)某些东西会使程序完全崩溃,而另外两个只是返回 -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!");
}

着色器绑定(bind)、附加、验证、编译等完全成功,此时游戏确实成功运行。但是,缺少统一的位置,因此我无法实现诸如照明之类的功能。

这是我的顶点着色器:

#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 都绑定(bind)到属性,因此我希望数字返回类似于“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 和兼容性配置文件中的固定功能管道)与每个程序统一位置混淆了。

and the other two are simply returning -1, which is a null value.

嗯,或多或少。 -1 表示没有该名称的 acivte 统一。请注意,使用位置 -1 调用 glUniform*() 函数被明确定义为没有错误 - 这样的调用没有任何效果。

您粘贴的着色器代码根本不使用 lightPositionlightColour 制服,因此它们不存在。您可能打算在片段着色器中使用它们,但我敢打赌,您没有以正确的方式进行操作 - 即,即使您在代码中声明并使用它们,它们可能仍然没有对着色器输出的影响,因此仍然不活动。

关于java - 为什么我的制服位置显示不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33329483/

相关文章:

java - 使用字符串/数组获取文件(android)

c++ - glulookat() 用法-opengl 和 C++

opengl - OpenGL 中 `glEnableVertexAttribArray(GLuint index)` 的用途是什么?

java - 使用 maven JDBC mysql 找不到类

java - Grails 2.3 无法创建 jvm 错误

java - 由于某种原因,我的客户对象为空。

OpenGL 将 VBO 保存到文件(Obj 或其他文件)

python - OpenGL 说 "from_param received a non-contiguous array"

opengl-es - Spritekit 和 OpenGL : smooth smoke trail

opengl - 选择性 nvidia #pragma optionNV(全部展开)