Esempio n. 1
0
  /**
   * Initialization method for any shader. Compiles and checks code.
   *
   * @param gl The global openGL instance.
   * @throws CompilationFailedException If the compilation of the GLSL code generated any errors.
   */
  public void init(GL3 gl) throws CompilationFailedException {
    try {
      // First, give the source to OpenGL and compile
      gl.glShaderSource(getShaderPointer(), 1, source, (int[]) null, 0);
      gl.glCompileShader(getShaderPointer());

      // Receive compilation status
      IntBuffer buf = Buffers.newDirectIntBuffer(1);
      gl.glGetShaderiv(getShaderPointer(), GL3.GL_COMPILE_STATUS, buf);
      int status = buf.get(0);

      // Check the status
      if (status == GL3.GL_FALSE) {
        // Prepare for additional information
        gl.glGetShaderiv(getShaderPointer(), GL3.GL_INFO_LOG_LENGTH, buf);
        int logLength = buf.get(0);
        byte[] reason = new byte[logLength];

        // Get additional information
        gl.glGetShaderInfoLog(getShaderPointer(), logLength, null, 0, reason, 0);

        throw new CompilationFailedException(
            "Compilation of " + filename + " failed, " + new String(reason));
      }
    } catch (UninitializedException e) {
      logger.error(e.getMessage());
    }
  }