So I decided to try writing a simple OpenGL app using Java, just to see how it compared to my other efforts, and I\'m running into an issue where my shaders refuse to compile. T
Although an answer already has peen provided and accepted, I'll just write here how I'd prefer it:
// Create shader from one or multiple source files
private int CreateCompiledShader(File[] source_files, int shader, GL3 gl){
int shaderloc = gl.glCreateShader(shader);
int nSources = source_files.size();
// the number of shader sources it known from the beginning
// so we can allocate the arrays right here
String[] sources = new String[nSources];
int[] sources_lengths = new int[nSources];
for(int i = 0; i < nSources; i++) {
// We don't need a buffered reader as we're reading the file as a whole
FileReader input = new FileReader(source_file[i]);
String buffer;
buffer = input.read();
sources[i] = buffer;
sources_lengths[i] = buffer.length();
if(input != null){
input.close();
}
}
// Frankly I really don't understand why you have to pass sources_lengths here
// It would take only 3 LOC in the binding's code
// to extract that from the sources array, Oh, well...
gl.glShaderSource(shaderloc, nSources, sources, sources_lengths, 0);
gl.glCompileShader(shaderloc);
// Actually if glGetError() returns an error you must call it in a loop
// as OpenGL errors can accumulate, and you have to pop them all from the list.
int error = gl.glGetError();
if(error != GL3.GL_NO_ERROR){
Logger.log(new GLU().gluErrorString(error), Logger.ERROR, "Shader compilation");
}
return shaderloc;
}
I took the liberty to remove all try/catch/finally blocks, as they were misplaced a bit: If reading any source file fails, shader loading cannot complete, so it makes no sense to continur gracefully. The propper way to deal with this would be a large try/catch block arround this, that cleans up OpenGL objects of shader compilation does not complete.