java 没有当前上下文的opengl es api

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11286819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:32:04  来源:igfitidea点击:

opengl es api with no current context

javaandroidopengl-es

提问by Serguei Fedorov

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:

我已经浏览了解决方案,但还没有真正找到一个。我收到此错误是因为执行似乎发生在 gl 线程之外。但是我不知道如何解决这个问题。代码如下:

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

正方形从形状延伸

回答by Tim

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.

如果这new Square(5,5);不是任何 opengl 回调的一部分(我假设您使用的是 glSurfaceView),那么我认为它不会在 OpenGL 线程上运行。它将在您的 glSurfaceView 创建时执行,我相信它是在主 android 线程上。

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

尝试new Square(5,5);onSurfaceCreated.