错误/libEGL(206):调用没有当前上下文的OpenGL ES API。

时间:2021-07-23 18:59:08

First of all, this is not a duplicate of other questions with similar error, because in these questions, the solution of the error is that there are various threads and there is a problem with various threads, but i'm not using various threads, my problem is not the same.

首先,这不是类似错误的其他问题的重复,因为在这些问题中,错误的解决方法是有各种各样的线程,而且有不同线程的问题,但是我没有使用不同的线程,我的问题不一样。

I have a simple Square with openGL es 1 and Android 1.5. The square is painted on the center of the screen.

我有一个简单的正方形,有openGL es 1和Android 1.5。正方形被画在屏幕的*。

I want that when the user press on the screen, or moves the finger on the screen, the square get's moved to that position. For do this i tryed with GLuUnProject, i tryed to obtain the opengl coordinate that matches with the window XY coordinate touched with the finger (to translate the polygon to that coordinate in the future), and i'm writting the coordinates on the LogCat.

我希望当用户按下屏幕,或者将手指移到屏幕上时,方块就会移动到那个位置。为此,我使用了GLuUnProject,我尝试获得与窗口XY坐标相匹配的opengl坐标(在未来将多边形转换为该坐标),并在LogCat上写坐标。

The coordinate's i'm receiving are not true cordinates, are wrong coordinates, also i'm getting the error of the tittle of the question. ERROR/libEGL(206): call to OpenGL ES API with no current context

坐标我接收的不是真正的坐标,是错误的坐标,我也得到了问题的一点误差。错误/libEGL(206):调用没有当前上下文的OpenGL ES API。

The LogCat:

LogCat:

11-07 09:43:40.012: DEBUG/XXXXXXXXX(203): X: -1.2918732
11-07 09:43:40.023: DEBUG/XXXXXXXXX(203): Y: 0.050911963
11-07 09:43:40.042: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.042: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.042: DEBUG/XXXXXXXXX(203): X: -1.2943747
11-07 09:43:40.052: DEBUG/XXXXXXXXX(203): Y: 0.04674524
11-07 09:43:40.152: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.152: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.172: DEBUG/XXXXXXXXX(203): X: 0.77298313
11-07 09:43:40.182: DEBUG/XXXXXXXXX(203): Y: -0.5083332
11-07 09:43:40.223: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.223: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.223: DEBUG/XXXXXXXXX(203): X: 0.77298313
11-07 09:43:40.223: DEBUG/XXXXXXXXX(203): Y: -0.5083332
11-07 09:43:40.402: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.402: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:40.402: DEBUG/XXXXXXXXX(203): X: -1.2943747
11-07 09:43:40.402: DEBUG/XXXXXXXXX(203): Y: 0.04674524
11-07 09:43:41.952: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:41.952: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:41.952: DEBUG/XXXXXXXXX(203): X: 0.77298313
11-07 09:43:41.952: DEBUG/XXXXXXXXX(203): Y: -0.5083332
11-07 09:43:42.042: ERROR/libEGL(203): call to OpenGL ES API with no current context
11-07 09:43:42.042: ERROR/libEGL(203): call to OpenGL ES API with no current context

My code:

我的代码:

public class MySurfaceView extends GLSurfaceView implements Renderer {  
private float INITIAL_Z = -35.0f;   
private Context context;
private Square square;
private float xrot;                 //X Rotation
private float yrot;                 //Y Rotation
private float zrot;                 //Z Rotation    
private float z = INITIAL_Z;            //Profundidad en el eje Z
private float x = 0.0f;             //eje X
private float y = 0.0f;             //eje Y

private MatrixGrabber mg = new MatrixGrabber(); //create the matrix grabber object in your initialization code    
private GL10 MyGl; //To make gl variable accesible on all the methods of the class
byte horizontal=-1; //0: LEFT  1:CENTER  2:RIGHT
byte vertical=-1; //0: TOP  1:CENTER  2:BOTTOM
float startX=-1;
float startY=-1;
float xMovement=0.0f;
float yMovement=0.0f;
private boolean movement_mode=false;

public MySurfaceView(Context context, Bitmap image, int width, byte horizontal, byte vertical) {
    super(context);
    this.context = context;
    setEGLConfigChooser(8, 8, 8, 8, 16, 0); //fondo transparente
    getHolder().setFormat(PixelFormat.TRANSLUCENT); //fondo transparente
    //Transformamos esta clase en renderizadora
    this.setRenderer(this);
    //Request focus, para que los botones reaccionen
    this.requestFocus();
    this.setFocusableInTouchMode(true);
    square = new Square(image);
    this.horizontal=horizontal;
    this.vertical=vertical;
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    MyGl=gl;
    gl.glDisable(GL10.GL_DITHER);               //dithering OFF
    gl.glEnable(GL10.GL_TEXTURE_2D);            //Texture Mapping ON
    gl.glShadeModel(GL10.GL_SMOOTH);            //Smooth Shading 
    gl.glClearDepthf(1.0f);                     //Depth Buffer Setup
    gl.glEnable(GL10.GL_DEPTH_TEST);            //Depth Testing ON
    gl.glDepthFunc(GL10.GL_LEQUAL);
    gl.glClearColor(0,0,0,0); //fondo transparente
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);         
    //Cargamos la textura del cubo.
    square.loadGLTexture(gl, this.context);     
}

