1

我使用 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);
}
}

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

4

1 回答 1

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 代码应该没问题。如果您发布着色器代码,也许我可以提供更多帮助。

于 2013-01-18T19:55:30.670 回答