public static int compileShader ( Class < ? > context , GL2 gl , int type , String name ) throws ShaderCompilationException { int prog = - 1 ; try ( InputStream in = context . getResourceAsStream ( name ) ) { int [ ] error = new int [ 1 ] ; String shaderdata = FileUtil . slurp ( in ) ; prog = gl . glCreateShader ( type ) ; gl . glShaderSource ( prog , 1 , new String [ ] { shaderdata } , null , 0 ) ; gl . glCompileShader ( prog ) ; // This worked best for me to capture error messages: gl . glGetObjectParameterivARB ( prog , GL2 . GL_OBJECT_INFO_LOG_LENGTH_ARB , error , 0 ) ; if ( error [ 0 ] > 1 ) { byte [ ] info = new byte [ error [ 0 ] ] ; gl . glGetInfoLogARB ( prog , info . length , error , 0 , info , 0 ) ; String out = new String ( info ) ; gl . glDeleteShader ( prog ) ; throw new ShaderCompilationException ( "Shader compilation error in '" + name + "': " + out ) ; } // Different way of catching errors. gl . glGetShaderiv ( prog , GL2 . GL_COMPILE_STATUS , error , 0 ) ; if ( error [ 0 ] > 1 ) { throw new ShaderCompilationException ( "Shader compilation of '" + name + "' failed." ) ; } } catch ( IOException e ) { throw new ShaderCompilationException ( "IO error loading shader: " + name , e ) ; } return prog ; }
Compile a shader from a file .