Beispiel #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());
    }
  }
 private static int createShader(final GL3 gl, int type, final String[] srcLines) {
   int shaderID = gl.glCreateShader(type);
   assert shaderID > 0;
   int[] lengths = new int[srcLines.length];
   for (int i = 0; i < srcLines.length; i++) {
     lengths[i] = srcLines[i].length();
   }
   gl.glShaderSource(shaderID, srcLines.length, srcLines, lengths, 0);
   gl.glCompileShader(shaderID);
   return shaderID;
 }