尝试glUseProgram时Opengl错误1281

时间:2015-05-08 12:49:58

标签: opengl glsl

我是否可以进一步调试此@foreach(var item in Model.RentCollection) { <td><a class="btn btn-info" href="@Url.Action("SendSms", "Home", new{ RentId=item.serial })"><i class="fa fa-envelope fa-2x"></i></a></td> } 错误? opengl

我正在从文件加载源代码,编译,链接,然后尝试在1281之后检查错误

在我的对象的绘制方法中..

glUseProgram

输出..

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);
}

这是我从.glsl文件中加载着色器源的方法..

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

顶点着色器源..

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;
    }
}

片段着色器源..

#version 120

uniform mat4 uMVPMatrix;
attribute vec4 vPosition;

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

1 个答案:

答案 0 :(得分:4)

乍一看,我不确定为什么glGetError会返回错误代码。但要回答您的具体问题“如何进一步调试此错误?”,我确实有一个建议。

将您的绘图代码更改为:

// 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);
}

请注意,这里的区别在于我们添加了一个代码块来记录glGetError 在调用glUseProgram之前记录的错误。原因是因为错误不一定源于您对glUseProgram 的调用。如果您看到使用上面的代码记录了1281错误,您可以确定该错误实际上源自在glUseProgram调用之前进行的OpenGL调用。

查看glGetError的{​​{3}}:

  

glGetError返回错误标志的值。每个可检测的错误   被赋予数字代码和符号名称。发生错误时   错误标志设置为适当的错误代码值。 没有别的   记录错误直到调用glGetError,错误代码是   返回,标志重置为GL_NO_ERROR。

因此,如果您之前的一个OpenGL调用(例如,您的compileSource函数中的某个内容)记录了1281错误,并且您没有在该点之间的任何地方调用glGetError以及您对glUseProgram的来电,您无法合理地假设错误实际上来自glUseProgram来电。

总之,glGetError不会返回OpenGL调用记录的最新错误。您需要更精确地调用glGetError以确定错误源自何处。这将允许您解决您遇到的问题,并确定哪个OpenGL调用正在记录错误。