Search code examples
javaopenglshaderlwjgl

LWJGL Opengl Shader error


In my createShaderProgram method after glLinkProgram(pID) and glValidateProgram(pID) i have some simple error checking.

int errorCheckValue = glGetError();
if(errorCheckValue != GL_NO_ERROR)
{
    System.err.println("Could not create shaders " + GLU.gluErrorString(errorCheckValue));
   //System.exit(-1);
}

This peace of code fire an error message --> Could not create shaders Invalid operation. Because i commented System.exit(-1) everything works fine but i dont know why this error occurs. Is there any way of code some error handling function that is more specific about the error - something more specific than just Invalid operation?

EDIT: Here is the rest of the code

private static int createShader(String source, int shaderType)
{
    int shaderID = 0;
    shaderID = glCreateShader(shaderType);
    glShaderSource(shaderID, source);
    glCompileShader(shaderID);

    if(glGetShaderi(shaderID, GL_COMPILE_STATUS) == GL_FALSE)
    {
        System.err.println("Shader failed to compile!");
        System.err.println(glGetShaderInfoLog(shaderID, 2048));
        System.exit(-1);
    }

    return shaderID;
}

public static int[] createShaderProgram(String vertFilename, String fragFilename, Attribute locations[])
{
    int pID, vertID, fragID = 0;
    pID = glCreateProgram();
    vertID = createShader(FileUtils.loadFileAsString(vertFilename), GL_VERTEX_SHADER);
    fragID = createShader(FileUtils.loadFileAsString(fragFilename), GL_FRAGMENT_SHADER);
    glAttachShader(pID, vertID);
    glAttachShader(pID, fragID);
    for(int i = 0; i < locations.length; i++){locations[i].bindAttribute(pID);}
    glLinkProgram(pID);
    glValidateProgram(pID);
    int errorCheckValue = glGetError();
    if(errorCheckValue != GL_NO_ERROR)
    {
        System.err.println("Could not create shaders " + GLU.gluErrorString(errorCheckValue));
        //System.exit(-1);
    }

    int[] result = new int[] {pID, vertID, fragID};

    return result;
}

Solution

  • You can check the link/validation status and print the program info log similar to how you are doing it with the shaders.

    glLinkProgram(pID);
    if (glGetProgrami(pID, GL_LINK_STATUS) == GL_FALSE) {
        System.err.println("Program failed to link");
        System.err.println(glGetProgramInfoLog(pID, glGetProgrami(pID, GL_INFO_LOG_LENGTH)));
    }
    

    (And the same with GL_VALIDATE_STATUS.)


    If you are using LWJGL 3 (latest nightly build), you can setup an OpenGL debug message callback with

    GLUtil.setupDebugMessageCallback();
    

    This will automatically choose the correct OpenGL extension (or core functionality) to setup a callback that prints an error message every time an OpenGL error occurs (so no need to call glGetError() any more).

    You can have a look at GLUtil's source code on how to manually setup the debug callbacks if you want more control over the output.