2012-12-11 38 views
0

我的應用程序中有2個GLSurfaceViews(android,opengles 2.0)。其中兩個重疊,所以我可以使用它的頂部作爲預覽。我的問題是,當我啓動應用程序時,頂部glsurfaceview的內容消失了(我設置了與背景glview不同的背景顏色,因此我可以在短時間之後區分內容消失或整個GLview)。我不知道從哪裏開始尋找問題。我的代碼如下。GLThread或GLSurfaceview重疊問題?

在此先感謝。

package jp.android.MyProject; 

import android.app.Activity; 
import android.widget.TextView; 

import android.view.ViewGroup.LayoutParams; 
import android.os.Bundle; 


public class MyProjectActivity extends Activity { 


MyOpenGLView myGLView; 
PreviewGLView previewView; 
private int sizeofPreview = 300; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    myGLView = new MyOpenGLView(this); 
    setContentView(myGLView); 
    myGLView.requestFocus(); 
    myGLView.setFocusableInTouchMode(true); 

    previewView = new PreviewGLView(this); 
    addContentView(previewView,new LayoutParams(sizeofPreview, sizeofPreview)); 

    previewView.setBackgroundColor(0xFF000000); 

    myGLView.textWindow = new TextView(this); 
    addContentView(myGLView.textWindow,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

    myGLView.touchWindow = new TextView(this); 
    addContentView(myGLView.touchWindow, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    myGLView.touchWindow.setY(600.0f); 
} 

@Override 
protected void onResume(){ 
    super.onResume(); 
    myGLView.onResume(); 
previewView.onResume(); 
} 

@Override 
protected void onPause(){ 
    super.onPause(); 
    myGLView.onPause(); 
    previewView.onPause(); 
} 

}

回答

1

我不相信你可以(或者應該)使用兩個重疊的GLSurfaceView在Android應用程序。它只是沒有在onDrawFrame(GL10 gl)這樣設計的

更好的方式來做到這將是把所有的代碼繪製的主場景和預覽場景分爲兩種不同的方法在GLSurfaceView.Renderer

然後,調用主要繪製方法,然後清除深度緩衝區但不是顏色緩衝區,然後調用該方法在主場景的頂部繪製預覽。你應該仍然在你的渲染器可以清除在onDrawFrame(GL10 gl)開始兩個緩衝區

public class MyRenderer implements GLSurfaceView.Renderer { 

/** 
* GLSurfaceView onto which stuff is rendered 
*/ 
private GLSurfaceView mGLView; 

/** 
* The current scene to render 
*/ 
public MyScene mScene; 

/** 
* The preview scene to draw over the top 
*/ 
public MyScene mPreviewScene; 

/** 
* 
* @param The GLSurfaceView this renderer is attached to 
*/ 
MyRenderer(GLSurfaceView mGLView;){ 
    this. mGLView = mGLView;; 
} 

public void onSurfaceCreated(GL10 gl, EGLConfig config) { 

    // clear the background 
    gl.glClearColor(0, 0, 0, 1); 

      // Enable depth testing 
    gl.glEnable(GL10.GL_DEPTH_TEST); 
    gl.glDepthMask(true); 
    gl.glDepthFunc(GL10.GL_LEQUAL); 

    // enable back face culling 
    gl.glEnable(GL10.GL_CULL_FACE); 
    gl.glFrontFace(GL10.GL_CCW);   
    gl.glCullFace(GL10.GL_BACK); 

} 

public void onSurfaceChanged(GL10 gl, int width, int height) { 

    // set the new viewport width/height 
    setViewportWidth(width); 
    setViewportHeight(height); 

} 


public void onDrawFrame(GL10 gl) { 

    // clear the canvas at the start of the draw call 
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 


     // draw scene 
     gl.glPushMatrix(); 

     // draw the foreground scene 
     if(mScene!=null){ 
      mScene.draw(gl); 
      // clear the depth buffer only!   
      gl.glClear(GL10.GL_DEPTH_BUFFER_BIT); 
     } 

     gl.glPopMatrix(); 

     gl.glPushMatrix(); 

     // draw the preview scene on top 
     if(mPreviewScene!=null){ 
      mPreviewScene.draw(gl); 
     } 

     gl.glPopMatrix(); 


    return; 

} 
} 

__

public interface MyScene{ 

    public void draw(GL10 gl); 
} 

__

public class MyOpenGLScene implements MyScene{ 

    // etc 

} 

__

public class PreviewGLScene implements MyScene{ 

    // etc 

} 

__

GLSurfaceView mGLView; 
MyOpenGLScene myScene; 
PreviewGLScene previewScene; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    mGLView = new GLSurfaceView(this); 
    MyRenderer renderer = new MyRenderer(mGLView); 

    myScene = new MyOpenGLScene(); 
    previewScene = new PreviewGLScene(); 

    renderer.mScene = myScene; 
    renderer.mPreviewScene = previewScene; 

    mGLView.setRenderer(renderer); 

    setContentView(myGLView); 
} 

注意:這裏的調用都在OpenGL ES 1.0中,但應該很容易計算出OpenGL ES 2.0的等價物。另外,你實際上並不需要重寫GLSurfaceView(儘管如果你有,並且正在GLSurfaceView中進行渲染,你可以將我放入MyRenderer的方法交換到一個GLSurfaceView中,結果應該是相同的)