我使用 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);
}
}
它奏效了,他们随机地停在我身上。你能弄清楚发生了什么吗?