java - 为什么我的 uniform 没有在 openGL 中初始化?

标签 java opengl glsl lwjgl uniform

为什么我的统一 vector 和 float 没有被初始化?我的着色器代码编译正确,我的着色器编译正确,但是当我尝试获取 vec4 lightDirection 的统一位置,并 float specularFactordiffuseFactor,它给了我一个错误。请注意,我实际上还没有使用这些 uniform 做任何事情,但这并不重要。

这是我的顶点着色器,它可以正确获取所有统一位置:

#version 330
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 texture_coord;
layout (location = 2) in vec3 normal;
layout (location = 3) in vec3 fNormal;

uniform mat4 worldMat;
uniform mat4 projection;
uniform mat4 transform;

out vec2 tex;
out vec3 n;
out vec3 fn;

void main() {
    fn=fNormal;
    n=normal;
    tex = texture_coord;
    gl_Position = projection * worldMat * transform * vec4(position, 1);

}

这是我的片段着色器,它只获取纹理样本统一位置:

#version 330
uniform sampler2D sampleTexture;
uniform vec4 lightDirection;
uniform float specularFactor;
uniform float diffuseFactor;


in vec2 tex;
in vec3 n;
in vec3 fn;

out vec4 fragColor;

void main() {
    fragColor=texture(sampleTexture, tex);

}

这是我用来获取统一位置的方法(我使用的是 Java):

public int loadUniform(String uniformName)throws Exception{
        int iD= glGetUniformLocation(program,uniformName);
        System.out.println("PROGRAM: "+program+" UNIFORM: "+uniformName+" "+iD);
        if(iD==-1) {
                throw new Exception("uniform:"+uniformName+" not initialized");
        }
        return iD;
}

现在,这是通过打印语句/异常在控制台中打印的内容。我很困惑。数字看起来肯定是错误的,但又怎么样。我不明白。

java.lang.Exception: uniform:lightDirection not initialized
    at shader_src.Shader.loadUniform(Shader.java:273)
    at shader_src.StaticShader.initValues(StaticShader.java:51)
    at application.Main.main(Main.java:94)
PROGRAM: 4 UNIFORM: worldMat 9
PROGRAM: 4 UNIFORM: projection 5
PROGRAM: 4 UNIFORM: transform 0
PROGRAM: 4 UNIFORM: sampleTexture 4
PROGRAM: 4 UNIFORM: lightDirection -1

最佳答案

glsl 编译器和链接器优化代码。不必要的代码将被删除,不必要的 uniform 和属性将不会成为 Activity 的程序资源。如果不使用统一变量或仅在本身已优化的部分代码中使用统一变量,则它们不会成为 Activity 的程序资源。 lightDirectionspecularFactordiffuseFactor 是未使用的变量,因此不是 Activity 资源,因此没有 Activity 资源,因此您无法获得这些变量的统一位置。

关于java - 为什么我的 uniform 没有在 openGL 中初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72412019/

相关文章:

java - 如何初始化 StringoriginalHandle = driver.getWindowHandle();一次?

c++ - OpenGL 性能随时间下降的可能原因

c++ - 来自 glGetError() 的 INVALID_ENUM 错误。 glGetError 与 glu、glew、glfw?

c++ - GLSL:如何使用投影矩阵计算光线方向?

c++ - GLSL - 统一缓冲对象奇怪的行为

java - Netty(4.0.4)版本压缩/解压字符串报错

java - 如何获取指定关键字在iText7中的位置?

java - SonarQube 与 Eclipse 集成 : Associate with SonarQube option not working for simple java project

无法在其他计算机上运行由Visual Studio 2010编译的opengl程序

glsl - 检查属性是否提供给着色器