LWJGL无法编译着色器

时间:2014-10-25 15:40:19

标签: java opengl glsl shader lwjgl

我正在尝试通过跟随" Quad with Projection"来加载带有投影的VBO。 LWJGL网站上的教程。

这是我加载着色器的功能。

private int loadShader(String filename, int type) {
    StringBuilder shaderSource = new StringBuilder();
    int shaderID = 0;

    try {
        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;
            while ((line = reader.readLine()) != null) {
                shaderSource.append(line).append("\n");
            }
        }
    } catch (IOException e) {
        System.err.println("Could not read file.");
        e.printStackTrace();
        System.exit(-1);
    }

    shaderID = GL20.glCreateShader(type);
    GL20.glShaderSource(shaderID, shaderSource);
    GL20.glCompileShader(shaderID);

    if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
        System.err.println("Could not compile shader.");
        System.exit(-1);
    }

    this.exitOnGLError("loadShader");

    return shaderID;
}

运行程序时,会发生此错误。

if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
    System.err.println("Could not compile shader.");
    System.exit(-1);
}

这是我的着色器代码:

顶点:

#version 150 core

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;

out vec4 pass_Color;
out vec2 pass_TextureCoord;

void main(void) {
    gl_Position = in_Position;
    // Override gl_Position with our new calculated position
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;

    pass_Color = in_Color;
    pass_TextureCoord = in_TextureCoord;
}

片段:

#version 150 core

uniform sampler2D texture_diffuse;

in vec4 pass_Color;
in vec2 pass_TextureCoord;

out vec4 out_Color;

void main(void) {
    out_Color = pass_Color;
    // Override out_Color with our texture pixel
    out_Color = texture2D(texture_diffuse, pass_TextureCoord);
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您的片段着色器无效,GLSL 1.50核心中的函数texture2D不可用。只需使用函数texture()即可。编译器从要调用的采样器均匀类型中导出要采样的纹理类型。

相关问题