user1851758
user1851758

Reputation: 97

OpenGL ES 2.0 crash (glGetUniformLocation: glError 1281)

I am creating an application which will read stl files and display them with OpenGL ES 2.0. I have had success displaying triangles which are hard coded and added to my triangle list in the onSurfaceCreated() method. The problem I am having is when I try to populate my list of triangle objects from the stl file and draw them. Best I can tell, the coordinates of the vertices are all valid and in the proper format. I always get the subject error. I am having trouble debugging this problem and don't know what to do. Below is a section of code from my triangle class where the problem occurs. Specifically the error is on the line of code

mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
MyRenderer.checkGlError("glGetUniformLocation");


public void draw(float[] mvpMatrix) {
    // Add program to OpenGL environment
    GLES20.glUseProgram(mProgram);

    // get handle to vertex shader's vPosition member
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    // Enable a handle to the facet vertices
    GLES20.glEnableVertexAttribArray(mPositionHandle);

    // Prepare the triangle coordinate data
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                                 GLES20.GL_FLOAT, false,
                                 vertexStride, vertexBuffer);

    // get handle to fragment shader's vColor member
    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    // Set color for drawing the facet
    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    // get handle to shape's transformation matrix
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    MyRenderer.checkGlError("glGetUniformLocation");

    // Apply the projection and view transformation
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    MyRenderer.checkGlError("glUniformMatrix4fv");

    // Draw the facet
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
    //If edge mode
    //GLES20.glUniform4fv(mColorHandle, 1, edgeColor, 0);
    //GLES20.glLineWidth(2.0f);
    //GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, vertexCount);

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

Any ideas on how to debug a situation like this? Also, I am somewhat unclear as to the best way to add/remove objects that are to be drawn during program execution. Is my idea of keeping a list of the objects and looping through them in onDraw ok?

Upvotes: 2

Views: 6826

Answers (3)

zelfe
zelfe

Reputation: 11

In my case I had forgotten to init the shaders. Loading vertex and fragment shaders solved the problem.

int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);

with:

public static int loadShader(int type, String shaderCode){
    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)
    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
    int shader = GLES20.glCreateShader(type);

    // add the source code to the shader and compile it
    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);
    return shader;
}

Upvotes: 1

escalator
escalator

Reputation: 889

Same here, when I try to create my drawing objects outside the OpenGL thread, I get the same error. Solution -> objects should be created in the OpenGL thread.

Upvotes: 8

user34212
user34212

Reputation: 21

I was just having the exact same problem working with displaying stl files. I'm guessing you're having the same problem I had, which is you are in the wrong thread when you are trying to create your new triangles. You've got to make sure your calls to OpenGL methods are on the right thread. I'm pretty new to this stuff myself. I basically just changed my program to queue up the STL that I have to add, and then, within the onDraw method, I instantiate the new stuff.

Upvotes: 2

Related Questions