public void onDrawFrame(GL10 gl) {
    //Limpiamos pantalla y Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glLoadIdentity();
    //Dibujado
    gl.glTranslatef(x, y, z);           //Move z units into the screen
    //gl.glScalef(0.8f, 0.8f, 0.8f);            //Escalamos para que quepa en la pantalla
    //Rotamos sobre los ejes.
    gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y
    gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f);   //Z
    //Dibujamos el cuadrado
    square.draw(gl);    
}

//si el surface cambia, resetea la vista, imagino que esto pasa cuando cambias de modo portrait/landscape o sacas el teclado físico en móviles tipo Droid.
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) {                       
        height = 1;                         
    }
    gl.glViewport(0, 0, width, height);     //Reset Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION);    //Select Projection Matrix
    gl.glLoadIdentity();                    //Reset Projection Matrix
    //Aspect Ratio de la ventana
    GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select Modelview Matrix
    gl.glLoadIdentity();                    //Reset Modelview Matrix
}

public boolean onTouchEvent(MotionEvent event) {
    float [] outputCoords=getOpenGLCoords(event.getX(), event.getY(), 0);
    x=(outputCoords[0]/outputCoords[3]);
    y=(outputCoords[1]/outputCoords[3]);
    //z=outputCoords[2]/outputCoords[3];
    Log.d("XXXXXXXXX", "X: "+x);
    Log.d("XXXXXXXXX", "Y: "+y);        
    return true; //El evento ha sido manejado
}

public float[] getOpenGLCoords(float xWin,float yWin,float zWin)
{
    int screenW=SectionManager.instance.getDisplayWidth();
    int screenH=SectionManager.instance.getDisplayHeight();
    //CODE FOR TRANSLATING FROM SCREEN COORDINATES TO OPENGL COORDINATES
    mg.getCurrentProjection(MyGl);
    mg.getCurrentModelView(MyGl);
    float [] modelMatrix = new float[16];
    float [] projMatrix = new float[16];
    modelMatrix=mg.mModelView;
    projMatrix=mg.mProjection;          
    int [] mView = new int[4];
    mView[0] = 0;
    mView[1] = 0;
    mView[2] = screenW; //width
    mView[3] = screenH; //height
    float [] outputCoords = new float[4];
    GLU.gluUnProject(xWin, ((float)screenH)-yWin, zWin, modelMatrix, 0, projMatrix, 0, mView, 0, outputCoords, 0);
    return outputCoords;
}
}

2 个解决方案

#1


2  

Check these two lines:

检查这两条线:

mg.getCurrentProjection(MyGl);
mg.getCurrentModelView(MyGl);

They are called on main thread while supposed to be called on rendering one. You don't have to store the MyGl attribute cause it is valid only in OpenGL callbacks (like onDrawFrame(GL10 gl) where you already have the gl variable. You need to make projection and modelview matrices attributes of yhe class and update them every time you draw the frame (and use the when you need to calculate gluUnProject).

它们在主线程上被调用,而在呈现一个线程时应该调用它们。您不需要存储MyGl属性,因为它只在OpenGL回调中有效(如onDrawFrame(GL10 gl),在那里您已经有了gl变量。您需要对yhe类的属性进行投影和建模,并在每次绘制框架时更新它们(并在需要计算gluUnProject时使用它)。

#2


0  

It may be because you are using the GL10 instance we are getting as a parameter in onSurfaceCreated(), onSurfaceChanged() and onDrawFrame() in your Renderer implementation. Since you intend to use OpenGL ES 2.0, we can and probably not use the instance and use an alternative instead. There are alternatives!This is the reasons we see those parameters names and unUsed or similar in codes across the net!

可能是因为您使用的是GL10实例,我们在onSurfaceCreated()、onSurfaceChanged()和onDrawFrame()中获得了一个参数。既然您打算使用OpenGL ES 2.0,我们可以而且可能不使用实例,而是使用替代方法。还有别的选择!这就是我们在网上看到这些参数名称和未使用或类似代码的原因!

#1


2  

Check these two lines:

检查这两条线:

mg.getCurrentProjection(MyGl);
mg.getCurrentModelView(MyGl);

They are called on main thread while supposed to be called on rendering one. You don't have to store the MyGl attribute cause it is valid only in OpenGL callbacks (like onDrawFrame(GL10 gl) where you already have the gl variable. You need to make projection and modelview matrices attributes of yhe class and update them every time you draw the frame (and use the when you need to calculate gluUnProject).

它们在主线程上被调用,而在呈现一个线程时应该调用它们。您不需要存储MyGl属性,因为它只在OpenGL回调中有效(如onDrawFrame(GL10 gl),在那里您已经有了gl变量。您需要对yhe类的属性进行投影和建模,并在每次绘制框架时更新它们(并在需要计算gluUnProject时使用它)。

#2


0  

It may be because you are using the GL10 instance we are getting as a parameter in onSurfaceCreated(), onSurfaceChanged() and onDrawFrame() in your Renderer implementation. Since you intend to use OpenGL ES 2.0, we can and probably not use the instance and use an alternative instead. There are alternatives!This is the reasons we see those parameters names and unUsed or similar in codes across the net!

可能是因为您使用的是GL10实例,我们在onSurfaceCreated()、onSurfaceChanged()和onDrawFrame()中获得了一个参数。既然您打算使用OpenGL ES 2.0,我们可以而且可能不使用实例,而是使用替代方法。还有别的选择!这就是我们在网上看到这些参数名称和未使用或类似代码的原因!