0

我的应用程序中有 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();
}

}

4

1 回答 1

1

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

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

然后在 期间onDrawFrame(GL10 gl),调用主绘制方法,然后清除深度缓冲区而不是颜色缓冲区,然后调用该方法在主场景的顶部绘制预览。您仍然应该onDrawFrame(GL10 gl)在 Renderer的开头清除两个缓冲区

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 的方法交换,结果应该是相同的)

于 2012-12-14T13:56:37.217 回答