簡體   English   中英

錯誤/libEGL(206):在沒有當前上下文的情況下調用 OpenGL ES API

[英]ERROR/libEGL(206): call to OpenGL ES API with no current context

我有一個帶有 openGL es 1 和 Android 1.5 的簡單 Square。 正方形被畫在屏幕的中央。

我希望當用戶按下屏幕或在屏幕上移動手指時,方塊會移動到該位置。 為此,我嘗試使用 GLuUnProject,嘗試獲取與手指觸摸的窗口 XY 坐標匹配的 opengl 坐標(將來將多邊形轉換為該坐標),然后我將坐標寫入 LogCat。

我收到的坐標不是真正的坐標,是錯誤的坐標,我也收到了問題標題的錯誤。 ERROR/libEGL(206): call to OpenGL ES API with no current context

日志貓:

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

我的代碼:

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;
}
}

注意可能的重復

我相信這不是其他有類似錯誤的問題的重復,因為在這些問題中,錯誤的解決方案是有各種線程,各種線程都有問題,但我沒有使用各種線程。

檢查這兩行:

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

它們在主線程上被調用,而應該在渲染時被調用。 您不必存儲 MyGl 屬性,因為它僅在 OpenGL 回調中有效(例如onDrawFrame(GL10 gl) ,其中您已經擁有 gl 變量。您需要制作 yhe 類的投影和模型視圖矩陣屬性並每次更新它們您繪制框架(並在需要計算 gluUnProject 時使用)。

這可能是因為您正在使用我們在 Renderer 實現中的 onSurfaceCreated()、onSurfaceChanged() 和 onDrawFrame() 中作為參數獲取的 GL10 實例。 由於您打算使用 OpenGL ES 2.0,我們可以也可能不使用該實例,而是使用替代方案。 還有其他選擇!這就是我們在網絡代碼中看到那些參數名稱和 unUsed 或類似的原因!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM