我一直在努力让基本着色器全天工作,最后得到一个工作版本(有点)。每当我使用最新方法初始化显示时,着色器无法加载,但当我以旧式方式(没有PixelFormat和ContextAttibs)时,着色器加载只是花花公子。这是我用来初始化OpenGL的代码:
PixelFormat pf = new PixelFormat();
ContextAttribs ca = new ContextAttribs(3, 3).withForwardCompatible(true).withProfileCore(true);
try {
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.setTitle("Shader Testing");
Display.setLocation((int) (Utils.screen_dim.getWidth() - WIDTH) / 2,
(int) (Utils.screen_dim.getHeight() - HEIGHT) / 2);
Display.setResizable(false);
Display.create(pf, ca);
} catch (LWJGLException e) {
Utils.printError("Failed to initialize the display", true);
}
以下是我如何初始化着色器(有点冗长):
shdrPID = glCreateProgram();
vSID = glCreateShader(GL_VERTEX_SHADER);
fSID = glCreateShader(GL_FRAGMENT_SHADER);
StringBuilder vertexShaderSource = new StringBuilder();
StringBuilder fragmentShaderSource = new StringBuilder();
//loads vertex shader source
try{
File vert_shdr_file = new File("src/vert.shdr");
BufferedReader reader = new BufferedReader(new FileReader(vert_shdr_file));
String ln;
while((ln = reader.readLine()) != null) {
vertexShaderSource.append(ln).append('\n');
}
reader.close();
}catch(IOException e) {
Display.destroy();
e.printStackTrace();
System.exit(1);
}
//loads fragment shader source
try{
File frag_shdr_file = new File("src/frag.shdr");
BufferedReader reader = new BufferedReader(new FileReader(frag_shdr_file));
String ln;
while((ln = reader.readLine()) != null) {
fragmentShaderSource.append(ln).append('\n');
}
reader.close();
}catch(IOException e) {
Display.destroy();
e.printStackTrace();
System.exit(1);
}
//compile vertex shader
glShaderSource(vSID, vertexShaderSource);
glCompileShader(vSID);
if(glGetShaderi(vSID, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Failed to load vertex shader...");
}
//compile fragment shader
glShaderSource(fSID, fragmentShaderSource);
glCompileShader(fSID);
if(glGetShaderi(fSID, GL_COMPILE_STATUS) == GL_FALSE) {
System.err.println("Failed to load fragment shader...");
}
glAttachShader(shdrPID, vSID);
glAttachShader(shdrPID, fSID);
glLinkProgram(shdrPID);
glValidateProgram(shdrPID);
我确实使用了一个稍微过时的教程来编写着色器加载脚本(here)。所以这是我的问题:
我在做什么,这是过时的?是否有更新的教程或地方我可以去学习更现代的加载着色器的方法?