设置gl_fragColor后OpenGl错误1281

时间:2017-09-27 12:47:31

标签: android opengl-es fragment-shader

我刚刚开始编写opengl-es 2.0编程,我现在正在努力寻找与设置wavefront对象颜色(https://pastebin.com/cEvpj8rt)有关的问题。绘图工作正常,直到我开始操作颜色,此时我遇到了opengl错误1281,我无法在我的代码中查明原因。我已经将着色器代码分解为我认为片段着色器工作所需的最低要求:

void main() {
    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
}

为了消除任何额外的错误来源,我将使用常数值设置颜色,如上所示。我怀疑错误在于上面的简单代码,但是我关注的是我改编的渲染器实现中的代码。 (它基于来自ar-core github repo的示例的渲染器。初始渲染器的完整代码可以在这里找到:https://github.com/google-ar/arcore-android-sdk/blob/master/samples/java_arcore_hello_ar/app/src/main/java/com/google/ar/core/examples/java/helloar/rendering/ObjectRenderer.java,而改编版本可以在这里看到:{{3}您可以在下面找到负责设置和绘制对象的代码的摘录。我认为这个问题与纹理有关,这就是我删除代码的原因。

由于我对手头的事情缺乏了解,我知道要求帮助有点多,但我很高兴此时提出任何暗示/建议。在以下方法中第一次绘制后发生错误:

public void draw(float[] cameraView, float[] cameraPerspective) {

    multiplyMM(mModelViewMatrix, 0, cameraView, 0, mModelMatrix, 0);
    multiplyMM(mModelViewProjectionMatrix, 0, cameraPerspective, 0, mModelViewMatrix, 0);

    glUseProgram(mProgram);

    glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferId);

    glVertexAttribPointer(mPositionAttribute, COORDS_PER_VERTEX,
            GL_FLOAT, false, 0, mVerticesBaseAddress);
    glVertexAttribPointer(mNormalAttribute, 3, 
            GL_FLOAT, false, 0, mNormalsBaseAddress);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // Set the ModelViewProjection matrix in the shader.
    glUniformMatrix4fv(mModelViewUniform, 1, 
            false, mModelViewMatrix, 0);
    glUniformMatrix4fv(mModelViewProjectionUniform, 1,
            false, mModelViewProjectionMatrix, 0);

    glEnableVertexAttribArray(mPositionAttribute);
    glEnableVertexAttribArray(mNormalAttribute);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
    glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    glDisableVertexAttribArray(mPositionAttribute);
    glDisableVertexAttribArray(mNormalAttribute);

    // this is where the error is detected
    OpenGlHelper.checkGLError(TAG, "After draw");
}

这里是用于初始化的方法:

public void createOnGlThread(Context context) throws IOException {

    InputStream objInputStream = context.getAssets()
                                 .open(OBJ_ASSET_NAME);
    Obj obj = ObjReader.read(objInputStream);
    obj = ObjUtils.convertToRenderable(obj);

    IntBuffer wideIndices = ObjData.getFaceVertexIndices(obj, 3);
    FloatBuffer vertices = ObjData.getVertices(obj);
    FloatBuffer texCoords = ObjData.getTexCoords(obj, 2);
    FloatBuffer normals = ObjData.getNormals(obj);

    ShortBuffer indices = ByteBuffer.allocateDirect(2 * wideIndices.limit())
            .order(ByteOrder.nativeOrder()).asShortBuffer();

    while (wideIndices.hasRemaining()) {
        indices.put((short) wideIndices.get());
    }
    indices.rewind();

    int[] buffers = new int[2];
    glGenBuffers(2, buffers, 0);
    mVertexBufferId = buffers[0];
    mIndexBufferId = buffers[1];

    // Load vertex buffer
    mVerticesBaseAddress = 0;
    mTexCoordsBaseAddress = mVerticesBaseAddress + 4 * vertices.limit();
    mNormalsBaseAddress = mTexCoordsBaseAddress + 4 * texCoords.limit();
    final int totalBytes = mNormalsBaseAddress + 4 * normals.limit();

    glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferId);
    glBufferData(GL_ARRAY_BUFFER, totalBytes, null, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, mVerticesBaseAddress,
            4 * vertices.limit(), vertices);
    glBufferSubData(GL_ARRAY_BUFFER, mTexCoordsBaseAddress, 
            4 * texCoords.limit(), texCoords);
    glBufferSubData(GL_ARRAY_BUFFER, mNormalsBaseAddress, 
            4 * normals.limit(), normals);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    // Load index buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferId);
    mIndexCount = indices.limit();
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2 * mIndexCount, 
                 indices, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    OpenGlHelper.checkGLError(TAG, "OBJ buffer load");

    mProgram = glCreateProgram();
    glAttachShader(mProgram, OpenGlHelper.loadGLShader(TAG, context,
            GL_VERTEX_SHADER, R.raw.sphere_vertex));
    glAttachShader(mProgram, OpenGlHelper.loadGLShader(TAG, context,
            GL_FRAGMENT_SHADER, R.raw.sphere_fragment));
    glLinkProgram(mProgram);
    glUseProgram(mProgram);

    OpenGlHelper.checkGLError(TAG, "Program creation");

    mModelViewUniform = glGetUniformLocation(mProgram, "u_ModelView");
    mModelViewProjectionUniform =
            glGetUniformLocation(mProgram, "u_ModelViewProjection");
    mPositionAttribute = glGetAttribLocation(mProgram, "a_Position");
    mNormalAttribute = glGetAttribLocation(mProgram, "a_Normal");

    OpenGlHelper.checkGLError(TAG, "Program parameters");

    setIdentityM(mModelMatrix, 0);
}

0 个答案:

没有答案