opengl es api with no current context

泪湿孤枕 提交于 2019-12-08 01:34:35

问题


I have looked through the solutions and haven't really found one. I am getting this error because it seems like the execution happens outside of the gl thread. However I am not sure how to fix this. The code is as follows:

public shape()
{
    super();        



    vertexShader = Shader.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); //<============
    fragmentShader = Shader.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);      

    ByteBuffer buffer = ByteBuffer.allocateDirect(getCoordinates().length * 4);
    buffer.order(ByteOrder.nativeOrder());
    vertexBuffer = buffer.asFloatBuffer();

    vertexBuffer.put(getCoordinates());
    vertexBuffer.position(0);

    ByteBuffer drawListBuffer = ByteBuffer.allocateDirect(getOrderOfDraw().length * 2);

    drawListBuffer.order(ByteOrder.nativeOrder());

    listBuffer = drawListBuffer.asShortBuffer();
    listBuffer.put(getOrderOfDraw());

    listBuffer.position(0);


     mProgram = GLES20.glCreateProgram();             // create empty OpenGL Program
     GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program
     GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
     GLES20.glLinkProgram(mProgram); 

}

and the calling renderer is

    Square square = new Square(5, 5);

public void onDrawFrame(GL10 unused) 
{

    unused.glLoadIdentity();
    unused.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    square.Draw();
}

Square extends from shape


回答1:


If that new Square(5,5); is not part of any of the opengl callbacks (I assume you're using a glSurfaceView), then I don't think it runs on the OpenGL thread. It will be executed when your glSurfaceView is created, which I believe is on the main android thread.

Try moving new Square(5,5); inside of onSurfaceCreated.



来源:https://stackoverflow.com/questions/11286819/opengl-es-api-with-no-current-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!