GLThread或GLSurfaceview重叠问题?

时间:2012-12-11 05:11:49

标签: android multithreading opengl-es-2.0 overlap glsurfaceview

我的应用程序中有2个GLSurfaceViews(android,opengles 2.0)。其中两个重叠,所以我可以使用顶部作为预览。我的问题是,当我启动应用程序时,顶部glsurfaceview的内容消失了(我从背景glview设置了不同的背景颜色。所以我可以在短时间之后区分内容消失或整个GLview)。我不知道从哪里开始寻找问题。我的代码如下。

提前致谢。

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 个答案:

答案 0 :(得分:1)

我不相信你可以(或者应该)在Android应用程序中使用两个重叠的GLSurfaceView。它不是那样设计的

更好的方法是将所有用于绘制主场景和预览场景的代码放入GLSurfaceView.Renderer中的两种不同方法

然后在onDrawFrame(GL10 gl)期间,调用主绘制方法,然后清除深度缓冲区,但不是颜色缓冲区,然后调用方法在主场景的顶部绘制预览。您仍应在渲染器中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中,结果应该是一样的)