/*
 * Copyright 2014 Lukas Tenbrink
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package ivorius.ivtoolkit.rendering;

import cpw.mods.fml.relauncher.ReflectionHelper;
import net.minecraft.client.renderer.OpenGlHelper;
import org.apache.logging.log4j.Logger;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.util.glu.GLU;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.GL_CLAMP_TO_EDGE;

public class IvOpenGLHelper
{
    public static int GL_VALIDATE_STATUS = GL20.GL_VALIDATE_STATUS;

    public static void glValidateProgram(int program)
    {
        Field useARBField = ReflectionHelper.findField(OpenGlHelper.class, "field_153214_y");
        useARBField.setAccessible(true);
        boolean useARB = false;

        try
        {
            useARB = useARBField.getBoolean(null);
        }
        catch (IllegalAccessException e)
        {
            e.printStackTrace();
        }

        if (useARB)
            ARBShaderObjects.glValidateProgramARB(program);
        else
            GL20.glValidateProgram(program);
    }

    public static int genStandardTexture()
    {
        int textureIndex = glGenTextures();

        glBindTexture(GL_TEXTURE_2D, textureIndex);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        return textureIndex;
    }

    public static void setUpOpenGLStandard2D(int screenWidth, int screenHeight)
    {
        glClear(GL_DEPTH_BUFFER_BIT);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0D, screenWidth, screenHeight, 0.0D, 1000.0D, 3000.0D);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0F, 0.0F, -2000.0F);
    }

    public static void checkGLError(Logger logger, String par1Str)
    {
        int i = GL11.glGetError();

        if (i != 0)
        {
            String s1 = GLU.gluErrorString(i);
            logger.error("########## GL ERROR ##########");
            logger.error("@ " + par1Str);
            logger.error(i + ": " + s1);
        }
    }
}

@@ -2658,23 +2658,24 @@
  String 
-par1Str
+category
 )%0A    %7B%0A
@@ -2686,17 +2686,44 @@
     int 
-i
+error;%0A        while ((error
  = GL11.
@@ -2738,24 +2738,9 @@
 or()
-;%0A%0A        if (i
+)
  != 
@@ -2795,17 +2795,21 @@
 rString(
-i
+error
 );%0A     
@@ -2899,15 +2899,16 @@
 %22 + 
-par1Str
+category
 );%0A 
@@ -2931,17 +2931,21 @@
 r.error(
-i
+error
  + %22: %22 

Make checkGLError run until no error is returned.