Opengl error 1281 when trying glUseProgram

Any ideas how I could debug this error opengl

further?1281

I am loading source from files, compiling, linking and then trying to check for errors after glUseProgram

In my method for drawing objects.

log.info(gl2.glIsProgram(shaderProgram)); // true
gl2.glUseProgram(shaderProgram);

int error;
while ((error = gl2.glGetError()) != GL2.GL_NO_ERROR) {
    throw new RuntimeException("glUseProgram" + ": glError " + error);
}

      

Output.

[13:38:08] INFO (IARectangle.java:99) - true
java.lang.RuntimeException: glUseProgram: glError 1281

      

This is how I load my shader source from .glsl files.

Vector<Integer> shaders = new Vector<Integer>();

try {

    shaders.add(compileSource(
        loadSource("shaders/vertexShader.glsl"),
        loadSource("shaders/fragmentShader.glsl")));

    return shaders;

} catch (Exception e) {
    e.printStackTrace();
    return shaders;
}

public String[] loadSource(String filename){

    StringBuilder sb = new StringBuilder();
    try {

        InputStream is = getClass().getClassLoader().getResourceAsStream(filename);
        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String line;
        while ((line = br.readLine()) != null) {

            sb.append(line);
            sb.append('\n');
        }
        is.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return new String[] { sb.toString() };
}

public final int compileSource(final String[] vertexSource, final String[] fragmentSource) throws Exception {

     vertexShaderProgram;
    int fragmentShaderProgram;
    int shaderProgram;

    // load vertexShader source, compile and verify
    vertexShaderProgram = gl2.glCreateShader(GL2.GL_VERTEX_SHADER);
    gl2.glShaderSource(vertexShaderProgram, 1, vertexSource, null, 0);
    gl2.glCompileShader(vertexShaderProgram);
    verifyCompile(gl2, vertexShaderProgram);

    // load fragmentShader source, compile and verify
    fragmentShaderProgram = gl2.glCreateShader(GL2.GL_FRAGMENT_SHADER);
    gl2.glShaderSource(fragmentShaderProgram, 1, fragmentSource, null, 0);
    gl2.glCompileShader(fragmentShaderProgram);
    verifyCompile(gl2, fragmentShaderProgram);

    shaderProgram = gl2.glCreateProgram();

    gl2.glAttachShader(shaderProgram, vertexShaderProgram);
    gl2.glAttachShader(shaderProgram, fragmentShaderProgram);
    gl2.glLinkProgram(shaderProgram);
    IntBuffer intBuffer = IntBuffer.allocate(1);
    gl2.glGetProgramiv(shaderProgram, GL2.GL_LINK_STATUS, intBuffer);

    if (intBuffer.get(0) != 1){

        String infoLog = null;
        gl2.glGetProgramiv(shaderProgram, GL2.GL_INFO_LOG_LENGTH, intBuffer);
        int size = intBuffer.get(0);
        log.error("Program link error: ");
        if (size > 0) {

            ByteBuffer byteBuffer = ByteBuffer.allocate(size);
            gl2.getGL2().glGetProgramInfoLog(shaderProgram, size, intBuffer, byteBuffer);
            byte[] sizeBytes = new byte[size];
            byteBuffer.get(sizeBytes, 0, size);
            infoLog = new String(sizeBytes);

            log.error("info: " + infoLog);

        } else {
            log.error("Unknown");
        }
        System.exit(1);
        return shaderProgram;

    } else {

        return shaderProgram;
    }
}

      

Vertex shader source.

#version 120

uniform mat4 uMVPMatrix;
attribute vec4 vPosition;

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    //gl_Position = uMVPMatrix * vPosition;
}

      

Fragment of a shader source.

#version 120

uniform vec4 vColor;

void main() {
    gl_FragColor = vColor;
}

      

+3


source to share


1 answer


At first glance, I'm not sure why the glGetError

error code is returning. But to answer your specific question, "How can I debug this error further?", I have a suggestion.

Change your code:

// Logging errors before the call to glUseProgram
int error;
while ((error = gl2.glGetError()) != GL2.GL_NO_ERROR) {
    log.info(error);
}

log.info(gl2.glIsProgram(shaderProgram)); // true
gl2.glUseProgram(shaderProgram);

int error;
while ((error = gl2.glGetError()) != GL2.GL_NO_ERROR) {
    throw new RuntimeException("glUseProgram" + ": glError " + error);
}

      

Note that the difference here is that we added a block of code to log the errors returned glGetError

before the call glUseProgram

. This is because the error does not necessarily occur when calledglUseProgram

. If you see error 1281 logged using the code above, you can determine that the error actually originates from an OpenGL call made before the call glUseProgram

.



Check out the documentation for glGetError

:

glGetError returns the value of the error flag. Each detected error is assigned a numeric code and a symbolic name. When an error occurs, the error flag is set to the corresponding error code value. No other errors are logged before glGetError is called, the error code is - and the reset flag is GL_NO_ERROR.

So, if one of your earlier OpenGL calls (maybe something in your function compileSource

, for example) recorded an error 1281

, and you didn't call glGetError

anywhere between that point and your call to glUseProgram

, you can't reasonably assume that the error is actually the case comes from a challenge glUseProgram

.

Thus, glGetError

does not return the most recent error recorded by the OpenGL call. You must call glGetError

in more granularity to pinpoint exactly where the error is coming from. This will allow you to troubleshoot the problem you are facing and determine which OpenGL call is logging the error.

+4


source







All Articles