OpenGL着色器无法正常工作?

时间:2013-01-02 02:06:33

标签: java opengl shader lwjgl

我使用LWJGL在Java中创建了一个类,它导入着色器并启用它们并禁用它们,因此当它们被启用时,我会得到一些很酷的效果。问题是,每次我初始化我的课程时它都会崩溃,而且由于“glCreateProgram”功能,这很可能。

public class Shader {
private int Shader;
private int VShader;
private int FShader;

public void InitShader() throws IOException, Exception
{
    Shader = glCreateProgram(); //This is where the exception is thrown.
    VShader = glCreateShader(GL_VERTEX_SHADER);
    FShader = glCreateShader(GL_FRAGMENT_SHADER);
    StringBuilder VSHADER_SOURCE = new StringBuilder();
    StringBuilder FSHADER_SOURCE = new StringBuilder();
        BufferedReader reader1 = new BufferedReader(new FileReader("shader.vert"));
        String text1;
        while ((text1 = reader1.readLine()) != null)
        {
            VSHADER_SOURCE.append(text1).append("\n");
        }
        reader1.close();
        BufferedReader reader2 = new BufferedReader(new FileReader("shader.frag"));
        String text2;
        while ((text2 = reader2.readLine()) != null)
        {
            FSHADER_SOURCE.append(text2).append("\n");
        }
        reader2.close();
    glShaderSource(VShader, VSHADER_SOURCE);
    glCompileShader(VShader);
    if (glGetShader(VShader, GL_COMPILE_STATUS) == GL_FALSE)
        throw new Exception();
    glShaderSource(FShader, FSHADER_SOURCE);
    glCompileShader(FShader);
    if (glGetShader(FShader, GL_COMPILE_STATUS) == GL_FALSE)
        throw new Exception();
    glLinkProgram(Shader);
    glValidateProgram(Shader);

}

public void EnableShader()
{
    glUseProgram(Shader);
}

public void DisableShader()
{
    glUseProgram(0);
}
}

有效,他们随机停在我身上。你能弄清楚发生了什么吗?

1 个答案:

答案 0 :(得分:0)

在任何地方创建包含此方法的类:

public static final void check(String errorMessage) {
int errorValue = GL11.glGetError();

if (errorValue != GL11.GL_NO_ERROR) {
String errorString = GLU.gluErrorString(errorValue);
System.err.println("ERROR - " + errorMessage + ": " + errorString);

if (Display.isCreated()) Display.destroy();
System.exit(-1);
}
}

并将YourClassName.check("things that just happened few lines above - just to know where you should seek for errors")放在每个设置方法的末尾,甚至更频繁。

您获得的错误意味着您在着色器程序中破坏了某些内容并且无法正确编译,因此您的Java代码应该没问题。如果您发布着色器代码,也许我可以提供更多帮助。